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 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
58 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub,
61 * Task management primitives implementation.
64 * Copyright (c) 1993 The University of Utah and
65 * the Computer Systems Laboratory (CSL). All rights reserved.
67 * Permission to use, copy, modify and distribute this software and its
68 * documentation is hereby granted, provided that both the copyright
69 * notice and this permission notice appear in all copies of the
70 * software, derivative works or modified versions, and any portions
71 * thereof, and that both notices appear in supporting documentation.
73 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
74 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
75 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
77 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
78 * improvements that they make and grant CSL redistribution rights.
82 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
83 * support for mandatory and extensible security protections. This notice
84 * is included in support of clause 2.2 (b) of the Apple Public License,
86 * Copyright (c) 2005 SPARTA, Inc.
89 #include <mach/mach_types.h>
90 #include <mach/boolean.h>
91 #include <mach/host_priv.h>
92 #include <mach/machine/vm_types.h>
93 #include <mach/vm_param.h>
94 #include <mach/mach_vm.h>
95 #include <mach/semaphore.h>
96 #include <mach/task_info.h>
97 #include <mach/task_inspect.h>
98 #include <mach/task_special_ports.h>
101 #include <ipc/ipc_importance.h>
102 #include <ipc/ipc_types.h>
103 #include <ipc/ipc_space.h>
104 #include <ipc/ipc_entry.h>
105 #include <ipc/ipc_hash.h>
107 #include <kern/kern_types.h>
108 #include <kern/mach_param.h>
109 #include <kern/misc_protos.h>
110 #include <kern/task.h>
111 #include <kern/thread.h>
112 #include <kern/coalition.h>
113 #include <kern/zalloc.h>
114 #include <kern/kalloc.h>
115 #include <kern/kern_cdata.h>
116 #include <kern/processor.h>
117 #include <kern/sched_prim.h> /* for thread_wakeup */
118 #include <kern/ipc_tt.h>
119 #include <kern/host.h>
120 #include <kern/clock.h>
121 #include <kern/timer.h>
122 #include <kern/assert.h>
123 #include <kern/sync_lock.h>
124 #include <kern/affinity.h>
125 #include <kern/exc_resource.h>
126 #include <kern/machine.h>
127 #include <kern/policy_internal.h>
128 #include <kern/restartable.h>
130 #include <corpses/task_corpse.h>
132 #include <kern/telemetry.h>
136 #include <kern/monotonic.h>
137 #include <machine/monotonic.h>
138 #endif /* MONOTONIC */
143 #include <vm/vm_map.h>
144 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
145 #include <vm/vm_pageout.h>
146 #include <vm/vm_protos.h>
147 #include <vm/vm_purgeable_internal.h>
148 #include <vm/vm_compressor_pager.h>
150 #include <sys/resource.h>
151 #include <sys/signalvar.h> /* for coredump */
152 #include <sys/bsdtask_info.h>
154 * Exported interfaces
157 #include <mach/task_server.h>
158 #include <mach/mach_host_server.h>
159 #include <mach/host_security_server.h>
160 #include <mach/mach_port_server.h>
162 #include <vm/vm_shared_region.h>
164 #include <libkern/OSDebug.h>
165 #include <libkern/OSAtomic.h>
166 #include <libkern/section_keywords.h>
168 #include <mach-o/loader.h>
170 #include <kern/sfi.h> /* picks up ledger.h */
173 #include <security/mac_mach_internal.h>
176 #include <IOKit/IOBSD.h>
179 extern int kpc_force_all_ctrs(task_t
, int);
182 SECURITY_READ_ONLY_LATE(task_t
) kernel_task
;
184 static SECURITY_READ_ONLY_LATE(zone_t
) task_zone
;
185 ZONE_INIT(&task_zone
, "tasks", sizeof(struct task
),
186 ZC_NOENCRYPT
| ZC_ZFREE_CLEARMEM
,
189 extern int exc_via_corpse_forking
;
190 extern int corpse_for_fatal_memkill
;
191 extern boolean_t
proc_send_synchronous_EXC_RESOURCE(void *p
);
192 extern void task_disown_frozen_csegs(task_t owner_task
);
194 /* Flag set by core audio when audio is playing. Used to stifle EXC_RESOURCE generation when active. */
195 int audio_active
= 0;
198 * structure for tracking zone usage
199 * Used either one per task/thread for all zones or <per-task,per-zone>.
201 typedef struct zinfo_usage_store_t
{
202 /* These fields may be updated atomically, and so must be 8 byte aligned */
203 uint64_t alloc
__attribute__((aligned(8))); /* allocation counter */
204 uint64_t free
__attribute__((aligned(8))); /* free counter */
205 } zinfo_usage_store_t
;
207 zinfo_usage_store_t tasks_tkm_private
;
208 zinfo_usage_store_t tasks_tkm_shared
;
210 /* A container to accumulate statistics for expired tasks */
211 expired_task_statistics_t dead_task_statistics
;
212 LCK_SPIN_DECLARE_ATTR(dead_task_statistics_lock
, &task_lck_grp
, &task_lck_attr
);
214 ledger_template_t task_ledger_template
= NULL
;
216 SECURITY_READ_ONLY_LATE(struct _task_ledger_indices
) task_ledgers
__attribute__((used
)) =
224 .alternate_accounting
= -1,
225 .alternate_accounting_compressed
= -1,
227 .phys_footprint
= -1,
228 .internal_compressed
= -1,
229 .purgeable_volatile
= -1,
230 .purgeable_nonvolatile
= -1,
231 .purgeable_volatile_compressed
= -1,
232 .purgeable_nonvolatile_compressed
= -1,
233 .tagged_nofootprint
= -1,
234 .tagged_footprint
= -1,
235 .tagged_nofootprint_compressed
= -1,
236 .tagged_footprint_compressed
= -1,
237 .network_volatile
= -1,
238 .network_nonvolatile
= -1,
239 .network_volatile_compressed
= -1,
240 .network_nonvolatile_compressed
= -1,
241 .media_nofootprint
= -1,
242 .media_footprint
= -1,
243 .media_nofootprint_compressed
= -1,
244 .media_footprint_compressed
= -1,
245 .graphics_nofootprint
= -1,
246 .graphics_footprint
= -1,
247 .graphics_nofootprint_compressed
= -1,
248 .graphics_footprint_compressed
= -1,
249 .neural_nofootprint
= -1,
250 .neural_footprint
= -1,
251 .neural_nofootprint_compressed
= -1,
252 .neural_footprint_compressed
= -1,
253 .platform_idle_wakeups
= -1,
254 .interrupt_wakeups
= -1,
256 .sfi_wait_times
= { 0 /* initialized at runtime */},
257 #endif /* CONFIG_SCHED_SFI */
258 .cpu_time_billed_to_me
= -1,
259 .cpu_time_billed_to_others
= -1,
260 .physical_writes
= -1,
261 .logical_writes
= -1,
262 .logical_writes_to_external
= -1,
263 #if DEBUG || DEVELOPMENT
265 .pages_grabbed_kern
= -1,
266 .pages_grabbed_iopl
= -1,
267 .pages_grabbed_upl
= -1,
270 .frozen_to_swap
= -1,
271 #endif /* CONFIG_FREEZE */
272 .energy_billed_to_me
= -1,
273 .energy_billed_to_others
= -1,
274 #if CONFIG_PHYS_WRITE_ACCT
275 .fs_metadata_writes
= -1,
276 #endif /* CONFIG_PHYS_WRITE_ACCT */
279 /* System sleep state */
280 boolean_t tasks_suspend_state
;
283 void init_task_ledgers(void);
284 void task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
285 void task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
286 void task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
);
287 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void);
288 void __attribute__((noinline
)) PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
);
289 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
);
291 kern_return_t
task_suspend_internal(task_t
);
292 kern_return_t
task_resume_internal(task_t
);
293 static kern_return_t
task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
);
295 extern kern_return_t
iokit_task_terminate(task_t task
);
296 extern void iokit_task_app_suspended_changed(task_t task
);
298 extern kern_return_t
exception_deliver(thread_t
, exception_type_t
, mach_exception_data_t
, mach_msg_type_number_t
, struct exception_action
*, lck_mtx_t
*);
299 extern void bsd_copythreadname(void *dst_uth
, void *src_uth
);
300 extern kern_return_t
thread_resume(thread_t thread
);
302 // Warn tasks when they hit 80% of their memory limit.
303 #define PHYS_FOOTPRINT_WARNING_LEVEL 80
305 #define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT 150 /* wakeups per second */
306 #define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL 300 /* in seconds. */
309 * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
311 * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
312 * stacktraces, aka micro-stackshots)
314 #define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER 70
316 int task_wakeups_monitor_interval
; /* In seconds. Time period over which wakeups rate is observed */
317 int task_wakeups_monitor_rate
; /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
319 unsigned int task_wakeups_monitor_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
321 int disable_exc_resource
; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
323 ledger_amount_t max_task_footprint
= 0; /* Per-task limit on physical memory consumption in bytes */
324 unsigned int max_task_footprint_warning_level
= 0; /* Per-task limit warning percentage */
325 int max_task_footprint_mb
= 0; /* Per-task limit on physical memory consumption in megabytes */
327 /* I/O Monitor Limits */
328 #define IOMON_DEFAULT_LIMIT (20480ull) /* MB of logical/physical I/O */
329 #define IOMON_DEFAULT_INTERVAL (86400ull) /* in seconds */
331 uint64_t task_iomon_limit_mb
; /* Per-task I/O monitor limit in MBs */
332 uint64_t task_iomon_interval_secs
; /* Per-task I/O monitor interval in secs */
334 #define IO_TELEMETRY_DEFAULT_LIMIT (10ll * 1024ll * 1024ll)
335 int64_t io_telemetry_limit
; /* Threshold to take a microstackshot (0 indicated I/O telemetry is turned off) */
336 int64_t global_logical_writes_count
= 0; /* Global count for logical writes */
337 int64_t global_logical_writes_to_external_count
= 0; /* Global count for logical writes to external storage*/
338 static boolean_t
global_update_logical_writes(int64_t, int64_t*);
340 #define TASK_MAX_THREAD_LIMIT 256
343 int pmap_ledgers_panic
= 1;
344 int pmap_ledgers_panic_leeway
= 3;
345 #endif /* MACH_ASSERT */
347 int task_max
= CONFIG_TASK_MAX
; /* Max number of tasks */
350 int hwm_user_cores
= 0; /* high watermark violations generate user core files */
354 extern uint32_t proc_platform(const struct proc
*);
355 extern uint32_t proc_min_sdk(struct proc
*);
356 extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
357 extern int proc_pid(struct proc
*p
);
358 extern int proc_selfpid(void);
359 extern struct proc
*current_proc(void);
360 extern char *proc_name_address(struct proc
*p
);
361 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
362 extern int kevent_proc_copy_uptrs(void *proc
, uint64_t *buf
, uint32_t bufsize
);
363 extern void workq_proc_suspended(struct proc
*p
);
364 extern void workq_proc_resumed(struct proc
*p
);
366 #if CONFIG_MEMORYSTATUS
367 extern void proc_memstat_terminated(struct proc
* p
, boolean_t set
);
368 extern void memorystatus_on_ledger_footprint_exceeded(int warning
, boolean_t memlimit_is_active
, boolean_t memlimit_is_fatal
);
369 extern void memorystatus_log_exception(const int max_footprint_mb
, boolean_t memlimit_is_active
, boolean_t memlimit_is_fatal
);
370 extern boolean_t
memorystatus_allowed_vm_map_fork(task_t task
);
371 extern uint64_t memorystatus_available_memory_internal(struct proc
*p
);
373 #if DEVELOPMENT || DEBUG
374 extern void memorystatus_abort_vm_map_fork(task_t
);
377 #endif /* CONFIG_MEMORYSTATUS */
379 #endif /* MACH_BSD */
381 #if DEVELOPMENT || DEBUG
382 int exc_resource_threads_enabled
;
383 #endif /* DEVELOPMENT || DEBUG */
385 #if (DEVELOPMENT || DEBUG)
386 uint32_t task_exc_guard_default
= TASK_EXC_GUARD_MP_DELIVER
| TASK_EXC_GUARD_MP_ONCE
| TASK_EXC_GUARD_MP_CORPSE
|
387 TASK_EXC_GUARD_VM_DELIVER
| TASK_EXC_GUARD_VM_ONCE
| TASK_EXC_GUARD_VM_CORPSE
;
389 uint32_t task_exc_guard_default
= 0;
394 static void task_hold_locked(task_t task
);
395 static void task_wait_locked(task_t task
, boolean_t until_not_runnable
);
396 static void task_release_locked(task_t task
);
398 static void task_synchronizer_destroy_all(task_t task
);
399 static os_ref_count_t
400 task_add_turnstile_watchports_locked(
402 struct task_watchports
*watchports
,
403 struct task_watchport_elem
**previous_elem_array
,
404 ipc_port_t
*portwatch_ports
,
405 uint32_t portwatch_count
);
407 static os_ref_count_t
408 task_remove_turnstile_watchports_locked(
410 struct task_watchports
*watchports
,
411 ipc_port_t
*port_freelist
);
413 static struct task_watchports
*
414 task_watchports_alloc_init(
420 task_watchports_deallocate(
421 struct task_watchports
*watchports
);
427 boolean_t is_64bit_data
)
429 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
431 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
436 * Switching to/from 64-bit address spaces
439 if (!task_has_64Bit_addr(task
)) {
440 task_set_64Bit_addr(task
);
443 if (task_has_64Bit_addr(task
)) {
444 task_clear_64Bit_addr(task
);
449 * Switching to/from 64-bit register state.
452 if (task_has_64Bit_data(task
)) {
456 task_set_64Bit_data(task
);
458 if (!task_has_64Bit_data(task
)) {
462 task_clear_64Bit_data(task
);
465 /* FIXME: On x86, the thread save state flavor can diverge from the
466 * task's 64-bit feature flag due to the 32-bit/64-bit register save
467 * state dichotomy. Since we can be pre-empted in this interval,
468 * certain routines may observe the thread as being in an inconsistent
469 * state with respect to its task's 64-bitness.
472 #if defined(__x86_64__) || defined(__arm64__)
473 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
474 thread_mtx_lock(thread
);
475 machine_thread_switch_addrmode(thread
);
476 thread_mtx_unlock(thread
);
478 #endif /* defined(__x86_64__) || defined(__arm64__) */
485 task_get_64bit_data(task_t task
)
487 return task_has_64Bit_data(task
);
491 task_set_platform_binary(
493 boolean_t is_platform
)
497 task
->t_flags
|= TF_PLATFORM
;
498 /* set exc guard default behavior for first-party code */
499 task
->task_exc_guard
= (task_exc_guard_default
& TASK_EXC_GUARD_ALL
);
501 task
->t_flags
&= ~(TF_PLATFORM
);
502 /* set exc guard default behavior for third-party code */
503 task
->task_exc_guard
= ((task_exc_guard_default
>> TASK_EXC_GUARD_THIRD_PARTY_DEFAULT_SHIFT
) & TASK_EXC_GUARD_ALL
);
509 * Set or clear per-task TF_CA_CLIENT_WI flag according to specified argument.
510 * Returns "false" if flag is already set, and "true" in other cases.
513 task_set_ca_client_wi(
515 boolean_t set_or_clear
)
520 /* Tasks can have only one CA_CLIENT work interval */
521 if (task
->t_flags
& TF_CA_CLIENT_WI
) {
524 task
->t_flags
|= TF_CA_CLIENT_WI
;
527 task
->t_flags
&= ~TF_CA_CLIENT_WI
;
536 mach_vm_address_t addr
,
540 task
->all_image_info_addr
= addr
;
541 task
->all_image_info_size
= size
;
546 task_set_mach_header_address(
548 mach_vm_address_t addr
)
551 task
->mach_header_vm_address
= addr
;
556 task_bank_reset(__unused task_t task
)
558 if (task
->bank_context
!= NULL
) {
559 bank_task_destroy(task
);
564 * NOTE: This should only be called when the P_LINTRANSIT
565 * flag is set (the proc_trans lock is held) on the
566 * proc associated with the task.
569 task_bank_init(__unused task_t task
)
571 if (task
->bank_context
!= NULL
) {
572 panic("Task bank init called with non null bank context for task: %p and bank_context: %p", task
, task
->bank_context
);
574 bank_task_initialize(task
);
578 task_set_did_exec_flag(task_t task
)
580 task
->t_procflags
|= TPF_DID_EXEC
;
584 task_clear_exec_copy_flag(task_t task
)
586 task
->t_procflags
&= ~TPF_EXEC_COPY
;
590 task_get_return_wait_event(task_t task
)
592 return (event_t
)&task
->returnwait_inheritor
;
596 task_clear_return_wait(task_t task
, uint32_t flags
)
598 if (flags
& TCRW_CLEAR_INITIAL_WAIT
) {
599 thread_wakeup(task_get_return_wait_event(task
));
602 if (flags
& TCRW_CLEAR_FINAL_WAIT
) {
603 is_write_lock(task
->itk_space
);
605 task
->t_returnwaitflags
&= ~TRW_LRETURNWAIT
;
606 task
->returnwait_inheritor
= NULL
;
608 if (task
->t_returnwaitflags
& TRW_LRETURNWAITER
) {
609 struct turnstile
*turnstile
= turnstile_prepare((uintptr_t) task_get_return_wait_event(task
),
610 NULL
, TURNSTILE_NULL
, TURNSTILE_ULOCK
);
612 waitq_wakeup64_all(&turnstile
->ts_waitq
,
613 CAST_EVENT64_T(task_get_return_wait_event(task
)),
616 turnstile_update_inheritor(turnstile
, NULL
,
617 TURNSTILE_IMMEDIATE_UPDATE
| TURNSTILE_INHERITOR_THREAD
);
618 turnstile_update_inheritor_complete(turnstile
, TURNSTILE_INTERLOCK_HELD
);
620 turnstile_complete((uintptr_t) task_get_return_wait_event(task
), NULL
, NULL
, TURNSTILE_ULOCK
);
622 task
->t_returnwaitflags
&= ~TRW_LRETURNWAITER
;
624 is_write_unlock(task
->itk_space
);
628 void __attribute__((noreturn
))
629 task_wait_to_return(void)
631 task_t task
= current_task();
633 is_write_lock(task
->itk_space
);
635 if (task
->t_returnwaitflags
& TRW_LRETURNWAIT
) {
636 struct turnstile
*turnstile
= turnstile_prepare((uintptr_t) task_get_return_wait_event(task
),
637 NULL
, TURNSTILE_NULL
, TURNSTILE_ULOCK
);
640 task
->t_returnwaitflags
|= TRW_LRETURNWAITER
;
641 turnstile_update_inheritor(turnstile
, task
->returnwait_inheritor
,
642 (TURNSTILE_DELAYED_UPDATE
| TURNSTILE_INHERITOR_THREAD
));
644 waitq_assert_wait64(&turnstile
->ts_waitq
,
645 CAST_EVENT64_T(task_get_return_wait_event(task
)),
646 THREAD_UNINT
, TIMEOUT_WAIT_FOREVER
);
648 is_write_unlock(task
->itk_space
);
650 turnstile_update_inheritor_complete(turnstile
, TURNSTILE_INTERLOCK_NOT_HELD
);
652 thread_block(THREAD_CONTINUE_NULL
);
654 is_write_lock(task
->itk_space
);
655 } while (task
->t_returnwaitflags
& TRW_LRETURNWAIT
);
657 turnstile_complete((uintptr_t) task_get_return_wait_event(task
), NULL
, NULL
, TURNSTILE_ULOCK
);
660 is_write_unlock(task
->itk_space
);
666 * Before jumping to userspace and allowing this process to execute any code,
667 * notify any interested parties.
669 mac_proc_notify_exec_complete(current_proc());
672 thread_bootstrap_return();
675 #ifdef CONFIG_32BIT_TELEMETRY
677 task_consume_32bit_log_flag(task_t task
)
679 if ((task
->t_procflags
& TPF_LOG_32BIT_TELEMETRY
) != 0) {
680 task
->t_procflags
&= ~TPF_LOG_32BIT_TELEMETRY
;
688 task_set_32bit_log_flag(task_t task
)
690 task
->t_procflags
|= TPF_LOG_32BIT_TELEMETRY
;
692 #endif /* CONFIG_32BIT_TELEMETRY */
695 task_is_exec_copy(task_t task
)
697 return task_is_exec_copy_internal(task
);
701 task_did_exec(task_t task
)
703 return task_did_exec_internal(task
);
707 task_is_active(task_t task
)
713 task_is_halting(task_t task
)
715 return task
->halting
;
718 #if TASK_REFERENCE_LEAK_DEBUG
719 #include <kern/btlog.h>
721 static btlog_t
*task_ref_btlog
;
722 #define TASK_REF_OP_INCR 0x1
723 #define TASK_REF_OP_DECR 0x2
725 #define TASK_REF_NUM_RECORDS 100000
726 #define TASK_REF_BTDEPTH 7
729 task_reference_internal(task_t task
)
731 void * bt
[TASK_REF_BTDEPTH
];
735 os_ref_retain(&task
->ref_count
);
737 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
738 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_INCR
,
743 task_deallocate_internal(task_t task
)
745 void * bt
[TASK_REF_BTDEPTH
];
748 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
749 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_DECR
,
752 return os_ref_release(&task
->ref_count
);
755 #endif /* TASK_REFERENCE_LEAK_DEBUG */
761 * Configure per-task memory limit.
762 * The boot-arg is interpreted as Megabytes,
763 * and takes precedence over the device tree.
764 * Setting the boot-arg to 0 disables task limits.
766 if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb
,
767 sizeof(max_task_footprint_mb
))) {
769 * No limit was found in boot-args, so go look in the device tree.
771 if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb
,
772 sizeof(max_task_footprint_mb
))) {
774 * No limit was found in device tree.
776 max_task_footprint_mb
= 0;
780 if (max_task_footprint_mb
!= 0) {
781 #if CONFIG_MEMORYSTATUS
782 if (max_task_footprint_mb
< 50) {
783 printf("Warning: max_task_pmem %d below minimum.\n",
784 max_task_footprint_mb
);
785 max_task_footprint_mb
= 50;
787 printf("Limiting task physical memory footprint to %d MB\n",
788 max_task_footprint_mb
);
790 max_task_footprint
= (ledger_amount_t
)max_task_footprint_mb
* 1024 * 1024; // Convert MB to bytes
793 * Configure the per-task memory limit warning level.
794 * This is computed as a percentage.
796 max_task_footprint_warning_level
= 0;
798 if (max_mem
< 0x40000000) {
800 * On devices with < 1GB of memory:
801 * -- set warnings to 50MB below the per-task limit.
803 if (max_task_footprint_mb
> 50) {
804 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 50) * 100) / max_task_footprint_mb
;
808 * On devices with >= 1GB of memory:
809 * -- set warnings to 100MB below the per-task limit.
811 if (max_task_footprint_mb
> 100) {
812 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 100) * 100) / max_task_footprint_mb
;
817 * Never allow warning level to land below the default.
819 if (max_task_footprint_warning_level
< PHYS_FOOTPRINT_WARNING_LEVEL
) {
820 max_task_footprint_warning_level
= PHYS_FOOTPRINT_WARNING_LEVEL
;
823 printf("Limiting task physical memory warning to %d%%\n", max_task_footprint_warning_level
);
826 printf("Warning: max_task_pmem specified, but jetsam not configured; ignoring.\n");
827 #endif /* CONFIG_MEMORYSTATUS */
830 #if DEVELOPMENT || DEBUG
831 if (!PE_parse_boot_argn("exc_resource_threads",
832 &exc_resource_threads_enabled
,
833 sizeof(exc_resource_threads_enabled
))) {
834 exc_resource_threads_enabled
= 1;
836 PE_parse_boot_argn("task_exc_guard_default",
837 &task_exc_guard_default
,
838 sizeof(task_exc_guard_default
));
839 #endif /* DEVELOPMENT || DEBUG */
842 if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores
,
843 sizeof(hwm_user_cores
))) {
848 proc_init_cpumon_params();
850 if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate
, sizeof(task_wakeups_monitor_rate
))) {
851 task_wakeups_monitor_rate
= TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT
;
854 if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval
, sizeof(task_wakeups_monitor_interval
))) {
855 task_wakeups_monitor_interval
= TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL
;
858 if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct
,
859 sizeof(task_wakeups_monitor_ustackshots_trigger_pct
))) {
860 task_wakeups_monitor_ustackshots_trigger_pct
= TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER
;
863 if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource
,
864 sizeof(disable_exc_resource
))) {
865 disable_exc_resource
= 0;
868 if (!PE_parse_boot_argn("task_iomon_limit_mb", &task_iomon_limit_mb
, sizeof(task_iomon_limit_mb
))) {
869 task_iomon_limit_mb
= IOMON_DEFAULT_LIMIT
;
872 if (!PE_parse_boot_argn("task_iomon_interval_secs", &task_iomon_interval_secs
, sizeof(task_iomon_interval_secs
))) {
873 task_iomon_interval_secs
= IOMON_DEFAULT_INTERVAL
;
876 if (!PE_parse_boot_argn("io_telemetry_limit", &io_telemetry_limit
, sizeof(io_telemetry_limit
))) {
877 io_telemetry_limit
= IO_TELEMETRY_DEFAULT_LIMIT
;
881 * If we have coalitions, coalition_init() will call init_task_ledgers() as it
882 * sets up the ledgers for the default coalition. If we don't have coalitions,
883 * then we have to call it now.
885 #if CONFIG_COALITIONS
886 assert(task_ledger_template
);
887 #else /* CONFIG_COALITIONS */
889 #endif /* CONFIG_COALITIONS */
891 #if TASK_REFERENCE_LEAK_DEBUG
892 task_ref_btlog
= btlog_create(TASK_REF_NUM_RECORDS
, TASK_REF_BTDEPTH
, TRUE
/* caller_will_remove_entries_for_element? */);
893 assert(task_ref_btlog
);
897 * Create the kernel task as the first task.
900 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, TRUE
, TRUE
, TF_NONE
, TPF_NONE
, TWF_NONE
, &kernel_task
) != KERN_SUCCESS
)
902 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, FALSE
, FALSE
, TF_NONE
, TPF_NONE
, TWF_NONE
, &kernel_task
) != KERN_SUCCESS
)
904 { panic("task_init\n");}
906 #if defined(HAS_APPLE_PAC)
907 kernel_task
->rop_pid
= ml_default_rop_pid();
908 kernel_task
->jop_pid
= ml_default_jop_pid();
909 // kernel_task never runs at EL0, but machine_thread_state_convert_from/to_user() relies on
910 // disable_user_jop to be false for kernel threads (e.g. in exception delivery on thread_exception_daemon)
911 ml_task_set_disable_user_jop(kernel_task
, FALSE
);
914 vm_map_deallocate(kernel_task
->map
);
915 kernel_task
->map
= kernel_map
;
919 * Create a task running in the kernel address space. It may
920 * have its own map of size mem_size and may have ipc privileges.
924 __unused task_t parent_task
,
925 __unused vm_offset_t map_base
,
926 __unused vm_size_t map_size
,
927 __unused task_t
*child_task
)
929 return KERN_INVALID_ARGUMENT
;
935 __unused ledger_port_array_t ledger_ports
,
936 __unused mach_msg_type_number_t num_ledger_ports
,
937 __unused boolean_t inherit_memory
,
938 __unused task_t
*child_task
) /* OUT */
940 if (parent_task
== TASK_NULL
) {
941 return KERN_INVALID_ARGUMENT
;
945 * No longer supported: too many calls assume that a task has a valid
952 host_security_create_task_token(
953 host_security_t host_security
,
955 __unused security_token_t sec_token
,
956 __unused audit_token_t audit_token
,
957 __unused host_priv_t host_priv
,
958 __unused ledger_port_array_t ledger_ports
,
959 __unused mach_msg_type_number_t num_ledger_ports
,
960 __unused boolean_t inherit_memory
,
961 __unused task_t
*child_task
) /* OUT */
963 if (parent_task
== TASK_NULL
) {
964 return KERN_INVALID_ARGUMENT
;
967 if (host_security
== HOST_NULL
) {
968 return KERN_INVALID_SECURITY
;
972 * No longer supported.
982 * Physical footprint: This is the sum of:
983 * + (internal - alternate_accounting)
984 * + (internal_compressed - alternate_accounting_compressed)
986 * + purgeable_nonvolatile
987 * + purgeable_nonvolatile_compressed
991 * The task's anonymous memory, which on iOS is always resident.
993 * internal_compressed
994 * Amount of this task's internal memory which is held by the compressor.
995 * Such memory is no longer actually resident for the task [i.e., resident in its pmap],
996 * and could be either decompressed back into memory, or paged out to storage, depending
997 * on our implementation.
1000 * IOKit mappings: The total size of all IOKit mappings in this task, regardless of
1001 * clean/dirty or internal/external state].
1003 * alternate_accounting
1004 * The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
1005 * are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
1009 * pages_grabbed counts all page grabs in a task. It is also broken out into three subtypes
1010 * which track UPL, IOPL and Kernel page grabs.
1013 init_task_ledgers(void)
1015 ledger_template_t t
;
1017 assert(task_ledger_template
== NULL
);
1018 assert(kernel_task
== TASK_NULL
);
1021 PE_parse_boot_argn("pmap_ledgers_panic",
1022 &pmap_ledgers_panic
,
1023 sizeof(pmap_ledgers_panic
));
1024 PE_parse_boot_argn("pmap_ledgers_panic_leeway",
1025 &pmap_ledgers_panic_leeway
,
1026 sizeof(pmap_ledgers_panic_leeway
));
1027 #endif /* MACH_ASSERT */
1029 if ((t
= ledger_template_create("Per-task ledger")) == NULL
) {
1030 panic("couldn't create task ledger template");
1033 task_ledgers
.cpu_time
= ledger_entry_add(t
, "cpu_time", "sched", "ns");
1034 task_ledgers
.tkm_private
= ledger_entry_add(t
, "tkm_private",
1035 "physmem", "bytes");
1036 task_ledgers
.tkm_shared
= ledger_entry_add(t
, "tkm_shared", "physmem",
1038 task_ledgers
.phys_mem
= ledger_entry_add(t
, "phys_mem", "physmem",
1040 task_ledgers
.wired_mem
= ledger_entry_add(t
, "wired_mem", "physmem",
1042 task_ledgers
.internal
= ledger_entry_add(t
, "internal", "physmem",
1044 task_ledgers
.iokit_mapped
= ledger_entry_add(t
, "iokit_mapped", "mappings",
1046 task_ledgers
.alternate_accounting
= ledger_entry_add(t
, "alternate_accounting", "physmem",
1048 task_ledgers
.alternate_accounting_compressed
= ledger_entry_add(t
, "alternate_accounting_compressed", "physmem",
1050 task_ledgers
.page_table
= ledger_entry_add(t
, "page_table", "physmem",
1052 task_ledgers
.phys_footprint
= ledger_entry_add(t
, "phys_footprint", "physmem",
1054 task_ledgers
.internal_compressed
= ledger_entry_add(t
, "internal_compressed", "physmem",
1056 task_ledgers
.purgeable_volatile
= ledger_entry_add(t
, "purgeable_volatile", "physmem", "bytes");
1057 task_ledgers
.purgeable_nonvolatile
= ledger_entry_add(t
, "purgeable_nonvolatile", "physmem", "bytes");
1058 task_ledgers
.purgeable_volatile_compressed
= ledger_entry_add(t
, "purgeable_volatile_compress", "physmem", "bytes");
1059 task_ledgers
.purgeable_nonvolatile_compressed
= ledger_entry_add(t
, "purgeable_nonvolatile_compress", "physmem", "bytes");
1060 #if DEBUG || DEVELOPMENT
1061 task_ledgers
.pages_grabbed
= ledger_entry_add(t
, "pages_grabbed", "physmem", "count");
1062 task_ledgers
.pages_grabbed_kern
= ledger_entry_add(t
, "pages_grabbed_kern", "physmem", "count");
1063 task_ledgers
.pages_grabbed_iopl
= ledger_entry_add(t
, "pages_grabbed_iopl", "physmem", "count");
1064 task_ledgers
.pages_grabbed_upl
= ledger_entry_add(t
, "pages_grabbed_upl", "physmem", "count");
1066 task_ledgers
.tagged_nofootprint
= ledger_entry_add(t
, "tagged_nofootprint", "physmem", "bytes");
1067 task_ledgers
.tagged_footprint
= ledger_entry_add(t
, "tagged_footprint", "physmem", "bytes");
1068 task_ledgers
.tagged_nofootprint_compressed
= ledger_entry_add(t
, "tagged_nofootprint_compressed", "physmem", "bytes");
1069 task_ledgers
.tagged_footprint_compressed
= ledger_entry_add(t
, "tagged_footprint_compressed", "physmem", "bytes");
1070 task_ledgers
.network_volatile
= ledger_entry_add(t
, "network_volatile", "physmem", "bytes");
1071 task_ledgers
.network_nonvolatile
= ledger_entry_add(t
, "network_nonvolatile", "physmem", "bytes");
1072 task_ledgers
.network_volatile_compressed
= ledger_entry_add(t
, "network_volatile_compressed", "physmem", "bytes");
1073 task_ledgers
.network_nonvolatile_compressed
= ledger_entry_add(t
, "network_nonvolatile_compressed", "physmem", "bytes");
1074 task_ledgers
.media_nofootprint
= ledger_entry_add(t
, "media_nofootprint", "physmem", "bytes");
1075 task_ledgers
.media_footprint
= ledger_entry_add(t
, "media_footprint", "physmem", "bytes");
1076 task_ledgers
.media_nofootprint_compressed
= ledger_entry_add(t
, "media_nofootprint_compressed", "physmem", "bytes");
1077 task_ledgers
.media_footprint_compressed
= ledger_entry_add(t
, "media_footprint_compressed", "physmem", "bytes");
1078 task_ledgers
.graphics_nofootprint
= ledger_entry_add(t
, "graphics_nofootprint", "physmem", "bytes");
1079 task_ledgers
.graphics_footprint
= ledger_entry_add(t
, "graphics_footprint", "physmem", "bytes");
1080 task_ledgers
.graphics_nofootprint_compressed
= ledger_entry_add(t
, "graphics_nofootprint_compressed", "physmem", "bytes");
1081 task_ledgers
.graphics_footprint_compressed
= ledger_entry_add(t
, "graphics_footprint_compressed", "physmem", "bytes");
1082 task_ledgers
.neural_nofootprint
= ledger_entry_add(t
, "neural_nofootprint", "physmem", "bytes");
1083 task_ledgers
.neural_footprint
= ledger_entry_add(t
, "neural_footprint", "physmem", "bytes");
1084 task_ledgers
.neural_nofootprint_compressed
= ledger_entry_add(t
, "neural_nofootprint_compressed", "physmem", "bytes");
1085 task_ledgers
.neural_footprint_compressed
= ledger_entry_add(t
, "neural_footprint_compressed", "physmem", "bytes");
1088 task_ledgers
.frozen_to_swap
= ledger_entry_add(t
, "frozen_to_swap", "physmem", "bytes");
1089 #endif /* CONFIG_FREEZE */
1091 task_ledgers
.platform_idle_wakeups
= ledger_entry_add(t
, "platform_idle_wakeups", "power",
1093 task_ledgers
.interrupt_wakeups
= ledger_entry_add(t
, "interrupt_wakeups", "power",
1096 #if CONFIG_SCHED_SFI
1097 sfi_class_id_t class_id
, ledger_alias
;
1098 for (class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1099 task_ledgers
.sfi_wait_times
[class_id
] = -1;
1102 /* don't account for UNSPECIFIED */
1103 for (class_id
= SFI_CLASS_UNSPECIFIED
+ 1; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1104 ledger_alias
= sfi_get_ledger_alias_for_class(class_id
);
1105 if (ledger_alias
!= SFI_CLASS_UNSPECIFIED
) {
1106 /* Check to see if alias has been registered yet */
1107 if (task_ledgers
.sfi_wait_times
[ledger_alias
] != -1) {
1108 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
];
1110 /* Otherwise, initialize it first */
1111 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
] = sfi_ledger_entry_add(t
, ledger_alias
);
1114 task_ledgers
.sfi_wait_times
[class_id
] = sfi_ledger_entry_add(t
, class_id
);
1117 if (task_ledgers
.sfi_wait_times
[class_id
] < 0) {
1118 panic("couldn't create entries for task ledger template for SFI class 0x%x", class_id
);
1122 assert(task_ledgers
.sfi_wait_times
[MAX_SFI_CLASS_ID
- 1] != -1);
1123 #endif /* CONFIG_SCHED_SFI */
1125 task_ledgers
.cpu_time_billed_to_me
= ledger_entry_add(t
, "cpu_time_billed_to_me", "sched", "ns");
1126 task_ledgers
.cpu_time_billed_to_others
= ledger_entry_add(t
, "cpu_time_billed_to_others", "sched", "ns");
1127 task_ledgers
.physical_writes
= ledger_entry_add(t
, "physical_writes", "res", "bytes");
1128 task_ledgers
.logical_writes
= ledger_entry_add(t
, "logical_writes", "res", "bytes");
1129 task_ledgers
.logical_writes_to_external
= ledger_entry_add(t
, "logical_writes_to_external", "res", "bytes");
1130 #if CONFIG_PHYS_WRITE_ACCT
1131 task_ledgers
.fs_metadata_writes
= ledger_entry_add(t
, "fs_metadata_writes", "res", "bytes");
1132 #endif /* CONFIG_PHYS_WRITE_ACCT */
1133 task_ledgers
.energy_billed_to_me
= ledger_entry_add(t
, "energy_billed_to_me", "power", "nj");
1134 task_ledgers
.energy_billed_to_others
= ledger_entry_add(t
, "energy_billed_to_others", "power", "nj");
1136 if ((task_ledgers
.cpu_time
< 0) ||
1137 (task_ledgers
.tkm_private
< 0) ||
1138 (task_ledgers
.tkm_shared
< 0) ||
1139 (task_ledgers
.phys_mem
< 0) ||
1140 (task_ledgers
.wired_mem
< 0) ||
1141 (task_ledgers
.internal
< 0) ||
1142 (task_ledgers
.iokit_mapped
< 0) ||
1143 (task_ledgers
.alternate_accounting
< 0) ||
1144 (task_ledgers
.alternate_accounting_compressed
< 0) ||
1145 (task_ledgers
.page_table
< 0) ||
1146 (task_ledgers
.phys_footprint
< 0) ||
1147 (task_ledgers
.internal_compressed
< 0) ||
1148 (task_ledgers
.purgeable_volatile
< 0) ||
1149 (task_ledgers
.purgeable_nonvolatile
< 0) ||
1150 (task_ledgers
.purgeable_volatile_compressed
< 0) ||
1151 (task_ledgers
.purgeable_nonvolatile_compressed
< 0) ||
1152 (task_ledgers
.tagged_nofootprint
< 0) ||
1153 (task_ledgers
.tagged_footprint
< 0) ||
1154 (task_ledgers
.tagged_nofootprint_compressed
< 0) ||
1155 (task_ledgers
.tagged_footprint_compressed
< 0) ||
1157 (task_ledgers
.frozen_to_swap
< 0) ||
1158 #endif /* CONFIG_FREEZE */
1159 (task_ledgers
.network_volatile
< 0) ||
1160 (task_ledgers
.network_nonvolatile
< 0) ||
1161 (task_ledgers
.network_volatile_compressed
< 0) ||
1162 (task_ledgers
.network_nonvolatile_compressed
< 0) ||
1163 (task_ledgers
.media_nofootprint
< 0) ||
1164 (task_ledgers
.media_footprint
< 0) ||
1165 (task_ledgers
.media_nofootprint_compressed
< 0) ||
1166 (task_ledgers
.media_footprint_compressed
< 0) ||
1167 (task_ledgers
.graphics_nofootprint
< 0) ||
1168 (task_ledgers
.graphics_footprint
< 0) ||
1169 (task_ledgers
.graphics_nofootprint_compressed
< 0) ||
1170 (task_ledgers
.graphics_footprint_compressed
< 0) ||
1171 (task_ledgers
.neural_nofootprint
< 0) ||
1172 (task_ledgers
.neural_footprint
< 0) ||
1173 (task_ledgers
.neural_nofootprint_compressed
< 0) ||
1174 (task_ledgers
.neural_footprint_compressed
< 0) ||
1175 (task_ledgers
.platform_idle_wakeups
< 0) ||
1176 (task_ledgers
.interrupt_wakeups
< 0) ||
1177 (task_ledgers
.cpu_time_billed_to_me
< 0) || (task_ledgers
.cpu_time_billed_to_others
< 0) ||
1178 (task_ledgers
.physical_writes
< 0) ||
1179 (task_ledgers
.logical_writes
< 0) ||
1180 (task_ledgers
.logical_writes_to_external
< 0) ||
1181 #if CONFIG_PHYS_WRITE_ACCT
1182 (task_ledgers
.fs_metadata_writes
< 0) ||
1183 #endif /* CONFIG_PHYS_WRITE_ACCT */
1184 (task_ledgers
.energy_billed_to_me
< 0) ||
1185 (task_ledgers
.energy_billed_to_others
< 0)
1187 panic("couldn't create entries for task ledger template");
1190 ledger_track_credit_only(t
, task_ledgers
.phys_footprint
);
1191 ledger_track_credit_only(t
, task_ledgers
.page_table
);
1192 ledger_track_credit_only(t
, task_ledgers
.internal
);
1193 ledger_track_credit_only(t
, task_ledgers
.internal_compressed
);
1194 ledger_track_credit_only(t
, task_ledgers
.iokit_mapped
);
1195 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting
);
1196 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting_compressed
);
1197 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile
);
1198 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile
);
1199 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile_compressed
);
1200 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
1201 #if DEBUG || DEVELOPMENT
1202 ledger_track_credit_only(t
, task_ledgers
.pages_grabbed
);
1203 ledger_track_credit_only(t
, task_ledgers
.pages_grabbed_kern
);
1204 ledger_track_credit_only(t
, task_ledgers
.pages_grabbed_iopl
);
1205 ledger_track_credit_only(t
, task_ledgers
.pages_grabbed_upl
);
1208 ledger_track_credit_only(t
, task_ledgers
.tagged_nofootprint
);
1209 ledger_track_credit_only(t
, task_ledgers
.tagged_footprint
);
1210 ledger_track_credit_only(t
, task_ledgers
.tagged_nofootprint_compressed
);
1211 ledger_track_credit_only(t
, task_ledgers
.tagged_footprint_compressed
);
1212 ledger_track_credit_only(t
, task_ledgers
.network_volatile
);
1213 ledger_track_credit_only(t
, task_ledgers
.network_nonvolatile
);
1214 ledger_track_credit_only(t
, task_ledgers
.network_volatile_compressed
);
1215 ledger_track_credit_only(t
, task_ledgers
.network_nonvolatile_compressed
);
1216 ledger_track_credit_only(t
, task_ledgers
.media_nofootprint
);
1217 ledger_track_credit_only(t
, task_ledgers
.media_footprint
);
1218 ledger_track_credit_only(t
, task_ledgers
.media_nofootprint_compressed
);
1219 ledger_track_credit_only(t
, task_ledgers
.media_footprint_compressed
);
1220 ledger_track_credit_only(t
, task_ledgers
.graphics_nofootprint
);
1221 ledger_track_credit_only(t
, task_ledgers
.graphics_footprint
);
1222 ledger_track_credit_only(t
, task_ledgers
.graphics_nofootprint_compressed
);
1223 ledger_track_credit_only(t
, task_ledgers
.graphics_footprint_compressed
);
1224 ledger_track_credit_only(t
, task_ledgers
.neural_nofootprint
);
1225 ledger_track_credit_only(t
, task_ledgers
.neural_footprint
);
1226 ledger_track_credit_only(t
, task_ledgers
.neural_nofootprint_compressed
);
1227 ledger_track_credit_only(t
, task_ledgers
.neural_footprint_compressed
);
1229 ledger_track_maximum(t
, task_ledgers
.phys_footprint
, 60);
1231 if (pmap_ledgers_panic
) {
1232 ledger_panic_on_negative(t
, task_ledgers
.phys_footprint
);
1233 ledger_panic_on_negative(t
, task_ledgers
.page_table
);
1234 ledger_panic_on_negative(t
, task_ledgers
.internal
);
1235 ledger_panic_on_negative(t
, task_ledgers
.internal_compressed
);
1236 ledger_panic_on_negative(t
, task_ledgers
.iokit_mapped
);
1237 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting
);
1238 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting_compressed
);
1239 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile
);
1240 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile
);
1241 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile_compressed
);
1242 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
1243 #if CONFIG_PHYS_WRITE_ACCT
1244 ledger_panic_on_negative(t
, task_ledgers
.fs_metadata_writes
);
1245 #endif /* CONFIG_PHYS_WRITE_ACCT */
1247 ledger_panic_on_negative(t
, task_ledgers
.tagged_nofootprint
);
1248 ledger_panic_on_negative(t
, task_ledgers
.tagged_footprint
);
1249 ledger_panic_on_negative(t
, task_ledgers
.tagged_nofootprint_compressed
);
1250 ledger_panic_on_negative(t
, task_ledgers
.tagged_footprint_compressed
);
1251 ledger_panic_on_negative(t
, task_ledgers
.network_volatile
);
1252 ledger_panic_on_negative(t
, task_ledgers
.network_nonvolatile
);
1253 ledger_panic_on_negative(t
, task_ledgers
.network_volatile_compressed
);
1254 ledger_panic_on_negative(t
, task_ledgers
.network_nonvolatile_compressed
);
1255 ledger_panic_on_negative(t
, task_ledgers
.media_nofootprint
);
1256 ledger_panic_on_negative(t
, task_ledgers
.media_footprint
);
1257 ledger_panic_on_negative(t
, task_ledgers
.media_nofootprint_compressed
);
1258 ledger_panic_on_negative(t
, task_ledgers
.media_footprint_compressed
);
1259 ledger_panic_on_negative(t
, task_ledgers
.graphics_nofootprint
);
1260 ledger_panic_on_negative(t
, task_ledgers
.graphics_footprint
);
1261 ledger_panic_on_negative(t
, task_ledgers
.graphics_nofootprint_compressed
);
1262 ledger_panic_on_negative(t
, task_ledgers
.graphics_footprint_compressed
);
1263 ledger_panic_on_negative(t
, task_ledgers
.neural_nofootprint
);
1264 ledger_panic_on_negative(t
, task_ledgers
.neural_footprint
);
1265 ledger_panic_on_negative(t
, task_ledgers
.neural_nofootprint_compressed
);
1266 ledger_panic_on_negative(t
, task_ledgers
.neural_footprint_compressed
);
1268 #endif /* MACH_ASSERT */
1270 #if CONFIG_MEMORYSTATUS
1271 ledger_set_callback(t
, task_ledgers
.phys_footprint
, task_footprint_exceeded
, NULL
, NULL
);
1272 #endif /* CONFIG_MEMORYSTATUS */
1274 ledger_set_callback(t
, task_ledgers
.interrupt_wakeups
,
1275 task_wakeups_rate_exceeded
, NULL
, NULL
);
1276 ledger_set_callback(t
, task_ledgers
.physical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_PHYSICAL_WRITES
, NULL
);
1279 ledger_template_complete_secure_alloc(t
);
1280 #else /* XNU_MONITOR */
1281 ledger_template_complete(t
);
1282 #endif /* XNU_MONITOR */
1283 task_ledger_template
= t
;
1286 os_refgrp_decl(static, task_refgrp
, "task", NULL
);
1289 task_create_internal(
1291 coalition_t
*parent_coalitions __unused
,
1292 boolean_t inherit_memory
,
1293 __unused boolean_t is_64bit
,
1294 boolean_t is_64bit_data
,
1296 uint32_t t_procflags
,
1297 uint8_t t_returnwaitflags
,
1298 task_t
*child_task
) /* OUT */
1301 vm_shared_region_t shared_region
;
1302 ledger_t ledger
= NULL
;
1304 new_task
= (task_t
) zalloc(task_zone
);
1306 if (new_task
== TASK_NULL
) {
1307 return KERN_RESOURCE_SHORTAGE
;
1310 /* one ref for just being alive; one for our caller */
1311 os_ref_init_count(&new_task
->ref_count
, &task_refgrp
, 2);
1313 /* allocate with active entries */
1314 assert(task_ledger_template
!= NULL
);
1315 if ((ledger
= ledger_instantiate(task_ledger_template
,
1316 LEDGER_CREATE_ACTIVE_ENTRIES
)) == NULL
) {
1317 zfree(task_zone
, new_task
);
1318 return KERN_RESOURCE_SHORTAGE
;
1321 #if defined(HAS_APPLE_PAC)
1322 ml_task_set_rop_pid(new_task
, parent_task
, inherit_memory
);
1323 ml_task_set_jop_pid(new_task
, parent_task
, inherit_memory
);
1324 ml_task_set_disable_user_jop(new_task
, inherit_memory
? parent_task
->disable_user_jop
: FALSE
);
1328 new_task
->ledger
= ledger
;
1330 #if defined(CONFIG_SCHED_MULTIQ)
1331 new_task
->sched_group
= sched_group_create();
1334 /* if inherit_memory is true, parent_task MUST not be NULL */
1335 if (!(t_flags
& TF_CORPSE_FORK
) && inherit_memory
) {
1336 new_task
->map
= vm_map_fork(ledger
, parent_task
->map
, 0);
1338 unsigned int pmap_flags
= is_64bit
? PMAP_CREATE_64BIT
: 0;
1339 new_task
->map
= vm_map_create(pmap_create_options(ledger
, 0, pmap_flags
),
1340 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
1341 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
1344 /* Inherit memlock limit from parent */
1346 vm_map_set_user_wire_limit(new_task
->map
, (vm_size_t
)parent_task
->map
->user_wire_limit
);
1349 lck_mtx_init(&new_task
->lock
, &task_lck_grp
, &task_lck_attr
);
1350 queue_init(&new_task
->threads
);
1351 new_task
->suspend_count
= 0;
1352 new_task
->thread_count
= 0;
1353 new_task
->active_thread_count
= 0;
1354 new_task
->user_stop_count
= 0;
1355 new_task
->legacy_stop_count
= 0;
1356 new_task
->active
= TRUE
;
1357 new_task
->halting
= FALSE
;
1358 new_task
->priv_flags
= 0;
1359 new_task
->t_flags
= t_flags
;
1360 new_task
->t_procflags
= t_procflags
;
1361 new_task
->t_returnwaitflags
= t_returnwaitflags
;
1362 new_task
->returnwait_inheritor
= current_thread();
1363 new_task
->importance
= 0;
1364 new_task
->crashed_thread_id
= 0;
1365 new_task
->exec_token
= 0;
1366 new_task
->watchports
= NULL
;
1367 new_task
->restartable_ranges
= NULL
;
1368 new_task
->task_exc_guard
= 0;
1370 new_task
->bank_context
= NULL
;
1373 new_task
->bsd_info
= NULL
;
1374 new_task
->corpse_info
= NULL
;
1375 #endif /* MACH_BSD */
1378 new_task
->crash_label
= NULL
;
1380 new_task
->mach_trap_filter_mask
= NULL
;
1381 new_task
->mach_kobj_filter_mask
= NULL
;
1384 #if CONFIG_MEMORYSTATUS
1385 if (max_task_footprint
!= 0) {
1386 ledger_set_limit(ledger
, task_ledgers
.phys_footprint
, max_task_footprint
, PHYS_FOOTPRINT_WARNING_LEVEL
);
1388 #endif /* CONFIG_MEMORYSTATUS */
1390 if (task_wakeups_monitor_rate
!= 0) {
1391 uint32_t flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
1392 int32_t rate
; // Ignored because of WAKEMON_SET_DEFAULTS
1393 task_wakeups_monitor_ctl(new_task
, &flags
, &rate
);
1396 #if CONFIG_IO_ACCOUNTING
1397 uint32_t flags
= IOMON_ENABLE
;
1398 task_io_monitor_ctl(new_task
, &flags
);
1399 #endif /* CONFIG_IO_ACCOUNTING */
1401 machine_task_init(new_task
, parent_task
, inherit_memory
);
1403 new_task
->task_debug
= NULL
;
1405 #if DEVELOPMENT || DEBUG
1406 new_task
->task_unnested
= FALSE
;
1407 new_task
->task_disconnected_count
= 0;
1409 queue_init(&new_task
->semaphore_list
);
1410 new_task
->semaphores_owned
= 0;
1412 ipc_task_init(new_task
, parent_task
);
1414 new_task
->vtimers
= 0;
1416 new_task
->shared_region
= NULL
;
1418 new_task
->affinity_space
= NULL
;
1420 new_task
->t_kpc
= 0;
1422 new_task
->pidsuspended
= FALSE
;
1423 new_task
->frozen
= FALSE
;
1424 new_task
->changing_freeze_state
= FALSE
;
1425 new_task
->rusage_cpu_flags
= 0;
1426 new_task
->rusage_cpu_percentage
= 0;
1427 new_task
->rusage_cpu_interval
= 0;
1428 new_task
->rusage_cpu_deadline
= 0;
1429 new_task
->rusage_cpu_callt
= NULL
;
1431 new_task
->suspends_outstanding
= 0;
1435 new_task
->hv_task_target
= NULL
;
1436 #endif /* HYPERVISOR */
1438 #if CONFIG_TASKWATCH
1439 queue_init(&new_task
->task_watchers
);
1440 new_task
->num_taskwatchers
= 0;
1441 new_task
->watchapplying
= 0;
1442 #endif /* CONFIG_TASKWATCH */
1444 new_task
->mem_notify_reserved
= 0;
1445 new_task
->memlimit_attrs_reserved
= 0;
1447 new_task
->requested_policy
= default_task_requested_policy
;
1448 new_task
->effective_policy
= default_task_effective_policy
;
1450 task_importance_init_from_parent(new_task
, parent_task
);
1452 if (parent_task
!= TASK_NULL
) {
1453 new_task
->sec_token
= parent_task
->sec_token
;
1454 new_task
->audit_token
= parent_task
->audit_token
;
1456 /* inherit the parent's shared region */
1457 shared_region
= vm_shared_region_get(parent_task
);
1458 vm_shared_region_set(new_task
, shared_region
);
1460 #if __has_feature(ptrauth_calls)
1461 /* use parent's shared_region_id */
1462 char *shared_region_id
= task_get_vm_shared_region_id_and_jop_pid(parent_task
, NULL
);
1463 if (shared_region_id
!= NULL
) {
1464 shared_region_key_alloc(shared_region_id
, FALSE
, 0); /* get a reference */
1466 task_set_shared_region_id(new_task
, shared_region_id
);
1467 #endif /* __has_feature(ptrauth_calls) */
1469 if (task_has_64Bit_addr(parent_task
)) {
1470 task_set_64Bit_addr(new_task
);
1473 if (task_has_64Bit_data(parent_task
)) {
1474 task_set_64Bit_data(new_task
);
1477 new_task
->all_image_info_addr
= parent_task
->all_image_info_addr
;
1478 new_task
->all_image_info_size
= parent_task
->all_image_info_size
;
1479 new_task
->mach_header_vm_address
= 0;
1481 if (inherit_memory
&& parent_task
->affinity_space
) {
1482 task_affinity_create(parent_task
, new_task
);
1485 new_task
->pset_hint
= parent_task
->pset_hint
= task_choose_pset(parent_task
);
1487 if (parent_task
->t_flags
& TF_NO_SMT
) {
1488 new_task
->t_flags
|= TF_NO_SMT
;
1491 if (parent_task
->t_flags
& TF_TECS
) {
1492 new_task
->t_flags
|= TF_TECS
;
1495 if (parent_task
->t_flags
& TF_FILTER_MSG
) {
1496 new_task
->t_flags
|= TF_FILTER_MSG
;
1499 new_task
->priority
= BASEPRI_DEFAULT
;
1500 new_task
->max_priority
= MAXPRI_USER
;
1502 task_policy_create(new_task
, parent_task
);
1504 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
1505 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
1508 task_set_64Bit_addr(new_task
);
1512 if (is_64bit_data
) {
1513 task_set_64Bit_data(new_task
);
1516 new_task
->all_image_info_addr
= (mach_vm_address_t
)0;
1517 new_task
->all_image_info_size
= (mach_vm_size_t
)0;
1519 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
1521 if (kernel_task
== TASK_NULL
) {
1522 new_task
->priority
= BASEPRI_KERNEL
;
1523 new_task
->max_priority
= MAXPRI_KERNEL
;
1525 new_task
->priority
= BASEPRI_DEFAULT
;
1526 new_task
->max_priority
= MAXPRI_USER
;
1530 bzero(new_task
->coalition
, sizeof(new_task
->coalition
));
1531 for (int i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1532 queue_chain_init(new_task
->task_coalition
[i
]);
1535 /* Allocate I/O Statistics */
1536 new_task
->task_io_stats
= kheap_alloc(KHEAP_DATA_BUFFERS
,
1537 sizeof(struct io_stat_info
), Z_WAITOK
| Z_ZERO
);
1538 assert(new_task
->task_io_stats
!= NULL
);
1540 bzero(&(new_task
->cpu_time_eqos_stats
), sizeof(new_task
->cpu_time_eqos_stats
));
1541 bzero(&(new_task
->cpu_time_rqos_stats
), sizeof(new_task
->cpu_time_rqos_stats
));
1543 bzero(&new_task
->extmod_statistics
, sizeof(new_task
->extmod_statistics
));
1545 /* Copy resource acc. info from Parent for Corpe Forked task. */
1546 if (parent_task
!= NULL
&& (t_flags
& TF_CORPSE_FORK
)) {
1547 task_rollup_accounting_info(new_task
, parent_task
);
1549 /* Initialize to zero for standard fork/spawn case */
1550 new_task
->total_user_time
= 0;
1551 new_task
->total_system_time
= 0;
1552 new_task
->total_ptime
= 0;
1553 new_task
->total_runnable_time
= 0;
1554 new_task
->faults
= 0;
1555 new_task
->pageins
= 0;
1556 new_task
->cow_faults
= 0;
1557 new_task
->messages_sent
= 0;
1558 new_task
->messages_received
= 0;
1559 new_task
->syscalls_mach
= 0;
1560 new_task
->syscalls_unix
= 0;
1561 new_task
->c_switch
= 0;
1562 new_task
->p_switch
= 0;
1563 new_task
->ps_switch
= 0;
1564 new_task
->decompressions
= 0;
1565 new_task
->low_mem_notified_warn
= 0;
1566 new_task
->low_mem_notified_critical
= 0;
1567 new_task
->purged_memory_warn
= 0;
1568 new_task
->purged_memory_critical
= 0;
1569 new_task
->low_mem_privileged_listener
= 0;
1570 new_task
->memlimit_is_active
= 0;
1571 new_task
->memlimit_is_fatal
= 0;
1572 new_task
->memlimit_active_exc_resource
= 0;
1573 new_task
->memlimit_inactive_exc_resource
= 0;
1574 new_task
->task_timer_wakeups_bin_1
= 0;
1575 new_task
->task_timer_wakeups_bin_2
= 0;
1576 new_task
->task_gpu_ns
= 0;
1577 new_task
->task_writes_counters_internal
.task_immediate_writes
= 0;
1578 new_task
->task_writes_counters_internal
.task_deferred_writes
= 0;
1579 new_task
->task_writes_counters_internal
.task_invalidated_writes
= 0;
1580 new_task
->task_writes_counters_internal
.task_metadata_writes
= 0;
1581 new_task
->task_writes_counters_external
.task_immediate_writes
= 0;
1582 new_task
->task_writes_counters_external
.task_deferred_writes
= 0;
1583 new_task
->task_writes_counters_external
.task_invalidated_writes
= 0;
1584 new_task
->task_writes_counters_external
.task_metadata_writes
= 0;
1585 #if CONFIG_PHYS_WRITE_ACCT
1586 new_task
->task_fs_metadata_writes
= 0;
1587 #endif /* CONFIG_PHYS_WRITE_ACCT */
1589 new_task
->task_energy
= 0;
1591 memset(&new_task
->task_monotonic
, 0, sizeof(new_task
->task_monotonic
));
1592 #endif /* MONOTONIC */
1596 #if CONFIG_COALITIONS
1597 if (!(t_flags
& TF_CORPSE_FORK
)) {
1598 /* TODO: there is no graceful failure path here... */
1599 if (parent_coalitions
&& parent_coalitions
[COALITION_TYPE_RESOURCE
]) {
1600 coalitions_adopt_task(parent_coalitions
, new_task
);
1601 } else if (parent_task
&& parent_task
->coalition
[COALITION_TYPE_RESOURCE
]) {
1603 * all tasks at least have a resource coalition, so
1604 * if the parent has one then inherit all coalitions
1605 * the parent is a part of
1607 coalitions_adopt_task(parent_task
->coalition
, new_task
);
1609 /* TODO: assert that new_task will be PID 1 (launchd) */
1610 coalitions_adopt_init_task(new_task
);
1613 * on exec, we need to transfer the coalition roles from the
1614 * parent task to the exec copy task.
1616 if (parent_task
&& (t_procflags
& TPF_EXEC_COPY
)) {
1617 int coal_roles
[COALITION_NUM_TYPES
];
1618 task_coalition_roles(parent_task
, coal_roles
);
1619 (void)coalitions_set_roles(new_task
->coalition
, new_task
, coal_roles
);
1622 coalitions_adopt_corpse_task(new_task
);
1625 if (new_task
->coalition
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
1626 panic("created task is not a member of a resource coalition");
1628 #endif /* CONFIG_COALITIONS */
1630 new_task
->dispatchqueue_offset
= 0;
1631 if (parent_task
!= NULL
) {
1632 new_task
->dispatchqueue_offset
= parent_task
->dispatchqueue_offset
;
1635 new_task
->task_can_transfer_memory_ownership
= FALSE
;
1636 new_task
->task_volatile_objects
= 0;
1637 new_task
->task_nonvolatile_objects
= 0;
1638 new_task
->task_objects_disowning
= FALSE
;
1639 new_task
->task_objects_disowned
= FALSE
;
1640 new_task
->task_owned_objects
= 0;
1641 queue_init(&new_task
->task_objq
);
1644 queue_init(&new_task
->task_frozen_cseg_q
);
1645 #endif /* CONFIG_FREEZE */
1647 task_objq_lock_init(new_task
);
1650 new_task
->task_legacy_footprint
= FALSE
;
1651 new_task
->task_extra_footprint_limit
= FALSE
;
1652 new_task
->task_ios13extended_footprint_limit
= FALSE
;
1653 #endif /* __arm64__ */
1654 new_task
->task_region_footprint
= FALSE
;
1655 new_task
->task_has_crossed_thread_limit
= FALSE
;
1656 new_task
->task_thread_limit
= 0;
1657 #if CONFIG_SECLUDED_MEMORY
1658 new_task
->task_can_use_secluded_mem
= FALSE
;
1659 new_task
->task_could_use_secluded_mem
= FALSE
;
1660 new_task
->task_could_also_use_secluded_mem
= FALSE
;
1661 new_task
->task_suppressed_secluded
= FALSE
;
1662 #endif /* CONFIG_SECLUDED_MEMORY */
1665 * t_flags is set up above. But since we don't
1666 * support darkwake mode being set that way
1667 * currently, we clear it out here explicitly.
1669 new_task
->t_flags
&= ~(TF_DARKWAKE_MODE
);
1671 queue_init(&new_task
->io_user_clients
);
1672 new_task
->loadTag
= 0;
1674 ipc_task_enable(new_task
);
1676 lck_mtx_lock(&tasks_threads_lock
);
1677 queue_enter(&tasks
, new_task
, task_t
, tasks
);
1679 if (tasks_suspend_state
) {
1680 task_suspend_internal(new_task
);
1682 lck_mtx_unlock(&tasks_threads_lock
);
1684 *child_task
= new_task
;
1685 return KERN_SUCCESS
;
1689 * task_rollup_accounting_info
1691 * Roll up accounting stats. Used to rollup stats
1692 * for exec copy task and corpse fork.
1695 task_rollup_accounting_info(task_t to_task
, task_t from_task
)
1697 assert(from_task
!= to_task
);
1699 to_task
->total_user_time
= from_task
->total_user_time
;
1700 to_task
->total_system_time
= from_task
->total_system_time
;
1701 to_task
->total_ptime
= from_task
->total_ptime
;
1702 to_task
->total_runnable_time
= from_task
->total_runnable_time
;
1703 to_task
->faults
= from_task
->faults
;
1704 to_task
->pageins
= from_task
->pageins
;
1705 to_task
->cow_faults
= from_task
->cow_faults
;
1706 to_task
->decompressions
= from_task
->decompressions
;
1707 to_task
->messages_sent
= from_task
->messages_sent
;
1708 to_task
->messages_received
= from_task
->messages_received
;
1709 to_task
->syscalls_mach
= from_task
->syscalls_mach
;
1710 to_task
->syscalls_unix
= from_task
->syscalls_unix
;
1711 to_task
->c_switch
= from_task
->c_switch
;
1712 to_task
->p_switch
= from_task
->p_switch
;
1713 to_task
->ps_switch
= from_task
->ps_switch
;
1714 to_task
->extmod_statistics
= from_task
->extmod_statistics
;
1715 to_task
->low_mem_notified_warn
= from_task
->low_mem_notified_warn
;
1716 to_task
->low_mem_notified_critical
= from_task
->low_mem_notified_critical
;
1717 to_task
->purged_memory_warn
= from_task
->purged_memory_warn
;
1718 to_task
->purged_memory_critical
= from_task
->purged_memory_critical
;
1719 to_task
->low_mem_privileged_listener
= from_task
->low_mem_privileged_listener
;
1720 *to_task
->task_io_stats
= *from_task
->task_io_stats
;
1721 to_task
->cpu_time_eqos_stats
= from_task
->cpu_time_eqos_stats
;
1722 to_task
->cpu_time_rqos_stats
= from_task
->cpu_time_rqos_stats
;
1723 to_task
->task_timer_wakeups_bin_1
= from_task
->task_timer_wakeups_bin_1
;
1724 to_task
->task_timer_wakeups_bin_2
= from_task
->task_timer_wakeups_bin_2
;
1725 to_task
->task_gpu_ns
= from_task
->task_gpu_ns
;
1726 to_task
->task_writes_counters_internal
.task_immediate_writes
= from_task
->task_writes_counters_internal
.task_immediate_writes
;
1727 to_task
->task_writes_counters_internal
.task_deferred_writes
= from_task
->task_writes_counters_internal
.task_deferred_writes
;
1728 to_task
->task_writes_counters_internal
.task_invalidated_writes
= from_task
->task_writes_counters_internal
.task_invalidated_writes
;
1729 to_task
->task_writes_counters_internal
.task_metadata_writes
= from_task
->task_writes_counters_internal
.task_metadata_writes
;
1730 to_task
->task_writes_counters_external
.task_immediate_writes
= from_task
->task_writes_counters_external
.task_immediate_writes
;
1731 to_task
->task_writes_counters_external
.task_deferred_writes
= from_task
->task_writes_counters_external
.task_deferred_writes
;
1732 to_task
->task_writes_counters_external
.task_invalidated_writes
= from_task
->task_writes_counters_external
.task_invalidated_writes
;
1733 to_task
->task_writes_counters_external
.task_metadata_writes
= from_task
->task_writes_counters_external
.task_metadata_writes
;
1734 #if CONFIG_PHYS_WRITE_ACCT
1735 to_task
->task_fs_metadata_writes
= from_task
->task_fs_metadata_writes
;
1736 #endif /* CONFIG_PHYS_WRITE_ACCT */
1737 to_task
->task_energy
= from_task
->task_energy
;
1739 /* Skip ledger roll up for memory accounting entries */
1740 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time
);
1741 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.platform_idle_wakeups
);
1742 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.interrupt_wakeups
);
1743 #if CONFIG_SCHED_SFI
1744 for (sfi_class_id_t class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1745 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.sfi_wait_times
[class_id
]);
1748 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_me
);
1749 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_others
);
1750 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.physical_writes
);
1751 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.logical_writes
);
1752 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.energy_billed_to_me
);
1753 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.energy_billed_to_others
);
1756 int task_dropped_imp_count
= 0;
1761 * Drop a reference on a task.
1767 ledger_amount_t credit
, debit
, interrupt_wakeups
, platform_idle_wakeups
;
1768 os_ref_count_t refs
;
1770 if (task
== TASK_NULL
) {
1774 refs
= task_deallocate_internal(task
);
1776 #if IMPORTANCE_INHERITANCE
1779 * If last ref potentially comes from the task's importance,
1780 * disconnect it. But more task refs may be added before
1781 * that completes, so wait for the reference to go to zero
1782 * naturally (it may happen on a recursive task_deallocate()
1783 * from the ipc_importance_disconnect_task() call).
1785 if (IIT_NULL
!= task
->task_imp_base
) {
1786 ipc_importance_disconnect_task(task
);
1790 #endif /* IMPORTANCE_INHERITANCE */
1797 * The task should be dead at this point. Ensure other resources
1798 * like threads, are gone before we trash the world.
1800 assert(queue_empty(&task
->threads
));
1801 assert(task
->bsd_info
== NULL
);
1802 assert(!is_active(task
->itk_space
));
1803 assert(!task
->active
);
1804 assert(task
->active_thread_count
== 0);
1806 lck_mtx_lock(&tasks_threads_lock
);
1807 assert(terminated_tasks_count
> 0);
1808 queue_remove(&terminated_tasks
, task
, task_t
, tasks
);
1809 terminated_tasks_count
--;
1810 lck_mtx_unlock(&tasks_threads_lock
);
1813 * remove the reference on bank context
1815 task_bank_reset(task
);
1817 if (task
->task_io_stats
) {
1818 kheap_free(KHEAP_DATA_BUFFERS
, task
->task_io_stats
,
1819 sizeof(struct io_stat_info
));
1823 * Give the machine dependent code a chance
1824 * to perform cleanup before ripping apart
1827 machine_task_terminate(task
);
1829 ipc_task_terminate(task
);
1831 /* let iokit know */
1832 iokit_task_terminate(task
);
1834 if (task
->affinity_space
) {
1835 task_affinity_deallocate(task
);
1839 if (task
->ledger
!= NULL
&&
1840 task
->map
!= NULL
&&
1841 task
->map
->pmap
!= NULL
&&
1842 task
->map
->pmap
->ledger
!= NULL
) {
1843 assert(task
->ledger
== task
->map
->pmap
->ledger
);
1845 #endif /* MACH_ASSERT */
1847 vm_owned_objects_disown(task
);
1848 assert(task
->task_objects_disowned
);
1849 if (task
->task_volatile_objects
!= 0 ||
1850 task
->task_nonvolatile_objects
!= 0 ||
1851 task
->task_owned_objects
!= 0) {
1852 panic("task_deallocate(%p): "
1853 "volatile_objects=%d nonvolatile_objects=%d owned=%d\n",
1855 task
->task_volatile_objects
,
1856 task
->task_nonvolatile_objects
,
1857 task
->task_owned_objects
);
1860 vm_map_deallocate(task
->map
);
1861 is_release(task
->itk_space
);
1862 if (task
->restartable_ranges
) {
1863 restartable_ranges_release(task
->restartable_ranges
);
1866 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
1867 &interrupt_wakeups
, &debit
);
1868 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
1869 &platform_idle_wakeups
, &debit
);
1871 #if defined(CONFIG_SCHED_MULTIQ)
1872 sched_group_destroy(task
->sched_group
);
1875 /* Accumulate statistics for dead tasks */
1876 lck_spin_lock(&dead_task_statistics_lock
);
1877 dead_task_statistics
.total_user_time
+= task
->total_user_time
;
1878 dead_task_statistics
.total_system_time
+= task
->total_system_time
;
1880 dead_task_statistics
.task_interrupt_wakeups
+= interrupt_wakeups
;
1881 dead_task_statistics
.task_platform_idle_wakeups
+= platform_idle_wakeups
;
1883 dead_task_statistics
.task_timer_wakeups_bin_1
+= task
->task_timer_wakeups_bin_1
;
1884 dead_task_statistics
.task_timer_wakeups_bin_2
+= task
->task_timer_wakeups_bin_2
;
1885 dead_task_statistics
.total_ptime
+= task
->total_ptime
;
1886 dead_task_statistics
.total_pset_switches
+= task
->ps_switch
;
1887 dead_task_statistics
.task_gpu_ns
+= task
->task_gpu_ns
;
1888 dead_task_statistics
.task_energy
+= task
->task_energy
;
1890 lck_spin_unlock(&dead_task_statistics_lock
);
1891 lck_mtx_destroy(&task
->lock
, &task_lck_grp
);
1893 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_private
, &credit
,
1895 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_private
.alloc
);
1896 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_private
.free
);
1898 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_shared
, &credit
,
1900 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_shared
.alloc
);
1901 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_shared
.free
);
1903 ledger_dereference(task
->ledger
);
1905 #if TASK_REFERENCE_LEAK_DEBUG
1906 btlog_remove_entries_for_element(task_ref_btlog
, task
);
1909 #if CONFIG_COALITIONS
1910 task_release_coalitions(task
);
1911 #endif /* CONFIG_COALITIONS */
1913 bzero(task
->coalition
, sizeof(task
->coalition
));
1916 /* clean up collected information since last reference to task is gone */
1917 if (task
->corpse_info
) {
1918 void *corpse_info_kernel
= kcdata_memory_get_begin_addr(task
->corpse_info
);
1919 task_crashinfo_destroy(task
->corpse_info
);
1920 task
->corpse_info
= NULL
;
1921 if (corpse_info_kernel
) {
1922 kheap_free(KHEAP_DATA_BUFFERS
, corpse_info_kernel
,
1923 CORPSEINFO_ALLOCATION_SIZE
);
1929 if (task
->crash_label
) {
1930 mac_exc_free_label(task
->crash_label
);
1931 task
->crash_label
= NULL
;
1935 assert(queue_empty(&task
->task_objq
));
1936 task_objq_lock_destroy(task
);
1938 zfree(task_zone
, task
);
1942 * task_name_deallocate:
1944 * Drop a reference on a task name.
1947 task_name_deallocate(
1948 task_name_t task_name
)
1950 return task_deallocate((task_t
)task_name
);
1954 * task_policy_set_deallocate:
1956 * Drop a reference on a task type.
1959 task_policy_set_deallocate(task_policy_set_t task_policy_set
)
1961 return task_deallocate((task_t
)task_policy_set
);
1965 * task_policy_get_deallocate:
1967 * Drop a reference on a task type.
1970 task_policy_get_deallocate(task_policy_get_t task_policy_get
)
1972 return task_deallocate((task_t
)task_policy_get
);
1976 * task_inspect_deallocate:
1978 * Drop a task inspection reference.
1981 task_inspect_deallocate(
1982 task_inspect_t task_inspect
)
1984 return task_deallocate((task_t
)task_inspect
);
1988 * task_read_deallocate:
1990 * Drop a reference on task read port.
1993 task_read_deallocate(
1994 task_read_t task_read
)
1996 return task_deallocate((task_t
)task_read
);
2000 * task_suspension_token_deallocate:
2002 * Drop a reference on a task suspension token.
2005 task_suspension_token_deallocate(
2006 task_suspension_token_t token
)
2008 return task_deallocate((task_t
)token
);
2013 * task_collect_crash_info:
2015 * collect crash info from bsd and mach based data
2018 task_collect_crash_info(
2021 struct label
*crash_label
,
2025 kern_return_t kr
= KERN_SUCCESS
;
2027 kcdata_descriptor_t crash_data
= NULL
;
2028 kcdata_descriptor_t crash_data_release
= NULL
;
2029 mach_msg_type_number_t size
= CORPSEINFO_ALLOCATION_SIZE
;
2030 mach_vm_offset_t crash_data_ptr
= 0;
2031 void *crash_data_kernel
= NULL
;
2032 void *crash_data_kernel_release
= NULL
;
2034 struct label
*label
, *free_label
;
2037 if (!corpses_enabled()) {
2038 return KERN_NOT_SUPPORTED
;
2042 free_label
= label
= mac_exc_create_label();
2047 assert(is_corpse_fork
|| task
->bsd_info
!= NULL
);
2048 if (task
->corpse_info
== NULL
&& (is_corpse_fork
|| task
->bsd_info
!= NULL
)) {
2050 /* Set the crash label, used by the exception delivery mac hook */
2051 free_label
= task
->crash_label
; // Most likely NULL.
2052 task
->crash_label
= label
;
2053 mac_exc_update_task_crash_label(task
, crash_label
);
2057 crash_data_kernel
= kheap_alloc(KHEAP_DATA_BUFFERS
,
2058 CORPSEINFO_ALLOCATION_SIZE
, Z_WAITOK
| Z_ZERO
);
2059 if (crash_data_kernel
== NULL
) {
2060 kr
= KERN_RESOURCE_SHORTAGE
;
2063 crash_data_ptr
= (mach_vm_offset_t
) crash_data_kernel
;
2065 /* Do not get a corpse ref for corpse fork */
2066 crash_data
= task_crashinfo_alloc_init((mach_vm_address_t
)crash_data_ptr
, size
,
2067 is_corpse_fork
? 0 : CORPSE_CRASHINFO_HAS_REF
,
2068 KCFLAG_USE_MEMCOPY
);
2071 crash_data_release
= task
->corpse_info
;
2072 crash_data_kernel_release
= kcdata_memory_get_begin_addr(crash_data_release
);
2073 task
->corpse_info
= crash_data
;
2078 kheap_free(KHEAP_DATA_BUFFERS
, crash_data_kernel
,
2079 CORPSEINFO_ALLOCATION_SIZE
);
2083 if (crash_data_release
!= NULL
) {
2084 task_crashinfo_destroy(crash_data_release
);
2086 if (crash_data_kernel_release
!= NULL
) {
2087 kheap_free(KHEAP_DATA_BUFFERS
, crash_data_kernel_release
,
2088 CORPSEINFO_ALLOCATION_SIZE
);
2096 if (free_label
!= NULL
) {
2097 mac_exc_free_label(free_label
);
2104 * task_deliver_crash_notification:
2106 * Makes outcall to registered host port for a corpse.
2109 task_deliver_crash_notification(
2112 exception_type_t etype
,
2113 mach_exception_subcode_t subcode
)
2115 kcdata_descriptor_t crash_info
= task
->corpse_info
;
2116 thread_t th_iter
= NULL
;
2117 kern_return_t kr
= KERN_SUCCESS
;
2118 wait_interrupt_t wsave
;
2119 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
2120 ipc_port_t task_port
, old_notify
;
2122 if (crash_info
== NULL
) {
2123 return KERN_FAILURE
;
2127 if (task_is_a_corpse_fork(task
)) {
2128 /* Populate code with EXC_{RESOURCE,GUARD} for corpse fork */
2132 /* Populate code with EXC_CRASH for corpses */
2133 code
[0] = EXC_CRASH
;
2135 /* Update the code[1] if the boot-arg corpse_for_fatal_memkill is set */
2136 if (corpse_for_fatal_memkill
) {
2141 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
2143 if (th_iter
->corpse_dup
== FALSE
) {
2144 ipc_thread_reset(th_iter
);
2149 /* Arm the no-sender notification for taskport */
2150 task_reference(task
);
2151 task_port
= convert_task_to_port(task
);
2153 require_ip_active(task_port
);
2154 ipc_port_nsrequest(task_port
, task_port
->ip_mscount
, ipc_port_make_sonce_locked(task_port
), &old_notify
);
2156 assert(IP_NULL
== old_notify
);
2158 wsave
= thread_interrupt_level(THREAD_UNINT
);
2159 kr
= exception_triage_thread(EXC_CORPSE_NOTIFY
, code
, EXCEPTION_CODE_MAX
, thread
);
2160 if (kr
!= KERN_SUCCESS
) {
2161 printf("Failed to send exception EXC_CORPSE_NOTIFY. error code: %d for pid %d\n", kr
, task_pid(task
));
2164 (void)thread_interrupt_level(wsave
);
2167 * Drop the send right on task port, will fire the
2168 * no-sender notification if exception deliver failed.
2170 ipc_port_release_send(task_port
);
2177 * Terminate the specified task. See comments on thread_terminate
2178 * (kern/thread.c) about problems with terminating the "current task."
2185 if (task
== TASK_NULL
) {
2186 return KERN_INVALID_ARGUMENT
;
2189 if (task
->bsd_info
) {
2190 return KERN_FAILURE
;
2193 return task_terminate_internal(task
);
2197 extern int proc_pid(struct proc
*);
2198 extern void proc_name_kdp(task_t t
, char *buf
, int size
);
2199 #endif /* MACH_ASSERT */
2201 #define VM_MAP_PARTIAL_REAP 0x54 /* 0x150 */
2203 __unused
task_partial_reap(task_t task
, __unused
int pid
)
2205 unsigned int reclaimed_resident
= 0;
2206 unsigned int reclaimed_compressed
= 0;
2207 uint64_t task_page_count
;
2209 task_page_count
= (get_task_phys_footprint(task
) / PAGE_SIZE_64
);
2211 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_START
),
2212 pid
, task_page_count
, 0, 0, 0);
2214 vm_map_partial_reap(task
->map
, &reclaimed_resident
, &reclaimed_compressed
);
2216 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_END
),
2217 pid
, reclaimed_resident
, reclaimed_compressed
, 0, 0);
2221 task_mark_corpse(task_t task
)
2223 kern_return_t kr
= KERN_SUCCESS
;
2224 thread_t self_thread
;
2226 wait_interrupt_t wsave
;
2228 struct label
*crash_label
= NULL
;
2231 assert(task
!= kernel_task
);
2232 assert(task
== current_task());
2233 assert(!task_is_a_corpse(task
));
2236 crash_label
= mac_exc_create_label_for_proc((struct proc
*)task
->bsd_info
);
2239 kr
= task_collect_crash_info(task
,
2244 if (kr
!= KERN_SUCCESS
) {
2248 self_thread
= current_thread();
2250 wsave
= thread_interrupt_level(THREAD_UNINT
);
2253 task_set_corpse_pending_report(task
);
2254 task_set_corpse(task
);
2255 task
->crashed_thread_id
= thread_tid(self_thread
);
2257 kr
= task_start_halt_locked(task
, TRUE
);
2258 assert(kr
== KERN_SUCCESS
);
2260 ipc_task_reset(task
);
2261 /* Remove the naked send right for task port, needed to arm no sender notification */
2262 task_set_special_port_internal(task
, TASK_KERNEL_PORT
, IPC_PORT_NULL
);
2263 ipc_task_enable(task
);
2266 /* terminate the ipc space */
2267 ipc_space_terminate(task
->itk_space
);
2269 /* Add it to global corpse task list */
2270 task_add_to_corpse_task_list(task
);
2272 task_start_halt(task
);
2273 thread_terminate_internal(self_thread
);
2275 (void) thread_interrupt_level(wsave
);
2276 assert(task
->halting
== TRUE
);
2280 mac_exc_free_label(crash_label
);
2288 * Clears the corpse pending bit on task.
2289 * Removes inspection bit on the threads.
2292 task_clear_corpse(task_t task
)
2294 thread_t th_iter
= NULL
;
2297 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
2299 thread_mtx_lock(th_iter
);
2300 th_iter
->inspection
= FALSE
;
2301 thread_mtx_unlock(th_iter
);
2304 thread_terminate_crashed_threads();
2305 /* remove the pending corpse report flag */
2306 task_clear_corpse_pending_report(task
);
2314 * Called whenever the Mach port system detects no-senders on
2315 * the task port of a corpse.
2316 * Each notification that comes in should terminate the task (corpse).
2319 task_port_notify(mach_msg_header_t
*msg
)
2321 mach_no_senders_notification_t
*notification
= (void *)msg
;
2322 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
2325 require_ip_active(port
);
2326 assert(IKOT_TASK_CONTROL
== ip_kotype(port
));
2327 task
= (task_t
) ip_get_kobject(port
);
2329 assert(task_is_a_corpse(task
));
2331 /* Remove the task from global corpse task list */
2332 task_remove_from_corpse_task_list(task
);
2334 task_clear_corpse(task
);
2335 task_terminate_internal(task
);
2339 * task_port_with_flavor_notify
2341 * Called whenever the Mach port system detects no-senders on
2342 * the task inspect or read port. These ports are allocated lazily and
2343 * should be deallocated here when there are no senders remaining.
2346 task_port_with_flavor_notify(mach_msg_header_t
*msg
)
2348 mach_no_senders_notification_t
*notification
= (void *)msg
;
2349 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
2351 mach_task_flavor_t flavor
;
2352 ipc_kobject_type_t kotype
;
2355 if (port
->ip_srights
> 0) {
2359 task
= (task_t
)port
->ip_kobject
;
2360 kotype
= ip_kotype(port
);
2361 if (task
!= TASK_NULL
) {
2362 assert((IKOT_TASK_READ
== kotype
) || (IKOT_TASK_INSPECT
== kotype
));
2363 task_reference_internal(task
);
2367 if (task
== TASK_NULL
) {
2368 /* The task is exiting or disabled; it will eventually deallocate the port */
2374 require_ip_active(port
);
2376 * Check for a stale no-senders notification. A call to any function
2377 * that vends out send rights to this port could resurrect it between
2378 * this notification being generated and actually being handled here.
2380 if (port
->ip_srights
> 0) {
2383 task_deallocate(task
);
2387 if (kotype
== IKOT_TASK_READ
) {
2388 flavor
= TASK_FLAVOR_READ
;
2390 flavor
= TASK_FLAVOR_INSPECT
;
2392 assert(task
->itk_self
[flavor
] == port
);
2393 task
->itk_self
[flavor
] = IP_NULL
;
2394 port
->ip_kobject
= IKOT_NONE
;
2397 task_deallocate(task
);
2399 ipc_port_dealloc_kernel(port
);
2403 * task_wait_till_threads_terminate_locked
2405 * Wait till all the threads in the task are terminated.
2406 * Might release the task lock and re-acquire it.
2409 task_wait_till_threads_terminate_locked(task_t task
)
2411 /* wait for all the threads in the task to terminate */
2412 while (task
->active_thread_count
!= 0) {
2413 assert_wait((event_t
)&task
->active_thread_count
, THREAD_UNINT
);
2415 thread_block(THREAD_CONTINUE_NULL
);
2422 * task_duplicate_map_and_threads
2424 * Copy vmmap of source task.
2425 * Copy active threads from source task to destination task.
2426 * Source task would be suspended during the copy.
2429 task_duplicate_map_and_threads(
2433 thread_t
*thread_ret
,
2434 uint64_t **udata_buffer
,
2438 kern_return_t kr
= KERN_SUCCESS
;
2440 thread_t thread
, self
, thread_return
= THREAD_NULL
;
2441 thread_t new_thread
= THREAD_NULL
, first_thread
= THREAD_NULL
;
2442 thread_t
*thread_array
;
2443 uint32_t active_thread_count
= 0, array_count
= 0, i
;
2445 uint64_t *buffer
= NULL
;
2447 int est_knotes
= 0, num_knotes
= 0;
2449 self
= current_thread();
2452 * Suspend the task to copy thread state, use the internal
2453 * variant so that no user-space process can resume
2454 * the task from under us
2456 kr
= task_suspend_internal(task
);
2457 if (kr
!= KERN_SUCCESS
) {
2461 if (task
->map
->disable_vmentry_reuse
== TRUE
) {
2463 * Quite likely GuardMalloc (or some debugging tool)
2464 * is being used on this task. And it has gone through
2465 * its limit. Making a corpse will likely encounter
2466 * a lot of VM entries that will need COW.
2470 #if DEVELOPMENT || DEBUG
2471 memorystatus_abort_vm_map_fork(task
);
2473 task_resume_internal(task
);
2474 return KERN_FAILURE
;
2477 /* Check with VM if vm_map_fork is allowed for this task */
2478 if (memorystatus_allowed_vm_map_fork(task
)) {
2479 /* Setup new task's vmmap, switch from parent task's map to it COW map */
2480 oldmap
= new_task
->map
;
2481 new_task
->map
= vm_map_fork(new_task
->ledger
,
2483 (VM_MAP_FORK_SHARE_IF_INHERIT_NONE
|
2484 VM_MAP_FORK_PRESERVE_PURGEABLE
|
2485 VM_MAP_FORK_CORPSE_FOOTPRINT
));
2486 vm_map_deallocate(oldmap
);
2488 /* copy ledgers that impact the memory footprint */
2489 vm_map_copy_footprint_ledgers(task
, new_task
);
2491 /* Get all the udata pointers from kqueue */
2492 est_knotes
= kevent_proc_copy_uptrs(p
, NULL
, 0);
2493 if (est_knotes
> 0) {
2494 buf_size
= (est_knotes
+ 32) * sizeof(uint64_t);
2495 buffer
= kheap_alloc(KHEAP_DATA_BUFFERS
, buf_size
, Z_WAITOK
);
2496 num_knotes
= kevent_proc_copy_uptrs(p
, buffer
, buf_size
);
2497 if (num_knotes
> est_knotes
+ 32) {
2498 num_knotes
= est_knotes
+ 32;
2503 active_thread_count
= task
->active_thread_count
;
2504 if (active_thread_count
== 0) {
2505 if (buffer
!= NULL
) {
2506 kheap_free(KHEAP_DATA_BUFFERS
, buffer
, buf_size
);
2508 task_resume_internal(task
);
2509 return KERN_FAILURE
;
2512 thread_array
= kheap_alloc(KHEAP_TEMP
,
2513 sizeof(thread_t
) * active_thread_count
, Z_WAITOK
);
2515 /* Iterate all the threads and drop the task lock before calling thread_create_with_continuation */
2517 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2518 /* Skip inactive threads */
2519 active
= thread
->active
;
2524 if (array_count
>= active_thread_count
) {
2528 thread_array
[array_count
++] = thread
;
2529 thread_reference(thread
);
2533 for (i
= 0; i
< array_count
; i
++) {
2534 kr
= thread_create_with_continuation(new_task
, &new_thread
, (thread_continue_t
)thread_corpse_continue
);
2535 if (kr
!= KERN_SUCCESS
) {
2539 /* Equivalent of current thread in corpse */
2540 if (thread_array
[i
] == self
) {
2541 thread_return
= new_thread
;
2542 new_task
->crashed_thread_id
= thread_tid(new_thread
);
2543 } else if (first_thread
== NULL
) {
2544 first_thread
= new_thread
;
2546 /* drop the extra ref returned by thread_create_with_continuation */
2547 thread_deallocate(new_thread
);
2550 kr
= thread_dup2(thread_array
[i
], new_thread
);
2551 if (kr
!= KERN_SUCCESS
) {
2552 thread_mtx_lock(new_thread
);
2553 new_thread
->corpse_dup
= TRUE
;
2554 thread_mtx_unlock(new_thread
);
2558 /* Copy thread name */
2559 bsd_copythreadname(new_thread
->uthread
, thread_array
[i
]->uthread
);
2560 new_thread
->thread_tag
= thread_array
[i
]->thread_tag
;
2561 thread_copy_resource_info(new_thread
, thread_array
[i
]);
2564 /* return the first thread if we couldn't find the equivalent of current */
2565 if (thread_return
== THREAD_NULL
) {
2566 thread_return
= first_thread
;
2567 } else if (first_thread
!= THREAD_NULL
) {
2568 /* drop the extra ref returned by thread_create_with_continuation */
2569 thread_deallocate(first_thread
);
2572 task_resume_internal(task
);
2574 for (i
= 0; i
< array_count
; i
++) {
2575 thread_deallocate(thread_array
[i
]);
2577 kheap_free(KHEAP_TEMP
, thread_array
, sizeof(thread_t
) * active_thread_count
);
2579 if (kr
== KERN_SUCCESS
) {
2580 *thread_ret
= thread_return
;
2581 *udata_buffer
= buffer
;
2583 *num_udata
= num_knotes
;
2585 if (thread_return
!= THREAD_NULL
) {
2586 thread_deallocate(thread_return
);
2588 if (buffer
!= NULL
) {
2589 kheap_free(KHEAP_DATA_BUFFERS
, buffer
, buf_size
);
2596 #if CONFIG_SECLUDED_MEMORY
2597 extern void task_set_can_use_secluded_mem_locked(
2599 boolean_t can_use_secluded_mem
);
2600 #endif /* CONFIG_SECLUDED_MEMORY */
2603 int debug4k_panic_on_terminate
= 0;
2604 #endif /* MACH_ASSERT */
2606 task_terminate_internal(
2609 thread_t thread
, self
;
2611 boolean_t interrupt_save
;
2614 assert(task
!= kernel_task
);
2616 self
= current_thread();
2617 self_task
= self
->task
;
2620 * Get the task locked and make sure that we are not racing
2621 * with someone else trying to terminate us.
2623 if (task
== self_task
) {
2625 } else if (task
< self_task
) {
2627 task_lock(self_task
);
2629 task_lock(self_task
);
2633 #if CONFIG_SECLUDED_MEMORY
2634 if (task
->task_can_use_secluded_mem
) {
2635 task_set_can_use_secluded_mem_locked(task
, FALSE
);
2637 task
->task_could_use_secluded_mem
= FALSE
;
2638 task
->task_could_also_use_secluded_mem
= FALSE
;
2640 if (task
->task_suppressed_secluded
) {
2641 stop_secluded_suppression(task
);
2643 #endif /* CONFIG_SECLUDED_MEMORY */
2645 if (!task
->active
) {
2647 * Task is already being terminated.
2648 * Just return an error. If we are dying, this will
2649 * just get us to our AST special handler and that
2650 * will get us to finalize the termination of ourselves.
2653 if (self_task
!= task
) {
2654 task_unlock(self_task
);
2657 return KERN_FAILURE
;
2660 if (task_corpse_pending_report(task
)) {
2662 * Task is marked for reporting as corpse.
2663 * Just return an error. This will
2664 * just get us to our AST special handler and that
2665 * will get us to finish the path to death
2668 if (self_task
!= task
) {
2669 task_unlock(self_task
);
2672 return KERN_FAILURE
;
2675 if (self_task
!= task
) {
2676 task_unlock(self_task
);
2680 * Make sure the current thread does not get aborted out of
2681 * the waits inside these operations.
2683 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
2686 * Indicate that we want all the threads to stop executing
2687 * at user space by holding the task (we would have held
2688 * each thread independently in thread_terminate_internal -
2689 * but this way we may be more likely to already find it
2690 * held there). Mark the task inactive, and prevent
2691 * further task operations via the task port.
2693 task_hold_locked(task
);
2694 task
->active
= FALSE
;
2695 ipc_task_disable(task
);
2697 #if CONFIG_TELEMETRY
2699 * Notify telemetry that this task is going away.
2701 telemetry_task_ctl_locked(task
, TF_TELEMETRY
, 0);
2705 * Terminate each thread in the task.
2707 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2708 thread_terminate_internal(thread
);
2712 if (task
->bsd_info
!= NULL
&& !task_is_exec_copy(task
)) {
2713 pid
= proc_pid(task
->bsd_info
);
2715 #endif /* MACH_BSD */
2719 proc_set_task_policy(task
, TASK_POLICY_ATTRIBUTE
,
2720 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
2722 /* Early object reap phase */
2724 // PR-17045188: Revisit implementation
2725 // task_partial_reap(task, pid);
2727 #if CONFIG_TASKWATCH
2729 * remove all task watchers
2731 task_removewatchers(task
);
2733 #endif /* CONFIG_TASKWATCH */
2736 * Destroy all synchronizers owned by the task.
2738 task_synchronizer_destroy_all(task
);
2741 * Clear the watchport boost on the task.
2743 task_remove_turnstile_watchports(task
);
2746 * Destroy the IPC space, leaving just a reference for it.
2748 ipc_space_terminate(task
->itk_space
);
2751 /* if some ledgers go negative on tear-down again... */
2752 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2753 task_ledgers
.phys_footprint
);
2754 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2755 task_ledgers
.internal
);
2756 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2757 task_ledgers
.internal_compressed
);
2758 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2759 task_ledgers
.iokit_mapped
);
2760 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2761 task_ledgers
.alternate_accounting
);
2762 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2763 task_ledgers
.alternate_accounting_compressed
);
2767 * If the current thread is a member of the task
2768 * being terminated, then the last reference to
2769 * the task will not be dropped until the thread
2770 * is finally reaped. To avoid incurring the
2771 * expense of removing the address space regions
2772 * at reap time, we do it explictly here.
2775 vm_map_lock(task
->map
);
2776 vm_map_disable_hole_optimization(task
->map
);
2777 vm_map_unlock(task
->map
);
2781 * Identify the pmap's process, in case the pmap ledgers drift
2782 * and we have to report it.
2785 if (task
->bsd_info
&& !task_is_exec_copy(task
)) {
2786 pid
= proc_pid(task
->bsd_info
);
2787 proc_name_kdp(task
, procname
, sizeof(procname
));
2790 strlcpy(procname
, "<unknown>", sizeof(procname
));
2792 pmap_set_process(task
->map
->pmap
, pid
, procname
);
2793 if (vm_map_page_shift(task
->map
) < (int)PAGE_SHIFT
) {
2794 DEBUG4K_LIFE("map %p procname: %s\n", task
->map
, procname
);
2795 if (debug4k_panic_on_terminate
) {
2796 panic("DEBUG4K: %s:%d %d[%s] map %p\n", __FUNCTION__
, __LINE__
, pid
, procname
, task
->map
);
2799 #endif /* MACH_ASSERT */
2801 vm_map_terminate(task
->map
);
2803 /* release our shared region */
2804 vm_shared_region_set(task
, NULL
);
2806 #if __has_feature(ptrauth_calls)
2807 task_set_shared_region_id(task
, NULL
);
2808 #endif /* __has_feature(ptrauth_calls) */
2810 lck_mtx_lock(&tasks_threads_lock
);
2811 queue_remove(&tasks
, task
, task_t
, tasks
);
2812 queue_enter(&terminated_tasks
, task
, task_t
, tasks
);
2814 terminated_tasks_count
++;
2815 lck_mtx_unlock(&tasks_threads_lock
);
2818 * We no longer need to guard against being aborted, so restore
2819 * the previous interruptible state.
2821 thread_interrupt_level(interrupt_save
);
2824 /* force the task to release all ctrs */
2825 if (task
->t_kpc
& TASK_KPC_FORCED_ALL_CTRS
) {
2826 kpc_force_all_ctrs(task
, 0);
2830 #if CONFIG_COALITIONS
2832 * Leave our coalitions. (drop activation but not reference)
2834 coalitions_remove_task(task
);
2838 extern int vm_compressor_available
;
2839 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
&& vm_compressor_available
) {
2840 task_disown_frozen_csegs(task
);
2841 assert(queue_empty(&task
->task_frozen_cseg_q
));
2843 #endif /* CONFIG_FREEZE */
2846 * Get rid of the task active reference on itself.
2848 task_deallocate(task
);
2850 return KERN_SUCCESS
;
2854 tasks_system_suspend(boolean_t suspend
)
2858 lck_mtx_lock(&tasks_threads_lock
);
2859 assert(tasks_suspend_state
!= suspend
);
2860 tasks_suspend_state
= suspend
;
2861 queue_iterate(&tasks
, task
, task_t
, tasks
) {
2862 if (task
== kernel_task
) {
2865 suspend
? task_suspend_internal(task
) : task_resume_internal(task
);
2867 lck_mtx_unlock(&tasks_threads_lock
);
2873 * Shut the current task down (except for the current thread) in
2874 * preparation for dramatic changes to the task (probably exec).
2875 * We hold the task and mark all other threads in the task for
2879 task_start_halt(task_t task
)
2881 kern_return_t kr
= KERN_SUCCESS
;
2883 kr
= task_start_halt_locked(task
, FALSE
);
2888 static kern_return_t
2889 task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
)
2891 thread_t thread
, self
;
2892 uint64_t dispatchqueue_offset
;
2894 assert(task
!= kernel_task
);
2896 self
= current_thread();
2898 if (task
!= self
->task
&& !task_is_a_corpse_fork(task
)) {
2899 return KERN_INVALID_ARGUMENT
;
2902 if (task
->halting
|| !task
->active
|| !self
->active
) {
2904 * Task or current thread is already being terminated.
2905 * Hurry up and return out of the current kernel context
2906 * so that we run our AST special handler to terminate
2909 return KERN_FAILURE
;
2912 task
->halting
= TRUE
;
2915 * Mark all the threads to keep them from starting any more
2916 * user-level execution. The thread_terminate_internal code
2917 * would do this on a thread by thread basis anyway, but this
2918 * gives us a better chance of not having to wait there.
2920 task_hold_locked(task
);
2921 dispatchqueue_offset
= get_dispatchqueue_offset_from_proc(task
->bsd_info
);
2924 * Terminate all the other threads in the task.
2926 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
)
2928 if (should_mark_corpse
) {
2929 thread_mtx_lock(thread
);
2930 thread
->inspection
= TRUE
;
2931 thread_mtx_unlock(thread
);
2933 if (thread
!= self
) {
2934 thread_terminate_internal(thread
);
2937 task
->dispatchqueue_offset
= dispatchqueue_offset
;
2939 task_release_locked(task
);
2941 return KERN_SUCCESS
;
2946 * task_complete_halt:
2948 * Complete task halt by waiting for threads to terminate, then clean
2949 * up task resources (VM, port namespace, etc...) and then let the
2950 * current thread go in the (practically empty) task context.
2952 * Note: task->halting flag is not cleared in order to avoid creation
2953 * of new thread in old exec'ed task.
2956 task_complete_halt(task_t task
)
2959 assert(task
->halting
);
2960 assert(task
== current_task());
2963 * Wait for the other threads to get shut down.
2964 * When the last other thread is reaped, we'll be
2967 if (task
->thread_count
> 1) {
2968 assert_wait((event_t
)&task
->halting
, THREAD_UNINT
);
2970 thread_block(THREAD_CONTINUE_NULL
);
2976 * Give the machine dependent code a chance
2977 * to perform cleanup of task-level resources
2978 * associated with the current thread before
2979 * ripping apart the task.
2981 machine_task_terminate(task
);
2984 * Destroy all synchronizers owned by the task.
2986 task_synchronizer_destroy_all(task
);
2989 * Destroy the contents of the IPC space, leaving just
2990 * a reference for it.
2992 ipc_space_clean(task
->itk_space
);
2995 * Clean out the address space, as we are going to be
2996 * getting a new one.
2998 vm_map_remove(task
->map
, task
->map
->min_offset
,
2999 task
->map
->max_offset
,
3003 * + remove immutable mappings
3004 * + allow gaps in the range
3006 (VM_MAP_REMOVE_NO_UNNESTING
|
3007 VM_MAP_REMOVE_IMMUTABLE
|
3008 VM_MAP_REMOVE_GAPS_OK
));
3011 * Kick out any IOKitUser handles to the task. At best they're stale,
3012 * at worst someone is racing a SUID exec.
3014 iokit_task_terminate(task
);
3020 * Suspend execution of the specified task.
3021 * This is a recursive-style suspension of the task, a count of
3022 * suspends is maintained.
3024 * CONDITIONS: the task is locked and active.
3032 assert(task
->active
);
3034 if (task
->suspend_count
++ > 0) {
3038 if (task
->bsd_info
) {
3039 workq_proc_suspended(task
->bsd_info
);
3043 * Iterate through all the threads and hold them.
3045 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3046 thread_mtx_lock(thread
);
3047 thread_hold(thread
);
3048 thread_mtx_unlock(thread
);
3055 * Same as the internal routine above, except that is must lock
3056 * and verify that the task is active. This differs from task_suspend
3057 * in that it places a kernel hold on the task rather than just a
3058 * user-level hold. This keeps users from over resuming and setting
3059 * it running out from under the kernel.
3061 * CONDITIONS: the caller holds a reference on the task
3067 if (task
== TASK_NULL
) {
3068 return KERN_INVALID_ARGUMENT
;
3073 if (!task
->active
) {
3076 return KERN_FAILURE
;
3079 task_hold_locked(task
);
3082 return KERN_SUCCESS
;
3088 boolean_t until_not_runnable
)
3090 if (task
== TASK_NULL
) {
3091 return KERN_INVALID_ARGUMENT
;
3096 if (!task
->active
) {
3099 return KERN_FAILURE
;
3102 task_wait_locked(task
, until_not_runnable
);
3105 return KERN_SUCCESS
;
3111 * Wait for all threads in task to stop.
3114 * Called with task locked, active, and held.
3119 boolean_t until_not_runnable
)
3121 thread_t thread
, self
;
3123 assert(task
->active
);
3124 assert(task
->suspend_count
> 0);
3126 self
= current_thread();
3129 * Iterate through all the threads and wait for them to
3130 * stop. Do not wait for the current thread if it is within
3133 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3134 if (thread
!= self
) {
3135 thread_wait(thread
, until_not_runnable
);
3141 task_is_app_suspended(task_t task
)
3143 return task
->pidsuspended
;
3147 * task_release_locked:
3149 * Release a kernel hold on a task.
3151 * CONDITIONS: the task is locked and active
3154 task_release_locked(
3159 assert(task
->active
);
3160 assert(task
->suspend_count
> 0);
3162 if (--task
->suspend_count
> 0) {
3166 if (task
->bsd_info
) {
3167 workq_proc_resumed(task
->bsd_info
);
3170 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3171 thread_mtx_lock(thread
);
3172 thread_release(thread
);
3173 thread_mtx_unlock(thread
);
3180 * Same as the internal routine above, except that it must lock
3181 * and verify that the task is active.
3183 * CONDITIONS: The caller holds a reference to the task
3189 if (task
== TASK_NULL
) {
3190 return KERN_INVALID_ARGUMENT
;
3195 if (!task
->active
) {
3198 return KERN_FAILURE
;
3201 task_release_locked(task
);
3204 return KERN_SUCCESS
;
3207 static kern_return_t
3208 task_threads_internal(
3210 thread_act_array_t
*threads_out
,
3211 mach_msg_type_number_t
*count
,
3212 mach_thread_flavor_t flavor
)
3214 mach_msg_type_number_t actual
;
3215 thread_t
*thread_list
;
3217 vm_size_t size
, size_needed
;
3221 size
= 0; addr
= NULL
;
3223 if (task
== TASK_NULL
) {
3224 return KERN_INVALID_ARGUMENT
;
3229 if (!task
->active
) {
3236 return KERN_FAILURE
;
3239 actual
= task
->thread_count
;
3241 /* do we have the memory we need? */
3242 size_needed
= actual
* sizeof(mach_port_t
);
3243 if (size_needed
<= size
) {
3247 /* unlock the task and allocate more memory */
3254 assert(size_needed
> 0);
3257 addr
= kalloc(size
);
3259 return KERN_RESOURCE_SHORTAGE
;
3263 /* OK, have memory and the task is locked & active */
3264 thread_list
= (thread_t
*)addr
;
3268 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
3269 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
3270 thread_reference_internal(thread
);
3271 thread_list
[j
++] = thread
;
3274 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
3277 size_needed
= actual
* sizeof(mach_port_t
);
3279 /* can unlock task now that we've got the thread refs */
3283 /* no threads, so return null pointer and deallocate memory */
3285 *threads_out
= NULL
;
3292 /* if we allocated too much, must copy */
3294 if (size_needed
< size
) {
3297 newaddr
= kalloc(size_needed
);
3299 for (i
= 0; i
< actual
; ++i
) {
3300 thread_deallocate(thread_list
[i
]);
3303 return KERN_RESOURCE_SHORTAGE
;
3306 bcopy(addr
, newaddr
, size_needed
);
3308 thread_list
= (thread_t
*)newaddr
;
3311 *threads_out
= thread_list
;
3314 /* do the conversion that Mig should handle */
3317 case THREAD_FLAVOR_CONTROL
:
3318 for (i
= 0; i
< actual
; ++i
) {
3319 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
3322 case THREAD_FLAVOR_READ
:
3323 for (i
= 0; i
< actual
; ++i
) {
3324 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_read_to_port(thread_list
[i
]);
3327 case THREAD_FLAVOR_INSPECT
:
3328 for (i
= 0; i
< actual
; ++i
) {
3329 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_inspect_to_port(thread_list
[i
]);
3333 return KERN_INVALID_ARGUMENT
;
3337 return KERN_SUCCESS
;
3343 thread_act_array_t
*threads_out
,
3344 mach_msg_type_number_t
*count
)
3346 return task_threads_internal(task
, threads_out
, count
, THREAD_FLAVOR_CONTROL
);
3351 task_threads_from_user(
3353 thread_act_array_t
*threads_out
,
3354 mach_msg_type_number_t
*count
)
3356 ipc_kobject_type_t kotype
;
3359 task_t task
= convert_port_to_task_check_type(port
, &kotype
, TASK_FLAVOR_INSPECT
, FALSE
);
3361 if (task
== TASK_NULL
) {
3362 return KERN_INVALID_ARGUMENT
;
3366 case IKOT_TASK_CONTROL
:
3367 kr
= task_threads_internal(task
, threads_out
, count
, THREAD_FLAVOR_CONTROL
);
3369 case IKOT_TASK_READ
:
3370 kr
= task_threads_internal(task
, threads_out
, count
, THREAD_FLAVOR_READ
);
3372 case IKOT_TASK_INSPECT
:
3373 kr
= task_threads_internal(task
, threads_out
, count
, THREAD_FLAVOR_INSPECT
);
3376 panic("strange kobject type");
3380 task_deallocate(task
);
3384 #define TASK_HOLD_NORMAL 0
3385 #define TASK_HOLD_PIDSUSPEND 1
3386 #define TASK_HOLD_LEGACY 2
3387 #define TASK_HOLD_LEGACY_ALL 3
3389 static kern_return_t
3394 if (!task
->active
&& !task_is_a_corpse(task
)) {
3395 return KERN_FAILURE
;
3398 /* Return success for corpse task */
3399 if (task_is_a_corpse(task
)) {
3400 return KERN_SUCCESS
;
3403 KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_IPC
, MACH_TASK_SUSPEND
),
3405 task
->thread_count
> 0 ?((thread_t
)queue_first(&task
->threads
))->thread_id
: 0,
3406 task
->user_stop_count
, task
->user_stop_count
+ 1);
3409 current_task()->suspends_outstanding
++;
3412 if (mode
== TASK_HOLD_LEGACY
) {
3413 task
->legacy_stop_count
++;
3416 if (task
->user_stop_count
++ > 0) {
3418 * If the stop count was positive, the task is
3419 * already stopped and we can exit.
3421 return KERN_SUCCESS
;
3425 * Put a kernel-level hold on the threads in the task (all
3426 * user-level task suspensions added together represent a
3427 * single kernel-level hold). We then wait for the threads
3428 * to stop executing user code.
3430 task_hold_locked(task
);
3431 task_wait_locked(task
, FALSE
);
3433 return KERN_SUCCESS
;
3436 static kern_return_t
3441 boolean_t release
= FALSE
;
3443 if (!task
->active
&& !task_is_a_corpse(task
)) {
3444 return KERN_FAILURE
;
3447 /* Return success for corpse task */
3448 if (task_is_a_corpse(task
)) {
3449 return KERN_SUCCESS
;
3452 if (mode
== TASK_HOLD_PIDSUSPEND
) {
3453 if (task
->pidsuspended
== FALSE
) {
3454 return KERN_FAILURE
;
3456 task
->pidsuspended
= FALSE
;
3459 if (task
->user_stop_count
> (task
->pidsuspended
? 1 : 0)) {
3460 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3461 MACHDBG_CODE(DBG_MACH_IPC
, MACH_TASK_RESUME
) | DBG_FUNC_NONE
,
3462 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
3463 task
->user_stop_count
, mode
, task
->legacy_stop_count
);
3467 * This is obviously not robust; if we suspend one task and then resume a different one,
3468 * we'll fly under the radar. This is only meant to catch the common case of a crashed
3469 * or buggy suspender.
3471 current_task()->suspends_outstanding
--;
3474 if (mode
== TASK_HOLD_LEGACY_ALL
) {
3475 if (task
->legacy_stop_count
>= task
->user_stop_count
) {
3476 task
->user_stop_count
= 0;
3479 task
->user_stop_count
-= task
->legacy_stop_count
;
3481 task
->legacy_stop_count
= 0;
3483 if (mode
== TASK_HOLD_LEGACY
&& task
->legacy_stop_count
> 0) {
3484 task
->legacy_stop_count
--;
3486 if (--task
->user_stop_count
== 0) {
3491 return KERN_FAILURE
;
3495 * Release the task if necessary.
3498 task_release_locked(task
);
3501 return KERN_SUCCESS
;
3505 get_task_suspended(task_t task
)
3507 return 0 != task
->user_stop_count
;
3513 * Implement an (old-fashioned) user-level suspension on a task.
3515 * Because the user isn't expecting to have to manage a suspension
3516 * token, we'll track it for him in the kernel in the form of a naked
3517 * send right to the task's resume port. All such send rights
3518 * account for a single suspension against the task (unlike task_suspend2()
3519 * where each caller gets a unique suspension count represented by a
3520 * unique send-once right).
3523 * The caller holds a reference to the task
3531 mach_port_name_t name
;
3533 if (task
== TASK_NULL
|| task
== kernel_task
) {
3534 return KERN_INVALID_ARGUMENT
;
3540 * place a legacy hold on the task.
3542 kr
= place_task_hold(task
, TASK_HOLD_LEGACY
);
3543 if (kr
!= KERN_SUCCESS
) {
3549 * Claim a send right on the task resume port, and request a no-senders
3550 * notification on that port (if none outstanding).
3552 (void)ipc_kobject_make_send_lazy_alloc_port((ipc_port_t
*) &task
->itk_resume
,
3553 (ipc_kobject_t
)task
, IKOT_TASK_RESUME
, true, OS_PTRAUTH_DISCRIMINATOR("task.itk_resume"));
3554 port
= task
->itk_resume
;
3558 * Copyout the send right into the calling task's IPC space. It won't know it is there,
3559 * but we'll look it up when calling a traditional resume. Any IPC operations that
3560 * deallocate the send right will auto-release the suspension.
3562 if ((kr
= ipc_kmsg_copyout_object(current_task()->itk_space
, ip_to_object(port
),
3563 MACH_MSG_TYPE_MOVE_SEND
, NULL
, NULL
, &name
)) != KERN_SUCCESS
) {
3564 printf("warning: %s(%d) failed to copyout suspension token for pid %d with error: %d\n",
3565 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
3566 task_pid(task
), kr
);
3575 * Release a user hold on a task.
3578 * The caller holds a reference to the task
3585 mach_port_name_t resume_port_name
;
3586 ipc_entry_t resume_port_entry
;
3587 ipc_space_t space
= current_task()->itk_space
;
3589 if (task
== TASK_NULL
|| task
== kernel_task
) {
3590 return KERN_INVALID_ARGUMENT
;
3593 /* release a legacy task hold */
3595 kr
= release_task_hold(task
, TASK_HOLD_LEGACY
);
3598 is_write_lock(space
);
3599 if (is_active(space
) && IP_VALID(task
->itk_resume
) &&
3600 ipc_hash_lookup(space
, ip_to_object(task
->itk_resume
), &resume_port_name
, &resume_port_entry
) == TRUE
) {
3602 * We found a suspension token in the caller's IPC space. Release a send right to indicate that
3603 * we are holding one less legacy hold on the task from this caller. If the release failed,
3604 * go ahead and drop all the rights, as someone either already released our holds or the task
3607 if (kr
== KERN_SUCCESS
) {
3608 ipc_right_dealloc(space
, resume_port_name
, resume_port_entry
);
3610 ipc_right_destroy(space
, resume_port_name
, resume_port_entry
, FALSE
, 0);
3612 /* space unlocked */
3614 is_write_unlock(space
);
3615 if (kr
== KERN_SUCCESS
) {
3616 printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
3617 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
3626 * Suspend the target task.
3627 * Making/holding a token/reference/port is the callers responsibility.
3630 task_suspend_internal(task_t task
)
3634 if (task
== TASK_NULL
|| task
== kernel_task
) {
3635 return KERN_INVALID_ARGUMENT
;
3639 kr
= place_task_hold(task
, TASK_HOLD_NORMAL
);
3645 * Suspend the target task, and return a suspension token. The token
3646 * represents a reference on the suspended task.
3651 task_suspension_token_t
*suspend_token
)
3655 kr
= task_suspend_internal(task
);
3656 if (kr
!= KERN_SUCCESS
) {
3657 *suspend_token
= TASK_NULL
;
3662 * Take a reference on the target task and return that to the caller
3663 * as a "suspension token," which can be converted into an SO right to
3664 * the now-suspended task's resume port.
3666 task_reference_internal(task
);
3667 *suspend_token
= task
;
3669 return KERN_SUCCESS
;
3674 * (reference/token/port management is caller's responsibility).
3677 task_resume_internal(
3678 task_suspension_token_t task
)
3682 if (task
== TASK_NULL
|| task
== kernel_task
) {
3683 return KERN_INVALID_ARGUMENT
;
3687 kr
= release_task_hold(task
, TASK_HOLD_NORMAL
);
3693 * Resume the task using a suspension token. Consumes the token's ref.
3697 task_suspension_token_t task
)
3701 kr
= task_resume_internal(task
);
3702 task_suspension_token_deallocate(task
);
3708 task_suspension_notify(mach_msg_header_t
*request_header
)
3710 ipc_port_t port
= request_header
->msgh_remote_port
;
3711 task_t task
= convert_port_to_task_suspension_token(port
);
3712 mach_msg_type_number_t not_count
;
3714 if (task
== TASK_NULL
|| task
== kernel_task
) {
3715 return TRUE
; /* nothing to do */
3717 switch (request_header
->msgh_id
) {
3718 case MACH_NOTIFY_SEND_ONCE
:
3719 /* release the hold held by this specific send-once right */
3721 release_task_hold(task
, TASK_HOLD_NORMAL
);
3725 case MACH_NOTIFY_NO_SENDERS
:
3726 not_count
= ((mach_no_senders_notification_t
*)request_header
)->not_count
;
3730 if (port
->ip_mscount
== not_count
) {
3731 /* release all the [remaining] outstanding legacy holds */
3732 assert(port
->ip_nsrequest
== IP_NULL
);
3734 release_task_hold(task
, TASK_HOLD_LEGACY_ALL
);
3736 } else if (port
->ip_nsrequest
== IP_NULL
) {
3737 ipc_port_t old_notify
;
3740 /* new send rights, re-arm notification at current make-send count */
3741 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
3742 assert(old_notify
== IP_NULL
);
3754 task_suspension_token_deallocate(task
); /* drop token reference */
3758 static kern_return_t
3759 task_pidsuspend_locked(task_t task
)
3763 if (task
->pidsuspended
) {
3768 task
->pidsuspended
= TRUE
;
3770 kr
= place_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3771 if (kr
!= KERN_SUCCESS
) {
3772 task
->pidsuspended
= FALSE
;
3782 * Suspends a task by placing a hold on its threads.
3785 * The caller holds a reference to the task
3793 if (task
== TASK_NULL
|| task
== kernel_task
) {
3794 return KERN_INVALID_ARGUMENT
;
3799 kr
= task_pidsuspend_locked(task
);
3803 if ((KERN_SUCCESS
== kr
) && task
->message_app_suspended
) {
3804 iokit_task_app_suspended_changed(task
);
3812 * Resumes a previously suspended task.
3815 * The caller holds a reference to the task
3823 if (task
== TASK_NULL
|| task
== kernel_task
) {
3824 return KERN_INVALID_ARGUMENT
;
3831 while (task
->changing_freeze_state
) {
3832 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3834 thread_block(THREAD_CONTINUE_NULL
);
3838 task
->changing_freeze_state
= TRUE
;
3841 kr
= release_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3845 if ((KERN_SUCCESS
== kr
) && task
->message_app_suspended
) {
3846 iokit_task_app_suspended_changed(task
);
3853 if (kr
== KERN_SUCCESS
) {
3854 task
->frozen
= FALSE
;
3856 task
->changing_freeze_state
= FALSE
;
3857 thread_wakeup(&task
->changing_freeze_state
);
3865 os_refgrp_decl(static, task_watchports_refgrp
, "task_watchports", NULL
);
3868 * task_add_turnstile_watchports:
3869 * Setup watchports to boost the main thread of the task.
3872 * task: task being spawned
3873 * thread: main thread of task
3874 * portwatch_ports: array of watchports
3875 * portwatch_count: number of watchports
3881 task_add_turnstile_watchports(
3884 ipc_port_t
*portwatch_ports
,
3885 uint32_t portwatch_count
)
3887 struct task_watchports
*watchports
= NULL
;
3888 struct task_watchport_elem
*previous_elem_array
[TASK_MAX_WATCHPORT_COUNT
] = {};
3889 os_ref_count_t refs
;
3891 /* Check if the task has terminated */
3892 if (!task
->active
) {
3896 assert(portwatch_count
<= TASK_MAX_WATCHPORT_COUNT
);
3898 watchports
= task_watchports_alloc_init(task
, thread
, portwatch_count
);
3900 /* Lock the ipc space */
3901 is_write_lock(task
->itk_space
);
3903 /* Setup watchports to boost the main thread */
3904 refs
= task_add_turnstile_watchports_locked(task
,
3905 watchports
, previous_elem_array
, portwatch_ports
,
3908 /* Drop the space lock */
3909 is_write_unlock(task
->itk_space
);
3912 task_watchports_deallocate(watchports
);
3915 /* Drop the ref on previous_elem_array */
3916 for (uint32_t i
= 0; i
< portwatch_count
&& previous_elem_array
[i
] != NULL
; i
++) {
3917 task_watchport_elem_deallocate(previous_elem_array
[i
]);
3922 * task_remove_turnstile_watchports:
3923 * Clear all turnstile boost on the task from watchports.
3926 * task: task being terminated
3932 task_remove_turnstile_watchports(
3935 os_ref_count_t refs
= TASK_MAX_WATCHPORT_COUNT
;
3936 struct task_watchports
*watchports
= NULL
;
3937 ipc_port_t port_freelist
[TASK_MAX_WATCHPORT_COUNT
] = {};
3938 uint32_t portwatch_count
;
3940 /* Lock the ipc space */
3941 is_write_lock(task
->itk_space
);
3943 /* Check if watchport boost exist */
3944 if (task
->watchports
== NULL
) {
3945 is_write_unlock(task
->itk_space
);
3948 watchports
= task
->watchports
;
3949 portwatch_count
= watchports
->tw_elem_array_count
;
3951 refs
= task_remove_turnstile_watchports_locked(task
, watchports
,
3954 is_write_unlock(task
->itk_space
);
3956 /* Drop all the port references */
3957 for (uint32_t i
= 0; i
< portwatch_count
&& port_freelist
[i
] != NULL
; i
++) {
3958 ip_release(port_freelist
[i
]);
3961 /* Clear the task and thread references for task_watchport */
3963 task_watchports_deallocate(watchports
);
3968 * task_transfer_turnstile_watchports:
3969 * Transfer all watchport turnstile boost from old task to new task.
3972 * old_task: task calling exec
3973 * new_task: new exec'ed task
3974 * thread: main thread of new task
3980 task_transfer_turnstile_watchports(
3983 thread_t new_thread
)
3985 struct task_watchports
*old_watchports
= NULL
;
3986 struct task_watchports
*new_watchports
= NULL
;
3987 os_ref_count_t old_refs
= TASK_MAX_WATCHPORT_COUNT
;
3988 os_ref_count_t new_refs
= TASK_MAX_WATCHPORT_COUNT
;
3989 uint32_t portwatch_count
;
3991 if (old_task
->watchports
== NULL
|| !new_task
->active
) {
3995 /* Get the watch port count from the old task */
3996 is_write_lock(old_task
->itk_space
);
3997 if (old_task
->watchports
== NULL
) {
3998 is_write_unlock(old_task
->itk_space
);
4002 portwatch_count
= old_task
->watchports
->tw_elem_array_count
;
4003 is_write_unlock(old_task
->itk_space
);
4005 new_watchports
= task_watchports_alloc_init(new_task
, new_thread
, portwatch_count
);
4007 /* Lock the ipc space for old task */
4008 is_write_lock(old_task
->itk_space
);
4010 /* Lock the ipc space for new task */
4011 is_write_lock(new_task
->itk_space
);
4013 /* Check if watchport boost exist */
4014 if (old_task
->watchports
== NULL
|| !new_task
->active
) {
4015 is_write_unlock(new_task
->itk_space
);
4016 is_write_unlock(old_task
->itk_space
);
4017 (void)task_watchports_release(new_watchports
);
4018 task_watchports_deallocate(new_watchports
);
4022 old_watchports
= old_task
->watchports
;
4023 assert(portwatch_count
== old_task
->watchports
->tw_elem_array_count
);
4025 /* Setup new task watchports */
4026 new_task
->watchports
= new_watchports
;
4028 for (uint32_t i
= 0; i
< portwatch_count
; i
++) {
4029 ipc_port_t port
= old_watchports
->tw_elem
[i
].twe_port
;
4032 task_watchport_elem_clear(&new_watchports
->tw_elem
[i
]);
4036 /* Lock the port and check if it has the entry */
4038 imq_lock(&port
->ip_messages
);
4040 task_watchport_elem_init(&new_watchports
->tw_elem
[i
], new_task
, port
);
4042 if (ipc_port_replace_watchport_elem_conditional_locked(port
,
4043 &old_watchports
->tw_elem
[i
], &new_watchports
->tw_elem
[i
]) == KERN_SUCCESS
) {
4044 task_watchport_elem_clear(&old_watchports
->tw_elem
[i
]);
4046 task_watchports_retain(new_watchports
);
4047 old_refs
= task_watchports_release(old_watchports
);
4049 /* Check if all ports are cleaned */
4050 if (old_refs
== 0) {
4051 old_task
->watchports
= NULL
;
4054 task_watchport_elem_clear(&new_watchports
->tw_elem
[i
]);
4056 /* mqueue and port unlocked by ipc_port_replace_watchport_elem_conditional_locked */
4059 /* Drop the reference on new task_watchports struct returned by task_watchports_alloc_init */
4060 new_refs
= task_watchports_release(new_watchports
);
4061 if (new_refs
== 0) {
4062 new_task
->watchports
= NULL
;
4065 is_write_unlock(new_task
->itk_space
);
4066 is_write_unlock(old_task
->itk_space
);
4068 /* Clear the task and thread references for old_watchport */
4069 if (old_refs
== 0) {
4070 task_watchports_deallocate(old_watchports
);
4073 /* Clear the task and thread references for new_watchport */
4074 if (new_refs
== 0) {
4075 task_watchports_deallocate(new_watchports
);
4080 * task_add_turnstile_watchports_locked:
4081 * Setup watchports to boost the main thread of the task.
4084 * task: task to boost
4085 * watchports: watchport structure to be attached to the task
4086 * previous_elem_array: an array of old watchport_elem to be returned to caller
4087 * portwatch_ports: array of watchports
4088 * portwatch_count: number of watchports
4091 * ipc space of the task locked.
4092 * returns array of old watchport_elem in previous_elem_array
4094 static os_ref_count_t
4095 task_add_turnstile_watchports_locked(
4097 struct task_watchports
*watchports
,
4098 struct task_watchport_elem
**previous_elem_array
,
4099 ipc_port_t
*portwatch_ports
,
4100 uint32_t portwatch_count
)
4102 os_ref_count_t refs
= TASK_MAX_WATCHPORT_COUNT
;
4104 /* Check if the task is still active */
4105 if (!task
->active
) {
4106 refs
= task_watchports_release(watchports
);
4110 assert(task
->watchports
== NULL
);
4111 task
->watchports
= watchports
;
4113 for (uint32_t i
= 0, j
= 0; i
< portwatch_count
; i
++) {
4114 ipc_port_t port
= portwatch_ports
[i
];
4116 task_watchport_elem_init(&watchports
->tw_elem
[i
], task
, port
);
4118 task_watchport_elem_clear(&watchports
->tw_elem
[i
]);
4123 imq_lock(&port
->ip_messages
);
4125 /* Check if port is in valid state to be setup as watchport */
4126 if (ipc_port_add_watchport_elem_locked(port
, &watchports
->tw_elem
[i
],
4127 &previous_elem_array
[j
]) != KERN_SUCCESS
) {
4128 task_watchport_elem_clear(&watchports
->tw_elem
[i
]);
4131 /* port and mqueue unlocked on return */
4134 task_watchports_retain(watchports
);
4135 if (previous_elem_array
[j
] != NULL
) {
4140 /* Drop the reference on task_watchport struct returned by os_ref_init */
4141 refs
= task_watchports_release(watchports
);
4143 task
->watchports
= NULL
;
4150 * task_remove_turnstile_watchports_locked:
4151 * Clear all turnstile boost on the task from watchports.
4154 * task: task to remove watchports from
4155 * watchports: watchports structure for the task
4156 * port_freelist: array of ports returned with ref to caller
4160 * ipc space of the task locked.
4161 * array of ports with refs are returned in port_freelist
4163 static os_ref_count_t
4164 task_remove_turnstile_watchports_locked(
4166 struct task_watchports
*watchports
,
4167 ipc_port_t
*port_freelist
)
4169 os_ref_count_t refs
= TASK_MAX_WATCHPORT_COUNT
;
4171 for (uint32_t i
= 0, j
= 0; i
< watchports
->tw_elem_array_count
; i
++) {
4172 ipc_port_t port
= watchports
->tw_elem
[i
].twe_port
;
4177 /* Lock the port and check if it has the entry */
4179 imq_lock(&port
->ip_messages
);
4180 if (ipc_port_clear_watchport_elem_internal_conditional_locked(port
,
4181 &watchports
->tw_elem
[i
]) == KERN_SUCCESS
) {
4182 task_watchport_elem_clear(&watchports
->tw_elem
[i
]);
4183 port_freelist
[j
++] = port
;
4184 refs
= task_watchports_release(watchports
);
4186 /* Check if all ports are cleaned */
4188 task
->watchports
= NULL
;
4192 /* mqueue and port unlocked by ipc_port_clear_watchport_elem_internal_conditional_locked */
4198 * task_watchports_alloc_init:
4199 * Allocate and initialize task watchport struct.
4204 static struct task_watchports
*
4205 task_watchports_alloc_init(
4210 struct task_watchports
*watchports
= kalloc(sizeof(struct task_watchports
) +
4211 count
* sizeof(struct task_watchport_elem
));
4213 task_reference(task
);
4214 thread_reference(thread
);
4215 watchports
->tw_task
= task
;
4216 watchports
->tw_thread
= thread
;
4217 watchports
->tw_elem_array_count
= count
;
4218 os_ref_init(&watchports
->tw_refcount
, &task_watchports_refgrp
);
4224 * task_watchports_deallocate:
4225 * Deallocate task watchport struct.
4231 task_watchports_deallocate(
4232 struct task_watchports
*watchports
)
4234 uint32_t portwatch_count
= watchports
->tw_elem_array_count
;
4236 task_deallocate(watchports
->tw_task
);
4237 thread_deallocate(watchports
->tw_thread
);
4238 kfree(watchports
, sizeof(struct task_watchports
) + portwatch_count
* sizeof(struct task_watchport_elem
));
4242 * task_watchport_elem_deallocate:
4243 * Deallocate task watchport element and release its ref on task_watchport.
4249 task_watchport_elem_deallocate(
4250 struct task_watchport_elem
*watchport_elem
)
4252 os_ref_count_t refs
= TASK_MAX_WATCHPORT_COUNT
;
4253 task_t task
= watchport_elem
->twe_task
;
4254 struct task_watchports
*watchports
= NULL
;
4255 ipc_port_t port
= NULL
;
4257 assert(task
!= NULL
);
4259 /* Take the space lock to modify the elememt */
4260 is_write_lock(task
->itk_space
);
4262 watchports
= task
->watchports
;
4263 assert(watchports
!= NULL
);
4265 port
= watchport_elem
->twe_port
;
4266 assert(port
!= NULL
);
4268 task_watchport_elem_clear(watchport_elem
);
4269 refs
= task_watchports_release(watchports
);
4272 task
->watchports
= NULL
;
4275 is_write_unlock(task
->itk_space
);
4279 task_watchports_deallocate(watchports
);
4284 * task_has_watchports:
4285 * Return TRUE if task has watchport boosts.
4291 task_has_watchports(task_t task
)
4293 return task
->watchports
!= NULL
;
4296 #if DEVELOPMENT || DEBUG
4298 extern void IOSleep(int);
4301 task_disconnect_page_mappings(task_t task
)
4305 if (task
== TASK_NULL
|| task
== kernel_task
) {
4306 return KERN_INVALID_ARGUMENT
;
4310 * this function is used to strip all of the mappings from
4311 * the pmap for the specified task to force the task to
4312 * re-fault all of the pages it is actively using... this
4313 * allows us to approximate the true working set of the
4314 * specified task. We only engage if at least 1 of the
4315 * threads in the task is runnable, but we want to continuously
4316 * sweep (at least for a while - I've arbitrarily set the limit at
4317 * 100 sweeps to be re-looked at as we gain experience) to get a better
4318 * view into what areas within a page are being visited (as opposed to only
4319 * seeing the first fault of a page after the task becomes
4320 * runnable)... in the future I may
4321 * try to block until awakened by a thread in this task
4322 * being made runnable, but for now we'll periodically poll from the
4323 * user level debug tool driving the sysctl
4325 for (n
= 0; n
< 100; n
++) {
4328 boolean_t do_unnest
;
4336 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4337 if (thread
->state
& TH_RUN
) {
4343 task
->task_disconnected_count
++;
4346 if (task
->task_unnested
== FALSE
) {
4347 if (runnable
== TRUE
) {
4348 task
->task_unnested
= TRUE
;
4354 if (runnable
== FALSE
) {
4358 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_START
,
4359 task
, do_unnest
, task
->task_disconnected_count
, 0, 0);
4361 page_count
= vm_map_disconnect_page_mappings(task
->map
, do_unnest
);
4363 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_END
,
4364 task
, page_count
, 0, 0, 0);
4370 return KERN_SUCCESS
;
4384 * The caller holds a reference to the task
4386 extern void vm_wake_compactor_swapper(void);
4387 extern queue_head_t c_swapout_list_head
;
4388 extern struct freezer_context freezer_context_global
;
4393 uint32_t *purgeable_count
,
4394 uint32_t *wired_count
,
4395 uint32_t *clean_count
,
4396 uint32_t *dirty_count
,
4397 uint32_t dirty_budget
,
4398 uint32_t *shared_count
,
4399 int *freezer_error_code
,
4400 boolean_t eval_only
)
4402 kern_return_t kr
= KERN_SUCCESS
;
4404 if (task
== TASK_NULL
|| task
== kernel_task
) {
4405 return KERN_INVALID_ARGUMENT
;
4410 while (task
->changing_freeze_state
) {
4411 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
4413 thread_block(THREAD_CONTINUE_NULL
);
4419 return KERN_FAILURE
;
4421 task
->changing_freeze_state
= TRUE
;
4423 freezer_context_global
.freezer_ctx_task
= task
;
4427 kr
= vm_map_freeze(task
,
4439 if ((kr
== KERN_SUCCESS
) && (eval_only
== FALSE
)) {
4440 task
->frozen
= TRUE
;
4442 freezer_context_global
.freezer_ctx_task
= NULL
;
4443 freezer_context_global
.freezer_ctx_uncompressed_pages
= 0;
4445 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
4447 * reset the counter tracking the # of swapped compressed pages
4448 * because we are now done with this freeze session and task.
4451 *dirty_count
= (uint32_t) (freezer_context_global
.freezer_ctx_swapped_bytes
/ PAGE_SIZE_64
); /*used to track pageouts*/
4454 freezer_context_global
.freezer_ctx_swapped_bytes
= 0;
4457 task
->changing_freeze_state
= FALSE
;
4458 thread_wakeup(&task
->changing_freeze_state
);
4462 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
&&
4463 (kr
== KERN_SUCCESS
) &&
4464 (eval_only
== FALSE
)) {
4465 vm_wake_compactor_swapper();
4467 * We do an explicit wakeup of the swapout thread here
4468 * because the compact_and_swap routines don't have
4469 * knowledge about these kind of "per-task packed c_segs"
4470 * and so will not be evaluating whether we need to do
4473 thread_wakeup((event_t
)&c_swapout_list_head
);
4482 * Thaw a currently frozen task.
4485 * The caller holds a reference to the task
4491 if (task
== TASK_NULL
|| task
== kernel_task
) {
4492 return KERN_INVALID_ARGUMENT
;
4497 while (task
->changing_freeze_state
) {
4498 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
4500 thread_block(THREAD_CONTINUE_NULL
);
4504 if (!task
->frozen
) {
4506 return KERN_FAILURE
;
4508 task
->frozen
= FALSE
;
4512 return KERN_SUCCESS
;
4516 task_update_frozen_to_swap_acct(task_t task
, int64_t amount
, freezer_acct_op_t op
)
4519 * We don't assert that the task lock is held because we call this
4520 * routine from the decompression path and we won't be holding the
4521 * task lock. However, since we are in the context of the task we are
4523 * In the case of the task_freeze path, we call it from behind the task
4524 * lock but we don't need to because we have a reference on the proc
4533 if (op
== CREDIT_TO_SWAP
) {
4534 ledger_credit_nocheck(task
->ledger
, task_ledgers
.frozen_to_swap
, amount
);
4535 } else if (op
== DEBIT_FROM_SWAP
) {
4536 ledger_debit_nocheck(task
->ledger
, task_ledgers
.frozen_to_swap
, amount
);
4538 panic("task_update_frozen_to_swap_acct: Invalid ledger op\n");
4541 #endif /* CONFIG_FREEZE */
4544 host_security_set_task_token(
4545 host_security_t host_security
,
4547 security_token_t sec_token
,
4548 audit_token_t audit_token
,
4549 host_priv_t host_priv
)
4551 ipc_port_t host_port
;
4554 if (task
== TASK_NULL
) {
4555 return KERN_INVALID_ARGUMENT
;
4558 if (host_security
== HOST_NULL
) {
4559 return KERN_INVALID_SECURITY
;
4563 task
->sec_token
= sec_token
;
4564 task
->audit_token
= audit_token
;
4567 if (host_priv
!= HOST_PRIV_NULL
) {
4568 kr
= host_get_host_priv_port(host_priv
, &host_port
);
4570 kr
= host_get_host_port(host_priv_self(), &host_port
);
4572 assert(kr
== KERN_SUCCESS
);
4574 kr
= task_set_special_port_internal(task
, TASK_HOST_PORT
, host_port
);
4579 task_send_trace_memory(
4580 __unused task_t target_task
,
4581 __unused
uint32_t pid
,
4582 __unused
uint64_t uniqueid
)
4584 return KERN_INVALID_ARGUMENT
;
4588 * This routine was added, pretty much exclusively, for registering the
4589 * RPC glue vector for in-kernel short circuited tasks. Rather than
4590 * removing it completely, I have only disabled that feature (which was
4591 * the only feature at the time). It just appears that we are going to
4592 * want to add some user data to tasks in the future (i.e. bsd info,
4593 * task names, etc...), so I left it in the formal task interface.
4598 task_flavor_t flavor
,
4599 __unused task_info_t task_info_in
, /* pointer to IN array */
4600 __unused mach_msg_type_number_t task_info_count
)
4602 if (task
== TASK_NULL
) {
4603 return KERN_INVALID_ARGUMENT
;
4607 case TASK_TRACE_MEMORY_INFO
:
4608 return KERN_NOT_SUPPORTED
;
4609 #endif // CONFIG_ATM
4611 return KERN_INVALID_ARGUMENT
;
4615 int radar_20146450
= 1;
4619 task_flavor_t flavor
,
4620 task_info_t task_info_out
,
4621 mach_msg_type_number_t
*task_info_count
)
4623 kern_return_t error
= KERN_SUCCESS
;
4624 mach_msg_type_number_t original_task_info_count
;
4626 if (task
== TASK_NULL
) {
4627 return KERN_INVALID_ARGUMENT
;
4630 original_task_info_count
= *task_info_count
;
4633 if ((task
!= current_task()) && (!task
->active
)) {
4635 return KERN_INVALID_ARGUMENT
;
4640 case TASK_BASIC_INFO_32
:
4641 case TASK_BASIC2_INFO_32
:
4642 #if defined(__arm__) || defined(__arm64__)
4643 case TASK_BASIC_INFO_64
:
4646 task_basic_info_32_t basic_info
;
4651 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
) {
4652 error
= KERN_INVALID_ARGUMENT
;
4656 basic_info
= (task_basic_info_32_t
)task_info_out
;
4658 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
4659 basic_info
->virtual_size
= (typeof(basic_info
->virtual_size
))vm_map_adjusted_size(map
);
4660 if (flavor
== TASK_BASIC2_INFO_32
) {
4662 * The "BASIC2" flavor gets the maximum resident
4663 * size instead of the current resident size...
4665 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
4667 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
4669 basic_info
->resident_size
*= PAGE_SIZE
;
4671 basic_info
->policy
= ((task
!= kernel_task
)?
4672 POLICY_TIMESHARE
: POLICY_RR
);
4673 basic_info
->suspend_count
= task
->user_stop_count
;
4675 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
4676 basic_info
->user_time
.seconds
=
4677 (typeof(basic_info
->user_time
.seconds
))secs
;
4678 basic_info
->user_time
.microseconds
= usecs
;
4680 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
4681 basic_info
->system_time
.seconds
=
4682 (typeof(basic_info
->system_time
.seconds
))secs
;
4683 basic_info
->system_time
.microseconds
= usecs
;
4685 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
4689 #if defined(__arm__) || defined(__arm64__)
4690 case TASK_BASIC_INFO_64_2
:
4692 task_basic_info_64_2_t basic_info
;
4697 if (*task_info_count
< TASK_BASIC_INFO_64_2_COUNT
) {
4698 error
= KERN_INVALID_ARGUMENT
;
4702 basic_info
= (task_basic_info_64_2_t
)task_info_out
;
4704 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
4705 basic_info
->virtual_size
= vm_map_adjusted_size(map
);
4706 basic_info
->resident_size
=
4707 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
4710 basic_info
->policy
= ((task
!= kernel_task
)?
4711 POLICY_TIMESHARE
: POLICY_RR
);
4712 basic_info
->suspend_count
= task
->user_stop_count
;
4714 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
4715 basic_info
->user_time
.seconds
=
4716 (typeof(basic_info
->user_time
.seconds
))secs
;
4717 basic_info
->user_time
.microseconds
= usecs
;
4719 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
4720 basic_info
->system_time
.seconds
=
4721 (typeof(basic_info
->system_time
.seconds
))secs
;
4722 basic_info
->system_time
.microseconds
= usecs
;
4724 *task_info_count
= TASK_BASIC_INFO_64_2_COUNT
;
4728 #else /* defined(__arm__) || defined(__arm64__) */
4729 case TASK_BASIC_INFO_64
:
4731 task_basic_info_64_t basic_info
;
4736 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
) {
4737 error
= KERN_INVALID_ARGUMENT
;
4741 basic_info
= (task_basic_info_64_t
)task_info_out
;
4743 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
4744 basic_info
->virtual_size
= vm_map_adjusted_size(map
);
4745 basic_info
->resident_size
=
4746 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
4749 basic_info
->policy
= ((task
!= kernel_task
)?
4750 POLICY_TIMESHARE
: POLICY_RR
);
4751 basic_info
->suspend_count
= task
->user_stop_count
;
4753 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
4754 basic_info
->user_time
.seconds
=
4755 (typeof(basic_info
->user_time
.seconds
))secs
;
4756 basic_info
->user_time
.microseconds
= usecs
;
4758 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
4759 basic_info
->system_time
.seconds
=
4760 (typeof(basic_info
->system_time
.seconds
))secs
;
4761 basic_info
->system_time
.microseconds
= usecs
;
4763 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
4766 #endif /* defined(__arm__) || defined(__arm64__) */
4768 case MACH_TASK_BASIC_INFO
:
4770 mach_task_basic_info_t basic_info
;
4775 if (*task_info_count
< MACH_TASK_BASIC_INFO_COUNT
) {
4776 error
= KERN_INVALID_ARGUMENT
;
4780 basic_info
= (mach_task_basic_info_t
)task_info_out
;
4782 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
4784 basic_info
->virtual_size
= vm_map_adjusted_size(map
);
4786 basic_info
->resident_size
=
4787 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
));
4788 basic_info
->resident_size
*= PAGE_SIZE_64
;
4790 basic_info
->resident_size_max
=
4791 (mach_vm_size_t
)(pmap_resident_max(map
->pmap
));
4792 basic_info
->resident_size_max
*= PAGE_SIZE_64
;
4794 basic_info
->policy
= ((task
!= kernel_task
) ?
4795 POLICY_TIMESHARE
: POLICY_RR
);
4797 basic_info
->suspend_count
= task
->user_stop_count
;
4799 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
4800 basic_info
->user_time
.seconds
=
4801 (typeof(basic_info
->user_time
.seconds
))secs
;
4802 basic_info
->user_time
.microseconds
= usecs
;
4804 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
4805 basic_info
->system_time
.seconds
=
4806 (typeof(basic_info
->system_time
.seconds
))secs
;
4807 basic_info
->system_time
.microseconds
= usecs
;
4809 *task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
4813 case TASK_THREAD_TIMES_INFO
:
4815 task_thread_times_info_t times_info
;
4818 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
4819 error
= KERN_INVALID_ARGUMENT
;
4823 times_info
= (task_thread_times_info_t
) task_info_out
;
4824 times_info
->user_time
.seconds
= 0;
4825 times_info
->user_time
.microseconds
= 0;
4826 times_info
->system_time
.seconds
= 0;
4827 times_info
->system_time
.microseconds
= 0;
4830 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4831 time_value_t user_time
, system_time
;
4833 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
4837 thread_read_times(thread
, &user_time
, &system_time
, NULL
);
4839 time_value_add(×_info
->user_time
, &user_time
);
4840 time_value_add(×_info
->system_time
, &system_time
);
4843 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
4847 case TASK_ABSOLUTETIME_INFO
:
4849 task_absolutetime_info_t info
;
4852 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
) {
4853 error
= KERN_INVALID_ARGUMENT
;
4857 info
= (task_absolutetime_info_t
)task_info_out
;
4858 info
->threads_user
= info
->threads_system
= 0;
4861 info
->total_user
= task
->total_user_time
;
4862 info
->total_system
= task
->total_system_time
;
4864 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4868 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
4873 thread_lock(thread
);
4875 tval
= timer_grab(&thread
->user_timer
);
4876 info
->threads_user
+= tval
;
4877 info
->total_user
+= tval
;
4879 tval
= timer_grab(&thread
->system_timer
);
4880 if (thread
->precise_user_kernel_time
) {
4881 info
->threads_system
+= tval
;
4882 info
->total_system
+= tval
;
4884 /* system_timer may represent either sys or user */
4885 info
->threads_user
+= tval
;
4886 info
->total_user
+= tval
;
4889 thread_unlock(thread
);
4894 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
4898 case TASK_DYLD_INFO
:
4900 task_dyld_info_t info
;
4903 * We added the format field to TASK_DYLD_INFO output. For
4904 * temporary backward compatibility, accept the fact that
4905 * clients may ask for the old version - distinquished by the
4906 * size of the expected result structure.
4908 #define TASK_LEGACY_DYLD_INFO_COUNT \
4909 offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
4911 if (*task_info_count
< TASK_LEGACY_DYLD_INFO_COUNT
) {
4912 error
= KERN_INVALID_ARGUMENT
;
4916 info
= (task_dyld_info_t
)task_info_out
;
4917 info
->all_image_info_addr
= task
->all_image_info_addr
;
4918 info
->all_image_info_size
= task
->all_image_info_size
;
4920 /* only set format on output for those expecting it */
4921 if (*task_info_count
>= TASK_DYLD_INFO_COUNT
) {
4922 info
->all_image_info_format
= task_has_64Bit_addr(task
) ?
4923 TASK_DYLD_ALL_IMAGE_INFO_64
:
4924 TASK_DYLD_ALL_IMAGE_INFO_32
;
4925 *task_info_count
= TASK_DYLD_INFO_COUNT
;
4927 *task_info_count
= TASK_LEGACY_DYLD_INFO_COUNT
;
4932 case TASK_EXTMOD_INFO
:
4934 task_extmod_info_t info
;
4937 if (*task_info_count
< TASK_EXTMOD_INFO_COUNT
) {
4938 error
= KERN_INVALID_ARGUMENT
;
4942 info
= (task_extmod_info_t
)task_info_out
;
4944 p
= get_bsdtask_info(task
);
4946 proc_getexecutableuuid(p
, info
->task_uuid
, sizeof(info
->task_uuid
));
4948 bzero(info
->task_uuid
, sizeof(info
->task_uuid
));
4950 info
->extmod_statistics
= task
->extmod_statistics
;
4951 *task_info_count
= TASK_EXTMOD_INFO_COUNT
;
4956 case TASK_KERNELMEMORY_INFO
:
4958 task_kernelmemory_info_t tkm_info
;
4959 ledger_amount_t credit
, debit
;
4961 if (*task_info_count
< TASK_KERNELMEMORY_INFO_COUNT
) {
4962 error
= KERN_INVALID_ARGUMENT
;
4966 tkm_info
= (task_kernelmemory_info_t
) task_info_out
;
4967 tkm_info
->total_palloc
= 0;
4968 tkm_info
->total_pfree
= 0;
4969 tkm_info
->total_salloc
= 0;
4970 tkm_info
->total_sfree
= 0;
4972 if (task
== kernel_task
) {
4974 * All shared allocs/frees from other tasks count against
4975 * the kernel private memory usage. If we are looking up
4976 * info for the kernel task, gather from everywhere.
4980 /* start by accounting for all the terminated tasks against the kernel */
4981 tkm_info
->total_palloc
= tasks_tkm_private
.alloc
+ tasks_tkm_shared
.alloc
;
4982 tkm_info
->total_pfree
= tasks_tkm_private
.free
+ tasks_tkm_shared
.free
;
4984 /* count all other task/thread shared alloc/free against the kernel */
4985 lck_mtx_lock(&tasks_threads_lock
);
4987 /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
4988 queue_iterate(&tasks
, task
, task_t
, tasks
) {
4989 if (task
== kernel_task
) {
4990 if (ledger_get_entries(task
->ledger
,
4991 task_ledgers
.tkm_private
, &credit
,
4992 &debit
) == KERN_SUCCESS
) {
4993 tkm_info
->total_palloc
+= credit
;
4994 tkm_info
->total_pfree
+= debit
;
4997 if (!ledger_get_entries(task
->ledger
,
4998 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
4999 tkm_info
->total_palloc
+= credit
;
5000 tkm_info
->total_pfree
+= debit
;
5003 lck_mtx_unlock(&tasks_threads_lock
);
5005 if (!ledger_get_entries(task
->ledger
,
5006 task_ledgers
.tkm_private
, &credit
, &debit
)) {
5007 tkm_info
->total_palloc
= credit
;
5008 tkm_info
->total_pfree
= debit
;
5010 if (!ledger_get_entries(task
->ledger
,
5011 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
5012 tkm_info
->total_salloc
= credit
;
5013 tkm_info
->total_sfree
= debit
;
5018 *task_info_count
= TASK_KERNELMEMORY_INFO_COUNT
;
5019 return KERN_SUCCESS
;
5023 case TASK_SCHED_FIFO_INFO
:
5025 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
) {
5026 error
= KERN_INVALID_ARGUMENT
;
5030 error
= KERN_INVALID_POLICY
;
5035 case TASK_SCHED_RR_INFO
:
5037 policy_rr_base_t rr_base
;
5038 uint32_t quantum_time
;
5039 uint64_t quantum_ns
;
5041 if (*task_info_count
< POLICY_RR_BASE_COUNT
) {
5042 error
= KERN_INVALID_ARGUMENT
;
5046 rr_base
= (policy_rr_base_t
) task_info_out
;
5048 if (task
!= kernel_task
) {
5049 error
= KERN_INVALID_POLICY
;
5053 rr_base
->base_priority
= task
->priority
;
5055 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
5056 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
5058 rr_base
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
5060 *task_info_count
= POLICY_RR_BASE_COUNT
;
5065 case TASK_SCHED_TIMESHARE_INFO
:
5067 policy_timeshare_base_t ts_base
;
5069 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
) {
5070 error
= KERN_INVALID_ARGUMENT
;
5074 ts_base
= (policy_timeshare_base_t
) task_info_out
;
5076 if (task
== kernel_task
) {
5077 error
= KERN_INVALID_POLICY
;
5081 ts_base
->base_priority
= task
->priority
;
5083 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
5087 case TASK_SECURITY_TOKEN
:
5089 security_token_t
*sec_token_p
;
5091 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
5092 error
= KERN_INVALID_ARGUMENT
;
5096 sec_token_p
= (security_token_t
*) task_info_out
;
5098 *sec_token_p
= task
->sec_token
;
5100 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
5104 case TASK_AUDIT_TOKEN
:
5106 audit_token_t
*audit_token_p
;
5108 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
5109 error
= KERN_INVALID_ARGUMENT
;
5113 audit_token_p
= (audit_token_t
*) task_info_out
;
5115 *audit_token_p
= task
->audit_token
;
5117 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
5121 case TASK_SCHED_INFO
:
5122 error
= KERN_INVALID_ARGUMENT
;
5125 case TASK_EVENTS_INFO
:
5127 task_events_info_t events_info
;
5130 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
5131 error
= KERN_INVALID_ARGUMENT
;
5135 events_info
= (task_events_info_t
) task_info_out
;
5138 events_info
->faults
= task
->faults
;
5139 events_info
->pageins
= task
->pageins
;
5140 events_info
->cow_faults
= task
->cow_faults
;
5141 events_info
->messages_sent
= task
->messages_sent
;
5142 events_info
->messages_received
= task
->messages_received
;
5143 events_info
->syscalls_mach
= task
->syscalls_mach
;
5144 events_info
->syscalls_unix
= task
->syscalls_unix
;
5146 events_info
->csw
= task
->c_switch
;
5148 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5149 events_info
->csw
+= thread
->c_switch
;
5150 events_info
->syscalls_mach
+= thread
->syscalls_mach
;
5151 events_info
->syscalls_unix
+= thread
->syscalls_unix
;
5155 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
5158 case TASK_AFFINITY_TAG_INFO
:
5160 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
) {
5161 error
= KERN_INVALID_ARGUMENT
;
5165 error
= task_affinity_info(task
, task_info_out
, task_info_count
);
5168 case TASK_POWER_INFO
:
5170 if (*task_info_count
< TASK_POWER_INFO_COUNT
) {
5171 error
= KERN_INVALID_ARGUMENT
;
5175 task_power_info_locked(task
, (task_power_info_t
)task_info_out
, NULL
, NULL
, NULL
);
5179 case TASK_POWER_INFO_V2
:
5181 if (*task_info_count
< TASK_POWER_INFO_V2_COUNT_OLD
) {
5182 error
= KERN_INVALID_ARGUMENT
;
5185 task_power_info_v2_t tpiv2
= (task_power_info_v2_t
) task_info_out
;
5186 task_power_info_locked(task
, &tpiv2
->cpu_energy
, &tpiv2
->gpu_energy
, tpiv2
, NULL
);
5191 case TASK_VM_INFO_PURGEABLE
:
5193 task_vm_info_t vm_info
;
5198 uint32_t platform
, sdk
;
5200 platform
= proc_platform(p
);
5201 sdk
= proc_min_sdk(p
);
5202 if (original_task_info_count
> TASK_VM_INFO_REV2_COUNT
&&
5203 platform
== PLATFORM_IOS
&&
5205 (sdk
>> 16) <= 12) {
5207 * Some iOS apps pass an incorrect value for
5208 * task_info_count, expressed in number of bytes
5209 * instead of number of "natural_t" elements.
5210 * For the sake of backwards binary compatibility
5211 * for apps built with an iOS12 or older SDK and using
5212 * the "rev2" data structure, let's fix task_info_count
5213 * for them, to avoid stomping past the actual end
5216 #if DEVELOPMENT || DEBUG
5217 printf("%s:%d %d[%s] rdar://49484582 task_info_count %d -> %d platform %d sdk %d.%d.%d\n", __FUNCTION__
, __LINE__
, proc_pid(p
), proc_name_address(p
), original_task_info_count
, TASK_VM_INFO_REV2_COUNT
, platform
, (sdk
>> 16), ((sdk
>> 8) & 0xff), (sdk
& 0xff));
5218 #endif /* DEVELOPMENT || DEBUG */
5219 DTRACE_VM4(workaround_task_vm_info_count
,
5220 mach_msg_type_number_t
, original_task_info_count
,
5221 mach_msg_type_number_t
, TASK_VM_INFO_REV2_COUNT
,
5224 original_task_info_count
= TASK_VM_INFO_REV2_COUNT
;
5225 *task_info_count
= original_task_info_count
;
5227 #endif /* __arm64__ */
5229 if (*task_info_count
< TASK_VM_INFO_REV0_COUNT
) {
5230 error
= KERN_INVALID_ARGUMENT
;
5234 vm_info
= (task_vm_info_t
)task_info_out
;
5236 if (task
== kernel_task
) {
5241 vm_map_lock_read(map
);
5244 vm_info
->virtual_size
= (typeof(vm_info
->virtual_size
))vm_map_adjusted_size(map
);
5245 vm_info
->region_count
= map
->hdr
.nentries
;
5246 vm_info
->page_size
= vm_map_page_size(map
);
5248 vm_info
->resident_size
= pmap_resident_count(map
->pmap
);
5249 vm_info
->resident_size
*= PAGE_SIZE
;
5250 vm_info
->resident_size_peak
= pmap_resident_max(map
->pmap
);
5251 vm_info
->resident_size_peak
*= PAGE_SIZE
;
5253 #define _VM_INFO(_name) \
5254 vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
5257 _VM_INFO(device_peak
);
5259 _VM_INFO(external_peak
);
5261 _VM_INFO(internal_peak
);
5263 _VM_INFO(reusable_peak
);
5264 _VM_INFO(compressed
);
5265 _VM_INFO(compressed_peak
);
5266 _VM_INFO(compressed_lifetime
);
5268 vm_info
->purgeable_volatile_pmap
= 0;
5269 vm_info
->purgeable_volatile_resident
= 0;
5270 vm_info
->purgeable_volatile_virtual
= 0;
5271 if (task
== kernel_task
) {
5273 * We do not maintain the detailed stats for the
5274 * kernel_pmap, so just count everything as
5277 vm_info
->internal
= vm_info
->resident_size
;
5279 * ... but since the memory held by the VM compressor
5280 * in the kernel address space ought to be attributed
5281 * to user-space tasks, we subtract it from "internal"
5282 * to give memory reporting tools a more accurate idea
5283 * of what the kernel itself is actually using, instead
5284 * of making it look like the kernel is leaking memory
5285 * when the system is under memory pressure.
5287 vm_info
->internal
-= (VM_PAGE_COMPRESSOR_COUNT
*
5290 mach_vm_size_t volatile_virtual_size
;
5291 mach_vm_size_t volatile_resident_size
;
5292 mach_vm_size_t volatile_compressed_size
;
5293 mach_vm_size_t volatile_pmap_size
;
5294 mach_vm_size_t volatile_compressed_pmap_size
;
5297 if (flavor
== TASK_VM_INFO_PURGEABLE
) {
5298 kr
= vm_map_query_volatile(
5300 &volatile_virtual_size
,
5301 &volatile_resident_size
,
5302 &volatile_compressed_size
,
5303 &volatile_pmap_size
,
5304 &volatile_compressed_pmap_size
);
5305 if (kr
== KERN_SUCCESS
) {
5306 vm_info
->purgeable_volatile_pmap
=
5308 if (radar_20146450
) {
5309 vm_info
->compressed
-=
5310 volatile_compressed_pmap_size
;
5312 vm_info
->purgeable_volatile_resident
=
5313 volatile_resident_size
;
5314 vm_info
->purgeable_volatile_virtual
=
5315 volatile_virtual_size
;
5319 *task_info_count
= TASK_VM_INFO_REV0_COUNT
;
5321 if (original_task_info_count
>= TASK_VM_INFO_REV1_COUNT
) {
5322 vm_info
->phys_footprint
=
5323 (mach_vm_size_t
) get_task_phys_footprint(task
);
5324 *task_info_count
= TASK_VM_INFO_REV1_COUNT
;
5326 if (original_task_info_count
>= TASK_VM_INFO_REV2_COUNT
) {
5327 vm_info
->min_address
= map
->min_offset
;
5328 vm_info
->max_address
= map
->max_offset
;
5329 *task_info_count
= TASK_VM_INFO_REV2_COUNT
;
5331 if (original_task_info_count
>= TASK_VM_INFO_REV3_COUNT
) {
5332 ledger_get_lifetime_max(task
->ledger
,
5333 task_ledgers
.phys_footprint
,
5334 &vm_info
->ledger_phys_footprint_peak
);
5335 ledger_get_balance(task
->ledger
,
5336 task_ledgers
.purgeable_nonvolatile
,
5337 &vm_info
->ledger_purgeable_nonvolatile
);
5338 ledger_get_balance(task
->ledger
,
5339 task_ledgers
.purgeable_nonvolatile_compressed
,
5340 &vm_info
->ledger_purgeable_novolatile_compressed
);
5341 ledger_get_balance(task
->ledger
,
5342 task_ledgers
.purgeable_volatile
,
5343 &vm_info
->ledger_purgeable_volatile
);
5344 ledger_get_balance(task
->ledger
,
5345 task_ledgers
.purgeable_volatile_compressed
,
5346 &vm_info
->ledger_purgeable_volatile_compressed
);
5347 ledger_get_balance(task
->ledger
,
5348 task_ledgers
.network_nonvolatile
,
5349 &vm_info
->ledger_tag_network_nonvolatile
);
5350 ledger_get_balance(task
->ledger
,
5351 task_ledgers
.network_nonvolatile_compressed
,
5352 &vm_info
->ledger_tag_network_nonvolatile_compressed
);
5353 ledger_get_balance(task
->ledger
,
5354 task_ledgers
.network_volatile
,
5355 &vm_info
->ledger_tag_network_volatile
);
5356 ledger_get_balance(task
->ledger
,
5357 task_ledgers
.network_volatile_compressed
,
5358 &vm_info
->ledger_tag_network_volatile_compressed
);
5359 ledger_get_balance(task
->ledger
,
5360 task_ledgers
.media_footprint
,
5361 &vm_info
->ledger_tag_media_footprint
);
5362 ledger_get_balance(task
->ledger
,
5363 task_ledgers
.media_footprint_compressed
,
5364 &vm_info
->ledger_tag_media_footprint_compressed
);
5365 ledger_get_balance(task
->ledger
,
5366 task_ledgers
.media_nofootprint
,
5367 &vm_info
->ledger_tag_media_nofootprint
);
5368 ledger_get_balance(task
->ledger
,
5369 task_ledgers
.media_nofootprint_compressed
,
5370 &vm_info
->ledger_tag_media_nofootprint_compressed
);
5371 ledger_get_balance(task
->ledger
,
5372 task_ledgers
.graphics_footprint
,
5373 &vm_info
->ledger_tag_graphics_footprint
);
5374 ledger_get_balance(task
->ledger
,
5375 task_ledgers
.graphics_footprint_compressed
,
5376 &vm_info
->ledger_tag_graphics_footprint_compressed
);
5377 ledger_get_balance(task
->ledger
,
5378 task_ledgers
.graphics_nofootprint
,
5379 &vm_info
->ledger_tag_graphics_nofootprint
);
5380 ledger_get_balance(task
->ledger
,
5381 task_ledgers
.graphics_nofootprint_compressed
,
5382 &vm_info
->ledger_tag_graphics_nofootprint_compressed
);
5383 ledger_get_balance(task
->ledger
,
5384 task_ledgers
.neural_footprint
,
5385 &vm_info
->ledger_tag_neural_footprint
);
5386 ledger_get_balance(task
->ledger
,
5387 task_ledgers
.neural_footprint_compressed
,
5388 &vm_info
->ledger_tag_neural_footprint_compressed
);
5389 ledger_get_balance(task
->ledger
,
5390 task_ledgers
.neural_nofootprint
,
5391 &vm_info
->ledger_tag_neural_nofootprint
);
5392 ledger_get_balance(task
->ledger
,
5393 task_ledgers
.neural_nofootprint_compressed
,
5394 &vm_info
->ledger_tag_neural_nofootprint_compressed
);
5395 *task_info_count
= TASK_VM_INFO_REV3_COUNT
;
5397 if (original_task_info_count
>= TASK_VM_INFO_REV4_COUNT
) {
5398 if (task
->bsd_info
) {
5399 vm_info
->limit_bytes_remaining
=
5400 memorystatus_available_memory_internal(task
->bsd_info
);
5402 vm_info
->limit_bytes_remaining
= 0;
5404 *task_info_count
= TASK_VM_INFO_REV4_COUNT
;
5406 if (original_task_info_count
>= TASK_VM_INFO_REV5_COUNT
) {
5408 integer_t total
= task
->decompressions
;
5409 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5410 total
+= thread
->decompressions
;
5412 vm_info
->decompressions
= total
;
5413 *task_info_count
= TASK_VM_INFO_REV5_COUNT
;
5416 if (task
!= kernel_task
) {
5417 vm_map_unlock_read(map
);
5423 case TASK_WAIT_STATE_INFO
:
5426 * Deprecated flavor. Currently allowing some results until all users
5427 * stop calling it. The results may not be accurate.
5429 task_wait_state_info_t wait_state_info
;
5430 uint64_t total_sfi_ledger_val
= 0;
5432 if (*task_info_count
< TASK_WAIT_STATE_INFO_COUNT
) {
5433 error
= KERN_INVALID_ARGUMENT
;
5437 wait_state_info
= (task_wait_state_info_t
) task_info_out
;
5439 wait_state_info
->total_wait_state_time
= 0;
5440 bzero(wait_state_info
->_reserved
, sizeof(wait_state_info
->_reserved
));
5442 #if CONFIG_SCHED_SFI
5443 int i
, prev_lentry
= -1;
5444 int64_t val_credit
, val_debit
;
5446 for (i
= 0; i
< MAX_SFI_CLASS_ID
; i
++) {
5449 * checking with prev_lentry != entry ensures adjacent classes
5450 * which share the same ledger do not add wait times twice.
5451 * Note: Use ledger() call to get data for each individual sfi class.
5453 if (prev_lentry
!= task_ledgers
.sfi_wait_times
[i
] &&
5454 KERN_SUCCESS
== ledger_get_entries(task
->ledger
,
5455 task_ledgers
.sfi_wait_times
[i
], &val_credit
, &val_debit
)) {
5456 total_sfi_ledger_val
+= val_credit
;
5458 prev_lentry
= task_ledgers
.sfi_wait_times
[i
];
5461 #endif /* CONFIG_SCHED_SFI */
5462 wait_state_info
->total_wait_sfi_state_time
= total_sfi_ledger_val
;
5463 *task_info_count
= TASK_WAIT_STATE_INFO_COUNT
;
5467 case TASK_VM_INFO_PURGEABLE_ACCOUNT
:
5469 #if DEVELOPMENT || DEBUG
5470 pvm_account_info_t acnt_info
;
5472 if (*task_info_count
< PVM_ACCOUNT_INFO_COUNT
) {
5473 error
= KERN_INVALID_ARGUMENT
;
5477 if (task_info_out
== NULL
) {
5478 error
= KERN_INVALID_ARGUMENT
;
5482 acnt_info
= (pvm_account_info_t
) task_info_out
;
5484 error
= vm_purgeable_account(task
, acnt_info
);
5486 *task_info_count
= PVM_ACCOUNT_INFO_COUNT
;
5489 #else /* DEVELOPMENT || DEBUG */
5490 error
= KERN_NOT_SUPPORTED
;
5492 #endif /* DEVELOPMENT || DEBUG */
5494 case TASK_FLAGS_INFO
:
5496 task_flags_info_t flags_info
;
5498 if (*task_info_count
< TASK_FLAGS_INFO_COUNT
) {
5499 error
= KERN_INVALID_ARGUMENT
;
5503 flags_info
= (task_flags_info_t
)task_info_out
;
5505 /* only publish the 64-bit flag of the task */
5506 flags_info
->flags
= task
->t_flags
& (TF_64B_ADDR
| TF_64B_DATA
);
5508 *task_info_count
= TASK_FLAGS_INFO_COUNT
;
5512 case TASK_DEBUG_INFO_INTERNAL
:
5514 #if DEVELOPMENT || DEBUG
5515 task_debug_info_internal_t dbg_info
;
5516 ipc_space_t space
= task
->itk_space
;
5517 if (*task_info_count
< TASK_DEBUG_INFO_INTERNAL_COUNT
) {
5518 error
= KERN_NOT_SUPPORTED
;
5522 if (task_info_out
== NULL
) {
5523 error
= KERN_INVALID_ARGUMENT
;
5526 dbg_info
= (task_debug_info_internal_t
) task_info_out
;
5527 dbg_info
->ipc_space_size
= 0;
5530 is_read_lock(space
);
5531 dbg_info
->ipc_space_size
= space
->is_table_size
;
5532 is_read_unlock(space
);
5535 dbg_info
->suspend_count
= task
->suspend_count
;
5537 error
= KERN_SUCCESS
;
5538 *task_info_count
= TASK_DEBUG_INFO_INTERNAL_COUNT
;
5540 #else /* DEVELOPMENT || DEBUG */
5541 error
= KERN_NOT_SUPPORTED
;
5543 #endif /* DEVELOPMENT || DEBUG */
5546 error
= KERN_INVALID_ARGUMENT
;
5554 * task_info_from_user
5556 * When calling task_info from user space,
5557 * this function will be executed as mig server side
5558 * instead of calling directly into task_info.
5559 * This gives the possibility to perform more security
5560 * checks on task_port.
5562 * In the case of TASK_DYLD_INFO, we require the more
5563 * privileged task_port not the less-privileged task_name_port.
5567 task_info_from_user(
5568 mach_port_t task_port
,
5569 task_flavor_t flavor
,
5570 task_info_t task_info_out
,
5571 mach_msg_type_number_t
*task_info_count
)
5576 if (flavor
== TASK_DYLD_INFO
) {
5577 task
= convert_port_to_task(task_port
);
5579 task
= convert_port_to_task_name(task_port
);
5582 ret
= task_info(task
, flavor
, task_info_out
, task_info_count
);
5584 task_deallocate(task
);
5592 * Returns power stats for the task.
5593 * Note: Called with task locked.
5596 task_power_info_locked(
5598 task_power_info_t info
,
5599 gpu_energy_data_t ginfo
,
5600 task_power_info_v2_t infov2
,
5601 uint64_t *runnable_time
)
5604 ledger_amount_t tmp
;
5606 uint64_t runnable_time_sum
= 0;
5608 task_lock_assert_owned(task
);
5610 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
5611 (ledger_amount_t
*)&info
->task_interrupt_wakeups
, &tmp
);
5612 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
5613 (ledger_amount_t
*)&info
->task_platform_idle_wakeups
, &tmp
);
5615 info
->task_timer_wakeups_bin_1
= task
->task_timer_wakeups_bin_1
;
5616 info
->task_timer_wakeups_bin_2
= task
->task_timer_wakeups_bin_2
;
5618 info
->total_user
= task
->total_user_time
;
5619 info
->total_system
= task
->total_system_time
;
5620 runnable_time_sum
= task
->total_runnable_time
;
5622 #if defined(__arm__) || defined(__arm64__)
5624 infov2
->task_energy
= task
->task_energy
;
5626 #endif /* defined(__arm__) || defined(__arm64__) */
5629 ginfo
->task_gpu_utilisation
= task
->task_gpu_ns
;
5633 infov2
->task_ptime
= task
->total_ptime
;
5634 infov2
->task_pset_switches
= task
->ps_switch
;
5637 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5641 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
5646 thread_lock(thread
);
5648 info
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
5649 info
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
5651 #if defined(__arm__) || defined(__arm64__)
5653 infov2
->task_energy
+= ml_energy_stat(thread
);
5655 #endif /* defined(__arm__) || defined(__arm64__) */
5657 tval
= timer_grab(&thread
->user_timer
);
5658 info
->total_user
+= tval
;
5661 tval
= timer_grab(&thread
->ptime
);
5662 infov2
->task_ptime
+= tval
;
5663 infov2
->task_pset_switches
+= thread
->ps_switch
;
5666 tval
= timer_grab(&thread
->system_timer
);
5667 if (thread
->precise_user_kernel_time
) {
5668 info
->total_system
+= tval
;
5670 /* system_timer may represent either sys or user */
5671 info
->total_user
+= tval
;
5674 tval
= timer_grab(&thread
->runnable_timer
);
5676 runnable_time_sum
+= tval
;
5679 ginfo
->task_gpu_utilisation
+= ml_gpu_stat(thread
);
5681 thread_unlock(thread
);
5685 if (runnable_time
) {
5686 *runnable_time
= runnable_time_sum
;
5691 * task_gpu_utilisation
5693 * Returns the total gpu time used by the all the threads of the task
5694 * (both dead and alive)
5697 task_gpu_utilisation(
5700 uint64_t gpu_time
= 0;
5701 #if defined(__x86_64__)
5705 gpu_time
+= task
->task_gpu_ns
;
5707 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5710 thread_lock(thread
);
5711 gpu_time
+= ml_gpu_stat(thread
);
5712 thread_unlock(thread
);
5717 #else /* defined(__x86_64__) */
5718 /* silence compiler warning */
5720 #endif /* defined(__x86_64__) */
5727 * Returns the total energy used by the all the threads of the task
5728 * (both dead and alive)
5734 uint64_t energy
= 0;
5738 energy
+= task
->task_energy
;
5740 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5743 thread_lock(thread
);
5744 energy
+= ml_energy_stat(thread
);
5745 thread_unlock(thread
);
5759 uint64_t cpu_ptime
= 0;
5763 cpu_ptime
+= task
->total_ptime
;
5765 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5766 cpu_ptime
+= timer_grab(&thread
->ptime
);
5777 __unused task_t task
)
5782 #endif /* __AMP__ */
5784 /* This function updates the cpu time in the arrays for each
5785 * effective and requested QoS class
5788 task_update_cpu_time_qos_stats(
5790 uint64_t *eqos_stats
,
5791 uint64_t *rqos_stats
)
5793 if (!eqos_stats
&& !rqos_stats
) {
5799 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5800 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
5804 thread_update_qos_cpu_time(thread
);
5808 eqos_stats
[THREAD_QOS_DEFAULT
] += task
->cpu_time_eqos_stats
.cpu_time_qos_default
;
5809 eqos_stats
[THREAD_QOS_MAINTENANCE
] += task
->cpu_time_eqos_stats
.cpu_time_qos_maintenance
;
5810 eqos_stats
[THREAD_QOS_BACKGROUND
] += task
->cpu_time_eqos_stats
.cpu_time_qos_background
;
5811 eqos_stats
[THREAD_QOS_UTILITY
] += task
->cpu_time_eqos_stats
.cpu_time_qos_utility
;
5812 eqos_stats
[THREAD_QOS_LEGACY
] += task
->cpu_time_eqos_stats
.cpu_time_qos_legacy
;
5813 eqos_stats
[THREAD_QOS_USER_INITIATED
] += task
->cpu_time_eqos_stats
.cpu_time_qos_user_initiated
;
5814 eqos_stats
[THREAD_QOS_USER_INTERACTIVE
] += task
->cpu_time_eqos_stats
.cpu_time_qos_user_interactive
;
5818 rqos_stats
[THREAD_QOS_DEFAULT
] += task
->cpu_time_rqos_stats
.cpu_time_qos_default
;
5819 rqos_stats
[THREAD_QOS_MAINTENANCE
] += task
->cpu_time_rqos_stats
.cpu_time_qos_maintenance
;
5820 rqos_stats
[THREAD_QOS_BACKGROUND
] += task
->cpu_time_rqos_stats
.cpu_time_qos_background
;
5821 rqos_stats
[THREAD_QOS_UTILITY
] += task
->cpu_time_rqos_stats
.cpu_time_qos_utility
;
5822 rqos_stats
[THREAD_QOS_LEGACY
] += task
->cpu_time_rqos_stats
.cpu_time_qos_legacy
;
5823 rqos_stats
[THREAD_QOS_USER_INITIATED
] += task
->cpu_time_rqos_stats
.cpu_time_qos_user_initiated
;
5824 rqos_stats
[THREAD_QOS_USER_INTERACTIVE
] += task
->cpu_time_rqos_stats
.cpu_time_qos_user_interactive
;
5833 task_purgable_info_t
*stats
)
5835 if (task
== TASK_NULL
|| stats
== NULL
) {
5836 return KERN_INVALID_ARGUMENT
;
5838 /* Take task reference */
5839 task_reference(task
);
5840 vm_purgeable_stats((vm_purgeable_info_t
)stats
, task
);
5841 /* Drop task reference */
5842 task_deallocate(task
);
5843 return KERN_SUCCESS
;
5856 task
->vtimers
|= which
;
5859 case TASK_VTIMER_USER
:
5860 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5862 thread_lock(thread
);
5863 if (thread
->precise_user_kernel_time
) {
5864 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
5866 thread
->vtimer_user_save
= timer_grab(&thread
->system_timer
);
5868 thread_unlock(thread
);
5873 case TASK_VTIMER_PROF
:
5874 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5876 thread_lock(thread
);
5877 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
5878 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
5879 thread_unlock(thread
);
5884 case TASK_VTIMER_RLIM
:
5885 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
5887 thread_lock(thread
);
5888 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
5889 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
5890 thread_unlock(thread
);
5904 assert(task
== current_task());
5908 task
->vtimers
&= ~which
;
5918 uint32_t *microsecs
)
5920 thread_t thread
= current_thread();
5922 clock_sec_t secs
= 0;
5925 assert(task
== current_task());
5927 spl_t s
= splsched();
5928 thread_lock(thread
);
5930 if ((task
->vtimers
& which
) != (uint32_t)which
) {
5931 thread_unlock(thread
);
5937 case TASK_VTIMER_USER
:
5938 if (thread
->precise_user_kernel_time
) {
5939 tdelt
= (uint32_t)timer_delta(&thread
->user_timer
,
5940 &thread
->vtimer_user_save
);
5942 tdelt
= (uint32_t)timer_delta(&thread
->system_timer
,
5943 &thread
->vtimer_user_save
);
5945 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
5948 case TASK_VTIMER_PROF
:
5949 tsum
= timer_grab(&thread
->user_timer
);
5950 tsum
+= timer_grab(&thread
->system_timer
);
5951 tdelt
= (uint32_t)(tsum
- thread
->vtimer_prof_save
);
5952 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
5953 /* if the time delta is smaller than a usec, ignore */
5954 if (*microsecs
!= 0) {
5955 thread
->vtimer_prof_save
= tsum
;
5959 case TASK_VTIMER_RLIM
:
5960 tsum
= timer_grab(&thread
->user_timer
);
5961 tsum
+= timer_grab(&thread
->system_timer
);
5962 tdelt
= (uint32_t)(tsum
- thread
->vtimer_rlim_save
);
5963 thread
->vtimer_rlim_save
= tsum
;
5964 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
5968 thread_unlock(thread
);
5975 * Change the assigned processor set for the task
5979 __unused task_t task
,
5980 __unused processor_set_t new_pset
,
5981 __unused boolean_t assign_threads
)
5983 return KERN_FAILURE
;
5987 * task_assign_default:
5989 * Version of task_assign to assign to default processor set.
5992 task_assign_default(
5994 boolean_t assign_threads
)
5996 return task_assign(task
, &pset0
, assign_threads
);
6000 * task_get_assignment
6002 * Return name of processor set that task is assigned to.
6005 task_get_assignment(
6007 processor_set_t
*pset
)
6009 if (!task
|| !task
->active
) {
6010 return KERN_FAILURE
;
6015 return KERN_SUCCESS
;
6019 get_task_dispatchqueue_offset(
6022 return task
->dispatchqueue_offset
;
6028 * Set scheduling policy and parameters, both base and limit, for
6029 * the given task. Policy must be a policy which is enabled for the
6030 * processor set. Change contained threads if requested.
6034 __unused task_t task
,
6035 __unused policy_t policy_id
,
6036 __unused policy_base_t base
,
6037 __unused mach_msg_type_number_t count
,
6038 __unused boolean_t set_limit
,
6039 __unused boolean_t change
)
6041 return KERN_FAILURE
;
6047 * Set scheduling policy and parameters, both base and limit, for
6048 * the given task. Policy can be any policy implemented by the
6049 * processor set, whether enabled or not. Change contained threads
6054 __unused task_t task
,
6055 __unused processor_set_t pset
,
6056 __unused policy_t policy_id
,
6057 __unused policy_base_t base
,
6058 __unused mach_msg_type_number_t base_count
,
6059 __unused policy_limit_t limit
,
6060 __unused mach_msg_type_number_t limit_count
,
6061 __unused boolean_t change
)
6063 return KERN_FAILURE
;
6068 __unused task_t task
,
6069 __unused vm_offset_t pc
,
6070 __unused vm_offset_t endpc
)
6072 return KERN_FAILURE
;
6076 task_synchronizer_destroy_all(task_t task
)
6079 * Destroy owned semaphores
6081 semaphore_destroy_all(task
);
6085 * Install default (machine-dependent) initial thread state
6086 * on the task. Subsequent thread creation will have this initial
6087 * state set on the thread by machine_thread_inherit_taskwide().
6088 * Flavors and structures are exactly the same as those to thread_set_state()
6094 thread_state_t state
,
6095 mach_msg_type_number_t state_count
)
6099 if (task
== TASK_NULL
) {
6100 return KERN_INVALID_ARGUMENT
;
6105 if (!task
->active
) {
6107 return KERN_FAILURE
;
6110 ret
= machine_task_set_state(task
, flavor
, state
, state_count
);
6117 * Examine the default (machine-dependent) initial thread state
6118 * on the task, as set by task_set_state(). Flavors and structures
6119 * are exactly the same as those passed to thread_get_state().
6125 thread_state_t state
,
6126 mach_msg_type_number_t
*state_count
)
6130 if (task
== TASK_NULL
) {
6131 return KERN_INVALID_ARGUMENT
;
6136 if (!task
->active
) {
6138 return KERN_FAILURE
;
6141 ret
= machine_task_get_state(task
, flavor
, state
, state_count
);
6148 static kern_return_t
__attribute__((noinline
, not_tail_called
))
6149 PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(
6150 mach_exception_code_t code
,
6151 mach_exception_subcode_t subcode
,
6155 if (1 == proc_selfpid()) {
6156 return KERN_NOT_SUPPORTED
; // initproc is immune
6159 mach_exception_data_type_t codes
[EXCEPTION_CODE_MAX
] = {
6163 task_t task
= current_task();
6166 /* (See jetsam-related comments below) */
6168 proc_memstat_terminated(task
->bsd_info
, TRUE
);
6169 kr
= task_enqueue_exception_with_corpse(task
, EXC_GUARD
, codes
, 2, reason
);
6170 proc_memstat_terminated(task
->bsd_info
, FALSE
);
6175 task_violated_guard(
6176 mach_exception_code_t code
,
6177 mach_exception_subcode_t subcode
,
6180 return PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(code
, subcode
, reason
);
6184 #if CONFIG_MEMORYSTATUS
6187 task_get_memlimit_is_active(task_t task
)
6189 assert(task
!= NULL
);
6191 if (task
->memlimit_is_active
== 1) {
6199 task_set_memlimit_is_active(task_t task
, boolean_t memlimit_is_active
)
6201 assert(task
!= NULL
);
6203 if (memlimit_is_active
) {
6204 task
->memlimit_is_active
= 1;
6206 task
->memlimit_is_active
= 0;
6211 task_get_memlimit_is_fatal(task_t task
)
6213 assert(task
!= NULL
);
6215 if (task
->memlimit_is_fatal
== 1) {
6223 task_set_memlimit_is_fatal(task_t task
, boolean_t memlimit_is_fatal
)
6225 assert(task
!= NULL
);
6227 if (memlimit_is_fatal
) {
6228 task
->memlimit_is_fatal
= 1;
6230 task
->memlimit_is_fatal
= 0;
6235 task_has_triggered_exc_resource(task_t task
, boolean_t memlimit_is_active
)
6237 boolean_t triggered
= FALSE
;
6239 assert(task
== current_task());
6242 * Returns true, if task has already triggered an exc_resource exception.
6245 if (memlimit_is_active
) {
6246 triggered
= (task
->memlimit_active_exc_resource
? TRUE
: FALSE
);
6248 triggered
= (task
->memlimit_inactive_exc_resource
? TRUE
: FALSE
);
6255 task_mark_has_triggered_exc_resource(task_t task
, boolean_t memlimit_is_active
)
6257 assert(task
== current_task());
6260 * We allow one exc_resource per process per active/inactive limit.
6261 * The limit's fatal attribute does not come into play.
6264 if (memlimit_is_active
) {
6265 task
->memlimit_active_exc_resource
= 1;
6267 task
->memlimit_inactive_exc_resource
= 1;
6271 #define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
6273 void __attribute__((noinline
))
6274 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
)
6276 task_t task
= current_task();
6278 const char *procname
= "unknown";
6279 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
6280 boolean_t send_sync_exc_resource
= FALSE
;
6283 pid
= proc_selfpid();
6287 * Cannot have ReportCrash analyzing
6288 * a suspended initproc.
6293 if (task
->bsd_info
!= NULL
) {
6294 procname
= proc_name_address(current_task()->bsd_info
);
6295 send_sync_exc_resource
= proc_send_synchronous_EXC_RESOURCE(current_task()->bsd_info
);
6299 if (hwm_user_cores
) {
6301 uint64_t starttime
, end
;
6302 clock_sec_t secs
= 0;
6303 uint32_t microsecs
= 0;
6305 starttime
= mach_absolute_time();
6307 * Trigger a coredump of this process. Don't proceed unless we know we won't
6308 * be filling up the disk; and ignore the core size resource limit for this
6311 if ((error
= coredump(current_task()->bsd_info
, HWM_USERCORE_MINSPACE
, COREDUMP_IGNORE_ULIMIT
)) != 0) {
6312 printf("couldn't take coredump of %s[%d]: %d\n", procname
, pid
, error
);
6315 * coredump() leaves the task suspended.
6317 task_resume_internal(current_task());
6319 end
= mach_absolute_time();
6320 absolutetime_to_microtime(end
- starttime
, &secs
, µsecs
);
6321 printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
6322 proc_name_address(current_task()->bsd_info
), pid
, (int)secs
, microsecs
);
6324 #endif /* CONFIG_COREDUMP */
6326 if (disable_exc_resource
) {
6327 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
6328 "supressed by a boot-arg.\n", procname
, pid
, max_footprint_mb
);
6333 * A task that has triggered an EXC_RESOURCE, should not be
6334 * jetsammed when the device is under memory pressure. Here
6335 * we set the P_MEMSTAT_TERMINATED flag so that the process
6336 * will be skipped if the memorystatus_thread wakes up.
6338 proc_memstat_terminated(current_task()->bsd_info
, TRUE
);
6340 code
[0] = code
[1] = 0;
6341 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_MEMORY
);
6342 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_HIGH_WATERMARK
);
6343 EXC_RESOURCE_HWM_ENCODE_LIMIT(code
[0], max_footprint_mb
);
6346 * Do not generate a corpse fork if the violation is a fatal one
6347 * or the process wants synchronous EXC_RESOURCE exceptions.
6349 if (is_fatal
|| send_sync_exc_resource
|| exc_via_corpse_forking
== 0) {
6350 /* Do not send a EXC_RESOURCE if corpse_for_fatal_memkill is set */
6351 if (send_sync_exc_resource
|| corpse_for_fatal_memkill
== 0) {
6353 * Use the _internal_ variant so that no user-space
6354 * process can resume our task from under us.
6356 task_suspend_internal(task
);
6357 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
6358 task_resume_internal(task
);
6362 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
6363 "supressed due to audio playback.\n", procname
, pid
, max_footprint_mb
);
6365 task_enqueue_exception_with_corpse(task
, EXC_RESOURCE
,
6366 code
, EXCEPTION_CODE_MAX
, NULL
);
6371 * After the EXC_RESOURCE has been handled, we must clear the
6372 * P_MEMSTAT_TERMINATED flag so that the process can again be
6373 * considered for jetsam if the memorystatus_thread wakes up.
6375 proc_memstat_terminated(current_task()->bsd_info
, FALSE
); /* clear the flag */
6379 * Callback invoked when a task exceeds its physical footprint limit.
6382 task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
6384 ledger_amount_t max_footprint
, max_footprint_mb
;
6386 boolean_t is_warning
;
6387 boolean_t memlimit_is_active
;
6388 boolean_t memlimit_is_fatal
;
6390 if (warning
== LEDGER_WARNING_DIPPED_BELOW
) {
6392 * Task memory limits only provide a warning on the way up.
6395 } else if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
6397 * This task is in danger of violating a memory limit,
6398 * It has exceeded a percentage level of the limit.
6403 * The task has exceeded the physical footprint limit.
6404 * This is not a warning but a true limit violation.
6409 task
= current_task();
6411 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &max_footprint
);
6412 max_footprint_mb
= max_footprint
>> 20;
6414 memlimit_is_active
= task_get_memlimit_is_active(task
);
6415 memlimit_is_fatal
= task_get_memlimit_is_fatal(task
);
6418 * If this is an actual violation (not a warning), then generate EXC_RESOURCE exception.
6419 * We only generate the exception once per process per memlimit (active/inactive limit).
6420 * To enforce this, we monitor state based on the memlimit's active/inactive attribute
6421 * and we disable it by marking that memlimit as exception triggered.
6423 if ((is_warning
== FALSE
) && (!task_has_triggered_exc_resource(task
, memlimit_is_active
))) {
6424 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND((int)max_footprint_mb
, memlimit_is_fatal
);
6425 memorystatus_log_exception((int)max_footprint_mb
, memlimit_is_active
, memlimit_is_fatal
);
6426 task_mark_has_triggered_exc_resource(task
, memlimit_is_active
);
6429 memorystatus_on_ledger_footprint_exceeded(is_warning
, memlimit_is_active
, memlimit_is_fatal
);
6432 extern int proc_check_footprint_priv(void);
6435 task_set_phys_footprint_limit(
6440 kern_return_t error
;
6442 boolean_t memlimit_is_active
;
6443 boolean_t memlimit_is_fatal
;
6445 if ((error
= proc_check_footprint_priv())) {
6446 return KERN_NO_ACCESS
;
6450 * This call should probably be obsoleted.
6451 * But for now, we default to current state.
6453 memlimit_is_active
= task_get_memlimit_is_active(task
);
6454 memlimit_is_fatal
= task_get_memlimit_is_fatal(task
);
6456 return task_set_phys_footprint_limit_internal(task
, new_limit_mb
, old_limit_mb
, memlimit_is_active
, memlimit_is_fatal
);
6460 task_convert_phys_footprint_limit(
6462 int *converted_limit_mb
)
6464 if (limit_mb
== -1) {
6468 if (max_task_footprint
!= 0) {
6469 *converted_limit_mb
= (int)(max_task_footprint
/ 1024 / 1024); /* bytes to MB */
6471 *converted_limit_mb
= (int)(LEDGER_LIMIT_INFINITY
>> 20);
6474 /* nothing to convert */
6475 *converted_limit_mb
= limit_mb
;
6477 return KERN_SUCCESS
;
6482 task_set_phys_footprint_limit_internal(
6486 boolean_t memlimit_is_active
,
6487 boolean_t memlimit_is_fatal
)
6489 ledger_amount_t old
;
6492 ret
= ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &old
);
6494 if (ret
!= KERN_SUCCESS
) {
6499 * Check that limit >> 20 will not give an "unexpected" 32-bit
6500 * result. There are, however, implicit assumptions that -1 mb limit
6501 * equates to LEDGER_LIMIT_INFINITY.
6503 assert(((old
& 0xFFF0000000000000LL
) == 0) || (old
== LEDGER_LIMIT_INFINITY
));
6506 *old_limit_mb
= (int)(old
>> 20);
6509 if (new_limit_mb
== -1) {
6511 * Caller wishes to remove the limit.
6513 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
6514 max_task_footprint
? max_task_footprint
: LEDGER_LIMIT_INFINITY
,
6515 max_task_footprint
? (uint8_t)max_task_footprint_warning_level
: 0);
6518 task_set_memlimit_is_active(task
, memlimit_is_active
);
6519 task_set_memlimit_is_fatal(task
, memlimit_is_fatal
);
6522 return KERN_SUCCESS
;
6525 #ifdef CONFIG_NOMONITORS
6526 return KERN_SUCCESS
;
6527 #endif /* CONFIG_NOMONITORS */
6531 if ((memlimit_is_active
== task_get_memlimit_is_active(task
)) &&
6532 (memlimit_is_fatal
== task_get_memlimit_is_fatal(task
)) &&
6533 (((ledger_amount_t
)new_limit_mb
<< 20) == old
)) {
6535 * memlimit state is not changing
6538 return KERN_SUCCESS
;
6541 task_set_memlimit_is_active(task
, memlimit_is_active
);
6542 task_set_memlimit_is_fatal(task
, memlimit_is_fatal
);
6544 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
6545 (ledger_amount_t
)new_limit_mb
<< 20, PHYS_FOOTPRINT_WARNING_LEVEL
);
6547 if (task
== current_task()) {
6548 ledger_check_new_balance(current_thread(), task
->ledger
,
6549 task_ledgers
.phys_footprint
);
6554 return KERN_SUCCESS
;
6558 task_get_phys_footprint_limit(
6562 ledger_amount_t limit
;
6565 ret
= ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &limit
);
6566 if (ret
!= KERN_SUCCESS
) {
6571 * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
6572 * result. There are, however, implicit assumptions that -1 mb limit
6573 * equates to LEDGER_LIMIT_INFINITY.
6575 assert(((limit
& 0xFFF0000000000000LL
) == 0) || (limit
== LEDGER_LIMIT_INFINITY
));
6576 *limit_mb
= (int)(limit
>> 20);
6578 return KERN_SUCCESS
;
6580 #else /* CONFIG_MEMORYSTATUS */
6582 task_set_phys_footprint_limit(
6583 __unused task_t task
,
6584 __unused
int new_limit_mb
,
6585 __unused
int *old_limit_mb
)
6587 return KERN_FAILURE
;
6591 task_get_phys_footprint_limit(
6592 __unused task_t task
,
6593 __unused
int *limit_mb
)
6595 return KERN_FAILURE
;
6597 #endif /* CONFIG_MEMORYSTATUS */
6600 task_set_thread_limit(task_t task
, uint16_t thread_limit
)
6602 assert(task
!= kernel_task
);
6603 if (thread_limit
<= TASK_MAX_THREAD_LIMIT
) {
6605 task
->task_thread_limit
= thread_limit
;
6610 #if XNU_TARGET_OS_OSX
6612 task_has_system_version_compat_enabled(task_t task
)
6614 boolean_t enabled
= FALSE
;
6617 enabled
= (task
->t_flags
& TF_SYS_VERSION_COMPAT
);
6624 task_set_system_version_compat_enabled(task_t task
, boolean_t enable_system_version_compat
)
6626 assert(task
== current_task());
6627 assert(task
!= kernel_task
);
6630 if (enable_system_version_compat
) {
6631 task
->t_flags
|= TF_SYS_VERSION_COMPAT
;
6633 task
->t_flags
&= ~TF_SYS_VERSION_COMPAT
;
6637 #endif /* XNU_TARGET_OS_OSX */
6640 * We need to export some functions to other components that
6641 * are currently implemented in macros within the osfmk
6642 * component. Just export them as functions of the same name.
6645 is_kerneltask(task_t t
)
6647 if (t
== kernel_task
) {
6655 is_corpsetask(task_t t
)
6657 return task_is_a_corpse(t
);
6661 task_t
current_task(void);
6665 return current_task_fast();
6668 #undef task_reference
6669 void task_reference(task_t task
);
6674 if (task
!= TASK_NULL
) {
6675 task_reference_internal(task
);
6679 /* defined in bsd/kern/kern_prot.c */
6680 extern int get_audit_token_pid(audit_token_t
*audit_token
);
6683 task_pid(task_t task
)
6686 return get_audit_token_pid(&task
->audit_token
);
6691 #if __has_feature(ptrauth_calls)
6693 * Get the shared region id and jop signing key for the task.
6694 * The function will allocate a kalloc buffer and return
6695 * it to caller, the caller needs to free it. This is used
6696 * for getting the information via task port.
6699 task_get_vm_shared_region_id_and_jop_pid(task_t task
, uint64_t *jop_pid
)
6702 char *shared_region_id
= NULL
;
6705 if (task
->shared_region_id
== NULL
) {
6709 len
= strlen(task
->shared_region_id
) + 1;
6711 /* don't hold task lock while allocating */
6713 shared_region_id
= kheap_alloc(KHEAP_DATA_BUFFERS
, len
, Z_WAITOK
);
6716 if (task
->shared_region_id
== NULL
) {
6718 kheap_free(KHEAP_DATA_BUFFERS
, shared_region_id
, len
);
6721 assert(len
== strlen(task
->shared_region_id
) + 1); /* should never change */
6722 strlcpy(shared_region_id
, task
->shared_region_id
, len
);
6725 /* find key from its auth pager */
6726 if (jop_pid
!= NULL
) {
6727 *jop_pid
= shared_region_find_key(shared_region_id
);
6730 return shared_region_id
;
6734 * set the shared region id for a task
6737 task_set_shared_region_id(task_t task
, char *id
)
6742 old_id
= task
->shared_region_id
;
6743 task
->shared_region_id
= id
;
6744 task
->shared_region_auth_remapped
= FALSE
;
6747 /* free any pre-existing shared region id */
6748 if (old_id
!= NULL
) {
6749 shared_region_key_dealloc(old_id
);
6750 kheap_free(KHEAP_DATA_BUFFERS
, old_id
, strlen(old_id
) + 1);
6753 #endif /* __has_feature(ptrauth_calls) */
6756 * This routine finds a thread in a task by its unique id
6757 * Returns a referenced thread or THREAD_NULL if the thread was not found
6759 * TODO: This is super inefficient - it's an O(threads in task) list walk!
6760 * We should make a tid hash, or transition all tid clients to thread ports
6762 * Precondition: No locks held (will take task lock)
6765 task_findtid(task_t task
, uint64_t tid
)
6767 thread_t self
= current_thread();
6768 thread_t found_thread
= THREAD_NULL
;
6769 thread_t iter_thread
= THREAD_NULL
;
6771 /* Short-circuit the lookup if we're looking up ourselves */
6772 if (tid
== self
->thread_id
|| tid
== TID_NULL
) {
6773 assert(self
->task
== task
);
6775 thread_reference(self
);
6782 queue_iterate(&task
->threads
, iter_thread
, thread_t
, task_threads
) {
6783 if (iter_thread
->thread_id
== tid
) {
6784 found_thread
= iter_thread
;
6785 thread_reference(found_thread
);
6792 return found_thread
;
6796 pid_from_task(task_t task
)
6800 if (task
->bsd_info
) {
6801 pid
= proc_pid(task
->bsd_info
);
6803 pid
= task_pid(task
);
6810 * Control the CPU usage monitor for a task.
6813 task_cpu_usage_monitor_ctl(task_t task
, uint32_t *flags
)
6815 int error
= KERN_SUCCESS
;
6817 if (*flags
& CPUMON_MAKE_FATAL
) {
6818 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_CPUMON
;
6820 error
= KERN_INVALID_ARGUMENT
;
6827 * Control the wakeups monitor for a task.
6830 task_wakeups_monitor_ctl(task_t task
, uint32_t *flags
, int32_t *rate_hz
)
6832 ledger_t ledger
= task
->ledger
;
6835 if (*flags
& WAKEMON_GET_PARAMS
) {
6836 ledger_amount_t limit
;
6839 ledger_get_limit(ledger
, task_ledgers
.interrupt_wakeups
, &limit
);
6840 ledger_get_period(ledger
, task_ledgers
.interrupt_wakeups
, &period
);
6842 if (limit
!= LEDGER_LIMIT_INFINITY
) {
6844 * An active limit means the wakeups monitor is enabled.
6846 *rate_hz
= (int32_t)(limit
/ (int64_t)(period
/ NSEC_PER_SEC
));
6847 *flags
= WAKEMON_ENABLE
;
6848 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
6849 *flags
|= WAKEMON_MAKE_FATAL
;
6852 *flags
= WAKEMON_DISABLE
;
6857 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
6860 return KERN_SUCCESS
;
6863 if (*flags
& WAKEMON_ENABLE
) {
6864 if (*flags
& WAKEMON_SET_DEFAULTS
) {
6865 *rate_hz
= task_wakeups_monitor_rate
;
6868 #ifndef CONFIG_NOMONITORS
6869 if (*flags
& WAKEMON_MAKE_FATAL
) {
6870 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
6872 #endif /* CONFIG_NOMONITORS */
6874 if (*rate_hz
<= 0) {
6876 return KERN_INVALID_ARGUMENT
;
6879 #ifndef CONFIG_NOMONITORS
6880 ledger_set_limit(ledger
, task_ledgers
.interrupt_wakeups
, *rate_hz
* task_wakeups_monitor_interval
,
6881 (uint8_t)task_wakeups_monitor_ustackshots_trigger_pct
);
6882 ledger_set_period(ledger
, task_ledgers
.interrupt_wakeups
, task_wakeups_monitor_interval
* NSEC_PER_SEC
);
6883 ledger_enable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
6884 #endif /* CONFIG_NOMONITORS */
6885 } else if (*flags
& WAKEMON_DISABLE
) {
6887 * Caller wishes to disable wakeups monitor on the task.
6889 * Disable telemetry if it was triggered by the wakeups monitor, and
6890 * remove the limit & callback on the wakeups ledger entry.
6892 #if CONFIG_TELEMETRY
6893 telemetry_task_ctl_locked(task
, TF_WAKEMON_WARNING
, 0);
6895 ledger_disable_refill(ledger
, task_ledgers
.interrupt_wakeups
);
6896 ledger_disable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
6900 return KERN_SUCCESS
;
6904 task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
6906 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
6907 #if CONFIG_TELEMETRY
6909 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
6910 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
6912 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 1);
6917 #if CONFIG_TELEMETRY
6919 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
6920 * exceeded the limit, turn telemetry off for the task.
6922 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 0);
6926 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS();
6930 void __attribute__((noinline
))
6931 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
6933 task_t task
= current_task();
6935 const char *procname
= "unknown";
6938 #ifdef EXC_RESOURCE_MONITORS
6939 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
6940 #endif /* EXC_RESOURCE_MONITORS */
6941 struct ledger_entry_info lei
;
6944 pid
= proc_selfpid();
6945 if (task
->bsd_info
!= NULL
) {
6946 procname
= proc_name_address(current_task()->bsd_info
);
6950 ledger_get_entry_info(task
->ledger
, task_ledgers
.interrupt_wakeups
, &lei
);
6953 * Disable the exception notification so we don't overwhelm
6954 * the listener with an endless stream of redundant exceptions.
6955 * TODO: detect whether another thread is already reporting the violation.
6957 uint32_t flags
= WAKEMON_DISABLE
;
6958 task_wakeups_monitor_ctl(task
, &flags
, NULL
);
6960 fatal
= task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
6961 trace_resource_violation(RMON_CPUWAKES_VIOLATED
, &lei
);
6962 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught waking the CPU %llu times "
6963 "over ~%llu seconds, averaging %llu wakes / second and "
6964 "violating a %slimit of %llu wakes over %llu seconds.\n",
6966 lei
.lei_balance
, lei
.lei_last_refill
/ NSEC_PER_SEC
,
6967 lei
.lei_last_refill
== 0 ? 0 :
6968 (NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
),
6969 fatal
? "FATAL " : "",
6970 lei
.lei_limit
, lei
.lei_refill_period
/ NSEC_PER_SEC
);
6972 kr
= send_resource_violation(send_cpu_wakes_violation
, task
, &lei
,
6973 fatal
? kRNFatalLimitFlag
: 0);
6975 printf("send_resource_violation(CPU wakes, ...): error %#x\n", kr
);
6978 #ifdef EXC_RESOURCE_MONITORS
6979 if (disable_exc_resource
) {
6980 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
6981 "supressed by a boot-arg\n", procname
, pid
);
6985 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
6986 "supressed due to audio playback\n", procname
, pid
);
6989 if (lei
.lei_last_refill
== 0) {
6990 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
6991 "supressed due to lei.lei_last_refill = 0 \n", procname
, pid
);
6994 code
[0] = code
[1] = 0;
6995 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_WAKEUPS
);
6996 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_WAKEUPS_MONITOR
);
6997 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code
[0],
6998 NSEC_PER_SEC
* lei
.lei_limit
/ lei
.lei_refill_period
);
6999 EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code
[0],
7000 lei
.lei_last_refill
);
7001 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code
[1],
7002 NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
);
7003 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
7004 #endif /* EXC_RESOURCE_MONITORS */
7007 task_terminate_internal(task
);
7012 global_update_logical_writes(int64_t io_delta
, int64_t *global_write_count
)
7014 int64_t old_count
, new_count
;
7015 boolean_t needs_telemetry
;
7018 new_count
= old_count
= *global_write_count
;
7019 new_count
+= io_delta
;
7020 if (new_count
>= io_telemetry_limit
) {
7022 needs_telemetry
= TRUE
;
7024 needs_telemetry
= FALSE
;
7026 } while (!OSCompareAndSwap64(old_count
, new_count
, global_write_count
));
7027 return needs_telemetry
;
7031 task_update_physical_writes(__unused task_t task
, __unused task_physical_write_flavor_t flavor
, __unused
uint64_t io_size
, __unused task_balance_flags_t flags
)
7033 #if CONFIG_PHYS_WRITE_ACCT
7039 * task == NULL means that we have to update kernel_task ledgers
7045 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_PHYS_WRITE_ACCT
)) | DBG_FUNC_NONE
,
7046 task_pid(task
), flavor
, io_size
, flags
, 0);
7047 DTRACE_IO4(physical_writes
, struct task
*, task
, task_physical_write_flavor_t
, flavor
, uint64_t, io_size
, task_balance_flags_t
, flags
);
7049 if (flags
& TASK_BALANCE_CREDIT
) {
7050 if (flavor
== TASK_PHYSICAL_WRITE_METADATA
) {
7051 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_fs_metadata_writes
));
7052 ledger_credit_nocheck(task
->ledger
, task_ledgers
.fs_metadata_writes
, io_size
);
7054 } else if (flags
& TASK_BALANCE_DEBIT
) {
7055 if (flavor
== TASK_PHYSICAL_WRITE_METADATA
) {
7056 OSAddAtomic64(-1 * io_size
, (SInt64
*)&(task
->task_fs_metadata_writes
));
7057 ledger_debit_nocheck(task
->ledger
, task_ledgers
.fs_metadata_writes
, io_size
);
7060 #endif /* CONFIG_PHYS_WRITE_ACCT */
7064 task_update_logical_writes(task_t task
, uint32_t io_size
, int flags
, void *vp
)
7066 int64_t io_delta
= 0;
7067 int64_t * global_counter_to_update
;
7068 boolean_t needs_telemetry
= FALSE
;
7069 boolean_t is_external_device
= FALSE
;
7070 int ledger_to_update
= 0;
7071 struct task_writes_counters
* writes_counters_to_update
;
7073 if ((!task
) || (!io_size
) || (!vp
)) {
7077 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_DATA_WRITE
)) | DBG_FUNC_NONE
,
7078 task_pid(task
), io_size
, flags
, (uintptr_t)VM_KERNEL_ADDRPERM(vp
), 0);
7079 DTRACE_IO4(logical_writes
, struct task
*, task
, uint32_t, io_size
, int, flags
, vnode
*, vp
);
7081 // Is the drive backing this vnode internal or external to the system?
7082 if (vnode_isonexternalstorage(vp
) == false) {
7083 global_counter_to_update
= &global_logical_writes_count
;
7084 ledger_to_update
= task_ledgers
.logical_writes
;
7085 writes_counters_to_update
= &task
->task_writes_counters_internal
;
7086 is_external_device
= FALSE
;
7088 global_counter_to_update
= &global_logical_writes_to_external_count
;
7089 ledger_to_update
= task_ledgers
.logical_writes_to_external
;
7090 writes_counters_to_update
= &task
->task_writes_counters_external
;
7091 is_external_device
= TRUE
;
7095 case TASK_WRITE_IMMEDIATE
:
7096 OSAddAtomic64(io_size
, (SInt64
*)&(writes_counters_to_update
->task_immediate_writes
));
7097 ledger_credit(task
->ledger
, ledger_to_update
, io_size
);
7098 if (!is_external_device
) {
7099 coalition_io_ledger_update(task
, FLAVOR_IO_LOGICAL_WRITES
, TRUE
, io_size
);
7102 case TASK_WRITE_DEFERRED
:
7103 OSAddAtomic64(io_size
, (SInt64
*)&(writes_counters_to_update
->task_deferred_writes
));
7104 ledger_credit(task
->ledger
, ledger_to_update
, io_size
);
7105 if (!is_external_device
) {
7106 coalition_io_ledger_update(task
, FLAVOR_IO_LOGICAL_WRITES
, TRUE
, io_size
);
7109 case TASK_WRITE_INVALIDATED
:
7110 OSAddAtomic64(io_size
, (SInt64
*)&(writes_counters_to_update
->task_invalidated_writes
));
7111 ledger_debit(task
->ledger
, ledger_to_update
, io_size
);
7112 if (!is_external_device
) {
7113 coalition_io_ledger_update(task
, FLAVOR_IO_LOGICAL_WRITES
, FALSE
, io_size
);
7116 case TASK_WRITE_METADATA
:
7117 OSAddAtomic64(io_size
, (SInt64
*)&(writes_counters_to_update
->task_metadata_writes
));
7118 ledger_credit(task
->ledger
, ledger_to_update
, io_size
);
7119 if (!is_external_device
) {
7120 coalition_io_ledger_update(task
, FLAVOR_IO_LOGICAL_WRITES
, TRUE
, io_size
);
7125 io_delta
= (flags
== TASK_WRITE_INVALIDATED
) ? ((int64_t)io_size
* -1ll) : ((int64_t)io_size
);
7126 if (io_telemetry_limit
!= 0) {
7127 /* If io_telemetry_limit is 0, disable global updates and I/O telemetry */
7128 needs_telemetry
= global_update_logical_writes(io_delta
, global_counter_to_update
);
7129 if (needs_telemetry
&& !is_external_device
) {
7130 act_set_io_telemetry_ast(current_thread());
7136 * Control the I/O monitor for a task.
7139 task_io_monitor_ctl(task_t task
, uint32_t *flags
)
7141 ledger_t ledger
= task
->ledger
;
7144 if (*flags
& IOMON_ENABLE
) {
7145 /* Configure the physical I/O ledger */
7146 ledger_set_limit(ledger
, task_ledgers
.physical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
7147 ledger_set_period(ledger
, task_ledgers
.physical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
7148 } else if (*flags
& IOMON_DISABLE
) {
7150 * Caller wishes to disable I/O monitor on the task.
7152 ledger_disable_refill(ledger
, task_ledgers
.physical_writes
);
7153 ledger_disable_callback(ledger
, task_ledgers
.physical_writes
);
7157 return KERN_SUCCESS
;
7161 task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
)
7164 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO((int)param0
);
7168 void __attribute__((noinline
))
7169 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
)
7172 task_t task
= current_task();
7173 #ifdef EXC_RESOURCE_MONITORS
7174 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
7175 #endif /* EXC_RESOURCE_MONITORS */
7176 struct ledger_entry_info lei
;
7180 pid
= proc_selfpid();
7183 * Get the ledger entry info. We need to do this before disabling the exception
7184 * to get correct values for all fields.
7187 case FLAVOR_IO_PHYSICAL_WRITES
:
7188 ledger_get_entry_info(task
->ledger
, task_ledgers
.physical_writes
, &lei
);
7194 * Disable the exception notification so we don't overwhelm
7195 * the listener with an endless stream of redundant exceptions.
7196 * TODO: detect whether another thread is already reporting the violation.
7198 uint32_t flags
= IOMON_DISABLE
;
7199 task_io_monitor_ctl(task
, &flags
);
7201 if (flavor
== FLAVOR_IO_LOGICAL_WRITES
) {
7202 trace_resource_violation(RMON_LOGWRITES_VIOLATED
, &lei
);
7204 os_log(OS_LOG_DEFAULT
, "process [%d] caught causing excessive I/O (flavor: %d). Task I/O: %lld MB. [Limit : %lld MB per %lld secs]\n",
7205 pid
, flavor
, (lei
.lei_balance
/ (1024 * 1024)), (lei
.lei_limit
/ (1024 * 1024)), (lei
.lei_refill_period
/ NSEC_PER_SEC
));
7207 kr
= send_resource_violation(send_disk_writes_violation
, task
, &lei
, kRNFlagsNone
);
7209 printf("send_resource_violation(disk_writes, ...): error %#x\n", kr
);
7212 #ifdef EXC_RESOURCE_MONITORS
7213 code
[0] = code
[1] = 0;
7214 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_IO
);
7215 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], flavor
);
7216 EXC_RESOURCE_IO_ENCODE_INTERVAL(code
[0], (lei
.lei_refill_period
/ NSEC_PER_SEC
));
7217 EXC_RESOURCE_IO_ENCODE_LIMIT(code
[0], (lei
.lei_limit
/ (1024 * 1024)));
7218 EXC_RESOURCE_IO_ENCODE_OBSERVED(code
[1], (lei
.lei_balance
/ (1024 * 1024)));
7219 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
7220 #endif /* EXC_RESOURCE_MONITORS */
7223 /* Placeholders for the task set/get voucher interfaces */
7225 task_get_mach_voucher(
7227 mach_voucher_selector_t __unused which
,
7228 ipc_voucher_t
*voucher
)
7230 if (TASK_NULL
== task
) {
7231 return KERN_INVALID_TASK
;
7235 return KERN_SUCCESS
;
7239 task_set_mach_voucher(
7241 ipc_voucher_t __unused voucher
)
7243 if (TASK_NULL
== task
) {
7244 return KERN_INVALID_TASK
;
7247 return KERN_SUCCESS
;
7251 task_swap_mach_voucher(
7252 __unused task_t task
,
7253 __unused ipc_voucher_t new_voucher
,
7254 ipc_voucher_t
*in_out_old_voucher
)
7257 * Currently this function is only called from a MIG generated
7258 * routine which doesn't release the reference on the voucher
7259 * addressed by in_out_old_voucher. To avoid leaking this reference,
7260 * a call to release it has been added here.
7262 ipc_voucher_release(*in_out_old_voucher
);
7263 return KERN_NOT_SUPPORTED
;
7267 task_set_gpu_denied(task_t task
, boolean_t denied
)
7272 task
->t_flags
|= TF_GPU_DENIED
;
7274 task
->t_flags
&= ~TF_GPU_DENIED
;
7281 task_is_gpu_denied(task_t task
)
7283 /* We don't need the lock to read this flag */
7284 return (task
->t_flags
& TF_GPU_DENIED
) ? TRUE
: FALSE
;
7289 get_task_memory_region_count(task_t task
)
7292 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
7293 return (uint64_t)get_map_nentries(map
);
7297 kdebug_trace_dyld_internal(uint32_t base_code
,
7298 struct dyld_kernel_image_info
*info
)
7300 static_assert(sizeof(info
->uuid
) >= 16);
7302 #if defined(__LP64__)
7303 uint64_t *uuid
= (uint64_t *)&(info
->uuid
);
7305 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
7306 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
), uuid
[0],
7307 uuid
[1], info
->load_addr
,
7308 (uint64_t)info
->fsid
.val
[0] | ((uint64_t)info
->fsid
.val
[1] << 32),
7310 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
7311 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 1),
7312 (uint64_t)info
->fsobjid
.fid_objno
|
7313 ((uint64_t)info
->fsobjid
.fid_generation
<< 32),
7315 #else /* defined(__LP64__) */
7316 uint32_t *uuid
= (uint32_t *)&(info
->uuid
);
7318 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
7319 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 2), uuid
[0],
7320 uuid
[1], uuid
[2], uuid
[3], 0);
7321 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
7322 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 3),
7323 (uint32_t)info
->load_addr
, info
->fsid
.val
[0], info
->fsid
.val
[1],
7324 info
->fsobjid
.fid_objno
, 0);
7325 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
7326 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 4),
7327 info
->fsobjid
.fid_generation
, 0, 0, 0, 0);
7328 #endif /* !defined(__LP64__) */
7331 static kern_return_t
7332 kdebug_trace_dyld(task_t task
, uint32_t base_code
,
7333 vm_map_copy_t infos_copy
, mach_msg_type_number_t infos_len
)
7336 dyld_kernel_image_info_array_t infos
;
7337 vm_map_offset_t map_data
;
7341 return KERN_INVALID_ADDRESS
;
7344 if (!kdebug_enable
||
7345 !kdebug_debugid_enabled(KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, 0))) {
7346 vm_map_copy_discard(infos_copy
);
7347 return KERN_SUCCESS
;
7350 if (task
== NULL
|| task
!= current_task()) {
7351 return KERN_INVALID_TASK
;
7354 kr
= vm_map_copyout(ipc_kernel_map
, &map_data
, (vm_map_copy_t
)infos_copy
);
7355 if (kr
!= KERN_SUCCESS
) {
7359 infos
= CAST_DOWN(dyld_kernel_image_info_array_t
, map_data
);
7361 for (mach_msg_type_number_t i
= 0; i
< infos_len
; i
++) {
7362 kdebug_trace_dyld_internal(base_code
, &(infos
[i
]));
7365 data
= CAST_DOWN(vm_offset_t
, map_data
);
7366 mach_vm_deallocate(ipc_kernel_map
, data
, infos_len
* sizeof(infos
[0]));
7367 return KERN_SUCCESS
;
7371 task_register_dyld_image_infos(task_t task
,
7372 dyld_kernel_image_info_array_t infos_copy
,
7373 mach_msg_type_number_t infos_len
)
7375 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_MAP_A
,
7376 (vm_map_copy_t
)infos_copy
, infos_len
);
7380 task_unregister_dyld_image_infos(task_t task
,
7381 dyld_kernel_image_info_array_t infos_copy
,
7382 mach_msg_type_number_t infos_len
)
7384 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_UNMAP_A
,
7385 (vm_map_copy_t
)infos_copy
, infos_len
);
7389 task_get_dyld_image_infos(__unused task_t task
,
7390 __unused dyld_kernel_image_info_array_t
* dyld_images
,
7391 __unused mach_msg_type_number_t
* dyld_imagesCnt
)
7393 return KERN_NOT_SUPPORTED
;
7397 task_register_dyld_shared_cache_image_info(task_t task
,
7398 dyld_kernel_image_info_t cache_img
,
7399 __unused boolean_t no_cache
,
7400 __unused boolean_t private_cache
)
7402 if (task
== NULL
|| task
!= current_task()) {
7403 return KERN_INVALID_TASK
;
7406 kdebug_trace_dyld_internal(DBG_DYLD_UUID_SHARED_CACHE_A
, &cache_img
);
7407 return KERN_SUCCESS
;
7411 task_register_dyld_set_dyld_state(__unused task_t task
,
7412 __unused
uint8_t dyld_state
)
7414 return KERN_NOT_SUPPORTED
;
7418 task_register_dyld_get_process_state(__unused task_t task
,
7419 __unused dyld_kernel_process_info_t
* dyld_process_state
)
7421 return KERN_NOT_SUPPORTED
;
7425 task_inspect(task_inspect_t task_insp
, task_inspect_flavor_t flavor
,
7426 task_inspect_info_t info_out
, mach_msg_type_number_t
*size_in_out
)
7429 task_t task
= (task_t
)task_insp
;
7430 kern_return_t kr
= KERN_SUCCESS
;
7431 mach_msg_type_number_t size
;
7433 if (task
== TASK_NULL
) {
7434 return KERN_INVALID_ARGUMENT
;
7437 size
= *size_in_out
;
7440 case TASK_INSPECT_BASIC_COUNTS
: {
7441 struct task_inspect_basic_counts
*bc
;
7442 uint64_t task_counts
[MT_CORE_NFIXED
] = { 0 };
7444 if (size
< TASK_INSPECT_BASIC_COUNTS_COUNT
) {
7445 kr
= KERN_INVALID_ARGUMENT
;
7449 mt_fixed_task_counts(task
, task_counts
);
7450 bc
= (struct task_inspect_basic_counts
*)info_out
;
7451 #ifdef MT_CORE_INSTRS
7452 bc
->instructions
= task_counts
[MT_CORE_INSTRS
];
7453 #else /* defined(MT_CORE_INSTRS) */
7454 bc
->instructions
= 0;
7455 #endif /* !defined(MT_CORE_INSTRS) */
7456 bc
->cycles
= task_counts
[MT_CORE_CYCLES
];
7457 size
= TASK_INSPECT_BASIC_COUNTS_COUNT
;
7461 kr
= KERN_INVALID_ARGUMENT
;
7465 if (kr
== KERN_SUCCESS
) {
7466 *size_in_out
= size
;
7469 #else /* MONOTONIC */
7470 #pragma unused(task_insp, flavor, info_out, size_in_out)
7471 return KERN_NOT_SUPPORTED
;
7472 #endif /* !MONOTONIC */
7475 #if CONFIG_SECLUDED_MEMORY
7476 int num_tasks_can_use_secluded_mem
= 0;
7479 task_set_can_use_secluded_mem(
7481 boolean_t can_use_secluded_mem
)
7483 if (!task
->task_could_use_secluded_mem
) {
7487 task_set_can_use_secluded_mem_locked(task
, can_use_secluded_mem
);
7492 task_set_can_use_secluded_mem_locked(
7494 boolean_t can_use_secluded_mem
)
7496 assert(task
->task_could_use_secluded_mem
);
7497 if (can_use_secluded_mem
&&
7498 secluded_for_apps
&& /* global boot-arg */
7499 !task
->task_can_use_secluded_mem
) {
7500 assert(num_tasks_can_use_secluded_mem
>= 0);
7502 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
7503 task
->task_can_use_secluded_mem
= TRUE
;
7504 } else if (!can_use_secluded_mem
&&
7505 task
->task_can_use_secluded_mem
) {
7506 assert(num_tasks_can_use_secluded_mem
> 0);
7508 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
7509 task
->task_can_use_secluded_mem
= FALSE
;
7514 task_set_could_use_secluded_mem(
7516 boolean_t could_use_secluded_mem
)
7518 task
->task_could_use_secluded_mem
= !!could_use_secluded_mem
;
7522 task_set_could_also_use_secluded_mem(
7524 boolean_t could_also_use_secluded_mem
)
7526 task
->task_could_also_use_secluded_mem
= !!could_also_use_secluded_mem
;
7530 task_can_use_secluded_mem(
7534 if (task
->task_can_use_secluded_mem
) {
7535 assert(task
->task_could_use_secluded_mem
);
7536 assert(num_tasks_can_use_secluded_mem
> 0);
7539 if (task
->task_could_also_use_secluded_mem
&&
7540 num_tasks_can_use_secluded_mem
> 0) {
7541 assert(num_tasks_can_use_secluded_mem
> 0);
7546 * If a single task is using more than some large amount of
7547 * memory (i.e. secluded_shutoff_trigger) and is approaching
7548 * its task limit, allow it to dip into secluded and begin
7549 * suppression of rebuilding secluded memory until that task exits.
7551 if (is_alloc
&& secluded_shutoff_trigger
!= 0) {
7552 uint64_t phys_used
= get_task_phys_footprint(task
);
7553 uint64_t limit
= get_task_phys_footprint_limit(task
);
7554 if (phys_used
> secluded_shutoff_trigger
&&
7555 limit
> secluded_shutoff_trigger
&&
7556 phys_used
> limit
- secluded_shutoff_headroom
) {
7557 start_secluded_suppression(task
);
7566 task_could_use_secluded_mem(
7569 return task
->task_could_use_secluded_mem
;
7573 task_could_also_use_secluded_mem(
7576 return task
->task_could_also_use_secluded_mem
;
7578 #endif /* CONFIG_SECLUDED_MEMORY */
7581 task_io_user_clients(task_t task
)
7583 return &task
->io_user_clients
;
7587 task_set_message_app_suspended(task_t task
, boolean_t enable
)
7589 task
->message_app_suspended
= enable
;
7593 task_copy_fields_for_exec(task_t dst_task
, task_t src_task
)
7595 dst_task
->vtimers
= src_task
->vtimers
;
7598 #if DEVELOPMENT || DEBUG
7599 int vm_region_footprint
= 0;
7600 #endif /* DEVELOPMENT || DEBUG */
7603 task_self_region_footprint(void)
7605 #if DEVELOPMENT || DEBUG
7606 if (vm_region_footprint
) {
7607 /* system-wide override */
7610 #endif /* DEVELOPMENT || DEBUG */
7611 return current_task()->task_region_footprint
;
7615 task_self_region_footprint_set(
7620 curtask
= current_task();
7623 curtask
->task_region_footprint
= TRUE
;
7625 curtask
->task_region_footprint
= FALSE
;
7627 task_unlock(curtask
);
7631 task_set_darkwake_mode(task_t task
, boolean_t set_mode
)
7638 task
->t_flags
|= TF_DARKWAKE_MODE
;
7640 task
->t_flags
&= ~(TF_DARKWAKE_MODE
);
7647 task_get_darkwake_mode(task_t task
)
7650 return (task
->t_flags
& TF_DARKWAKE_MODE
) != 0;
7654 task_get_exc_guard_behavior(
7656 task_exc_guard_behavior_t
*behaviorp
)
7658 if (task
== TASK_NULL
) {
7659 return KERN_INVALID_TASK
;
7661 *behaviorp
= task
->task_exc_guard
;
7662 return KERN_SUCCESS
;
7665 #ifndef TASK_EXC_GUARD_ALL
7666 /* Temporary define until two branches are merged */
7667 #define TASK_EXC_GUARD_ALL (TASK_EXC_GUARD_VM_ALL | 0xf0)
7671 task_set_exc_guard_behavior(
7673 task_exc_guard_behavior_t behavior
)
7675 if (task
== TASK_NULL
) {
7676 return KERN_INVALID_TASK
;
7678 if (behavior
& ~TASK_EXC_GUARD_ALL
) {
7679 return KERN_INVALID_VALUE
;
7681 task
->task_exc_guard
= behavior
;
7682 return KERN_SUCCESS
;
7686 extern int legacy_footprint_entitlement_mode
;
7687 extern void memorystatus_act_on_legacy_footprint_entitlement(struct proc
*, boolean_t
);
7688 extern void memorystatus_act_on_ios13extended_footprint_entitlement(struct proc
*);
7692 task_set_legacy_footprint(
7696 task
->task_legacy_footprint
= TRUE
;
7701 task_set_extra_footprint_limit(
7704 if (task
->task_extra_footprint_limit
) {
7708 if (task
->task_extra_footprint_limit
) {
7712 task
->task_extra_footprint_limit
= TRUE
;
7714 memorystatus_act_on_legacy_footprint_entitlement(task
->bsd_info
, TRUE
);
7718 task_set_ios13extended_footprint_limit(
7721 if (task
->task_ios13extended_footprint_limit
) {
7725 if (task
->task_ios13extended_footprint_limit
) {
7729 task
->task_ios13extended_footprint_limit
= TRUE
;
7731 memorystatus_act_on_ios13extended_footprint_entitlement(task
->bsd_info
);
7733 #endif /* __arm64__ */
7735 static inline ledger_amount_t
7736 task_ledger_get_balance(
7740 ledger_amount_t amount
;
7742 ledger_get_balance(ledger
, ledger_idx
, &amount
);
7747 * Gather the amount of memory counted in a task's footprint due to
7748 * being in a specific set of ledgers.
7751 task_ledgers_footprint(
7753 ledger_amount_t
*ledger_resident
,
7754 ledger_amount_t
*ledger_compressed
)
7756 *ledger_resident
= 0;
7757 *ledger_compressed
= 0;
7759 /* purgeable non-volatile memory */
7760 *ledger_resident
+= task_ledger_get_balance(ledger
, task_ledgers
.purgeable_nonvolatile
);
7761 *ledger_compressed
+= task_ledger_get_balance(ledger
, task_ledgers
.purgeable_nonvolatile_compressed
);
7763 /* "default" tagged memory */
7764 *ledger_resident
+= task_ledger_get_balance(ledger
, task_ledgers
.tagged_footprint
);
7765 *ledger_compressed
+= task_ledger_get_balance(ledger
, task_ledgers
.tagged_footprint_compressed
);
7767 /* "network" currently never counts in the footprint... */
7769 /* "media" tagged memory */
7770 *ledger_resident
+= task_ledger_get_balance(ledger
, task_ledgers
.media_footprint
);
7771 *ledger_compressed
+= task_ledger_get_balance(ledger
, task_ledgers
.media_footprint_compressed
);
7773 /* "graphics" tagged memory */
7774 *ledger_resident
+= task_ledger_get_balance(ledger
, task_ledgers
.graphics_footprint
);
7775 *ledger_compressed
+= task_ledger_get_balance(ledger
, task_ledgers
.graphics_footprint_compressed
);
7777 /* "neural" tagged memory */
7778 *ledger_resident
+= task_ledger_get_balance(ledger
, task_ledgers
.neural_footprint
);
7779 *ledger_compressed
+= task_ledger_get_balance(ledger
, task_ledgers
.neural_footprint_compressed
);
7783 task_set_memory_ownership_transfer(
7788 task
->task_can_transfer_memory_ownership
= !!value
;
7793 task_copy_vmobjects(task_t task
, vm_object_query_t query
, size_t len
, size_t *num
)
7795 vm_object_t find_vmo
;
7798 task_objq_lock(task
);
7799 if (query
!= NULL
) {
7800 queue_iterate(&task
->task_objq
, find_vmo
, vm_object_t
, task_objq
)
7802 vm_object_query_t p
= &query
[size
++];
7804 /* make sure to not overrun */
7805 if (size
* sizeof(vm_object_query_data_t
) > len
) {
7810 bzero(p
, sizeof(*p
));
7811 p
->object_id
= (vm_object_id_t
) VM_KERNEL_ADDRPERM(find_vmo
);
7812 p
->virtual_size
= find_vmo
->internal
? find_vmo
->vo_size
: 0;
7813 p
->resident_size
= find_vmo
->resident_page_count
* PAGE_SIZE
;
7814 p
->wired_size
= find_vmo
->wired_page_count
* PAGE_SIZE
;
7815 p
->reusable_size
= find_vmo
->reusable_page_count
* PAGE_SIZE
;
7816 p
->vo_no_footprint
= find_vmo
->vo_no_footprint
;
7817 p
->vo_ledger_tag
= find_vmo
->vo_ledger_tag
;
7818 p
->purgable
= find_vmo
->purgable
;
7820 if (find_vmo
->internal
&& find_vmo
->pager_created
&& find_vmo
->pager
!= NULL
) {
7821 p
->compressed_size
= vm_compressor_pager_get_count(find_vmo
->pager
) * PAGE_SIZE
;
7823 p
->compressed_size
= 0;
7827 size
= (size_t)task
->task_owned_objects
;
7829 task_objq_unlock(task
);
7835 task_set_filter_msg_flag(
7839 assert(task
!= TASK_NULL
);
7843 task
->t_flags
|= TF_FILTER_MSG
;
7845 task
->t_flags
&= ~TF_FILTER_MSG
;
7851 task_get_filter_msg_flag(
7860 flags
= os_atomic_load(&task
->t_flags
, relaxed
);
7861 return (flags
& TF_FILTER_MSG
) ? TRUE
: FALSE
;
7867 if (task
== TASK_NULL
) {
7870 return vm_map_is_exotic(get_task_map(task
));
7877 if (task
== TASK_NULL
) {
7880 return vm_map_is_alien(get_task_map(task
));
7886 /* Set the filter mask for Mach traps. */
7888 mac_task_set_mach_filter_mask(task_t task
, uint8_t *maskptr
)
7892 task
->mach_trap_filter_mask
= maskptr
;
7895 /* Set the filter mask for kobject msgs. */
7897 mac_task_set_kobj_filter_mask(task_t task
, uint8_t *maskptr
)
7901 task
->mach_kobj_filter_mask
= maskptr
;
7904 /* Hook for mach trap/sc filter evaluation policy. */
7905 mac_task_mach_filter_cbfunc_t mac_task_mach_trap_evaluate
= NULL
;
7907 /* Hook for kobj message filter evaluation policy. */
7908 mac_task_kobj_filter_cbfunc_t mac_task_kobj_msg_evaluate
= NULL
;
7910 /* Set the callback hooks for the filtering policy. */
7912 mac_task_register_filter_callbacks(
7913 const mac_task_mach_filter_cbfunc_t mach_cbfunc
,
7914 const mac_task_kobj_filter_cbfunc_t kobj_cbfunc
)
7916 if (mach_cbfunc
!= NULL
) {
7917 if (mac_task_mach_trap_evaluate
!= NULL
) {
7918 return KERN_FAILURE
;
7920 mac_task_mach_trap_evaluate
= mach_cbfunc
;
7922 if (kobj_cbfunc
!= NULL
) {
7923 if (mac_task_kobj_msg_evaluate
!= NULL
) {
7924 return KERN_FAILURE
;
7926 mac_task_kobj_msg_evaluate
= kobj_cbfunc
;
7929 return KERN_SUCCESS
;
7931 #endif /* CONFIG_MACF */
7934 task_transfer_mach_filter_bits(
7939 /* Copy mach trap and kernel object mask pointers to new task. */
7940 new_task
->mach_trap_filter_mask
= old_task
->mach_trap_filter_mask
;
7941 new_task
->mach_kobj_filter_mask
= old_task
->mach_kobj_filter_mask
;
7943 /* If filter message flag is set then set it in the new task. */
7944 if (task_get_filter_msg_flag(old_task
)) {
7945 new_task
->t_flags
|= TF_FILTER_MSG
;
7950 #if __has_feature(ptrauth_calls)
7952 #define PAC_EXCEPTION_ENTITLEMENT "com.apple.private.pac.exception"
7955 task_set_pac_exception_fatal_flag(
7958 assert(task
!= TASK_NULL
);
7960 if (!IOTaskHasEntitlement(task
, PAC_EXCEPTION_ENTITLEMENT
)) {
7965 task
->t_flags
|= TF_PAC_EXC_FATAL
;
7970 task_is_pac_exception_fatal(
7975 assert(task
!= TASK_NULL
);
7977 flags
= os_atomic_load(&task
->t_flags
, relaxed
);
7978 return (bool)(flags
& TF_PAC_EXC_FATAL
);
7980 #endif /* __has_feature(ptrauth_calls) */
7983 task_set_tecs(task_t task
)
7985 if (task
== TASK_NULL
) {
7986 task
= current_task();
7989 if (!machine_csv(CPUVN_CI
)) {
7993 LCK_MTX_ASSERT(&task
->lock
, LCK_MTX_ASSERT_NOTOWNED
);
7997 task
->t_flags
|= TF_TECS
;
8000 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
8001 machine_tecs(thread
);