2 * Copyright (c) 2000-2016 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>
129 #include <corpses/task_corpse.h>
131 #include <kern/telemetry.h>
135 #include <kern/monotonic.h>
136 #include <machine/monotonic.h>
137 #endif /* MONOTONIC */
142 #include <vm/vm_map.h>
143 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
144 #include <vm/vm_pageout.h>
145 #include <vm/vm_protos.h>
146 #include <vm/vm_purgeable_internal.h>
148 #include <sys/resource.h>
149 #include <sys/signalvar.h> /* for coredump */
152 * Exported interfaces
155 #include <mach/task_server.h>
156 #include <mach/mach_host_server.h>
157 #include <mach/host_security_server.h>
158 #include <mach/mach_port_server.h>
160 #include <vm/vm_shared_region.h>
162 #include <libkern/OSDebug.h>
163 #include <libkern/OSAtomic.h>
166 #include <atm/atm_internal.h>
169 #include <kern/sfi.h> /* picks up ledger.h */
172 #include <security/mac_mach_internal.h>
176 extern int kpc_force_all_ctrs(task_t
, int);
181 lck_attr_t task_lck_attr
;
182 lck_grp_t task_lck_grp
;
183 lck_grp_attr_t task_lck_grp_attr
;
185 extern int exc_via_corpse_forking
;
186 extern int corpse_for_fatal_memkill
;
188 /* Flag set by core audio when audio is playing. Used to stifle EXC_RESOURCE generation when active. */
189 int audio_active
= 0;
191 zinfo_usage_store_t tasks_tkm_private
;
192 zinfo_usage_store_t tasks_tkm_shared
;
194 /* A container to accumulate statistics for expired tasks */
195 expired_task_statistics_t dead_task_statistics
;
196 lck_spin_t dead_task_statistics_lock
;
198 ledger_template_t task_ledger_template
= NULL
;
200 struct _task_ledger_indices task_ledgers
__attribute__((used
)) =
201 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
203 { 0 /* initialized at runtime */},
204 #endif /* !CONFIG_EMBEDDED */
210 /* System sleep state */
211 boolean_t tasks_suspend_state
;
214 void init_task_ledgers(void);
215 void task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
216 void task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
217 void task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
);
218 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void);
219 void __attribute__((noinline
)) PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
);
220 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
);
222 kern_return_t
task_suspend_internal(task_t
);
223 kern_return_t
task_resume_internal(task_t
);
224 static kern_return_t
task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
);
226 extern kern_return_t
iokit_task_terminate(task_t task
);
228 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
*);
229 extern void bsd_copythreadname(void *dst_uth
, void *src_uth
);
230 extern kern_return_t
thread_resume(thread_t thread
);
232 // Warn tasks when they hit 80% of their memory limit.
233 #define PHYS_FOOTPRINT_WARNING_LEVEL 80
235 #define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT 150 /* wakeups per second */
236 #define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL 300 /* in seconds. */
239 * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
241 * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
242 * stacktraces, aka micro-stackshots)
244 #define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER 70
246 int task_wakeups_monitor_interval
; /* In seconds. Time period over which wakeups rate is observed */
247 int task_wakeups_monitor_rate
; /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
249 int task_wakeups_monitor_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
251 int disable_exc_resource
; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
253 ledger_amount_t max_task_footprint
= 0; /* Per-task limit on physical memory consumption in bytes */
254 int max_task_footprint_warning_level
= 0; /* Per-task limit warning percentage */
255 int max_task_footprint_mb
= 0; /* Per-task limit on physical memory consumption in megabytes */
257 /* I/O Monitor Limits */
258 #define IOMON_DEFAULT_LIMIT (20480ull) /* MB of logical/physical I/O */
259 #define IOMON_DEFAULT_INTERVAL (86400ull) /* in seconds */
261 uint64_t task_iomon_limit_mb
; /* Per-task I/O monitor limit in MBs */
262 uint64_t task_iomon_interval_secs
; /* Per-task I/O monitor interval in secs */
264 #define IO_TELEMETRY_DEFAULT_LIMIT (10ll * 1024ll * 1024ll)
265 int64_t io_telemetry_limit
; /* Threshold to take a microstackshot (0 indicated I/O telemetry is turned off) */
266 int64_t global_logical_writes_count
= 0; /* Global count for logical writes */
267 static boolean_t
global_update_logical_writes(int64_t);
270 int pmap_ledgers_panic
= 1;
271 #endif /* MACH_ASSERT */
273 int task_max
= CONFIG_TASK_MAX
; /* Max number of tasks */
276 int hwm_user_cores
= 0; /* high watermark violations generate user core files */
280 extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
281 extern int proc_pid(struct proc
*p
);
282 extern int proc_selfpid(void);
283 extern char *proc_name_address(struct proc
*p
);
284 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
285 extern int kevent_proc_copy_uptrs(void *proc
, uint64_t *buf
, int bufsize
);
287 #if CONFIG_MEMORYSTATUS
288 extern void proc_memstat_terminated(struct proc
* p
, boolean_t set
);
289 extern void memorystatus_on_ledger_footprint_exceeded(int warning
, boolean_t memlimit_is_active
, boolean_t memlimit_is_fatal
);
290 extern void memorystatus_log_exception(const int max_footprint_mb
, boolean_t memlimit_is_active
, boolean_t memlimit_is_fatal
);
291 extern boolean_t
memorystatus_allowed_vm_map_fork(task_t task
);
293 #if DEVELOPMENT || DEBUG
294 extern void memorystatus_abort_vm_map_fork(task_t
);
297 #endif /* CONFIG_MEMORYSTATUS */
299 #endif /* MACH_BSD */
303 static void task_hold_locked(task_t task
);
304 static void task_wait_locked(task_t task
, boolean_t until_not_runnable
);
305 static void task_release_locked(task_t task
);
307 static void task_synchronizer_destroy_all(task_t task
);
310 task_backing_store_privileged(
314 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
325 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
327 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
332 if (task_has_64BitAddr(task
))
334 task_set_64BitAddr(task
);
336 if ( !task_has_64BitAddr(task
))
338 task_clear_64BitAddr(task
);
340 /* FIXME: On x86, the thread save state flavor can diverge from the
341 * task's 64-bit feature flag due to the 32-bit/64-bit register save
342 * state dichotomy. Since we can be pre-empted in this interval,
343 * certain routines may observe the thread as being in an inconsistent
344 * state with respect to its task's 64-bitness.
347 #if defined(__x86_64__) || defined(__arm64__)
348 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
349 thread_mtx_lock(thread
);
350 machine_thread_switch_addrmode(thread
);
351 thread_mtx_unlock(thread
);
353 #if defined(__arm64__)
354 /* specifically, if running on H9 */
355 if (thread
== current_thread()) {
358 spl_t spl
= splsched();
360 * This call tell that the current thread changed it's 32bitness.
361 * Other thread were no more on core when 32bitness was changed,
362 * but current_thread() is on core and the previous call to
363 * machine_thread_going_on_core() gave 32bitness which is now wrong.
365 * This is needed for bring-up, a different callback should be used
368 * TODO: Remove this callout when we no longer support 32-bit code on H9
371 urgency
= thread_get_urgency(thread
, &arg1
, &arg2
);
372 machine_thread_going_on_core(thread
, urgency
, 0, 0, mach_approximate_time());
373 thread_unlock(thread
);
376 #endif /* defined(__arm64__) */
378 #endif /* defined(__x86_64__) || defined(__arm64__) */
385 task_set_platform_binary(
387 boolean_t is_platform
)
391 task
->t_flags
|= TF_PLATFORM
;
393 task
->t_flags
&= ~(TF_PLATFORM
);
399 * Set or clear per-task TF_CA_CLIENT_WI flag according to specified argument.
400 * Returns "false" if flag is already set, and "true" in other cases.
403 task_set_ca_client_wi(
405 boolean_t set_or_clear
)
410 /* Tasks can have only one CA_CLIENT work interval */
411 if (task
->t_flags
& TF_CA_CLIENT_WI
)
414 task
->t_flags
|= TF_CA_CLIENT_WI
;
416 task
->t_flags
&= ~TF_CA_CLIENT_WI
;
425 mach_vm_address_t addr
,
429 task
->all_image_info_addr
= addr
;
430 task
->all_image_info_size
= size
;
435 task_atm_reset(__unused task_t task
) {
438 if (task
->atm_context
!= NULL
) {
439 atm_task_descriptor_destroy(task
->atm_context
);
440 task
->atm_context
= NULL
;
447 task_bank_reset(__unused task_t task
) {
449 if (task
->bank_context
!= NULL
) {
450 bank_task_destroy(task
);
455 * NOTE: This should only be called when the P_LINTRANSIT
456 * flag is set (the proc_trans lock is held) on the
457 * proc associated with the task.
460 task_bank_init(__unused task_t task
) {
462 if (task
->bank_context
!= NULL
) {
463 panic("Task bank init called with non null bank context for task: %p and bank_context: %p", task
, task
->bank_context
);
465 bank_task_initialize(task
);
469 task_set_did_exec_flag(task_t task
)
471 task
->t_procflags
|= TPF_DID_EXEC
;
475 task_clear_exec_copy_flag(task_t task
)
477 task
->t_procflags
&= ~TPF_EXEC_COPY
;
481 * This wait event is t_procflags instead of t_flags because t_flags is volatile
483 * TODO: store the flags in the same place as the event
484 * rdar://problem/28501994
487 task_get_return_wait_event(task_t task
)
489 return (event_t
)&task
->t_procflags
;
493 task_clear_return_wait(task_t task
)
497 task
->t_flags
&= ~TF_LRETURNWAIT
;
499 if (task
->t_flags
& TF_LRETURNWAITER
) {
500 thread_wakeup(task_get_return_wait_event(task
));
501 task
->t_flags
&= ~TF_LRETURNWAITER
;
507 void __attribute__((noreturn
))
508 task_wait_to_return(void)
512 task
= current_task();
515 if (task
->t_flags
& TF_LRETURNWAIT
) {
517 task
->t_flags
|= TF_LRETURNWAITER
;
518 assert_wait(task_get_return_wait_event(task
), THREAD_UNINT
);
521 thread_block(THREAD_CONTINUE_NULL
);
524 } while (task
->t_flags
& TF_LRETURNWAIT
);
529 thread_bootstrap_return();
532 #ifdef CONFIG_32BIT_TELEMETRY
534 task_consume_32bit_log_flag(task_t task
)
536 if ((task
->t_procflags
& TPF_LOG_32BIT_TELEMETRY
) != 0) {
537 task
->t_procflags
&= ~TPF_LOG_32BIT_TELEMETRY
;
545 task_set_32bit_log_flag(task_t task
)
547 task
->t_procflags
|= TPF_LOG_32BIT_TELEMETRY
;
549 #endif /* CONFIG_32BIT_TELEMETRY */
552 task_is_exec_copy(task_t task
)
554 return task_is_exec_copy_internal(task
);
558 task_did_exec(task_t task
)
560 return task_did_exec_internal(task
);
564 task_is_active(task_t task
)
570 task_is_halting(task_t task
)
572 return task
->halting
;
575 #if TASK_REFERENCE_LEAK_DEBUG
576 #include <kern/btlog.h>
578 static btlog_t
*task_ref_btlog
;
579 #define TASK_REF_OP_INCR 0x1
580 #define TASK_REF_OP_DECR 0x2
582 #define TASK_REF_NUM_RECORDS 100000
583 #define TASK_REF_BTDEPTH 7
586 task_reference_internal(task_t task
)
588 void * bt
[TASK_REF_BTDEPTH
];
591 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
593 (void)hw_atomic_add(&(task
)->ref_count
, 1);
594 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_INCR
,
599 task_deallocate_internal(task_t task
)
601 void * bt
[TASK_REF_BTDEPTH
];
604 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
606 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_DECR
,
608 return hw_atomic_sub(&(task
)->ref_count
, 1);
611 #endif /* TASK_REFERENCE_LEAK_DEBUG */
617 lck_grp_attr_setdefault(&task_lck_grp_attr
);
618 lck_grp_init(&task_lck_grp
, "task", &task_lck_grp_attr
);
619 lck_attr_setdefault(&task_lck_attr
);
620 lck_mtx_init(&tasks_threads_lock
, &task_lck_grp
, &task_lck_attr
);
621 lck_mtx_init(&tasks_corpse_lock
, &task_lck_grp
, &task_lck_attr
);
625 task_max
* sizeof(struct task
),
626 TASK_CHUNK
* sizeof(struct task
),
629 zone_change(task_zone
, Z_NOENCRYPT
, TRUE
);
633 #endif /* CONFIG_EMBEDDED */
636 * Configure per-task memory limit.
637 * The boot-arg is interpreted as Megabytes,
638 * and takes precedence over the device tree.
639 * Setting the boot-arg to 0 disables task limits.
641 if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb
,
642 sizeof (max_task_footprint_mb
))) {
644 * No limit was found in boot-args, so go look in the device tree.
646 if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb
,
647 sizeof(max_task_footprint_mb
))) {
649 * No limit was found in device tree.
651 max_task_footprint_mb
= 0;
655 if (max_task_footprint_mb
!= 0) {
656 #if CONFIG_MEMORYSTATUS
657 if (max_task_footprint_mb
< 50) {
658 printf("Warning: max_task_pmem %d below minimum.\n",
659 max_task_footprint_mb
);
660 max_task_footprint_mb
= 50;
662 printf("Limiting task physical memory footprint to %d MB\n",
663 max_task_footprint_mb
);
665 max_task_footprint
= (ledger_amount_t
)max_task_footprint_mb
* 1024 * 1024; // Convert MB to bytes
668 * Configure the per-task memory limit warning level.
669 * This is computed as a percentage.
671 max_task_footprint_warning_level
= 0;
673 if (max_mem
< 0x40000000) {
675 * On devices with < 1GB of memory:
676 * -- set warnings to 50MB below the per-task limit.
678 if (max_task_footprint_mb
> 50) {
679 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 50) * 100) / max_task_footprint_mb
;
683 * On devices with >= 1GB of memory:
684 * -- set warnings to 100MB below the per-task limit.
686 if (max_task_footprint_mb
> 100) {
687 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 100) * 100) / max_task_footprint_mb
;
692 * Never allow warning level to land below the default.
694 if (max_task_footprint_warning_level
< PHYS_FOOTPRINT_WARNING_LEVEL
) {
695 max_task_footprint_warning_level
= PHYS_FOOTPRINT_WARNING_LEVEL
;
698 printf("Limiting task physical memory warning to %d%%\n", max_task_footprint_warning_level
);
701 printf("Warning: max_task_pmem specified, but jetsam not configured; ignoring.\n");
702 #endif /* CONFIG_MEMORYSTATUS */
706 PE_parse_boot_argn("pmap_ledgers_panic", &pmap_ledgers_panic
,
707 sizeof (pmap_ledgers_panic
));
708 #endif /* MACH_ASSERT */
711 if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores
,
712 sizeof (hwm_user_cores
))) {
717 proc_init_cpumon_params();
719 if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate
, sizeof (task_wakeups_monitor_rate
))) {
720 task_wakeups_monitor_rate
= TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT
;
723 if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval
, sizeof (task_wakeups_monitor_interval
))) {
724 task_wakeups_monitor_interval
= TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL
;
727 if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct
,
728 sizeof (task_wakeups_monitor_ustackshots_trigger_pct
))) {
729 task_wakeups_monitor_ustackshots_trigger_pct
= TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER
;
732 if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource
,
733 sizeof (disable_exc_resource
))) {
734 disable_exc_resource
= 0;
737 if (!PE_parse_boot_argn("task_iomon_limit_mb", &task_iomon_limit_mb
, sizeof (task_iomon_limit_mb
))) {
738 task_iomon_limit_mb
= IOMON_DEFAULT_LIMIT
;
741 if (!PE_parse_boot_argn("task_iomon_interval_secs", &task_iomon_interval_secs
, sizeof (task_iomon_interval_secs
))) {
742 task_iomon_interval_secs
= IOMON_DEFAULT_INTERVAL
;
745 if (!PE_parse_boot_argn("io_telemetry_limit", &io_telemetry_limit
, sizeof (io_telemetry_limit
))) {
746 io_telemetry_limit
= IO_TELEMETRY_DEFAULT_LIMIT
;
750 * If we have coalitions, coalition_init() will call init_task_ledgers() as it
751 * sets up the ledgers for the default coalition. If we don't have coalitions,
752 * then we have to call it now.
754 #if CONFIG_COALITIONS
755 assert(task_ledger_template
);
756 #else /* CONFIG_COALITIONS */
758 #endif /* CONFIG_COALITIONS */
760 #if TASK_REFERENCE_LEAK_DEBUG
761 task_ref_btlog
= btlog_create(TASK_REF_NUM_RECORDS
, TASK_REF_BTDEPTH
, TRUE
/* caller_will_remove_entries_for_element? */);
762 assert(task_ref_btlog
);
766 * Create the kernel task as the first task.
769 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, TRUE
, TF_NONE
, TPF_NONE
, &kernel_task
) != KERN_SUCCESS
)
771 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, FALSE
, TF_NONE
, TPF_NONE
, &kernel_task
) != KERN_SUCCESS
)
773 panic("task_init\n");
776 vm_map_deallocate(kernel_task
->map
);
777 kernel_task
->map
= kernel_map
;
778 lck_spin_init(&dead_task_statistics_lock
, &task_lck_grp
, &task_lck_attr
);
782 * Create a task running in the kernel address space. It may
783 * have its own map of size mem_size and may have ipc privileges.
787 __unused task_t parent_task
,
788 __unused vm_offset_t map_base
,
789 __unused vm_size_t map_size
,
790 __unused task_t
*child_task
)
792 return (KERN_INVALID_ARGUMENT
);
798 __unused ledger_port_array_t ledger_ports
,
799 __unused mach_msg_type_number_t num_ledger_ports
,
800 __unused boolean_t inherit_memory
,
801 __unused task_t
*child_task
) /* OUT */
803 if (parent_task
== TASK_NULL
)
804 return(KERN_INVALID_ARGUMENT
);
807 * No longer supported: too many calls assume that a task has a valid
810 return(KERN_FAILURE
);
814 host_security_create_task_token(
815 host_security_t host_security
,
817 __unused security_token_t sec_token
,
818 __unused audit_token_t audit_token
,
819 __unused host_priv_t host_priv
,
820 __unused ledger_port_array_t ledger_ports
,
821 __unused mach_msg_type_number_t num_ledger_ports
,
822 __unused boolean_t inherit_memory
,
823 __unused task_t
*child_task
) /* OUT */
825 if (parent_task
== TASK_NULL
)
826 return(KERN_INVALID_ARGUMENT
);
828 if (host_security
== HOST_NULL
)
829 return(KERN_INVALID_SECURITY
);
832 * No longer supported.
834 return(KERN_FAILURE
);
842 * Physical footprint: This is the sum of:
843 * + (internal - alternate_accounting)
844 * + (internal_compressed - alternate_accounting_compressed)
846 * + purgeable_nonvolatile
847 * + purgeable_nonvolatile_compressed
851 * The task's anonymous memory, which on iOS is always resident.
853 * internal_compressed
854 * Amount of this task's internal memory which is held by the compressor.
855 * Such memory is no longer actually resident for the task [i.e., resident in its pmap],
856 * and could be either decompressed back into memory, or paged out to storage, depending
857 * on our implementation.
860 * IOKit mappings: The total size of all IOKit mappings in this task, regardless of
861 clean/dirty or internal/external state].
863 * alternate_accounting
864 * The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
865 * are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
869 init_task_ledgers(void)
873 assert(task_ledger_template
== NULL
);
874 assert(kernel_task
== TASK_NULL
);
877 PE_parse_boot_argn("pmap_ledgers_panic", &pmap_ledgers_panic
,
878 sizeof (pmap_ledgers_panic
));
879 #endif /* MACH_ASSERT */
881 if ((t
= ledger_template_create("Per-task ledger")) == NULL
)
882 panic("couldn't create task ledger template");
884 task_ledgers
.cpu_time
= ledger_entry_add(t
, "cpu_time", "sched", "ns");
885 task_ledgers
.tkm_private
= ledger_entry_add(t
, "tkm_private",
887 task_ledgers
.tkm_shared
= ledger_entry_add(t
, "tkm_shared", "physmem",
889 task_ledgers
.phys_mem
= ledger_entry_add(t
, "phys_mem", "physmem",
891 task_ledgers
.wired_mem
= ledger_entry_add(t
, "wired_mem", "physmem",
893 task_ledgers
.internal
= ledger_entry_add(t
, "internal", "physmem",
895 task_ledgers
.iokit_mapped
= ledger_entry_add(t
, "iokit_mapped", "mappings",
897 task_ledgers
.alternate_accounting
= ledger_entry_add(t
, "alternate_accounting", "physmem",
899 task_ledgers
.alternate_accounting_compressed
= ledger_entry_add(t
, "alternate_accounting_compressed", "physmem",
901 task_ledgers
.page_table
= ledger_entry_add(t
, "page_table", "physmem",
903 task_ledgers
.phys_footprint
= ledger_entry_add(t
, "phys_footprint", "physmem",
905 task_ledgers
.internal_compressed
= ledger_entry_add(t
, "internal_compressed", "physmem",
907 task_ledgers
.purgeable_volatile
= ledger_entry_add(t
, "purgeable_volatile", "physmem", "bytes");
908 task_ledgers
.purgeable_nonvolatile
= ledger_entry_add(t
, "purgeable_nonvolatile", "physmem", "bytes");
909 task_ledgers
.purgeable_volatile_compressed
= ledger_entry_add(t
, "purgeable_volatile_compress", "physmem", "bytes");
910 task_ledgers
.purgeable_nonvolatile_compressed
= ledger_entry_add(t
, "purgeable_nonvolatile_compress", "physmem", "bytes");
911 task_ledgers
.platform_idle_wakeups
= ledger_entry_add(t
, "platform_idle_wakeups", "power",
913 task_ledgers
.interrupt_wakeups
= ledger_entry_add(t
, "interrupt_wakeups", "power",
917 sfi_class_id_t class_id
, ledger_alias
;
918 for (class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
919 task_ledgers
.sfi_wait_times
[class_id
] = -1;
922 /* don't account for UNSPECIFIED */
923 for (class_id
= SFI_CLASS_UNSPECIFIED
+ 1; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
924 ledger_alias
= sfi_get_ledger_alias_for_class(class_id
);
925 if (ledger_alias
!= SFI_CLASS_UNSPECIFIED
) {
926 /* Check to see if alias has been registered yet */
927 if (task_ledgers
.sfi_wait_times
[ledger_alias
] != -1) {
928 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
];
930 /* Otherwise, initialize it first */
931 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
] = sfi_ledger_entry_add(t
, ledger_alias
);
934 task_ledgers
.sfi_wait_times
[class_id
] = sfi_ledger_entry_add(t
, class_id
);
937 if (task_ledgers
.sfi_wait_times
[class_id
] < 0) {
938 panic("couldn't create entries for task ledger template for SFI class 0x%x", class_id
);
942 assert(task_ledgers
.sfi_wait_times
[MAX_SFI_CLASS_ID
-1] != -1);
943 #endif /* CONFIG_SCHED_SFI */
945 task_ledgers
.cpu_time_billed_to_me
= ledger_entry_add(t
, "cpu_time_billed_to_me", "sched", "ns");
946 task_ledgers
.cpu_time_billed_to_others
= ledger_entry_add(t
, "cpu_time_billed_to_others", "sched", "ns");
947 task_ledgers
.physical_writes
= ledger_entry_add(t
, "physical_writes", "res", "bytes");
948 task_ledgers
.logical_writes
= ledger_entry_add(t
, "logical_writes", "res", "bytes");
949 task_ledgers
.energy_billed_to_me
= ledger_entry_add(t
, "energy_billed_to_me", "power", "nj");
950 task_ledgers
.energy_billed_to_others
= ledger_entry_add(t
, "energy_billed_to_others", "power", "nj");
952 if ((task_ledgers
.cpu_time
< 0) ||
953 (task_ledgers
.tkm_private
< 0) ||
954 (task_ledgers
.tkm_shared
< 0) ||
955 (task_ledgers
.phys_mem
< 0) ||
956 (task_ledgers
.wired_mem
< 0) ||
957 (task_ledgers
.internal
< 0) ||
958 (task_ledgers
.iokit_mapped
< 0) ||
959 (task_ledgers
.alternate_accounting
< 0) ||
960 (task_ledgers
.alternate_accounting_compressed
< 0) ||
961 (task_ledgers
.page_table
< 0) ||
962 (task_ledgers
.phys_footprint
< 0) ||
963 (task_ledgers
.internal_compressed
< 0) ||
964 (task_ledgers
.purgeable_volatile
< 0) ||
965 (task_ledgers
.purgeable_nonvolatile
< 0) ||
966 (task_ledgers
.purgeable_volatile_compressed
< 0) ||
967 (task_ledgers
.purgeable_nonvolatile_compressed
< 0) ||
968 (task_ledgers
.platform_idle_wakeups
< 0) ||
969 (task_ledgers
.interrupt_wakeups
< 0) ||
970 (task_ledgers
.cpu_time_billed_to_me
< 0) || (task_ledgers
.cpu_time_billed_to_others
< 0) ||
971 (task_ledgers
.physical_writes
< 0) ||
972 (task_ledgers
.logical_writes
< 0) ||
973 (task_ledgers
.energy_billed_to_me
< 0) ||
974 (task_ledgers
.energy_billed_to_others
< 0)
976 panic("couldn't create entries for task ledger template");
979 ledger_track_credit_only(t
, task_ledgers
.phys_footprint
);
980 ledger_track_credit_only(t
, task_ledgers
.page_table
);
981 ledger_track_credit_only(t
, task_ledgers
.internal
);
982 ledger_track_credit_only(t
, task_ledgers
.internal_compressed
);
983 ledger_track_credit_only(t
, task_ledgers
.iokit_mapped
);
984 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting
);
985 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting_compressed
);
986 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile
);
987 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile
);
988 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile_compressed
);
989 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
991 ledger_track_maximum(t
, task_ledgers
.phys_footprint
, 60);
993 if (pmap_ledgers_panic
) {
994 ledger_panic_on_negative(t
, task_ledgers
.phys_footprint
);
995 ledger_panic_on_negative(t
, task_ledgers
.page_table
);
996 ledger_panic_on_negative(t
, task_ledgers
.internal
);
997 ledger_panic_on_negative(t
, task_ledgers
.internal_compressed
);
998 ledger_panic_on_negative(t
, task_ledgers
.iokit_mapped
);
999 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting
);
1000 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting_compressed
);
1001 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile
);
1002 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile
);
1003 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile_compressed
);
1004 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
1006 #endif /* MACH_ASSERT */
1008 #if CONFIG_MEMORYSTATUS
1009 ledger_set_callback(t
, task_ledgers
.phys_footprint
, task_footprint_exceeded
, NULL
, NULL
);
1010 #endif /* CONFIG_MEMORYSTATUS */
1012 ledger_set_callback(t
, task_ledgers
.interrupt_wakeups
,
1013 task_wakeups_rate_exceeded
, NULL
, NULL
);
1014 ledger_set_callback(t
, task_ledgers
.physical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_PHYSICAL_WRITES
, NULL
);
1015 ledger_set_callback(t
, task_ledgers
.logical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_LOGICAL_WRITES
, NULL
);
1017 ledger_template_complete(t
);
1018 task_ledger_template
= t
;
1022 task_create_internal(
1024 coalition_t
*parent_coalitions __unused
,
1025 boolean_t inherit_memory
,
1026 __unused boolean_t is_64bit
,
1028 uint32_t t_procflags
,
1029 task_t
*child_task
) /* OUT */
1032 vm_shared_region_t shared_region
;
1033 ledger_t ledger
= NULL
;
1035 new_task
= (task_t
) zalloc(task_zone
);
1037 if (new_task
== TASK_NULL
)
1038 return(KERN_RESOURCE_SHORTAGE
);
1040 /* one ref for just being alive; one for our caller */
1041 new_task
->ref_count
= 2;
1043 /* allocate with active entries */
1044 assert(task_ledger_template
!= NULL
);
1045 if ((ledger
= ledger_instantiate(task_ledger_template
,
1046 LEDGER_CREATE_ACTIVE_ENTRIES
)) == NULL
) {
1047 zfree(task_zone
, new_task
);
1048 return(KERN_RESOURCE_SHORTAGE
);
1052 new_task
->ledger
= ledger
;
1054 #if defined(CONFIG_SCHED_MULTIQ)
1055 new_task
->sched_group
= sched_group_create();
1058 /* if inherit_memory is true, parent_task MUST not be NULL */
1059 if (!(t_flags
& TF_CORPSE_FORK
) && inherit_memory
)
1060 new_task
->map
= vm_map_fork(ledger
, parent_task
->map
, 0);
1062 new_task
->map
= vm_map_create(pmap_create(ledger
, 0, is_64bit
),
1063 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
1064 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
1066 /* Inherit memlock limit from parent */
1068 vm_map_set_user_wire_limit(new_task
->map
, (vm_size_t
)parent_task
->map
->user_wire_limit
);
1070 lck_mtx_init(&new_task
->lock
, &task_lck_grp
, &task_lck_attr
);
1071 queue_init(&new_task
->threads
);
1072 new_task
->suspend_count
= 0;
1073 new_task
->thread_count
= 0;
1074 new_task
->active_thread_count
= 0;
1075 new_task
->user_stop_count
= 0;
1076 new_task
->legacy_stop_count
= 0;
1077 new_task
->active
= TRUE
;
1078 new_task
->halting
= FALSE
;
1079 new_task
->user_data
= NULL
;
1080 new_task
->priv_flags
= 0;
1081 new_task
->t_flags
= t_flags
;
1082 new_task
->t_procflags
= t_procflags
;
1083 new_task
->importance
= 0;
1084 new_task
->crashed_thread_id
= 0;
1085 new_task
->exec_token
= 0;
1088 new_task
->atm_context
= NULL
;
1090 new_task
->bank_context
= NULL
;
1093 new_task
->bsd_info
= NULL
;
1094 new_task
->corpse_info
= NULL
;
1095 #endif /* MACH_BSD */
1098 new_task
->crash_label
= NULL
;
1101 #if CONFIG_MEMORYSTATUS
1102 if (max_task_footprint
!= 0) {
1103 ledger_set_limit(ledger
, task_ledgers
.phys_footprint
, max_task_footprint
, PHYS_FOOTPRINT_WARNING_LEVEL
);
1105 #endif /* CONFIG_MEMORYSTATUS */
1107 if (task_wakeups_monitor_rate
!= 0) {
1108 uint32_t flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
1109 int32_t rate
; // Ignored because of WAKEMON_SET_DEFAULTS
1110 task_wakeups_monitor_ctl(new_task
, &flags
, &rate
);
1113 #if CONFIG_IO_ACCOUNTING
1114 uint32_t flags
= IOMON_ENABLE
;
1115 task_io_monitor_ctl(new_task
, &flags
);
1116 #endif /* CONFIG_IO_ACCOUNTING */
1118 machine_task_init(new_task
, parent_task
, inherit_memory
);
1120 new_task
->task_debug
= NULL
;
1122 #if DEVELOPMENT || DEBUG
1123 new_task
->task_unnested
= FALSE
;
1124 new_task
->task_disconnected_count
= 0;
1126 queue_init(&new_task
->semaphore_list
);
1127 new_task
->semaphores_owned
= 0;
1129 ipc_task_init(new_task
, parent_task
);
1131 new_task
->vtimers
= 0;
1133 new_task
->shared_region
= NULL
;
1135 new_task
->affinity_space
= NULL
;
1137 new_task
->t_kpc
= 0;
1139 new_task
->pidsuspended
= FALSE
;
1140 new_task
->frozen
= FALSE
;
1141 new_task
->changing_freeze_state
= FALSE
;
1142 new_task
->rusage_cpu_flags
= 0;
1143 new_task
->rusage_cpu_percentage
= 0;
1144 new_task
->rusage_cpu_interval
= 0;
1145 new_task
->rusage_cpu_deadline
= 0;
1146 new_task
->rusage_cpu_callt
= NULL
;
1148 new_task
->suspends_outstanding
= 0;
1152 new_task
->hv_task_target
= NULL
;
1153 #endif /* HYPERVISOR */
1156 queue_init(&new_task
->task_watchers
);
1157 new_task
->num_taskwatchers
= 0;
1158 new_task
->watchapplying
= 0;
1159 #endif /* CONFIG_EMBEDDED */
1161 new_task
->mem_notify_reserved
= 0;
1162 new_task
->memlimit_attrs_reserved
= 0;
1163 #if IMPORTANCE_INHERITANCE
1164 new_task
->task_imp_base
= NULL
;
1165 #endif /* IMPORTANCE_INHERITANCE */
1167 new_task
->requested_policy
= default_task_requested_policy
;
1168 new_task
->effective_policy
= default_task_effective_policy
;
1170 if (parent_task
!= TASK_NULL
) {
1171 new_task
->sec_token
= parent_task
->sec_token
;
1172 new_task
->audit_token
= parent_task
->audit_token
;
1174 /* inherit the parent's shared region */
1175 shared_region
= vm_shared_region_get(parent_task
);
1176 vm_shared_region_set(new_task
, shared_region
);
1178 if(task_has_64BitAddr(parent_task
))
1179 task_set_64BitAddr(new_task
);
1180 new_task
->all_image_info_addr
= parent_task
->all_image_info_addr
;
1181 new_task
->all_image_info_size
= parent_task
->all_image_info_size
;
1183 if (inherit_memory
&& parent_task
->affinity_space
)
1184 task_affinity_create(parent_task
, new_task
);
1186 new_task
->pset_hint
= parent_task
->pset_hint
= task_choose_pset(parent_task
);
1188 #if IMPORTANCE_INHERITANCE
1189 ipc_importance_task_t new_task_imp
= IIT_NULL
;
1190 boolean_t inherit_receive
= TRUE
;
1192 if (task_is_marked_importance_donor(parent_task
)) {
1193 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
1194 assert(IIT_NULL
!= new_task_imp
);
1195 ipc_importance_task_mark_donor(new_task_imp
, TRUE
);
1198 /* Embedded only wants to inherit for exec copy task */
1199 if ((t_procflags
& TPF_EXEC_COPY
) == 0) {
1200 inherit_receive
= FALSE
;
1202 #endif /* CONFIG_EMBEDDED */
1204 if (inherit_receive
) {
1205 if (task_is_marked_importance_receiver(parent_task
)) {
1206 if (IIT_NULL
== new_task_imp
)
1207 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
1208 assert(IIT_NULL
!= new_task_imp
);
1209 ipc_importance_task_mark_receiver(new_task_imp
, TRUE
);
1211 if (task_is_marked_importance_denap_receiver(parent_task
)) {
1212 if (IIT_NULL
== new_task_imp
)
1213 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
1214 assert(IIT_NULL
!= new_task_imp
);
1215 ipc_importance_task_mark_denap_receiver(new_task_imp
, TRUE
);
1219 if (IIT_NULL
!= new_task_imp
) {
1220 assert(new_task
->task_imp_base
== new_task_imp
);
1221 ipc_importance_task_release(new_task_imp
);
1223 #endif /* IMPORTANCE_INHERITANCE */
1225 new_task
->priority
= BASEPRI_DEFAULT
;
1226 new_task
->max_priority
= MAXPRI_USER
;
1228 task_policy_create(new_task
, parent_task
);
1230 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
1231 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
1234 task_set_64BitAddr(new_task
);
1236 new_task
->all_image_info_addr
= (mach_vm_address_t
)0;
1237 new_task
->all_image_info_size
= (mach_vm_size_t
)0;
1239 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
1241 if (kernel_task
== TASK_NULL
) {
1242 new_task
->priority
= BASEPRI_KERNEL
;
1243 new_task
->max_priority
= MAXPRI_KERNEL
;
1245 new_task
->priority
= BASEPRI_DEFAULT
;
1246 new_task
->max_priority
= MAXPRI_USER
;
1250 bzero(new_task
->coalition
, sizeof(new_task
->coalition
));
1251 for (int i
= 0; i
< COALITION_NUM_TYPES
; i
++)
1252 queue_chain_init(new_task
->task_coalition
[i
]);
1254 /* Allocate I/O Statistics */
1255 new_task
->task_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
1256 assert(new_task
->task_io_stats
!= NULL
);
1257 bzero(new_task
->task_io_stats
, sizeof(struct io_stat_info
));
1259 bzero(&(new_task
->cpu_time_eqos_stats
), sizeof(new_task
->cpu_time_eqos_stats
));
1260 bzero(&(new_task
->cpu_time_rqos_stats
), sizeof(new_task
->cpu_time_rqos_stats
));
1262 bzero(&new_task
->extmod_statistics
, sizeof(new_task
->extmod_statistics
));
1264 /* Copy resource acc. info from Parent for Corpe Forked task. */
1265 if (parent_task
!= NULL
&& (t_flags
& TF_CORPSE_FORK
)) {
1266 task_rollup_accounting_info(new_task
, parent_task
);
1268 /* Initialize to zero for standard fork/spawn case */
1269 new_task
->total_user_time
= 0;
1270 new_task
->total_system_time
= 0;
1271 new_task
->total_ptime
= 0;
1272 new_task
->faults
= 0;
1273 new_task
->pageins
= 0;
1274 new_task
->cow_faults
= 0;
1275 new_task
->messages_sent
= 0;
1276 new_task
->messages_received
= 0;
1277 new_task
->syscalls_mach
= 0;
1278 new_task
->syscalls_unix
= 0;
1279 new_task
->c_switch
= 0;
1280 new_task
->p_switch
= 0;
1281 new_task
->ps_switch
= 0;
1282 new_task
->low_mem_notified_warn
= 0;
1283 new_task
->low_mem_notified_critical
= 0;
1284 new_task
->purged_memory_warn
= 0;
1285 new_task
->purged_memory_critical
= 0;
1286 new_task
->low_mem_privileged_listener
= 0;
1287 new_task
->memlimit_is_active
= 0;
1288 new_task
->memlimit_is_fatal
= 0;
1289 new_task
->memlimit_active_exc_resource
= 0;
1290 new_task
->memlimit_inactive_exc_resource
= 0;
1291 new_task
->task_timer_wakeups_bin_1
= 0;
1292 new_task
->task_timer_wakeups_bin_2
= 0;
1293 new_task
->task_gpu_ns
= 0;
1294 new_task
->task_immediate_writes
= 0;
1295 new_task
->task_deferred_writes
= 0;
1296 new_task
->task_invalidated_writes
= 0;
1297 new_task
->task_metadata_writes
= 0;
1298 new_task
->task_energy
= 0;
1300 memset(&new_task
->task_monotonic
, 0, sizeof(new_task
->task_monotonic
));
1301 #endif /* MONOTONIC */
1305 #if CONFIG_COALITIONS
1306 if (!(t_flags
& TF_CORPSE_FORK
)) {
1307 /* TODO: there is no graceful failure path here... */
1308 if (parent_coalitions
&& parent_coalitions
[COALITION_TYPE_RESOURCE
]) {
1309 coalitions_adopt_task(parent_coalitions
, new_task
);
1310 } else if (parent_task
&& parent_task
->coalition
[COALITION_TYPE_RESOURCE
]) {
1312 * all tasks at least have a resource coalition, so
1313 * if the parent has one then inherit all coalitions
1314 * the parent is a part of
1316 coalitions_adopt_task(parent_task
->coalition
, new_task
);
1318 /* TODO: assert that new_task will be PID 1 (launchd) */
1319 coalitions_adopt_init_task(new_task
);
1322 * on exec, we need to transfer the coalition roles from the
1323 * parent task to the exec copy task.
1325 if (parent_task
&& (t_procflags
& TPF_EXEC_COPY
)) {
1326 int coal_roles
[COALITION_NUM_TYPES
];
1327 task_coalition_roles(parent_task
, coal_roles
);
1328 (void)coalitions_set_roles(new_task
->coalition
, new_task
, coal_roles
);
1331 coalitions_adopt_corpse_task(new_task
);
1334 if (new_task
->coalition
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
1335 panic("created task is not a member of a resource coalition");
1337 #endif /* CONFIG_COALITIONS */
1339 new_task
->dispatchqueue_offset
= 0;
1340 if (parent_task
!= NULL
) {
1341 new_task
->dispatchqueue_offset
= parent_task
->dispatchqueue_offset
;
1344 if (vm_backing_store_low
&& parent_task
!= NULL
)
1345 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
1347 new_task
->task_volatile_objects
= 0;
1348 new_task
->task_nonvolatile_objects
= 0;
1349 new_task
->task_purgeable_disowning
= FALSE
;
1350 new_task
->task_purgeable_disowned
= FALSE
;
1351 queue_init(&new_task
->task_objq
);
1352 task_objq_lock_init(new_task
);
1354 new_task
->task_region_footprint
= FALSE
;
1356 #if CONFIG_SECLUDED_MEMORY
1357 new_task
->task_can_use_secluded_mem
= FALSE
;
1358 new_task
->task_could_use_secluded_mem
= FALSE
;
1359 new_task
->task_could_also_use_secluded_mem
= FALSE
;
1360 #endif /* CONFIG_SECLUDED_MEMORY */
1362 queue_init(&new_task
->io_user_clients
);
1364 ipc_task_enable(new_task
);
1366 lck_mtx_lock(&tasks_threads_lock
);
1367 queue_enter(&tasks
, new_task
, task_t
, tasks
);
1369 if (tasks_suspend_state
) {
1370 task_suspend_internal(new_task
);
1372 lck_mtx_unlock(&tasks_threads_lock
);
1374 *child_task
= new_task
;
1375 return(KERN_SUCCESS
);
1379 * task_rollup_accounting_info
1381 * Roll up accounting stats. Used to rollup stats
1382 * for exec copy task and corpse fork.
1385 task_rollup_accounting_info(task_t to_task
, task_t from_task
)
1387 assert(from_task
!= to_task
);
1389 to_task
->total_user_time
= from_task
->total_user_time
;
1390 to_task
->total_system_time
= from_task
->total_system_time
;
1391 to_task
->total_ptime
= from_task
->total_ptime
;
1392 to_task
->faults
= from_task
->faults
;
1393 to_task
->pageins
= from_task
->pageins
;
1394 to_task
->cow_faults
= from_task
->cow_faults
;
1395 to_task
->messages_sent
= from_task
->messages_sent
;
1396 to_task
->messages_received
= from_task
->messages_received
;
1397 to_task
->syscalls_mach
= from_task
->syscalls_mach
;
1398 to_task
->syscalls_unix
= from_task
->syscalls_unix
;
1399 to_task
->c_switch
= from_task
->c_switch
;
1400 to_task
->p_switch
= from_task
->p_switch
;
1401 to_task
->ps_switch
= from_task
->ps_switch
;
1402 to_task
->extmod_statistics
= from_task
->extmod_statistics
;
1403 to_task
->low_mem_notified_warn
= from_task
->low_mem_notified_warn
;
1404 to_task
->low_mem_notified_critical
= from_task
->low_mem_notified_critical
;
1405 to_task
->purged_memory_warn
= from_task
->purged_memory_warn
;
1406 to_task
->purged_memory_critical
= from_task
->purged_memory_critical
;
1407 to_task
->low_mem_privileged_listener
= from_task
->low_mem_privileged_listener
;
1408 *to_task
->task_io_stats
= *from_task
->task_io_stats
;
1409 to_task
->cpu_time_eqos_stats
= from_task
->cpu_time_eqos_stats
;
1410 to_task
->cpu_time_rqos_stats
= from_task
->cpu_time_rqos_stats
;
1411 to_task
->task_timer_wakeups_bin_1
= from_task
->task_timer_wakeups_bin_1
;
1412 to_task
->task_timer_wakeups_bin_2
= from_task
->task_timer_wakeups_bin_2
;
1413 to_task
->task_gpu_ns
= from_task
->task_gpu_ns
;
1414 to_task
->task_immediate_writes
= from_task
->task_immediate_writes
;
1415 to_task
->task_deferred_writes
= from_task
->task_deferred_writes
;
1416 to_task
->task_invalidated_writes
= from_task
->task_invalidated_writes
;
1417 to_task
->task_metadata_writes
= from_task
->task_metadata_writes
;
1418 to_task
->task_energy
= from_task
->task_energy
;
1420 /* Skip ledger roll up for memory accounting entries */
1421 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time
);
1422 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.platform_idle_wakeups
);
1423 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.interrupt_wakeups
);
1424 #if CONFIG_SCHED_SFI
1425 for (sfi_class_id_t class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1426 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.sfi_wait_times
[class_id
]);
1429 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_me
);
1430 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_others
);
1431 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.physical_writes
);
1432 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.logical_writes
);
1433 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.energy_billed_to_me
);
1434 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.energy_billed_to_others
);
1437 int task_dropped_imp_count
= 0;
1442 * Drop a reference on a task.
1448 ledger_amount_t credit
, debit
, interrupt_wakeups
, platform_idle_wakeups
;
1451 if (task
== TASK_NULL
)
1454 refs
= task_deallocate_internal(task
);
1456 #if IMPORTANCE_INHERITANCE
1460 atomic_load_explicit(&task
->ref_count
, memory_order_acquire
);
1464 * If last ref potentially comes from the task's importance,
1465 * disconnect it. But more task refs may be added before
1466 * that completes, so wait for the reference to go to zero
1467 * naturually (it may happen on a recursive task_deallocate()
1468 * from the ipc_importance_disconnect_task() call).
1470 if (IIT_NULL
!= task
->task_imp_base
)
1471 ipc_importance_disconnect_task(task
);
1478 atomic_load_explicit(&task
->ref_count
, memory_order_acquire
);
1480 #endif /* IMPORTANCE_INHERITANCE */
1482 lck_mtx_lock(&tasks_threads_lock
);
1483 queue_remove(&terminated_tasks
, task
, task_t
, tasks
);
1484 terminated_tasks_count
--;
1485 lck_mtx_unlock(&tasks_threads_lock
);
1488 * remove the reference on atm descriptor
1490 task_atm_reset(task
);
1493 * remove the reference on bank context
1495 task_bank_reset(task
);
1497 if (task
->task_io_stats
)
1498 kfree(task
->task_io_stats
, sizeof(struct io_stat_info
));
1501 * Give the machine dependent code a chance
1502 * to perform cleanup before ripping apart
1505 machine_task_terminate(task
);
1507 ipc_task_terminate(task
);
1509 /* let iokit know */
1510 iokit_task_terminate(task
);
1512 if (task
->affinity_space
)
1513 task_affinity_deallocate(task
);
1516 if (task
->ledger
!= NULL
&&
1517 task
->map
!= NULL
&&
1518 task
->map
->pmap
!= NULL
&&
1519 task
->map
->pmap
->ledger
!= NULL
) {
1520 assert(task
->ledger
== task
->map
->pmap
->ledger
);
1522 #endif /* MACH_ASSERT */
1524 vm_purgeable_disown(task
);
1525 assert(task
->task_purgeable_disowned
);
1526 if (task
->task_volatile_objects
!= 0 ||
1527 task
->task_nonvolatile_objects
!= 0) {
1528 panic("task_deallocate(%p): "
1529 "volatile_objects=%d nonvolatile_objects=%d\n",
1531 task
->task_volatile_objects
,
1532 task
->task_nonvolatile_objects
);
1535 vm_map_deallocate(task
->map
);
1536 is_release(task
->itk_space
);
1538 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
1539 &interrupt_wakeups
, &debit
);
1540 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
1541 &platform_idle_wakeups
, &debit
);
1543 #if defined(CONFIG_SCHED_MULTIQ)
1544 sched_group_destroy(task
->sched_group
);
1547 /* Accumulate statistics for dead tasks */
1548 lck_spin_lock(&dead_task_statistics_lock
);
1549 dead_task_statistics
.total_user_time
+= task
->total_user_time
;
1550 dead_task_statistics
.total_system_time
+= task
->total_system_time
;
1552 dead_task_statistics
.task_interrupt_wakeups
+= interrupt_wakeups
;
1553 dead_task_statistics
.task_platform_idle_wakeups
+= platform_idle_wakeups
;
1555 dead_task_statistics
.task_timer_wakeups_bin_1
+= task
->task_timer_wakeups_bin_1
;
1556 dead_task_statistics
.task_timer_wakeups_bin_2
+= task
->task_timer_wakeups_bin_2
;
1557 dead_task_statistics
.total_ptime
+= task
->total_ptime
;
1558 dead_task_statistics
.total_pset_switches
+= task
->ps_switch
;
1559 dead_task_statistics
.task_gpu_ns
+= task
->task_gpu_ns
;
1560 dead_task_statistics
.task_energy
+= task
->task_energy
;
1562 lck_spin_unlock(&dead_task_statistics_lock
);
1563 lck_mtx_destroy(&task
->lock
, &task_lck_grp
);
1565 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_private
, &credit
,
1567 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_private
.alloc
);
1568 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_private
.free
);
1570 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_shared
, &credit
,
1572 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_shared
.alloc
);
1573 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_shared
.free
);
1575 ledger_dereference(task
->ledger
);
1577 #if TASK_REFERENCE_LEAK_DEBUG
1578 btlog_remove_entries_for_element(task_ref_btlog
, task
);
1581 #if CONFIG_COALITIONS
1582 task_release_coalitions(task
);
1583 #endif /* CONFIG_COALITIONS */
1585 bzero(task
->coalition
, sizeof(task
->coalition
));
1588 /* clean up collected information since last reference to task is gone */
1589 if (task
->corpse_info
) {
1590 void *corpse_info_kernel
= kcdata_memory_get_begin_addr(task
->corpse_info
);
1591 task_crashinfo_destroy(task
->corpse_info
);
1592 task
->corpse_info
= NULL
;
1593 if (corpse_info_kernel
) {
1594 kfree(corpse_info_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1600 if (task
->crash_label
) {
1601 mac_exc_free_label(task
->crash_label
);
1602 task
->crash_label
= NULL
;
1606 assert(queue_empty(&task
->task_objq
));
1608 zfree(task_zone
, task
);
1612 * task_name_deallocate:
1614 * Drop a reference on a task name.
1617 task_name_deallocate(
1618 task_name_t task_name
)
1620 return(task_deallocate((task_t
)task_name
));
1624 * task_inspect_deallocate:
1626 * Drop a task inspection reference.
1629 task_inspect_deallocate(
1630 task_inspect_t task_inspect
)
1632 return(task_deallocate((task_t
)task_inspect
));
1636 * task_suspension_token_deallocate:
1638 * Drop a reference on a task suspension token.
1641 task_suspension_token_deallocate(
1642 task_suspension_token_t token
)
1644 return(task_deallocate((task_t
)token
));
1649 * task_collect_crash_info:
1651 * collect crash info from bsd and mach based data
1654 task_collect_crash_info(
1657 struct label
*crash_label
,
1661 kern_return_t kr
= KERN_SUCCESS
;
1663 kcdata_descriptor_t crash_data
= NULL
;
1664 kcdata_descriptor_t crash_data_release
= NULL
;
1665 mach_msg_type_number_t size
= CORPSEINFO_ALLOCATION_SIZE
;
1666 mach_vm_offset_t crash_data_ptr
= 0;
1667 void *crash_data_kernel
= NULL
;
1668 void *crash_data_kernel_release
= NULL
;
1670 struct label
*label
, *free_label
;
1673 if (!corpses_enabled()) {
1674 return KERN_NOT_SUPPORTED
;
1678 free_label
= label
= mac_exc_create_label();
1683 assert(is_corpse_fork
|| task
->bsd_info
!= NULL
);
1684 if (task
->corpse_info
== NULL
&& (is_corpse_fork
|| task
->bsd_info
!= NULL
)) {
1686 /* Set the crash label, used by the exception delivery mac hook */
1687 free_label
= task
->crash_label
; // Most likely NULL.
1688 task
->crash_label
= label
;
1689 mac_exc_update_task_crash_label(task
, crash_label
);
1693 crash_data_kernel
= (void *) kalloc(CORPSEINFO_ALLOCATION_SIZE
);
1694 if (crash_data_kernel
== NULL
) {
1695 kr
= KERN_RESOURCE_SHORTAGE
;
1698 bzero(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1699 crash_data_ptr
= (mach_vm_offset_t
) crash_data_kernel
;
1701 /* Do not get a corpse ref for corpse fork */
1702 crash_data
= task_crashinfo_alloc_init((mach_vm_address_t
)crash_data_ptr
, size
,
1703 is_corpse_fork
? 0 : CORPSE_CRASHINFO_HAS_REF
,
1704 KCFLAG_USE_MEMCOPY
);
1707 crash_data_release
= task
->corpse_info
;
1708 crash_data_kernel_release
= kcdata_memory_get_begin_addr(crash_data_release
);
1709 task
->corpse_info
= crash_data
;
1714 kfree(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1718 if (crash_data_release
!= NULL
) {
1719 task_crashinfo_destroy(crash_data_release
);
1721 if (crash_data_kernel_release
!= NULL
) {
1722 kfree(crash_data_kernel_release
, CORPSEINFO_ALLOCATION_SIZE
);
1730 if (free_label
!= NULL
) {
1731 mac_exc_free_label(free_label
);
1738 * task_deliver_crash_notification:
1740 * Makes outcall to registered host port for a corpse.
1743 task_deliver_crash_notification(
1746 exception_type_t etype
,
1747 mach_exception_subcode_t subcode
)
1749 kcdata_descriptor_t crash_info
= task
->corpse_info
;
1750 thread_t th_iter
= NULL
;
1751 kern_return_t kr
= KERN_SUCCESS
;
1752 wait_interrupt_t wsave
;
1753 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1754 ipc_port_t task_port
, old_notify
;
1756 if (crash_info
== NULL
)
1757 return KERN_FAILURE
;
1760 if (task_is_a_corpse_fork(task
)) {
1761 /* Populate code with EXC_{RESOURCE,GUARD} for corpse fork */
1765 /* Populate code with EXC_CRASH for corpses */
1766 code
[0] = EXC_CRASH
;
1768 /* Update the code[1] if the boot-arg corpse_for_fatal_memkill is set */
1769 if (corpse_for_fatal_memkill
) {
1774 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1776 if (th_iter
->corpse_dup
== FALSE
) {
1777 ipc_thread_reset(th_iter
);
1782 /* Arm the no-sender notification for taskport */
1783 task_reference(task
);
1784 task_port
= convert_task_to_port(task
);
1786 assert(ip_active(task_port
));
1787 ipc_port_nsrequest(task_port
, task_port
->ip_mscount
, ipc_port_make_sonce_locked(task_port
), &old_notify
);
1789 assert(IP_NULL
== old_notify
);
1791 wsave
= thread_interrupt_level(THREAD_UNINT
);
1792 kr
= exception_triage_thread(EXC_CORPSE_NOTIFY
, code
, EXCEPTION_CODE_MAX
, thread
);
1793 if (kr
!= KERN_SUCCESS
) {
1794 printf("Failed to send exception EXC_CORPSE_NOTIFY. error code: %d for pid %d\n", kr
, task_pid(task
));
1797 (void)thread_interrupt_level(wsave
);
1800 * Drop the send right on task port, will fire the
1801 * no-sender notification if exception deliver failed.
1803 ipc_port_release_send(task_port
);
1810 * Terminate the specified task. See comments on thread_terminate
1811 * (kern/thread.c) about problems with terminating the "current task."
1818 if (task
== TASK_NULL
)
1819 return (KERN_INVALID_ARGUMENT
);
1822 return (KERN_FAILURE
);
1824 return (task_terminate_internal(task
));
1828 extern int proc_pid(struct proc
*);
1829 extern void proc_name_kdp(task_t t
, char *buf
, int size
);
1830 #endif /* MACH_ASSERT */
1832 #define VM_MAP_PARTIAL_REAP 0x54 /* 0x150 */
1834 __unused
task_partial_reap(task_t task
, __unused
int pid
)
1836 unsigned int reclaimed_resident
= 0;
1837 unsigned int reclaimed_compressed
= 0;
1838 uint64_t task_page_count
;
1840 task_page_count
= (get_task_phys_footprint(task
) / PAGE_SIZE_64
);
1842 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_START
),
1843 pid
, task_page_count
, 0, 0, 0);
1845 vm_map_partial_reap(task
->map
, &reclaimed_resident
, &reclaimed_compressed
);
1847 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_END
),
1848 pid
, reclaimed_resident
, reclaimed_compressed
, 0, 0);
1852 task_mark_corpse(task_t task
)
1854 kern_return_t kr
= KERN_SUCCESS
;
1855 thread_t self_thread
;
1857 wait_interrupt_t wsave
;
1859 struct label
*crash_label
= NULL
;
1862 assert(task
!= kernel_task
);
1863 assert(task
== current_task());
1864 assert(!task_is_a_corpse(task
));
1867 crash_label
= mac_exc_create_label_for_proc((struct proc
*)task
->bsd_info
);
1870 kr
= task_collect_crash_info(task
,
1875 if (kr
!= KERN_SUCCESS
) {
1879 self_thread
= current_thread();
1881 wsave
= thread_interrupt_level(THREAD_UNINT
);
1884 task_set_corpse_pending_report(task
);
1885 task_set_corpse(task
);
1886 task
->crashed_thread_id
= thread_tid(self_thread
);
1888 kr
= task_start_halt_locked(task
, TRUE
);
1889 assert(kr
== KERN_SUCCESS
);
1891 ipc_task_reset(task
);
1892 /* Remove the naked send right for task port, needed to arm no sender notification */
1893 task_set_special_port(task
, TASK_KERNEL_PORT
, IPC_PORT_NULL
);
1894 ipc_task_enable(task
);
1897 /* terminate the ipc space */
1898 ipc_space_terminate(task
->itk_space
);
1900 /* Add it to global corpse task list */
1901 task_add_to_corpse_task_list(task
);
1903 task_start_halt(task
);
1904 thread_terminate_internal(self_thread
);
1906 (void) thread_interrupt_level(wsave
);
1907 assert(task
->halting
== TRUE
);
1911 mac_exc_free_label(crash_label
);
1919 * Clears the corpse pending bit on task.
1920 * Removes inspection bit on the threads.
1923 task_clear_corpse(task_t task
)
1925 thread_t th_iter
= NULL
;
1928 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1930 thread_mtx_lock(th_iter
);
1931 th_iter
->inspection
= FALSE
;
1932 thread_mtx_unlock(th_iter
);
1935 thread_terminate_crashed_threads();
1936 /* remove the pending corpse report flag */
1937 task_clear_corpse_pending_report(task
);
1945 * Called whenever the Mach port system detects no-senders on
1946 * the task port of a corpse.
1947 * Each notification that comes in should terminate the task (corpse).
1950 task_port_notify(mach_msg_header_t
*msg
)
1952 mach_no_senders_notification_t
*notification
= (void *)msg
;
1953 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
1956 assert(ip_active(port
));
1957 assert(IKOT_TASK
== ip_kotype(port
));
1958 task
= (task_t
) port
->ip_kobject
;
1960 assert(task_is_a_corpse(task
));
1962 /* Remove the task from global corpse task list */
1963 task_remove_from_corpse_task_list(task
);
1965 task_clear_corpse(task
);
1966 task_terminate_internal(task
);
1970 * task_wait_till_threads_terminate_locked
1972 * Wait till all the threads in the task are terminated.
1973 * Might release the task lock and re-acquire it.
1976 task_wait_till_threads_terminate_locked(task_t task
)
1978 /* wait for all the threads in the task to terminate */
1979 while (task
->active_thread_count
!= 0) {
1980 assert_wait((event_t
)&task
->active_thread_count
, THREAD_UNINT
);
1982 thread_block(THREAD_CONTINUE_NULL
);
1989 * task_duplicate_map_and_threads
1991 * Copy vmmap of source task.
1992 * Copy active threads from source task to destination task.
1993 * Source task would be suspended during the copy.
1996 task_duplicate_map_and_threads(
2000 thread_t
*thread_ret
,
2001 uint64_t **udata_buffer
,
2005 kern_return_t kr
= KERN_SUCCESS
;
2007 thread_t thread
, self
, thread_return
= THREAD_NULL
;
2008 thread_t new_thread
= THREAD_NULL
;
2009 thread_t
*thread_array
;
2010 uint32_t active_thread_count
= 0, array_count
= 0, i
;
2012 uint64_t *buffer
= NULL
;
2014 int est_knotes
= 0, num_knotes
= 0;
2016 self
= current_thread();
2019 * Suspend the task to copy thread state, use the internal
2020 * variant so that no user-space process can resume
2021 * the task from under us
2023 kr
= task_suspend_internal(task
);
2024 if (kr
!= KERN_SUCCESS
) {
2028 if (task
->map
->disable_vmentry_reuse
== TRUE
) {
2030 * Quite likely GuardMalloc (or some debugging tool)
2031 * is being used on this task. And it has gone through
2032 * its limit. Making a corpse will likely encounter
2033 * a lot of VM entries that will need COW.
2037 #if DEVELOPMENT || DEBUG
2038 memorystatus_abort_vm_map_fork(task
);
2040 task_resume_internal(task
);
2041 return KERN_FAILURE
;
2044 /* Check with VM if vm_map_fork is allowed for this task */
2045 if (memorystatus_allowed_vm_map_fork(task
)) {
2047 /* Setup new task's vmmap, switch from parent task's map to it COW map */
2048 oldmap
= new_task
->map
;
2049 new_task
->map
= vm_map_fork(new_task
->ledger
,
2051 (VM_MAP_FORK_SHARE_IF_INHERIT_NONE
|
2052 VM_MAP_FORK_PRESERVE_PURGEABLE
));
2053 vm_map_deallocate(oldmap
);
2055 /* Get all the udata pointers from kqueue */
2056 est_knotes
= kevent_proc_copy_uptrs(p
, NULL
, 0);
2057 if (est_knotes
> 0) {
2058 buf_size
= (est_knotes
+ 32) * sizeof(uint64_t);
2059 buffer
= (uint64_t *) kalloc(buf_size
);
2060 num_knotes
= kevent_proc_copy_uptrs(p
, buffer
, buf_size
);
2061 if (num_knotes
> est_knotes
+ 32) {
2062 num_knotes
= est_knotes
+ 32;
2067 active_thread_count
= task
->active_thread_count
;
2068 if (active_thread_count
== 0) {
2069 if (buffer
!= NULL
) {
2070 kfree(buffer
, buf_size
);
2072 task_resume_internal(task
);
2073 return KERN_FAILURE
;
2076 thread_array
= (thread_t
*) kalloc(sizeof(thread_t
) * active_thread_count
);
2078 /* Iterate all the threads and drop the task lock before calling thread_create_with_continuation */
2080 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2081 /* Skip inactive threads */
2082 active
= thread
->active
;
2087 if (array_count
>= active_thread_count
) {
2091 thread_array
[array_count
++] = thread
;
2092 thread_reference(thread
);
2096 for (i
= 0; i
< array_count
; i
++) {
2098 kr
= thread_create_with_continuation(new_task
, &new_thread
, (thread_continue_t
)thread_corpse_continue
);
2099 if (kr
!= KERN_SUCCESS
) {
2103 /* Equivalent of current thread in corpse */
2104 if (thread_array
[i
] == self
) {
2105 thread_return
= new_thread
;
2106 new_task
->crashed_thread_id
= thread_tid(new_thread
);
2108 /* drop the extra ref returned by thread_create_with_continuation */
2109 thread_deallocate(new_thread
);
2112 kr
= thread_dup2(thread_array
[i
], new_thread
);
2113 if (kr
!= KERN_SUCCESS
) {
2114 thread_mtx_lock(new_thread
);
2115 new_thread
->corpse_dup
= TRUE
;
2116 thread_mtx_unlock(new_thread
);
2120 /* Copy thread name */
2121 bsd_copythreadname(new_thread
->uthread
, thread_array
[i
]->uthread
);
2122 thread_copy_resource_info(new_thread
, thread_array
[i
]);
2125 task_resume_internal(task
);
2127 for (i
= 0; i
< array_count
; i
++) {
2128 thread_deallocate(thread_array
[i
]);
2130 kfree(thread_array
, sizeof(thread_t
) * active_thread_count
);
2132 if (kr
== KERN_SUCCESS
) {
2133 *thread_ret
= thread_return
;
2134 *udata_buffer
= buffer
;
2136 *num_udata
= num_knotes
;
2138 if (thread_return
!= THREAD_NULL
) {
2139 thread_deallocate(thread_return
);
2141 if (buffer
!= NULL
) {
2142 kfree(buffer
, buf_size
);
2149 #if CONFIG_SECLUDED_MEMORY
2150 extern void task_set_can_use_secluded_mem_locked(
2152 boolean_t can_use_secluded_mem
);
2153 #endif /* CONFIG_SECLUDED_MEMORY */
2156 task_terminate_internal(
2159 thread_t thread
, self
;
2161 boolean_t interrupt_save
;
2164 assert(task
!= kernel_task
);
2166 self
= current_thread();
2167 self_task
= self
->task
;
2170 * Get the task locked and make sure that we are not racing
2171 * with someone else trying to terminate us.
2173 if (task
== self_task
)
2176 if (task
< self_task
) {
2178 task_lock(self_task
);
2181 task_lock(self_task
);
2185 #if CONFIG_SECLUDED_MEMORY
2186 if (task
->task_can_use_secluded_mem
) {
2187 task_set_can_use_secluded_mem_locked(task
, FALSE
);
2189 task
->task_could_use_secluded_mem
= FALSE
;
2190 task
->task_could_also_use_secluded_mem
= FALSE
;
2191 #endif /* CONFIG_SECLUDED_MEMORY */
2193 if (!task
->active
) {
2195 * Task is already being terminated.
2196 * Just return an error. If we are dying, this will
2197 * just get us to our AST special handler and that
2198 * will get us to finalize the termination of ourselves.
2201 if (self_task
!= task
)
2202 task_unlock(self_task
);
2204 return (KERN_FAILURE
);
2207 if (task_corpse_pending_report(task
)) {
2209 * Task is marked for reporting as corpse.
2210 * Just return an error. This will
2211 * just get us to our AST special handler and that
2212 * will get us to finish the path to death
2215 if (self_task
!= task
)
2216 task_unlock(self_task
);
2218 return (KERN_FAILURE
);
2221 if (self_task
!= task
)
2222 task_unlock(self_task
);
2225 * Make sure the current thread does not get aborted out of
2226 * the waits inside these operations.
2228 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
2231 * Indicate that we want all the threads to stop executing
2232 * at user space by holding the task (we would have held
2233 * each thread independently in thread_terminate_internal -
2234 * but this way we may be more likely to already find it
2235 * held there). Mark the task inactive, and prevent
2236 * further task operations via the task port.
2238 task_hold_locked(task
);
2239 task
->active
= FALSE
;
2240 ipc_task_disable(task
);
2242 #if CONFIG_TELEMETRY
2244 * Notify telemetry that this task is going away.
2246 telemetry_task_ctl_locked(task
, TF_TELEMETRY
, 0);
2250 * Terminate each thread in the task.
2252 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2253 thread_terminate_internal(thread
);
2257 if (task
->bsd_info
!= NULL
&& !task_is_exec_copy(task
)) {
2258 pid
= proc_pid(task
->bsd_info
);
2260 #endif /* MACH_BSD */
2264 proc_set_task_policy(task
, TASK_POLICY_ATTRIBUTE
,
2265 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
2267 /* Early object reap phase */
2269 // PR-17045188: Revisit implementation
2270 // task_partial_reap(task, pid);
2274 * remove all task watchers
2276 task_removewatchers(task
);
2278 #endif /* CONFIG_EMBEDDED */
2281 * Destroy all synchronizers owned by the task.
2283 task_synchronizer_destroy_all(task
);
2286 * Destroy the IPC space, leaving just a reference for it.
2288 ipc_space_terminate(task
->itk_space
);
2291 /* if some ledgers go negative on tear-down again... */
2292 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2293 task_ledgers
.phys_footprint
);
2294 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2295 task_ledgers
.internal
);
2296 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2297 task_ledgers
.internal_compressed
);
2298 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2299 task_ledgers
.iokit_mapped
);
2300 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2301 task_ledgers
.alternate_accounting
);
2302 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2303 task_ledgers
.alternate_accounting_compressed
);
2307 * If the current thread is a member of the task
2308 * being terminated, then the last reference to
2309 * the task will not be dropped until the thread
2310 * is finally reaped. To avoid incurring the
2311 * expense of removing the address space regions
2312 * at reap time, we do it explictly here.
2315 vm_map_lock(task
->map
);
2316 vm_map_disable_hole_optimization(task
->map
);
2317 vm_map_unlock(task
->map
);
2321 * Identify the pmap's process, in case the pmap ledgers drift
2322 * and we have to report it.
2325 if (task
->bsd_info
&& !task_is_exec_copy(task
)) {
2326 pid
= proc_pid(task
->bsd_info
);
2327 proc_name_kdp(task
, procname
, sizeof (procname
));
2330 strlcpy(procname
, "<unknown>", sizeof (procname
));
2332 pmap_set_process(task
->map
->pmap
, pid
, procname
);
2333 #endif /* MACH_ASSERT */
2335 vm_map_remove(task
->map
,
2336 task
->map
->min_offset
,
2337 task
->map
->max_offset
,
2341 * + remove immutable mappings
2343 (VM_MAP_REMOVE_NO_UNNESTING
|
2344 VM_MAP_REMOVE_IMMUTABLE
));
2346 /* release our shared region */
2347 vm_shared_region_set(task
, NULL
);
2350 lck_mtx_lock(&tasks_threads_lock
);
2351 queue_remove(&tasks
, task
, task_t
, tasks
);
2352 queue_enter(&terminated_tasks
, task
, task_t
, tasks
);
2354 terminated_tasks_count
++;
2355 lck_mtx_unlock(&tasks_threads_lock
);
2358 * We no longer need to guard against being aborted, so restore
2359 * the previous interruptible state.
2361 thread_interrupt_level(interrupt_save
);
2364 /* force the task to release all ctrs */
2365 if (task
->t_kpc
& TASK_KPC_FORCED_ALL_CTRS
)
2366 kpc_force_all_ctrs(task
, 0);
2369 #if CONFIG_COALITIONS
2371 * Leave our coalitions. (drop activation but not reference)
2373 coalitions_remove_task(task
);
2377 * Get rid of the task active reference on itself.
2379 task_deallocate(task
);
2381 return (KERN_SUCCESS
);
2385 tasks_system_suspend(boolean_t suspend
)
2389 lck_mtx_lock(&tasks_threads_lock
);
2390 assert(tasks_suspend_state
!= suspend
);
2391 tasks_suspend_state
= suspend
;
2392 queue_iterate(&tasks
, task
, task_t
, tasks
) {
2393 if (task
== kernel_task
) {
2396 suspend
? task_suspend_internal(task
) : task_resume_internal(task
);
2398 lck_mtx_unlock(&tasks_threads_lock
);
2404 * Shut the current task down (except for the current thread) in
2405 * preparation for dramatic changes to the task (probably exec).
2406 * We hold the task and mark all other threads in the task for
2410 task_start_halt(task_t task
)
2412 kern_return_t kr
= KERN_SUCCESS
;
2414 kr
= task_start_halt_locked(task
, FALSE
);
2419 static kern_return_t
2420 task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
)
2422 thread_t thread
, self
;
2423 uint64_t dispatchqueue_offset
;
2425 assert(task
!= kernel_task
);
2427 self
= current_thread();
2429 if (task
!= self
->task
&& !task_is_a_corpse_fork(task
))
2430 return (KERN_INVALID_ARGUMENT
);
2432 if (task
->halting
|| !task
->active
|| !self
->active
) {
2434 * Task or current thread is already being terminated.
2435 * Hurry up and return out of the current kernel context
2436 * so that we run our AST special handler to terminate
2439 return (KERN_FAILURE
);
2442 task
->halting
= TRUE
;
2445 * Mark all the threads to keep them from starting any more
2446 * user-level execution. The thread_terminate_internal code
2447 * would do this on a thread by thread basis anyway, but this
2448 * gives us a better chance of not having to wait there.
2450 task_hold_locked(task
);
2451 dispatchqueue_offset
= get_dispatchqueue_offset_from_proc(task
->bsd_info
);
2454 * Terminate all the other threads in the task.
2456 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
)
2458 if (should_mark_corpse
) {
2459 thread_mtx_lock(thread
);
2460 thread
->inspection
= TRUE
;
2461 thread_mtx_unlock(thread
);
2464 thread_terminate_internal(thread
);
2466 task
->dispatchqueue_offset
= dispatchqueue_offset
;
2468 task_release_locked(task
);
2470 return KERN_SUCCESS
;
2475 * task_complete_halt:
2477 * Complete task halt by waiting for threads to terminate, then clean
2478 * up task resources (VM, port namespace, etc...) and then let the
2479 * current thread go in the (practically empty) task context.
2481 * Note: task->halting flag is not cleared in order to avoid creation
2482 * of new thread in old exec'ed task.
2485 task_complete_halt(task_t task
)
2488 assert(task
->halting
);
2489 assert(task
== current_task());
2492 * Wait for the other threads to get shut down.
2493 * When the last other thread is reaped, we'll be
2496 if (task
->thread_count
> 1) {
2497 assert_wait((event_t
)&task
->halting
, THREAD_UNINT
);
2499 thread_block(THREAD_CONTINUE_NULL
);
2505 * Give the machine dependent code a chance
2506 * to perform cleanup of task-level resources
2507 * associated with the current thread before
2508 * ripping apart the task.
2510 machine_task_terminate(task
);
2513 * Destroy all synchronizers owned by the task.
2515 task_synchronizer_destroy_all(task
);
2518 * Destroy the contents of the IPC space, leaving just
2519 * a reference for it.
2521 ipc_space_clean(task
->itk_space
);
2524 * Clean out the address space, as we are going to be
2525 * getting a new one.
2527 vm_map_remove(task
->map
, task
->map
->min_offset
,
2528 task
->map
->max_offset
,
2532 * + remove immutable mappings
2534 (VM_MAP_REMOVE_NO_UNNESTING
|
2535 VM_MAP_REMOVE_IMMUTABLE
));
2538 * Kick out any IOKitUser handles to the task. At best they're stale,
2539 * at worst someone is racing a SUID exec.
2541 iokit_task_terminate(task
);
2547 * Suspend execution of the specified task.
2548 * This is a recursive-style suspension of the task, a count of
2549 * suspends is maintained.
2551 * CONDITIONS: the task is locked and active.
2559 assert(task
->active
);
2561 if (task
->suspend_count
++ > 0)
2565 * Iterate through all the threads and hold them.
2567 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2568 thread_mtx_lock(thread
);
2569 thread_hold(thread
);
2570 thread_mtx_unlock(thread
);
2577 * Same as the internal routine above, except that is must lock
2578 * and verify that the task is active. This differs from task_suspend
2579 * in that it places a kernel hold on the task rather than just a
2580 * user-level hold. This keeps users from over resuming and setting
2581 * it running out from under the kernel.
2583 * CONDITIONS: the caller holds a reference on the task
2589 if (task
== TASK_NULL
)
2590 return (KERN_INVALID_ARGUMENT
);
2594 if (!task
->active
) {
2597 return (KERN_FAILURE
);
2600 task_hold_locked(task
);
2603 return (KERN_SUCCESS
);
2609 boolean_t until_not_runnable
)
2611 if (task
== TASK_NULL
)
2612 return (KERN_INVALID_ARGUMENT
);
2616 if (!task
->active
) {
2619 return (KERN_FAILURE
);
2622 task_wait_locked(task
, until_not_runnable
);
2625 return (KERN_SUCCESS
);
2631 * Wait for all threads in task to stop.
2634 * Called with task locked, active, and held.
2639 boolean_t until_not_runnable
)
2641 thread_t thread
, self
;
2643 assert(task
->active
);
2644 assert(task
->suspend_count
> 0);
2646 self
= current_thread();
2649 * Iterate through all the threads and wait for them to
2650 * stop. Do not wait for the current thread if it is within
2653 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2655 thread_wait(thread
, until_not_runnable
);
2660 * task_release_locked:
2662 * Release a kernel hold on a task.
2664 * CONDITIONS: the task is locked and active
2667 task_release_locked(
2672 assert(task
->active
);
2673 assert(task
->suspend_count
> 0);
2675 if (--task
->suspend_count
> 0)
2678 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2679 thread_mtx_lock(thread
);
2680 thread_release(thread
);
2681 thread_mtx_unlock(thread
);
2688 * Same as the internal routine above, except that it must lock
2689 * and verify that the task is active.
2691 * CONDITIONS: The caller holds a reference to the task
2697 if (task
== TASK_NULL
)
2698 return (KERN_INVALID_ARGUMENT
);
2702 if (!task
->active
) {
2705 return (KERN_FAILURE
);
2708 task_release_locked(task
);
2711 return (KERN_SUCCESS
);
2717 thread_act_array_t
*threads_out
,
2718 mach_msg_type_number_t
*count
)
2720 mach_msg_type_number_t actual
;
2721 thread_t
*thread_list
;
2723 vm_size_t size
, size_needed
;
2727 if (task
== TASK_NULL
)
2728 return (KERN_INVALID_ARGUMENT
);
2730 size
= 0; addr
= NULL
;
2734 if (!task
->active
) {
2740 return (KERN_FAILURE
);
2743 actual
= task
->thread_count
;
2745 /* do we have the memory we need? */
2746 size_needed
= actual
* sizeof (mach_port_t
);
2747 if (size_needed
<= size
)
2750 /* unlock the task and allocate more memory */
2756 assert(size_needed
> 0);
2759 addr
= kalloc(size
);
2761 return (KERN_RESOURCE_SHORTAGE
);
2764 /* OK, have memory and the task is locked & active */
2765 thread_list
= (thread_t
*)addr
;
2769 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
2770 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
2771 thread_reference_internal(thread
);
2772 thread_list
[j
++] = thread
;
2775 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
2778 size_needed
= actual
* sizeof (mach_port_t
);
2780 /* can unlock task now that we've got the thread refs */
2784 /* no threads, so return null pointer and deallocate memory */
2786 *threads_out
= NULL
;
2793 /* if we allocated too much, must copy */
2795 if (size_needed
< size
) {
2798 newaddr
= kalloc(size_needed
);
2800 for (i
= 0; i
< actual
; ++i
)
2801 thread_deallocate(thread_list
[i
]);
2803 return (KERN_RESOURCE_SHORTAGE
);
2806 bcopy(addr
, newaddr
, size_needed
);
2808 thread_list
= (thread_t
*)newaddr
;
2811 *threads_out
= thread_list
;
2814 /* do the conversion that Mig should handle */
2816 for (i
= 0; i
< actual
; ++i
)
2817 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
2820 return (KERN_SUCCESS
);
2823 #define TASK_HOLD_NORMAL 0
2824 #define TASK_HOLD_PIDSUSPEND 1
2825 #define TASK_HOLD_LEGACY 2
2826 #define TASK_HOLD_LEGACY_ALL 3
2828 static kern_return_t
2833 if (!task
->active
&& !task_is_a_corpse(task
)) {
2834 return (KERN_FAILURE
);
2837 /* Return success for corpse task */
2838 if (task_is_a_corpse(task
)) {
2839 return KERN_SUCCESS
;
2842 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2843 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_SUSPEND
) | DBG_FUNC_NONE
,
2844 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2845 task
->user_stop_count
, task
->user_stop_count
+ 1, 0);
2848 current_task()->suspends_outstanding
++;
2851 if (mode
== TASK_HOLD_LEGACY
)
2852 task
->legacy_stop_count
++;
2854 if (task
->user_stop_count
++ > 0) {
2856 * If the stop count was positive, the task is
2857 * already stopped and we can exit.
2859 return (KERN_SUCCESS
);
2863 * Put a kernel-level hold on the threads in the task (all
2864 * user-level task suspensions added together represent a
2865 * single kernel-level hold). We then wait for the threads
2866 * to stop executing user code.
2868 task_hold_locked(task
);
2869 task_wait_locked(task
, FALSE
);
2871 return (KERN_SUCCESS
);
2874 static kern_return_t
2879 boolean_t release
= FALSE
;
2881 if (!task
->active
&& !task_is_a_corpse(task
)) {
2882 return (KERN_FAILURE
);
2885 /* Return success for corpse task */
2886 if (task_is_a_corpse(task
)) {
2887 return KERN_SUCCESS
;
2890 if (mode
== TASK_HOLD_PIDSUSPEND
) {
2891 if (task
->pidsuspended
== FALSE
) {
2892 return (KERN_FAILURE
);
2894 task
->pidsuspended
= FALSE
;
2897 if (task
->user_stop_count
> (task
->pidsuspended
? 1 : 0)) {
2899 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2900 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_RESUME
) | DBG_FUNC_NONE
,
2901 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2902 task
->user_stop_count
, mode
, task
->legacy_stop_count
);
2906 * This is obviously not robust; if we suspend one task and then resume a different one,
2907 * we'll fly under the radar. This is only meant to catch the common case of a crashed
2908 * or buggy suspender.
2910 current_task()->suspends_outstanding
--;
2913 if (mode
== TASK_HOLD_LEGACY_ALL
) {
2914 if (task
->legacy_stop_count
>= task
->user_stop_count
) {
2915 task
->user_stop_count
= 0;
2918 task
->user_stop_count
-= task
->legacy_stop_count
;
2920 task
->legacy_stop_count
= 0;
2922 if (mode
== TASK_HOLD_LEGACY
&& task
->legacy_stop_count
> 0)
2923 task
->legacy_stop_count
--;
2924 if (--task
->user_stop_count
== 0)
2929 return (KERN_FAILURE
);
2933 * Release the task if necessary.
2936 task_release_locked(task
);
2938 return (KERN_SUCCESS
);
2945 * Implement an (old-fashioned) user-level suspension on a task.
2947 * Because the user isn't expecting to have to manage a suspension
2948 * token, we'll track it for him in the kernel in the form of a naked
2949 * send right to the task's resume port. All such send rights
2950 * account for a single suspension against the task (unlike task_suspend2()
2951 * where each caller gets a unique suspension count represented by a
2952 * unique send-once right).
2955 * The caller holds a reference to the task
2962 mach_port_t port
, send
, old_notify
;
2963 mach_port_name_t name
;
2965 if (task
== TASK_NULL
|| task
== kernel_task
)
2966 return (KERN_INVALID_ARGUMENT
);
2971 * Claim a send right on the task resume port, and request a no-senders
2972 * notification on that port (if none outstanding).
2974 if (task
->itk_resume
== IP_NULL
) {
2975 task
->itk_resume
= ipc_port_alloc_kernel();
2976 if (!IP_VALID(task
->itk_resume
))
2977 panic("failed to create resume port");
2978 ipc_kobject_set(task
->itk_resume
, (ipc_kobject_t
)task
, IKOT_TASK_RESUME
);
2981 port
= task
->itk_resume
;
2983 assert(ip_active(port
));
2985 send
= ipc_port_make_send_locked(port
);
2986 assert(IP_VALID(send
));
2988 if (port
->ip_nsrequest
== IP_NULL
) {
2989 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
2990 assert(old_notify
== IP_NULL
);
2997 * place a legacy hold on the task.
2999 kr
= place_task_hold(task
, TASK_HOLD_LEGACY
);
3000 if (kr
!= KERN_SUCCESS
) {
3002 ipc_port_release_send(send
);
3009 * Copyout the send right into the calling task's IPC space. It won't know it is there,
3010 * but we'll look it up when calling a traditional resume. Any IPC operations that
3011 * deallocate the send right will auto-release the suspension.
3013 if ((kr
= ipc_kmsg_copyout_object(current_task()->itk_space
, (ipc_object_t
)send
,
3014 MACH_MSG_TYPE_MOVE_SEND
, &name
)) != KERN_SUCCESS
) {
3015 printf("warning: %s(%d) failed to copyout suspension token for pid %d with error: %d\n",
3016 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
3017 task_pid(task
), kr
);
3026 * Release a user hold on a task.
3029 * The caller holds a reference to the task
3036 mach_port_name_t resume_port_name
;
3037 ipc_entry_t resume_port_entry
;
3038 ipc_space_t space
= current_task()->itk_space
;
3040 if (task
== TASK_NULL
|| task
== kernel_task
)
3041 return (KERN_INVALID_ARGUMENT
);
3043 /* release a legacy task hold */
3045 kr
= release_task_hold(task
, TASK_HOLD_LEGACY
);
3048 is_write_lock(space
);
3049 if (is_active(space
) && IP_VALID(task
->itk_resume
) &&
3050 ipc_hash_lookup(space
, (ipc_object_t
)task
->itk_resume
, &resume_port_name
, &resume_port_entry
) == TRUE
) {
3052 * We found a suspension token in the caller's IPC space. Release a send right to indicate that
3053 * we are holding one less legacy hold on the task from this caller. If the release failed,
3054 * go ahead and drop all the rights, as someone either already released our holds or the task
3057 if (kr
== KERN_SUCCESS
)
3058 ipc_right_dealloc(space
, resume_port_name
, resume_port_entry
);
3060 ipc_right_destroy(space
, resume_port_name
, resume_port_entry
, FALSE
, 0);
3061 /* space unlocked */
3063 is_write_unlock(space
);
3064 if (kr
== KERN_SUCCESS
)
3065 printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
3066 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
3074 * Suspend the target task.
3075 * Making/holding a token/reference/port is the callers responsibility.
3078 task_suspend_internal(task_t task
)
3082 if (task
== TASK_NULL
|| task
== kernel_task
)
3083 return (KERN_INVALID_ARGUMENT
);
3086 kr
= place_task_hold(task
, TASK_HOLD_NORMAL
);
3092 * Suspend the target task, and return a suspension token. The token
3093 * represents a reference on the suspended task.
3098 task_suspension_token_t
*suspend_token
)
3102 kr
= task_suspend_internal(task
);
3103 if (kr
!= KERN_SUCCESS
) {
3104 *suspend_token
= TASK_NULL
;
3109 * Take a reference on the target task and return that to the caller
3110 * as a "suspension token," which can be converted into an SO right to
3111 * the now-suspended task's resume port.
3113 task_reference_internal(task
);
3114 *suspend_token
= task
;
3116 return (KERN_SUCCESS
);
3121 * (reference/token/port management is caller's responsibility).
3124 task_resume_internal(
3125 task_suspension_token_t task
)
3129 if (task
== TASK_NULL
|| task
== kernel_task
)
3130 return (KERN_INVALID_ARGUMENT
);
3133 kr
= release_task_hold(task
, TASK_HOLD_NORMAL
);
3139 * Resume the task using a suspension token. Consumes the token's ref.
3143 task_suspension_token_t task
)
3147 kr
= task_resume_internal(task
);
3148 task_suspension_token_deallocate(task
);
3154 task_suspension_notify(mach_msg_header_t
*request_header
)
3156 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
3157 task_t task
= convert_port_to_task_suspension_token(port
);
3158 mach_msg_type_number_t not_count
;
3160 if (task
== TASK_NULL
|| task
== kernel_task
)
3161 return TRUE
; /* nothing to do */
3163 switch (request_header
->msgh_id
) {
3165 case MACH_NOTIFY_SEND_ONCE
:
3166 /* release the hold held by this specific send-once right */
3168 release_task_hold(task
, TASK_HOLD_NORMAL
);
3172 case MACH_NOTIFY_NO_SENDERS
:
3173 not_count
= ((mach_no_senders_notification_t
*)request_header
)->not_count
;
3177 if (port
->ip_mscount
== not_count
) {
3179 /* release all the [remaining] outstanding legacy holds */
3180 assert(port
->ip_nsrequest
== IP_NULL
);
3182 release_task_hold(task
, TASK_HOLD_LEGACY_ALL
);
3185 } else if (port
->ip_nsrequest
== IP_NULL
) {
3186 ipc_port_t old_notify
;
3189 /* new send rights, re-arm notification at current make-send count */
3190 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
3191 assert(old_notify
== IP_NULL
);
3203 task_suspension_token_deallocate(task
); /* drop token reference */
3208 task_pidsuspend_locked(task_t task
)
3212 if (task
->pidsuspended
) {
3217 task
->pidsuspended
= TRUE
;
3219 kr
= place_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3220 if (kr
!= KERN_SUCCESS
) {
3221 task
->pidsuspended
= FALSE
;
3231 * Suspends a task by placing a hold on its threads.
3234 * The caller holds a reference to the task
3242 if (task
== TASK_NULL
|| task
== kernel_task
)
3243 return (KERN_INVALID_ARGUMENT
);
3247 kr
= task_pidsuspend_locked(task
);
3256 * Resumes a previously suspended task.
3259 * The caller holds a reference to the task
3267 if (task
== TASK_NULL
|| task
== kernel_task
)
3268 return (KERN_INVALID_ARGUMENT
);
3274 while (task
->changing_freeze_state
) {
3276 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3278 thread_block(THREAD_CONTINUE_NULL
);
3282 task
->changing_freeze_state
= TRUE
;
3285 kr
= release_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3293 if (kr
== KERN_SUCCESS
)
3294 task
->frozen
= FALSE
;
3295 task
->changing_freeze_state
= FALSE
;
3296 thread_wakeup(&task
->changing_freeze_state
);
3305 #if DEVELOPMENT || DEBUG
3307 extern void IOSleep(int);
3310 task_disconnect_page_mappings(task_t task
)
3314 if (task
== TASK_NULL
|| task
== kernel_task
)
3315 return (KERN_INVALID_ARGUMENT
);
3318 * this function is used to strip all of the mappings from
3319 * the pmap for the specified task to force the task to
3320 * re-fault all of the pages it is actively using... this
3321 * allows us to approximate the true working set of the
3322 * specified task. We only engage if at least 1 of the
3323 * threads in the task is runnable, but we want to continuously
3324 * sweep (at least for a while - I've arbitrarily set the limit at
3325 * 100 sweeps to be re-looked at as we gain experience) to get a better
3326 * view into what areas within a page are being visited (as opposed to only
3327 * seeing the first fault of a page after the task becomes
3328 * runnable)... in the future I may
3329 * try to block until awakened by a thread in this task
3330 * being made runnable, but for now we'll periodically poll from the
3331 * user level debug tool driving the sysctl
3333 for (n
= 0; n
< 100; n
++) {
3336 boolean_t do_unnest
;
3344 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3346 if (thread
->state
& TH_RUN
) {
3352 task
->task_disconnected_count
++;
3354 if (task
->task_unnested
== FALSE
) {
3355 if (runnable
== TRUE
) {
3356 task
->task_unnested
= TRUE
;
3362 if (runnable
== FALSE
)
3365 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_START
,
3366 task
, do_unnest
, task
->task_disconnected_count
, 0, 0);
3368 page_count
= vm_map_disconnect_page_mappings(task
->map
, do_unnest
);
3370 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_END
,
3371 task
, page_count
, 0, 0, 0);
3376 return (KERN_SUCCESS
);
3390 * The caller holds a reference to the task
3392 extern void vm_wake_compactor_swapper(void);
3393 extern queue_head_t c_swapout_list_head
;
3398 uint32_t *purgeable_count
,
3399 uint32_t *wired_count
,
3400 uint32_t *clean_count
,
3401 uint32_t *dirty_count
,
3402 uint32_t dirty_budget
,
3404 boolean_t walk_only
)
3406 kern_return_t kr
= KERN_SUCCESS
;
3408 if (task
== TASK_NULL
|| task
== kernel_task
)
3409 return (KERN_INVALID_ARGUMENT
);
3413 while (task
->changing_freeze_state
) {
3415 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3417 thread_block(THREAD_CONTINUE_NULL
);
3423 return (KERN_FAILURE
);
3425 task
->changing_freeze_state
= TRUE
;
3430 panic("task_freeze - walk_only == TRUE");
3432 kr
= vm_map_freeze(task
->map
, purgeable_count
, wired_count
, clean_count
, dirty_count
, dirty_budget
, shared
);
3437 if (walk_only
== FALSE
&& kr
== KERN_SUCCESS
)
3438 task
->frozen
= TRUE
;
3439 task
->changing_freeze_state
= FALSE
;
3440 thread_wakeup(&task
->changing_freeze_state
);
3444 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
) {
3445 vm_wake_compactor_swapper();
3447 * We do an explicit wakeup of the swapout thread here
3448 * because the compact_and_swap routines don't have
3449 * knowledge about these kind of "per-task packed c_segs"
3450 * and so will not be evaluating whether we need to do
3453 thread_wakeup((event_t
)&c_swapout_list_head
);
3462 * Thaw a currently frozen task.
3465 * The caller holds a reference to the task
3471 if (task
== TASK_NULL
|| task
== kernel_task
)
3472 return (KERN_INVALID_ARGUMENT
);
3476 while (task
->changing_freeze_state
) {
3478 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3480 thread_block(THREAD_CONTINUE_NULL
);
3484 if (!task
->frozen
) {
3486 return (KERN_FAILURE
);
3488 task
->frozen
= FALSE
;
3492 return (KERN_SUCCESS
);
3495 #endif /* CONFIG_FREEZE */
3498 host_security_set_task_token(
3499 host_security_t host_security
,
3501 security_token_t sec_token
,
3502 audit_token_t audit_token
,
3503 host_priv_t host_priv
)
3505 ipc_port_t host_port
;
3508 if (task
== TASK_NULL
)
3509 return(KERN_INVALID_ARGUMENT
);
3511 if (host_security
== HOST_NULL
)
3512 return(KERN_INVALID_SECURITY
);
3515 task
->sec_token
= sec_token
;
3516 task
->audit_token
= audit_token
;
3520 if (host_priv
!= HOST_PRIV_NULL
) {
3521 kr
= host_get_host_priv_port(host_priv
, &host_port
);
3523 kr
= host_get_host_port(host_priv_self(), &host_port
);
3525 assert(kr
== KERN_SUCCESS
);
3526 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
3531 task_send_trace_memory(
3533 __unused
uint32_t pid
,
3534 __unused
uint64_t uniqueid
)
3536 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
3537 if (target_task
== TASK_NULL
)
3538 return (KERN_INVALID_ARGUMENT
);
3541 kr
= atm_send_proc_inspect_notification(target_task
,
3549 * This routine was added, pretty much exclusively, for registering the
3550 * RPC glue vector for in-kernel short circuited tasks. Rather than
3551 * removing it completely, I have only disabled that feature (which was
3552 * the only feature at the time). It just appears that we are going to
3553 * want to add some user data to tasks in the future (i.e. bsd info,
3554 * task names, etc...), so I left it in the formal task interface.
3559 task_flavor_t flavor
,
3560 __unused task_info_t task_info_in
, /* pointer to IN array */
3561 __unused mach_msg_type_number_t task_info_count
)
3563 if (task
== TASK_NULL
)
3564 return(KERN_INVALID_ARGUMENT
);
3569 case TASK_TRACE_MEMORY_INFO
:
3571 if (task_info_count
!= TASK_TRACE_MEMORY_INFO_COUNT
)
3572 return (KERN_INVALID_ARGUMENT
);
3574 assert(task_info_in
!= NULL
);
3575 task_trace_memory_info_t mem_info
;
3576 mem_info
= (task_trace_memory_info_t
) task_info_in
;
3577 kern_return_t kr
= atm_register_trace_memory(task
,
3578 mem_info
->user_memory_address
,
3579 mem_info
->buffer_size
);
3585 return (KERN_INVALID_ARGUMENT
);
3587 return (KERN_SUCCESS
);
3590 int radar_20146450
= 1;
3594 task_flavor_t flavor
,
3595 task_info_t task_info_out
,
3596 mach_msg_type_number_t
*task_info_count
)
3598 kern_return_t error
= KERN_SUCCESS
;
3599 mach_msg_type_number_t original_task_info_count
;
3601 if (task
== TASK_NULL
)
3602 return (KERN_INVALID_ARGUMENT
);
3604 original_task_info_count
= *task_info_count
;
3607 if ((task
!= current_task()) && (!task
->active
)) {
3609 return (KERN_INVALID_ARGUMENT
);
3614 case TASK_BASIC_INFO_32
:
3615 case TASK_BASIC2_INFO_32
:
3616 #if defined(__arm__) || defined(__arm64__)
3617 case TASK_BASIC_INFO_64
:
3620 task_basic_info_32_t basic_info
;
3625 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
) {
3626 error
= KERN_INVALID_ARGUMENT
;
3630 basic_info
= (task_basic_info_32_t
)task_info_out
;
3632 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3633 basic_info
->virtual_size
= (typeof(basic_info
->virtual_size
))map
->size
;
3634 if (flavor
== TASK_BASIC2_INFO_32
) {
3636 * The "BASIC2" flavor gets the maximum resident
3637 * size instead of the current resident size...
3639 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
3641 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
3643 basic_info
->resident_size
*= PAGE_SIZE
;
3645 basic_info
->policy
= ((task
!= kernel_task
)?
3646 POLICY_TIMESHARE
: POLICY_RR
);
3647 basic_info
->suspend_count
= task
->user_stop_count
;
3649 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3650 basic_info
->user_time
.seconds
=
3651 (typeof(basic_info
->user_time
.seconds
))secs
;
3652 basic_info
->user_time
.microseconds
= usecs
;
3654 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3655 basic_info
->system_time
.seconds
=
3656 (typeof(basic_info
->system_time
.seconds
))secs
;
3657 basic_info
->system_time
.microseconds
= usecs
;
3659 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
3663 #if defined(__arm__) || defined(__arm64__)
3664 case TASK_BASIC_INFO_64_2
:
3666 task_basic_info_64_2_t basic_info
;
3671 if (*task_info_count
< TASK_BASIC_INFO_64_2_COUNT
) {
3672 error
= KERN_INVALID_ARGUMENT
;
3676 basic_info
= (task_basic_info_64_2_t
)task_info_out
;
3678 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3679 basic_info
->virtual_size
= map
->size
;
3680 basic_info
->resident_size
=
3681 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
3684 basic_info
->policy
= ((task
!= kernel_task
)?
3685 POLICY_TIMESHARE
: POLICY_RR
);
3686 basic_info
->suspend_count
= task
->user_stop_count
;
3688 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3689 basic_info
->user_time
.seconds
=
3690 (typeof(basic_info
->user_time
.seconds
))secs
;
3691 basic_info
->user_time
.microseconds
= usecs
;
3693 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3694 basic_info
->system_time
.seconds
=
3695 (typeof(basic_info
->system_time
.seconds
))secs
;
3696 basic_info
->system_time
.microseconds
= usecs
;
3698 *task_info_count
= TASK_BASIC_INFO_64_2_COUNT
;
3702 #else /* defined(__arm__) || defined(__arm64__) */
3703 case TASK_BASIC_INFO_64
:
3705 task_basic_info_64_t basic_info
;
3710 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
) {
3711 error
= KERN_INVALID_ARGUMENT
;
3715 basic_info
= (task_basic_info_64_t
)task_info_out
;
3717 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3718 basic_info
->virtual_size
= map
->size
;
3719 basic_info
->resident_size
=
3720 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
3723 basic_info
->policy
= ((task
!= kernel_task
)?
3724 POLICY_TIMESHARE
: POLICY_RR
);
3725 basic_info
->suspend_count
= task
->user_stop_count
;
3727 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3728 basic_info
->user_time
.seconds
=
3729 (typeof(basic_info
->user_time
.seconds
))secs
;
3730 basic_info
->user_time
.microseconds
= usecs
;
3732 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3733 basic_info
->system_time
.seconds
=
3734 (typeof(basic_info
->system_time
.seconds
))secs
;
3735 basic_info
->system_time
.microseconds
= usecs
;
3737 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
3740 #endif /* defined(__arm__) || defined(__arm64__) */
3742 case MACH_TASK_BASIC_INFO
:
3744 mach_task_basic_info_t basic_info
;
3749 if (*task_info_count
< MACH_TASK_BASIC_INFO_COUNT
) {
3750 error
= KERN_INVALID_ARGUMENT
;
3754 basic_info
= (mach_task_basic_info_t
)task_info_out
;
3756 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
3758 basic_info
->virtual_size
= map
->size
;
3760 basic_info
->resident_size
=
3761 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
));
3762 basic_info
->resident_size
*= PAGE_SIZE_64
;
3764 basic_info
->resident_size_max
=
3765 (mach_vm_size_t
)(pmap_resident_max(map
->pmap
));
3766 basic_info
->resident_size_max
*= PAGE_SIZE_64
;
3768 basic_info
->policy
= ((task
!= kernel_task
) ?
3769 POLICY_TIMESHARE
: POLICY_RR
);
3771 basic_info
->suspend_count
= task
->user_stop_count
;
3773 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3774 basic_info
->user_time
.seconds
=
3775 (typeof(basic_info
->user_time
.seconds
))secs
;
3776 basic_info
->user_time
.microseconds
= usecs
;
3778 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3779 basic_info
->system_time
.seconds
=
3780 (typeof(basic_info
->system_time
.seconds
))secs
;
3781 basic_info
->system_time
.microseconds
= usecs
;
3783 *task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
3787 case TASK_THREAD_TIMES_INFO
:
3789 task_thread_times_info_t times_info
;
3792 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
3793 error
= KERN_INVALID_ARGUMENT
;
3797 times_info
= (task_thread_times_info_t
) task_info_out
;
3798 times_info
->user_time
.seconds
= 0;
3799 times_info
->user_time
.microseconds
= 0;
3800 times_info
->system_time
.seconds
= 0;
3801 times_info
->system_time
.microseconds
= 0;
3804 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3805 time_value_t user_time
, system_time
;
3807 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3810 thread_read_times(thread
, &user_time
, &system_time
);
3812 time_value_add(×_info
->user_time
, &user_time
);
3813 time_value_add(×_info
->system_time
, &system_time
);
3816 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
3820 case TASK_ABSOLUTETIME_INFO
:
3822 task_absolutetime_info_t info
;
3825 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
) {
3826 error
= KERN_INVALID_ARGUMENT
;
3830 info
= (task_absolutetime_info_t
)task_info_out
;
3831 info
->threads_user
= info
->threads_system
= 0;
3834 info
->total_user
= task
->total_user_time
;
3835 info
->total_system
= task
->total_system_time
;
3837 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3841 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3845 thread_lock(thread
);
3847 tval
= timer_grab(&thread
->user_timer
);
3848 info
->threads_user
+= tval
;
3849 info
->total_user
+= tval
;
3851 tval
= timer_grab(&thread
->system_timer
);
3852 if (thread
->precise_user_kernel_time
) {
3853 info
->threads_system
+= tval
;
3854 info
->total_system
+= tval
;
3856 /* system_timer may represent either sys or user */
3857 info
->threads_user
+= tval
;
3858 info
->total_user
+= tval
;
3861 thread_unlock(thread
);
3866 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
3870 case TASK_DYLD_INFO
:
3872 task_dyld_info_t info
;
3875 * We added the format field to TASK_DYLD_INFO output. For
3876 * temporary backward compatibility, accept the fact that
3877 * clients may ask for the old version - distinquished by the
3878 * size of the expected result structure.
3880 #define TASK_LEGACY_DYLD_INFO_COUNT \
3881 offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
3883 if (*task_info_count
< TASK_LEGACY_DYLD_INFO_COUNT
) {
3884 error
= KERN_INVALID_ARGUMENT
;
3888 info
= (task_dyld_info_t
)task_info_out
;
3889 info
->all_image_info_addr
= task
->all_image_info_addr
;
3890 info
->all_image_info_size
= task
->all_image_info_size
;
3892 /* only set format on output for those expecting it */
3893 if (*task_info_count
>= TASK_DYLD_INFO_COUNT
) {
3894 info
->all_image_info_format
= task_has_64BitAddr(task
) ?
3895 TASK_DYLD_ALL_IMAGE_INFO_64
:
3896 TASK_DYLD_ALL_IMAGE_INFO_32
;
3897 *task_info_count
= TASK_DYLD_INFO_COUNT
;
3899 *task_info_count
= TASK_LEGACY_DYLD_INFO_COUNT
;
3904 case TASK_EXTMOD_INFO
:
3906 task_extmod_info_t info
;
3909 if (*task_info_count
< TASK_EXTMOD_INFO_COUNT
) {
3910 error
= KERN_INVALID_ARGUMENT
;
3914 info
= (task_extmod_info_t
)task_info_out
;
3916 p
= get_bsdtask_info(task
);
3918 proc_getexecutableuuid(p
, info
->task_uuid
, sizeof(info
->task_uuid
));
3920 bzero(info
->task_uuid
, sizeof(info
->task_uuid
));
3922 info
->extmod_statistics
= task
->extmod_statistics
;
3923 *task_info_count
= TASK_EXTMOD_INFO_COUNT
;
3928 case TASK_KERNELMEMORY_INFO
:
3930 task_kernelmemory_info_t tkm_info
;
3931 ledger_amount_t credit
, debit
;
3933 if (*task_info_count
< TASK_KERNELMEMORY_INFO_COUNT
) {
3934 error
= KERN_INVALID_ARGUMENT
;
3938 tkm_info
= (task_kernelmemory_info_t
) task_info_out
;
3939 tkm_info
->total_palloc
= 0;
3940 tkm_info
->total_pfree
= 0;
3941 tkm_info
->total_salloc
= 0;
3942 tkm_info
->total_sfree
= 0;
3944 if (task
== kernel_task
) {
3946 * All shared allocs/frees from other tasks count against
3947 * the kernel private memory usage. If we are looking up
3948 * info for the kernel task, gather from everywhere.
3952 /* start by accounting for all the terminated tasks against the kernel */
3953 tkm_info
->total_palloc
= tasks_tkm_private
.alloc
+ tasks_tkm_shared
.alloc
;
3954 tkm_info
->total_pfree
= tasks_tkm_private
.free
+ tasks_tkm_shared
.free
;
3956 /* count all other task/thread shared alloc/free against the kernel */
3957 lck_mtx_lock(&tasks_threads_lock
);
3959 /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
3960 queue_iterate(&tasks
, task
, task_t
, tasks
) {
3961 if (task
== kernel_task
) {
3962 if (ledger_get_entries(task
->ledger
,
3963 task_ledgers
.tkm_private
, &credit
,
3964 &debit
) == KERN_SUCCESS
) {
3965 tkm_info
->total_palloc
+= credit
;
3966 tkm_info
->total_pfree
+= debit
;
3969 if (!ledger_get_entries(task
->ledger
,
3970 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3971 tkm_info
->total_palloc
+= credit
;
3972 tkm_info
->total_pfree
+= debit
;
3975 lck_mtx_unlock(&tasks_threads_lock
);
3977 if (!ledger_get_entries(task
->ledger
,
3978 task_ledgers
.tkm_private
, &credit
, &debit
)) {
3979 tkm_info
->total_palloc
= credit
;
3980 tkm_info
->total_pfree
= debit
;
3982 if (!ledger_get_entries(task
->ledger
,
3983 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3984 tkm_info
->total_salloc
= credit
;
3985 tkm_info
->total_sfree
= debit
;
3990 *task_info_count
= TASK_KERNELMEMORY_INFO_COUNT
;
3991 return KERN_SUCCESS
;
3995 case TASK_SCHED_FIFO_INFO
:
3998 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
) {
3999 error
= KERN_INVALID_ARGUMENT
;
4003 error
= KERN_INVALID_POLICY
;
4008 case TASK_SCHED_RR_INFO
:
4010 policy_rr_base_t rr_base
;
4011 uint32_t quantum_time
;
4012 uint64_t quantum_ns
;
4014 if (*task_info_count
< POLICY_RR_BASE_COUNT
) {
4015 error
= KERN_INVALID_ARGUMENT
;
4019 rr_base
= (policy_rr_base_t
) task_info_out
;
4021 if (task
!= kernel_task
) {
4022 error
= KERN_INVALID_POLICY
;
4026 rr_base
->base_priority
= task
->priority
;
4028 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
4029 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
4031 rr_base
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
4033 *task_info_count
= POLICY_RR_BASE_COUNT
;
4038 case TASK_SCHED_TIMESHARE_INFO
:
4040 policy_timeshare_base_t ts_base
;
4042 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
) {
4043 error
= KERN_INVALID_ARGUMENT
;
4047 ts_base
= (policy_timeshare_base_t
) task_info_out
;
4049 if (task
== kernel_task
) {
4050 error
= KERN_INVALID_POLICY
;
4054 ts_base
->base_priority
= task
->priority
;
4056 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
4060 case TASK_SECURITY_TOKEN
:
4062 security_token_t
*sec_token_p
;
4064 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
4065 error
= KERN_INVALID_ARGUMENT
;
4069 sec_token_p
= (security_token_t
*) task_info_out
;
4071 *sec_token_p
= task
->sec_token
;
4073 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
4077 case TASK_AUDIT_TOKEN
:
4079 audit_token_t
*audit_token_p
;
4081 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
4082 error
= KERN_INVALID_ARGUMENT
;
4086 audit_token_p
= (audit_token_t
*) task_info_out
;
4088 *audit_token_p
= task
->audit_token
;
4090 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
4094 case TASK_SCHED_INFO
:
4095 error
= KERN_INVALID_ARGUMENT
;
4098 case TASK_EVENTS_INFO
:
4100 task_events_info_t events_info
;
4103 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
4104 error
= KERN_INVALID_ARGUMENT
;
4108 events_info
= (task_events_info_t
) task_info_out
;
4111 events_info
->faults
= task
->faults
;
4112 events_info
->pageins
= task
->pageins
;
4113 events_info
->cow_faults
= task
->cow_faults
;
4114 events_info
->messages_sent
= task
->messages_sent
;
4115 events_info
->messages_received
= task
->messages_received
;
4116 events_info
->syscalls_mach
= task
->syscalls_mach
;
4117 events_info
->syscalls_unix
= task
->syscalls_unix
;
4119 events_info
->csw
= task
->c_switch
;
4121 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4122 events_info
->csw
+= thread
->c_switch
;
4123 events_info
->syscalls_mach
+= thread
->syscalls_mach
;
4124 events_info
->syscalls_unix
+= thread
->syscalls_unix
;
4128 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
4131 case TASK_AFFINITY_TAG_INFO
:
4133 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
) {
4134 error
= KERN_INVALID_ARGUMENT
;
4138 error
= task_affinity_info(task
, task_info_out
, task_info_count
);
4141 case TASK_POWER_INFO
:
4143 if (*task_info_count
< TASK_POWER_INFO_COUNT
) {
4144 error
= KERN_INVALID_ARGUMENT
;
4148 task_power_info_locked(task
, (task_power_info_t
)task_info_out
, NULL
, NULL
);
4152 case TASK_POWER_INFO_V2
:
4154 if (*task_info_count
< TASK_POWER_INFO_V2_COUNT_OLD
) {
4155 error
= KERN_INVALID_ARGUMENT
;
4158 task_power_info_v2_t tpiv2
= (task_power_info_v2_t
) task_info_out
;
4159 task_power_info_locked(task
, &tpiv2
->cpu_energy
, &tpiv2
->gpu_energy
, tpiv2
);
4164 case TASK_VM_INFO_PURGEABLE
:
4166 task_vm_info_t vm_info
;
4169 if (*task_info_count
< TASK_VM_INFO_REV0_COUNT
) {
4170 error
= KERN_INVALID_ARGUMENT
;
4174 vm_info
= (task_vm_info_t
)task_info_out
;
4176 if (task
== kernel_task
) {
4181 vm_map_lock_read(map
);
4184 vm_info
->virtual_size
= (typeof(vm_info
->virtual_size
))map
->size
;
4185 vm_info
->region_count
= map
->hdr
.nentries
;
4186 vm_info
->page_size
= vm_map_page_size(map
);
4188 vm_info
->resident_size
= pmap_resident_count(map
->pmap
);
4189 vm_info
->resident_size
*= PAGE_SIZE
;
4190 vm_info
->resident_size_peak
= pmap_resident_max(map
->pmap
);
4191 vm_info
->resident_size_peak
*= PAGE_SIZE
;
4193 #define _VM_INFO(_name) \
4194 vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
4197 _VM_INFO(device_peak
);
4199 _VM_INFO(external_peak
);
4201 _VM_INFO(internal_peak
);
4203 _VM_INFO(reusable_peak
);
4204 _VM_INFO(compressed
);
4205 _VM_INFO(compressed_peak
);
4206 _VM_INFO(compressed_lifetime
);
4208 vm_info
->purgeable_volatile_pmap
= 0;
4209 vm_info
->purgeable_volatile_resident
= 0;
4210 vm_info
->purgeable_volatile_virtual
= 0;
4211 if (task
== kernel_task
) {
4213 * We do not maintain the detailed stats for the
4214 * kernel_pmap, so just count everything as
4217 vm_info
->internal
= vm_info
->resident_size
;
4219 * ... but since the memory held by the VM compressor
4220 * in the kernel address space ought to be attributed
4221 * to user-space tasks, we subtract it from "internal"
4222 * to give memory reporting tools a more accurate idea
4223 * of what the kernel itself is actually using, instead
4224 * of making it look like the kernel is leaking memory
4225 * when the system is under memory pressure.
4227 vm_info
->internal
-= (VM_PAGE_COMPRESSOR_COUNT
*
4230 mach_vm_size_t volatile_virtual_size
;
4231 mach_vm_size_t volatile_resident_size
;
4232 mach_vm_size_t volatile_compressed_size
;
4233 mach_vm_size_t volatile_pmap_size
;
4234 mach_vm_size_t volatile_compressed_pmap_size
;
4237 if (flavor
== TASK_VM_INFO_PURGEABLE
) {
4238 kr
= vm_map_query_volatile(
4240 &volatile_virtual_size
,
4241 &volatile_resident_size
,
4242 &volatile_compressed_size
,
4243 &volatile_pmap_size
,
4244 &volatile_compressed_pmap_size
);
4245 if (kr
== KERN_SUCCESS
) {
4246 vm_info
->purgeable_volatile_pmap
=
4248 if (radar_20146450
) {
4249 vm_info
->compressed
-=
4250 volatile_compressed_pmap_size
;
4252 vm_info
->purgeable_volatile_resident
=
4253 volatile_resident_size
;
4254 vm_info
->purgeable_volatile_virtual
=
4255 volatile_virtual_size
;
4259 *task_info_count
= TASK_VM_INFO_REV0_COUNT
;
4261 if (original_task_info_count
>= TASK_VM_INFO_REV1_COUNT
) {
4262 vm_info
->phys_footprint
=
4263 (mach_vm_size_t
) get_task_phys_footprint(task
);
4264 *task_info_count
= TASK_VM_INFO_REV1_COUNT
;
4266 if (original_task_info_count
>= TASK_VM_INFO_REV2_COUNT
) {
4267 vm_info
->min_address
= map
->min_offset
;
4268 vm_info
->max_address
= map
->max_offset
;
4269 *task_info_count
= TASK_VM_INFO_REV2_COUNT
;
4272 if (task
!= kernel_task
) {
4273 vm_map_unlock_read(map
);
4279 case TASK_WAIT_STATE_INFO
:
4282 * Deprecated flavor. Currently allowing some results until all users
4283 * stop calling it. The results may not be accurate.
4285 task_wait_state_info_t wait_state_info
;
4286 uint64_t total_sfi_ledger_val
= 0;
4288 if (*task_info_count
< TASK_WAIT_STATE_INFO_COUNT
) {
4289 error
= KERN_INVALID_ARGUMENT
;
4293 wait_state_info
= (task_wait_state_info_t
) task_info_out
;
4295 wait_state_info
->total_wait_state_time
= 0;
4296 bzero(wait_state_info
->_reserved
, sizeof(wait_state_info
->_reserved
));
4298 #if CONFIG_SCHED_SFI
4299 int i
, prev_lentry
= -1;
4300 int64_t val_credit
, val_debit
;
4302 for (i
= 0; i
< MAX_SFI_CLASS_ID
; i
++){
4305 * checking with prev_lentry != entry ensures adjacent classes
4306 * which share the same ledger do not add wait times twice.
4307 * Note: Use ledger() call to get data for each individual sfi class.
4309 if (prev_lentry
!= task_ledgers
.sfi_wait_times
[i
] &&
4310 KERN_SUCCESS
== ledger_get_entries(task
->ledger
,
4311 task_ledgers
.sfi_wait_times
[i
], &val_credit
, &val_debit
)) {
4312 total_sfi_ledger_val
+= val_credit
;
4314 prev_lentry
= task_ledgers
.sfi_wait_times
[i
];
4317 #endif /* CONFIG_SCHED_SFI */
4318 wait_state_info
->total_wait_sfi_state_time
= total_sfi_ledger_val
;
4319 *task_info_count
= TASK_WAIT_STATE_INFO_COUNT
;
4323 case TASK_VM_INFO_PURGEABLE_ACCOUNT
:
4325 #if DEVELOPMENT || DEBUG
4326 pvm_account_info_t acnt_info
;
4328 if (*task_info_count
< PVM_ACCOUNT_INFO_COUNT
) {
4329 error
= KERN_INVALID_ARGUMENT
;
4333 if (task_info_out
== NULL
) {
4334 error
= KERN_INVALID_ARGUMENT
;
4338 acnt_info
= (pvm_account_info_t
) task_info_out
;
4340 error
= vm_purgeable_account(task
, acnt_info
);
4342 *task_info_count
= PVM_ACCOUNT_INFO_COUNT
;
4345 #else /* DEVELOPMENT || DEBUG */
4346 error
= KERN_NOT_SUPPORTED
;
4348 #endif /* DEVELOPMENT || DEBUG */
4350 case TASK_FLAGS_INFO
:
4352 task_flags_info_t flags_info
;
4354 if (*task_info_count
< TASK_FLAGS_INFO_COUNT
) {
4355 error
= KERN_INVALID_ARGUMENT
;
4359 flags_info
= (task_flags_info_t
)task_info_out
;
4361 /* only publish the 64-bit flag of the task */
4362 flags_info
->flags
= task
->t_flags
& TF_64B_ADDR
;
4364 *task_info_count
= TASK_FLAGS_INFO_COUNT
;
4368 case TASK_DEBUG_INFO_INTERNAL
:
4370 #if DEVELOPMENT || DEBUG
4371 task_debug_info_internal_t dbg_info
;
4372 if (*task_info_count
< TASK_DEBUG_INFO_INTERNAL_COUNT
) {
4373 error
= KERN_NOT_SUPPORTED
;
4377 if (task_info_out
== NULL
) {
4378 error
= KERN_INVALID_ARGUMENT
;
4381 dbg_info
= (task_debug_info_internal_t
) task_info_out
;
4382 dbg_info
->ipc_space_size
= 0;
4383 if (task
->itk_space
){
4384 dbg_info
->ipc_space_size
= task
->itk_space
->is_table_size
;
4387 dbg_info
->suspend_count
= task
->suspend_count
;
4389 error
= KERN_SUCCESS
;
4390 *task_info_count
= TASK_DEBUG_INFO_INTERNAL_COUNT
;
4392 #else /* DEVELOPMENT || DEBUG */
4393 error
= KERN_NOT_SUPPORTED
;
4395 #endif /* DEVELOPMENT || DEBUG */
4398 error
= KERN_INVALID_ARGUMENT
;
4406 * task_info_from_user
4408 * When calling task_info from user space,
4409 * this function will be executed as mig server side
4410 * instead of calling directly into task_info.
4411 * This gives the possibility to perform more security
4412 * checks on task_port.
4414 * In the case of TASK_DYLD_INFO, we require the more
4415 * privileged task_port not the less-privileged task_name_port.
4419 task_info_from_user(
4420 mach_port_t task_port
,
4421 task_flavor_t flavor
,
4422 task_info_t task_info_out
,
4423 mach_msg_type_number_t
*task_info_count
)
4428 if (flavor
== TASK_DYLD_INFO
)
4429 task
= convert_port_to_task(task_port
);
4431 task
= convert_port_to_task_name(task_port
);
4433 ret
= task_info(task
, flavor
, task_info_out
, task_info_count
);
4435 task_deallocate(task
);
4443 * Returns power stats for the task.
4444 * Note: Called with task locked.
4447 task_power_info_locked(
4449 task_power_info_t info
,
4450 gpu_energy_data_t ginfo
,
4451 task_power_info_v2_t infov2
)
4454 ledger_amount_t tmp
;
4456 task_lock_assert_owned(task
);
4458 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
4459 (ledger_amount_t
*)&info
->task_interrupt_wakeups
, &tmp
);
4460 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
4461 (ledger_amount_t
*)&info
->task_platform_idle_wakeups
, &tmp
);
4463 info
->task_timer_wakeups_bin_1
= task
->task_timer_wakeups_bin_1
;
4464 info
->task_timer_wakeups_bin_2
= task
->task_timer_wakeups_bin_2
;
4466 info
->total_user
= task
->total_user_time
;
4467 info
->total_system
= task
->total_system_time
;
4471 infov2
->task_energy
= task
->task_energy
;
4476 ginfo
->task_gpu_utilisation
= task
->task_gpu_ns
;
4480 infov2
->task_ptime
= task
->total_ptime
;
4481 infov2
->task_pset_switches
= task
->ps_switch
;
4484 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4488 if (thread
->options
& TH_OPT_IDLE_THREAD
)
4492 thread_lock(thread
);
4494 info
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
4495 info
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
4499 infov2
->task_energy
+= ml_energy_stat(thread
);
4503 tval
= timer_grab(&thread
->user_timer
);
4504 info
->total_user
+= tval
;
4507 tval
= timer_grab(&thread
->ptime
);
4508 infov2
->task_ptime
+= tval
;
4509 infov2
->task_pset_switches
+= thread
->ps_switch
;
4512 tval
= timer_grab(&thread
->system_timer
);
4513 if (thread
->precise_user_kernel_time
) {
4514 info
->total_system
+= tval
;
4516 /* system_timer may represent either sys or user */
4517 info
->total_user
+= tval
;
4521 ginfo
->task_gpu_utilisation
+= ml_gpu_stat(thread
);
4523 thread_unlock(thread
);
4529 * task_gpu_utilisation
4531 * Returns the total gpu time used by the all the threads of the task
4532 * (both dead and alive)
4535 task_gpu_utilisation(
4538 uint64_t gpu_time
= 0;
4539 #if !CONFIG_EMBEDDED
4543 gpu_time
+= task
->task_gpu_ns
;
4545 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4548 thread_lock(thread
);
4549 gpu_time
+= ml_gpu_stat(thread
);
4550 thread_unlock(thread
);
4555 #else /* CONFIG_EMBEDDED */
4556 /* silence compiler warning */
4558 #endif /* !CONFIG_EMBEDDED */
4565 * Returns the total energy used by the all the threads of the task
4566 * (both dead and alive)
4572 uint64_t energy
= 0;
4576 energy
+= task
->task_energy
;
4578 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4581 thread_lock(thread
);
4582 energy
+= ml_energy_stat(thread
);
4583 thread_unlock(thread
);
4594 __unused task_t task
)
4600 /* This function updates the cpu time in the arrays for each
4601 * effective and requested QoS class
4604 task_update_cpu_time_qos_stats(
4606 uint64_t *eqos_stats
,
4607 uint64_t *rqos_stats
)
4609 if (!eqos_stats
&& !rqos_stats
) {
4615 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4616 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
4620 thread_update_qos_cpu_time(thread
);
4624 eqos_stats
[THREAD_QOS_DEFAULT
] += task
->cpu_time_eqos_stats
.cpu_time_qos_default
;
4625 eqos_stats
[THREAD_QOS_MAINTENANCE
] += task
->cpu_time_eqos_stats
.cpu_time_qos_maintenance
;
4626 eqos_stats
[THREAD_QOS_BACKGROUND
] += task
->cpu_time_eqos_stats
.cpu_time_qos_background
;
4627 eqos_stats
[THREAD_QOS_UTILITY
] += task
->cpu_time_eqos_stats
.cpu_time_qos_utility
;
4628 eqos_stats
[THREAD_QOS_LEGACY
] += task
->cpu_time_eqos_stats
.cpu_time_qos_legacy
;
4629 eqos_stats
[THREAD_QOS_USER_INITIATED
] += task
->cpu_time_eqos_stats
.cpu_time_qos_user_initiated
;
4630 eqos_stats
[THREAD_QOS_USER_INTERACTIVE
] += task
->cpu_time_eqos_stats
.cpu_time_qos_user_interactive
;
4634 rqos_stats
[THREAD_QOS_DEFAULT
] += task
->cpu_time_rqos_stats
.cpu_time_qos_default
;
4635 rqos_stats
[THREAD_QOS_MAINTENANCE
] += task
->cpu_time_rqos_stats
.cpu_time_qos_maintenance
;
4636 rqos_stats
[THREAD_QOS_BACKGROUND
] += task
->cpu_time_rqos_stats
.cpu_time_qos_background
;
4637 rqos_stats
[THREAD_QOS_UTILITY
] += task
->cpu_time_rqos_stats
.cpu_time_qos_utility
;
4638 rqos_stats
[THREAD_QOS_LEGACY
] += task
->cpu_time_rqos_stats
.cpu_time_qos_legacy
;
4639 rqos_stats
[THREAD_QOS_USER_INITIATED
] += task
->cpu_time_rqos_stats
.cpu_time_qos_user_initiated
;
4640 rqos_stats
[THREAD_QOS_USER_INTERACTIVE
] += task
->cpu_time_rqos_stats
.cpu_time_qos_user_interactive
;
4649 task_purgable_info_t
*stats
)
4651 if (task
== TASK_NULL
|| stats
== NULL
)
4652 return KERN_INVALID_ARGUMENT
;
4653 /* Take task reference */
4654 task_reference(task
);
4655 vm_purgeable_stats((vm_purgeable_info_t
)stats
, task
);
4656 /* Drop task reference */
4657 task_deallocate(task
);
4658 return KERN_SUCCESS
;
4671 task
->vtimers
|= which
;
4675 case TASK_VTIMER_USER
:
4676 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4678 thread_lock(thread
);
4679 if (thread
->precise_user_kernel_time
)
4680 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
4682 thread
->vtimer_user_save
= timer_grab(&thread
->system_timer
);
4683 thread_unlock(thread
);
4688 case TASK_VTIMER_PROF
:
4689 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4691 thread_lock(thread
);
4692 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
4693 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
4694 thread_unlock(thread
);
4699 case TASK_VTIMER_RLIM
:
4700 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4702 thread_lock(thread
);
4703 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
4704 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
4705 thread_unlock(thread
);
4719 assert(task
== current_task());
4723 task
->vtimers
&= ~which
;
4733 uint32_t *microsecs
)
4735 thread_t thread
= current_thread();
4737 clock_sec_t secs
= 0;
4740 assert(task
== current_task());
4742 spl_t s
= splsched();
4743 thread_lock(thread
);
4745 if ((task
->vtimers
& which
) != (uint32_t)which
) {
4746 thread_unlock(thread
);
4753 case TASK_VTIMER_USER
:
4754 if (thread
->precise_user_kernel_time
) {
4755 tdelt
= (uint32_t)timer_delta(&thread
->user_timer
,
4756 &thread
->vtimer_user_save
);
4758 tdelt
= (uint32_t)timer_delta(&thread
->system_timer
,
4759 &thread
->vtimer_user_save
);
4761 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4764 case TASK_VTIMER_PROF
:
4765 tsum
= timer_grab(&thread
->user_timer
);
4766 tsum
+= timer_grab(&thread
->system_timer
);
4767 tdelt
= (uint32_t)(tsum
- thread
->vtimer_prof_save
);
4768 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4769 /* if the time delta is smaller than a usec, ignore */
4770 if (*microsecs
!= 0)
4771 thread
->vtimer_prof_save
= tsum
;
4774 case TASK_VTIMER_RLIM
:
4775 tsum
= timer_grab(&thread
->user_timer
);
4776 tsum
+= timer_grab(&thread
->system_timer
);
4777 tdelt
= (uint32_t)(tsum
- thread
->vtimer_rlim_save
);
4778 thread
->vtimer_rlim_save
= tsum
;
4779 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4783 thread_unlock(thread
);
4790 * Change the assigned processor set for the task
4794 __unused task_t task
,
4795 __unused processor_set_t new_pset
,
4796 __unused boolean_t assign_threads
)
4798 return(KERN_FAILURE
);
4802 * task_assign_default:
4804 * Version of task_assign to assign to default processor set.
4807 task_assign_default(
4809 boolean_t assign_threads
)
4811 return (task_assign(task
, &pset0
, assign_threads
));
4815 * task_get_assignment
4817 * Return name of processor set that task is assigned to.
4820 task_get_assignment(
4822 processor_set_t
*pset
)
4824 if (!task
|| !task
->active
)
4825 return KERN_FAILURE
;
4829 return KERN_SUCCESS
;
4833 get_task_dispatchqueue_offset(
4836 return task
->dispatchqueue_offset
;
4842 * Set scheduling policy and parameters, both base and limit, for
4843 * the given task. Policy must be a policy which is enabled for the
4844 * processor set. Change contained threads if requested.
4848 __unused task_t task
,
4849 __unused policy_t policy_id
,
4850 __unused policy_base_t base
,
4851 __unused mach_msg_type_number_t count
,
4852 __unused boolean_t set_limit
,
4853 __unused boolean_t change
)
4855 return(KERN_FAILURE
);
4861 * Set scheduling policy and parameters, both base and limit, for
4862 * the given task. Policy can be any policy implemented by the
4863 * processor set, whether enabled or not. Change contained threads
4868 __unused task_t task
,
4869 __unused processor_set_t pset
,
4870 __unused policy_t policy_id
,
4871 __unused policy_base_t base
,
4872 __unused mach_msg_type_number_t base_count
,
4873 __unused policy_limit_t limit
,
4874 __unused mach_msg_type_number_t limit_count
,
4875 __unused boolean_t change
)
4877 return(KERN_FAILURE
);
4882 __unused task_t task
,
4883 __unused vm_offset_t pc
,
4884 __unused vm_offset_t endpc
)
4886 return KERN_FAILURE
;
4890 task_synchronizer_destroy_all(task_t task
)
4893 * Destroy owned semaphores
4895 semaphore_destroy_all(task
);
4899 * Install default (machine-dependent) initial thread state
4900 * on the task. Subsequent thread creation will have this initial
4901 * state set on the thread by machine_thread_inherit_taskwide().
4902 * Flavors and structures are exactly the same as those to thread_set_state()
4908 thread_state_t state
,
4909 mach_msg_type_number_t state_count
)
4913 if (task
== TASK_NULL
) {
4914 return (KERN_INVALID_ARGUMENT
);
4919 if (!task
->active
) {
4921 return (KERN_FAILURE
);
4924 ret
= machine_task_set_state(task
, flavor
, state
, state_count
);
4931 * Examine the default (machine-dependent) initial thread state
4932 * on the task, as set by task_set_state(). Flavors and structures
4933 * are exactly the same as those passed to thread_get_state().
4939 thread_state_t state
,
4940 mach_msg_type_number_t
*state_count
)
4944 if (task
== TASK_NULL
) {
4945 return (KERN_INVALID_ARGUMENT
);
4950 if (!task
->active
) {
4952 return (KERN_FAILURE
);
4955 ret
= machine_task_get_state(task
, flavor
, state
, state_count
);
4962 static kern_return_t
__attribute__((noinline
,not_tail_called
))
4963 PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(
4964 mach_exception_code_t code
,
4965 mach_exception_subcode_t subcode
,
4969 if (1 == proc_selfpid())
4970 return KERN_NOT_SUPPORTED
; // initproc is immune
4972 mach_exception_data_type_t codes
[EXCEPTION_CODE_MAX
] = {
4976 task_t task
= current_task();
4979 /* (See jetsam-related comments below) */
4981 proc_memstat_terminated(task
->bsd_info
, TRUE
);
4982 kr
= task_enqueue_exception_with_corpse(task
, EXC_GUARD
, codes
, 2, reason
);
4983 proc_memstat_terminated(task
->bsd_info
, FALSE
);
4987 extern kern_return_t
4988 task_violated_guard(mach_exception_code_t
, mach_exception_subcode_t
, void *);
4991 task_violated_guard(
4992 mach_exception_code_t code
,
4993 mach_exception_subcode_t subcode
,
4996 return PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(code
, subcode
, reason
);
5000 #if CONFIG_MEMORYSTATUS
5003 task_get_memlimit_is_active(task_t task
)
5005 assert (task
!= NULL
);
5007 if (task
->memlimit_is_active
== 1) {
5015 task_set_memlimit_is_active(task_t task
, boolean_t memlimit_is_active
)
5017 assert (task
!= NULL
);
5019 if (memlimit_is_active
) {
5020 task
->memlimit_is_active
= 1;
5022 task
->memlimit_is_active
= 0;
5027 task_get_memlimit_is_fatal(task_t task
)
5029 assert(task
!= NULL
);
5031 if (task
->memlimit_is_fatal
== 1) {
5039 task_set_memlimit_is_fatal(task_t task
, boolean_t memlimit_is_fatal
)
5041 assert (task
!= NULL
);
5043 if (memlimit_is_fatal
) {
5044 task
->memlimit_is_fatal
= 1;
5046 task
->memlimit_is_fatal
= 0;
5051 task_has_triggered_exc_resource(task_t task
, boolean_t memlimit_is_active
)
5053 boolean_t triggered
= FALSE
;
5055 assert(task
== current_task());
5058 * Returns true, if task has already triggered an exc_resource exception.
5061 if (memlimit_is_active
) {
5062 triggered
= (task
->memlimit_active_exc_resource
? TRUE
: FALSE
);
5064 triggered
= (task
->memlimit_inactive_exc_resource
? TRUE
: FALSE
);
5071 task_mark_has_triggered_exc_resource(task_t task
, boolean_t memlimit_is_active
)
5073 assert(task
== current_task());
5076 * We allow one exc_resource per process per active/inactive limit.
5077 * The limit's fatal attribute does not come into play.
5080 if (memlimit_is_active
) {
5081 task
->memlimit_active_exc_resource
= 1;
5083 task
->memlimit_inactive_exc_resource
= 1;
5087 #define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
5089 void __attribute__((noinline
))
5090 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
)
5092 task_t task
= current_task();
5094 const char *procname
= "unknown";
5095 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5098 pid
= proc_selfpid();
5102 * Cannot have ReportCrash analyzing
5103 * a suspended initproc.
5108 if (task
->bsd_info
!= NULL
)
5109 procname
= proc_name_address(current_task()->bsd_info
);
5112 if (hwm_user_cores
) {
5114 uint64_t starttime
, end
;
5115 clock_sec_t secs
= 0;
5116 uint32_t microsecs
= 0;
5118 starttime
= mach_absolute_time();
5120 * Trigger a coredump of this process. Don't proceed unless we know we won't
5121 * be filling up the disk; and ignore the core size resource limit for this
5124 if ((error
= coredump(current_task()->bsd_info
, HWM_USERCORE_MINSPACE
, COREDUMP_IGNORE_ULIMIT
)) != 0) {
5125 printf("couldn't take coredump of %s[%d]: %d\n", procname
, pid
, error
);
5128 * coredump() leaves the task suspended.
5130 task_resume_internal(current_task());
5132 end
= mach_absolute_time();
5133 absolutetime_to_microtime(end
- starttime
, &secs
, µsecs
);
5134 printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
5135 proc_name_address(current_task()->bsd_info
), pid
, (int)secs
, microsecs
);
5137 #endif /* CONFIG_COREDUMP */
5139 if (disable_exc_resource
) {
5140 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
5141 "supressed by a boot-arg.\n", procname
, pid
, max_footprint_mb
);
5146 * A task that has triggered an EXC_RESOURCE, should not be
5147 * jetsammed when the device is under memory pressure. Here
5148 * we set the P_MEMSTAT_TERMINATED flag so that the process
5149 * will be skipped if the memorystatus_thread wakes up.
5151 proc_memstat_terminated(current_task()->bsd_info
, TRUE
);
5153 code
[0] = code
[1] = 0;
5154 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_MEMORY
);
5155 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_HIGH_WATERMARK
);
5156 EXC_RESOURCE_HWM_ENCODE_LIMIT(code
[0], max_footprint_mb
);
5158 /* Do not generate a corpse fork if the violation is a fatal one */
5159 if (is_fatal
|| exc_via_corpse_forking
== 0) {
5160 /* Do not send a EXC_RESOURCE is corpse_for_fatal_memkill is set */
5161 if (corpse_for_fatal_memkill
== 0) {
5163 * Use the _internal_ variant so that no user-space
5164 * process can resume our task from under us.
5166 task_suspend_internal(task
);
5167 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5168 task_resume_internal(task
);
5172 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
5173 "supressed due to audio playback.\n", procname
, pid
, max_footprint_mb
);
5175 task_enqueue_exception_with_corpse(task
, EXC_RESOURCE
,
5176 code
, EXCEPTION_CODE_MAX
, NULL
);
5181 * After the EXC_RESOURCE has been handled, we must clear the
5182 * P_MEMSTAT_TERMINATED flag so that the process can again be
5183 * considered for jetsam if the memorystatus_thread wakes up.
5185 proc_memstat_terminated(current_task()->bsd_info
, FALSE
); /* clear the flag */
5189 * Callback invoked when a task exceeds its physical footprint limit.
5192 task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
5194 ledger_amount_t max_footprint
, max_footprint_mb
;
5196 boolean_t is_warning
;
5197 boolean_t memlimit_is_active
;
5198 boolean_t memlimit_is_fatal
;
5200 if (warning
== LEDGER_WARNING_DIPPED_BELOW
) {
5202 * Task memory limits only provide a warning on the way up.
5205 } else if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
5207 * This task is in danger of violating a memory limit,
5208 * It has exceeded a percentage level of the limit.
5213 * The task has exceeded the physical footprint limit.
5214 * This is not a warning but a true limit violation.
5219 task
= current_task();
5221 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &max_footprint
);
5222 max_footprint_mb
= max_footprint
>> 20;
5224 memlimit_is_active
= task_get_memlimit_is_active(task
);
5225 memlimit_is_fatal
= task_get_memlimit_is_fatal(task
);
5228 * If this is an actual violation (not a warning), then generate EXC_RESOURCE exception.
5229 * We only generate the exception once per process per memlimit (active/inactive limit).
5230 * To enforce this, we monitor state based on the memlimit's active/inactive attribute
5231 * and we disable it by marking that memlimit as exception triggered.
5233 if ((is_warning
== FALSE
) && (!task_has_triggered_exc_resource(task
, memlimit_is_active
))) {
5234 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND((int)max_footprint_mb
, memlimit_is_fatal
);
5235 memorystatus_log_exception((int)max_footprint_mb
, memlimit_is_active
, memlimit_is_fatal
);
5236 task_mark_has_triggered_exc_resource(task
, memlimit_is_active
);
5239 memorystatus_on_ledger_footprint_exceeded(is_warning
, memlimit_is_active
, memlimit_is_fatal
);
5242 extern int proc_check_footprint_priv(void);
5245 task_set_phys_footprint_limit(
5250 kern_return_t error
;
5252 boolean_t memlimit_is_active
;
5253 boolean_t memlimit_is_fatal
;
5255 if ((error
= proc_check_footprint_priv())) {
5256 return (KERN_NO_ACCESS
);
5260 * This call should probably be obsoleted.
5261 * But for now, we default to current state.
5263 memlimit_is_active
= task_get_memlimit_is_active(task
);
5264 memlimit_is_fatal
= task_get_memlimit_is_fatal(task
);
5266 return task_set_phys_footprint_limit_internal(task
, new_limit_mb
, old_limit_mb
, memlimit_is_active
, memlimit_is_fatal
);
5270 task_convert_phys_footprint_limit(
5272 int *converted_limit_mb
)
5274 if (limit_mb
== -1) {
5278 if (max_task_footprint
!= 0) {
5279 *converted_limit_mb
= (int)(max_task_footprint
/ 1024 / 1024); /* bytes to MB */
5281 *converted_limit_mb
= (int)(LEDGER_LIMIT_INFINITY
>> 20);
5284 /* nothing to convert */
5285 *converted_limit_mb
= limit_mb
;
5287 return (KERN_SUCCESS
);
5292 task_set_phys_footprint_limit_internal(
5296 boolean_t memlimit_is_active
,
5297 boolean_t memlimit_is_fatal
)
5299 ledger_amount_t old
;
5301 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &old
);
5304 * Check that limit >> 20 will not give an "unexpected" 32-bit
5305 * result. There are, however, implicit assumptions that -1 mb limit
5306 * equates to LEDGER_LIMIT_INFINITY.
5308 assert(((old
& 0xFFF0000000000000LL
) == 0) || (old
== LEDGER_LIMIT_INFINITY
));
5311 *old_limit_mb
= (int)(old
>> 20);
5314 if (new_limit_mb
== -1) {
5316 * Caller wishes to remove the limit.
5318 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
5319 max_task_footprint
? max_task_footprint
: LEDGER_LIMIT_INFINITY
,
5320 max_task_footprint
? max_task_footprint_warning_level
: 0);
5323 task_set_memlimit_is_active(task
, memlimit_is_active
);
5324 task_set_memlimit_is_fatal(task
, memlimit_is_fatal
);
5327 return (KERN_SUCCESS
);
5330 #ifdef CONFIG_NOMONITORS
5331 return (KERN_SUCCESS
);
5332 #endif /* CONFIG_NOMONITORS */
5336 if ((memlimit_is_active
== task_get_memlimit_is_active(task
)) &&
5337 (memlimit_is_fatal
== task_get_memlimit_is_fatal(task
)) &&
5338 (((ledger_amount_t
)new_limit_mb
<< 20) == old
)) {
5340 * memlimit state is not changing
5343 return(KERN_SUCCESS
);
5346 task_set_memlimit_is_active(task
, memlimit_is_active
);
5347 task_set_memlimit_is_fatal(task
, memlimit_is_fatal
);
5349 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
5350 (ledger_amount_t
)new_limit_mb
<< 20, PHYS_FOOTPRINT_WARNING_LEVEL
);
5352 if (task
== current_task()) {
5353 ledger_check_new_balance(current_thread(), task
->ledger
,
5354 task_ledgers
.phys_footprint
);
5359 return (KERN_SUCCESS
);
5363 task_get_phys_footprint_limit(
5367 ledger_amount_t limit
;
5369 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &limit
);
5371 * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
5372 * result. There are, however, implicit assumptions that -1 mb limit
5373 * equates to LEDGER_LIMIT_INFINITY.
5375 assert(((limit
& 0xFFF0000000000000LL
) == 0) || (limit
== LEDGER_LIMIT_INFINITY
));
5376 *limit_mb
= (int)(limit
>> 20);
5378 return (KERN_SUCCESS
);
5380 #else /* CONFIG_MEMORYSTATUS */
5382 task_set_phys_footprint_limit(
5383 __unused task_t task
,
5384 __unused
int new_limit_mb
,
5385 __unused
int *old_limit_mb
)
5387 return (KERN_FAILURE
);
5391 task_get_phys_footprint_limit(
5392 __unused task_t task
,
5393 __unused
int *limit_mb
)
5395 return (KERN_FAILURE
);
5397 #endif /* CONFIG_MEMORYSTATUS */
5400 * We need to export some functions to other components that
5401 * are currently implemented in macros within the osfmk
5402 * component. Just export them as functions of the same name.
5404 boolean_t
is_kerneltask(task_t t
)
5406 if (t
== kernel_task
)
5412 boolean_t
is_corpsetask(task_t t
)
5414 return (task_is_a_corpse(t
));
5418 task_t
current_task(void);
5419 task_t
current_task(void)
5421 return (current_task_fast());
5424 #undef task_reference
5425 void task_reference(task_t task
);
5430 if (task
!= TASK_NULL
)
5431 task_reference_internal(task
);
5434 /* defined in bsd/kern/kern_prot.c */
5435 extern int get_audit_token_pid(audit_token_t
*audit_token
);
5437 int task_pid(task_t task
)
5440 return get_audit_token_pid(&task
->audit_token
);
5446 * This routine finds a thread in a task by its unique id
5447 * Returns a referenced thread or THREAD_NULL if the thread was not found
5449 * TODO: This is super inefficient - it's an O(threads in task) list walk!
5450 * We should make a tid hash, or transition all tid clients to thread ports
5452 * Precondition: No locks held (will take task lock)
5455 task_findtid(task_t task
, uint64_t tid
)
5457 thread_t self
= current_thread();
5458 thread_t found_thread
= THREAD_NULL
;
5459 thread_t iter_thread
= THREAD_NULL
;
5461 /* Short-circuit the lookup if we're looking up ourselves */
5462 if (tid
== self
->thread_id
|| tid
== TID_NULL
) {
5463 assert(self
->task
== task
);
5465 thread_reference(self
);
5472 queue_iterate(&task
->threads
, iter_thread
, thread_t
, task_threads
) {
5473 if (iter_thread
->thread_id
== tid
) {
5474 found_thread
= iter_thread
;
5475 thread_reference(found_thread
);
5482 return (found_thread
);
5485 int pid_from_task(task_t task
)
5489 if (task
->bsd_info
) {
5490 pid
= proc_pid(task
->bsd_info
);
5492 pid
= task_pid(task
);
5499 * Control the CPU usage monitor for a task.
5502 task_cpu_usage_monitor_ctl(task_t task
, uint32_t *flags
)
5504 int error
= KERN_SUCCESS
;
5506 if (*flags
& CPUMON_MAKE_FATAL
) {
5507 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_CPUMON
;
5509 error
= KERN_INVALID_ARGUMENT
;
5516 * Control the wakeups monitor for a task.
5519 task_wakeups_monitor_ctl(task_t task
, uint32_t *flags
, int32_t *rate_hz
)
5521 ledger_t ledger
= task
->ledger
;
5524 if (*flags
& WAKEMON_GET_PARAMS
) {
5525 ledger_amount_t limit
;
5528 ledger_get_limit(ledger
, task_ledgers
.interrupt_wakeups
, &limit
);
5529 ledger_get_period(ledger
, task_ledgers
.interrupt_wakeups
, &period
);
5531 if (limit
!= LEDGER_LIMIT_INFINITY
) {
5533 * An active limit means the wakeups monitor is enabled.
5535 *rate_hz
= (int32_t)(limit
/ (int64_t)(period
/ NSEC_PER_SEC
));
5536 *flags
= WAKEMON_ENABLE
;
5537 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
5538 *flags
|= WAKEMON_MAKE_FATAL
;
5541 *flags
= WAKEMON_DISABLE
;
5546 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
5549 return KERN_SUCCESS
;
5552 if (*flags
& WAKEMON_ENABLE
) {
5553 if (*flags
& WAKEMON_SET_DEFAULTS
) {
5554 *rate_hz
= task_wakeups_monitor_rate
;
5557 #ifndef CONFIG_NOMONITORS
5558 if (*flags
& WAKEMON_MAKE_FATAL
) {
5559 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
5561 #endif /* CONFIG_NOMONITORS */
5563 if (*rate_hz
<= 0) {
5565 return KERN_INVALID_ARGUMENT
;
5568 #ifndef CONFIG_NOMONITORS
5569 ledger_set_limit(ledger
, task_ledgers
.interrupt_wakeups
, *rate_hz
* task_wakeups_monitor_interval
,
5570 task_wakeups_monitor_ustackshots_trigger_pct
);
5571 ledger_set_period(ledger
, task_ledgers
.interrupt_wakeups
, task_wakeups_monitor_interval
* NSEC_PER_SEC
);
5572 ledger_enable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
5573 #endif /* CONFIG_NOMONITORS */
5574 } else if (*flags
& WAKEMON_DISABLE
) {
5576 * Caller wishes to disable wakeups monitor on the task.
5578 * Disable telemetry if it was triggered by the wakeups monitor, and
5579 * remove the limit & callback on the wakeups ledger entry.
5581 #if CONFIG_TELEMETRY
5582 telemetry_task_ctl_locked(task
, TF_WAKEMON_WARNING
, 0);
5584 ledger_disable_refill(ledger
, task_ledgers
.interrupt_wakeups
);
5585 ledger_disable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
5589 return KERN_SUCCESS
;
5593 task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
5595 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
5596 #if CONFIG_TELEMETRY
5598 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
5599 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
5601 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 1);
5606 #if CONFIG_TELEMETRY
5608 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
5609 * exceeded the limit, turn telemetry off for the task.
5611 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 0);
5615 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS();
5619 void __attribute__((noinline
))
5620 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
5622 task_t task
= current_task();
5624 const char *procname
= "unknown";
5627 #ifdef EXC_RESOURCE_MONITORS
5628 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5629 #endif /* EXC_RESOURCE_MONITORS */
5630 struct ledger_entry_info lei
;
5633 pid
= proc_selfpid();
5634 if (task
->bsd_info
!= NULL
)
5635 procname
= proc_name_address(current_task()->bsd_info
);
5638 ledger_get_entry_info(task
->ledger
, task_ledgers
.interrupt_wakeups
, &lei
);
5641 * Disable the exception notification so we don't overwhelm
5642 * the listener with an endless stream of redundant exceptions.
5643 * TODO: detect whether another thread is already reporting the violation.
5645 uint32_t flags
= WAKEMON_DISABLE
;
5646 task_wakeups_monitor_ctl(task
, &flags
, NULL
);
5648 fatal
= task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
5649 trace_resource_violation(RMON_CPUWAKES_VIOLATED
, &lei
);
5650 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught waking the CPU %llu times "
5651 "over ~%llu seconds, averaging %llu wakes / second and "
5652 "violating a %slimit of %llu wakes over %llu seconds.\n",
5654 lei
.lei_balance
, lei
.lei_last_refill
/ NSEC_PER_SEC
,
5655 lei
.lei_last_refill
== 0 ? 0 :
5656 (NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
),
5657 fatal
? "FATAL " : "",
5658 lei
.lei_limit
, lei
.lei_refill_period
/ NSEC_PER_SEC
);
5660 kr
= send_resource_violation(send_cpu_wakes_violation
, task
, &lei
,
5661 fatal
? kRNFatalLimitFlag
: 0);
5663 printf("send_resource_violation(CPU wakes, ...): error %#x\n", kr
);
5666 #ifdef EXC_RESOURCE_MONITORS
5667 if (disable_exc_resource
) {
5668 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5669 "supressed by a boot-arg\n", procname
, pid
);
5673 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5674 "supressed due to audio playback\n", procname
, pid
);
5677 if (lei
.lei_last_refill
== 0) {
5678 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5679 "supressed due to lei.lei_last_refill = 0 \n", procname
, pid
);
5682 code
[0] = code
[1] = 0;
5683 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_WAKEUPS
);
5684 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_WAKEUPS_MONITOR
);
5685 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code
[0],
5686 NSEC_PER_SEC
* lei
.lei_limit
/ lei
.lei_refill_period
);
5687 EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code
[0],
5688 lei
.lei_last_refill
);
5689 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code
[1],
5690 NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
);
5691 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5692 #endif /* EXC_RESOURCE_MONITORS */
5695 task_terminate_internal(task
);
5700 global_update_logical_writes(int64_t io_delta
)
5702 int64_t old_count
, new_count
;
5703 boolean_t needs_telemetry
;
5706 new_count
= old_count
= global_logical_writes_count
;
5707 new_count
+= io_delta
;
5708 if (new_count
>= io_telemetry_limit
) {
5710 needs_telemetry
= TRUE
;
5712 needs_telemetry
= FALSE
;
5714 } while(!OSCompareAndSwap64(old_count
, new_count
, &global_logical_writes_count
));
5715 return needs_telemetry
;
5718 void task_update_logical_writes(task_t task
, uint32_t io_size
, int flags
, void *vp
)
5720 int64_t io_delta
= 0;
5721 boolean_t needs_telemetry
= FALSE
;
5723 if ((!task
) || (!io_size
) || (!vp
))
5726 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_DATA_WRITE
)) | DBG_FUNC_NONE
,
5727 task_pid(task
), io_size
, flags
, (uintptr_t)VM_KERNEL_ADDRPERM(vp
), 0);
5728 DTRACE_IO4(logical_writes
, struct task
*, task
, uint32_t, io_size
, int, flags
, vnode
*, vp
);
5730 case TASK_WRITE_IMMEDIATE
:
5731 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_immediate_writes
));
5732 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5734 case TASK_WRITE_DEFERRED
:
5735 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_deferred_writes
));
5736 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5738 case TASK_WRITE_INVALIDATED
:
5739 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_invalidated_writes
));
5740 ledger_debit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5742 case TASK_WRITE_METADATA
:
5743 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_metadata_writes
));
5744 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5748 io_delta
= (flags
== TASK_WRITE_INVALIDATED
) ? ((int64_t)io_size
* -1ll) : ((int64_t)io_size
);
5749 if (io_telemetry_limit
!= 0) {
5750 /* If io_telemetry_limit is 0, disable global updates and I/O telemetry */
5751 needs_telemetry
= global_update_logical_writes(io_delta
);
5752 if (needs_telemetry
) {
5753 act_set_io_telemetry_ast(current_thread());
5759 * Control the I/O monitor for a task.
5762 task_io_monitor_ctl(task_t task
, uint32_t *flags
)
5764 ledger_t ledger
= task
->ledger
;
5767 if (*flags
& IOMON_ENABLE
) {
5768 /* Configure the physical I/O ledger */
5769 ledger_set_limit(ledger
, task_ledgers
.physical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5770 ledger_set_period(ledger
, task_ledgers
.physical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5772 /* Configure the logical I/O ledger */
5773 ledger_set_limit(ledger
, task_ledgers
.logical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5774 ledger_set_period(ledger
, task_ledgers
.logical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5776 } else if (*flags
& IOMON_DISABLE
) {
5778 * Caller wishes to disable I/O monitor on the task.
5780 ledger_disable_refill(ledger
, task_ledgers
.physical_writes
);
5781 ledger_disable_callback(ledger
, task_ledgers
.physical_writes
);
5782 ledger_disable_refill(ledger
, task_ledgers
.logical_writes
);
5783 ledger_disable_callback(ledger
, task_ledgers
.logical_writes
);
5787 return KERN_SUCCESS
;
5791 task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
)
5794 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO((int)param0
);
5798 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
)
5801 task_t task
= current_task();
5802 #ifdef EXC_RESOURCE_MONITORS
5803 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5804 #endif /* EXC_RESOURCE_MONITORS */
5805 struct ledger_entry_info lei
;
5809 pid
= proc_selfpid();
5812 * Get the ledger entry info. We need to do this before disabling the exception
5813 * to get correct values for all fields.
5816 case FLAVOR_IO_PHYSICAL_WRITES
:
5817 ledger_get_entry_info(task
->ledger
, task_ledgers
.physical_writes
, &lei
);
5819 case FLAVOR_IO_LOGICAL_WRITES
:
5820 ledger_get_entry_info(task
->ledger
, task_ledgers
.logical_writes
, &lei
);
5826 * Disable the exception notification so we don't overwhelm
5827 * the listener with an endless stream of redundant exceptions.
5828 * TODO: detect whether another thread is already reporting the violation.
5830 uint32_t flags
= IOMON_DISABLE
;
5831 task_io_monitor_ctl(task
, &flags
);
5833 if (flavor
== FLAVOR_IO_LOGICAL_WRITES
) {
5834 trace_resource_violation(RMON_LOGWRITES_VIOLATED
, &lei
);
5836 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",
5837 pid
, flavor
, (lei
.lei_balance
/ (1024 * 1024)), (lei
.lei_limit
/ (1024 * 1024)), (lei
.lei_refill_period
/ NSEC_PER_SEC
));
5839 kr
= send_resource_violation(send_disk_writes_violation
, task
, &lei
, kRNFlagsNone
);
5841 printf("send_resource_violation(disk_writes, ...): error %#x\n", kr
);
5844 #ifdef EXC_RESOURCE_MONITORS
5845 code
[0] = code
[1] = 0;
5846 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_IO
);
5847 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], flavor
);
5848 EXC_RESOURCE_IO_ENCODE_INTERVAL(code
[0], (lei
.lei_refill_period
/ NSEC_PER_SEC
));
5849 EXC_RESOURCE_IO_ENCODE_LIMIT(code
[0], (lei
.lei_limit
/ (1024 * 1024)));
5850 EXC_RESOURCE_IO_ENCODE_OBSERVED(code
[1], (lei
.lei_balance
/ (1024 * 1024)));
5851 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5852 #endif /* EXC_RESOURCE_MONITORS */
5855 /* Placeholders for the task set/get voucher interfaces */
5857 task_get_mach_voucher(
5859 mach_voucher_selector_t __unused which
,
5860 ipc_voucher_t
*voucher
)
5862 if (TASK_NULL
== task
)
5863 return KERN_INVALID_TASK
;
5866 return KERN_SUCCESS
;
5870 task_set_mach_voucher(
5872 ipc_voucher_t __unused voucher
)
5874 if (TASK_NULL
== task
)
5875 return KERN_INVALID_TASK
;
5877 return KERN_SUCCESS
;
5881 task_swap_mach_voucher(
5883 ipc_voucher_t new_voucher
,
5884 ipc_voucher_t
*in_out_old_voucher
)
5886 if (TASK_NULL
== task
)
5887 return KERN_INVALID_TASK
;
5889 *in_out_old_voucher
= new_voucher
;
5890 return KERN_SUCCESS
;
5893 void task_set_gpu_denied(task_t task
, boolean_t denied
)
5898 task
->t_flags
|= TF_GPU_DENIED
;
5900 task
->t_flags
&= ~TF_GPU_DENIED
;
5906 boolean_t
task_is_gpu_denied(task_t task
)
5908 /* We don't need the lock to read this flag */
5909 return (task
->t_flags
& TF_GPU_DENIED
) ? TRUE
: FALSE
;
5913 uint64_t get_task_memory_region_count(task_t task
)
5916 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
5917 return((uint64_t)get_map_nentries(map
));
5921 kdebug_trace_dyld_internal(uint32_t base_code
,
5922 struct dyld_kernel_image_info
*info
)
5924 static_assert(sizeof(info
->uuid
) >= 16);
5926 #if defined(__LP64__)
5927 uint64_t *uuid
= (uint64_t *)&(info
->uuid
);
5929 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5930 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
), uuid
[0],
5931 uuid
[1], info
->load_addr
,
5932 (uint64_t)info
->fsid
.val
[0] | ((uint64_t)info
->fsid
.val
[1] << 32),
5934 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5935 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 1),
5936 (uint64_t)info
->fsobjid
.fid_objno
|
5937 ((uint64_t)info
->fsobjid
.fid_generation
<< 32),
5939 #else /* defined(__LP64__) */
5940 uint32_t *uuid
= (uint32_t *)&(info
->uuid
);
5942 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5943 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 2), uuid
[0],
5944 uuid
[1], uuid
[2], uuid
[3], 0);
5945 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5946 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 3),
5947 (uint32_t)info
->load_addr
, info
->fsid
.val
[0], info
->fsid
.val
[1],
5948 info
->fsobjid
.fid_objno
, 0);
5949 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5950 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 4),
5951 info
->fsobjid
.fid_generation
, 0, 0, 0, 0);
5952 #endif /* !defined(__LP64__) */
5955 static kern_return_t
5956 kdebug_trace_dyld(task_t task
, uint32_t base_code
,
5957 vm_map_copy_t infos_copy
, mach_msg_type_number_t infos_len
)
5960 dyld_kernel_image_info_array_t infos
;
5961 vm_map_offset_t map_data
;
5965 return KERN_INVALID_ADDRESS
;
5968 if (!kdebug_enable
||
5969 !kdebug_debugid_enabled(KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, 0)))
5971 vm_map_copy_discard(infos_copy
);
5972 return KERN_SUCCESS
;
5975 if (task
== NULL
|| task
!= current_task()) {
5976 return KERN_INVALID_TASK
;
5979 kr
= vm_map_copyout(ipc_kernel_map
, &map_data
, (vm_map_copy_t
)infos_copy
);
5980 if (kr
!= KERN_SUCCESS
) {
5984 infos
= CAST_DOWN(dyld_kernel_image_info_array_t
, map_data
);
5986 for (mach_msg_type_number_t i
= 0; i
< infos_len
; i
++) {
5987 kdebug_trace_dyld_internal(base_code
, &(infos
[i
]));
5990 data
= CAST_DOWN(vm_offset_t
, map_data
);
5991 mach_vm_deallocate(ipc_kernel_map
, data
, infos_len
* sizeof(infos
[0]));
5992 return KERN_SUCCESS
;
5996 task_register_dyld_image_infos(task_t task
,
5997 dyld_kernel_image_info_array_t infos_copy
,
5998 mach_msg_type_number_t infos_len
)
6000 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_MAP_A
,
6001 (vm_map_copy_t
)infos_copy
, infos_len
);
6005 task_unregister_dyld_image_infos(task_t task
,
6006 dyld_kernel_image_info_array_t infos_copy
,
6007 mach_msg_type_number_t infos_len
)
6009 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_UNMAP_A
,
6010 (vm_map_copy_t
)infos_copy
, infos_len
);
6014 task_get_dyld_image_infos(__unused task_t task
,
6015 __unused dyld_kernel_image_info_array_t
* dyld_images
,
6016 __unused mach_msg_type_number_t
* dyld_imagesCnt
)
6018 return KERN_NOT_SUPPORTED
;
6022 task_register_dyld_shared_cache_image_info(task_t task
,
6023 dyld_kernel_image_info_t cache_img
,
6024 __unused boolean_t no_cache
,
6025 __unused boolean_t private_cache
)
6027 if (task
== NULL
|| task
!= current_task()) {
6028 return KERN_INVALID_TASK
;
6031 kdebug_trace_dyld_internal(DBG_DYLD_UUID_SHARED_CACHE_A
, &cache_img
);
6032 return KERN_SUCCESS
;
6036 task_register_dyld_set_dyld_state(__unused task_t task
,
6037 __unused
uint8_t dyld_state
)
6039 return KERN_NOT_SUPPORTED
;
6043 task_register_dyld_get_process_state(__unused task_t task
,
6044 __unused dyld_kernel_process_info_t
* dyld_process_state
)
6046 return KERN_NOT_SUPPORTED
;
6050 task_inspect(task_inspect_t task_insp
, task_inspect_flavor_t flavor
,
6051 task_inspect_info_t info_out
, mach_msg_type_number_t
*size_in_out
)
6054 task_t task
= (task_t
)task_insp
;
6055 kern_return_t kr
= KERN_SUCCESS
;
6056 mach_msg_type_number_t size
;
6058 if (task
== TASK_NULL
) {
6059 return KERN_INVALID_ARGUMENT
;
6062 size
= *size_in_out
;
6065 case TASK_INSPECT_BASIC_COUNTS
: {
6066 struct task_inspect_basic_counts
*bc
;
6067 uint64_t task_counts
[MT_CORE_NFIXED
];
6069 if (size
< TASK_INSPECT_BASIC_COUNTS_COUNT
) {
6070 kr
= KERN_INVALID_ARGUMENT
;
6074 mt_fixed_task_counts(task
, task_counts
);
6075 bc
= (struct task_inspect_basic_counts
*)info_out
;
6076 #ifdef MT_CORE_INSTRS
6077 bc
->instructions
= task_counts
[MT_CORE_INSTRS
];
6078 #else /* defined(MT_CORE_INSTRS) */
6079 bc
->instructions
= 0;
6080 #endif /* !defined(MT_CORE_INSTRS) */
6081 bc
->cycles
= task_counts
[MT_CORE_CYCLES
];
6082 size
= TASK_INSPECT_BASIC_COUNTS_COUNT
;
6086 kr
= KERN_INVALID_ARGUMENT
;
6090 if (kr
== KERN_SUCCESS
) {
6091 *size_in_out
= size
;
6094 #else /* MONOTONIC */
6095 #pragma unused(task_insp, flavor, info_out, size_in_out)
6096 return KERN_NOT_SUPPORTED
;
6097 #endif /* !MONOTONIC */
6100 #if CONFIG_SECLUDED_MEMORY
6101 int num_tasks_can_use_secluded_mem
= 0;
6104 task_set_can_use_secluded_mem(
6106 boolean_t can_use_secluded_mem
)
6108 if (!task
->task_could_use_secluded_mem
) {
6112 task_set_can_use_secluded_mem_locked(task
, can_use_secluded_mem
);
6117 task_set_can_use_secluded_mem_locked(
6119 boolean_t can_use_secluded_mem
)
6121 assert(task
->task_could_use_secluded_mem
);
6122 if (can_use_secluded_mem
&&
6123 secluded_for_apps
&& /* global boot-arg */
6124 !task
->task_can_use_secluded_mem
) {
6125 assert(num_tasks_can_use_secluded_mem
>= 0);
6127 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
6128 task
->task_can_use_secluded_mem
= TRUE
;
6129 } else if (!can_use_secluded_mem
&&
6130 task
->task_can_use_secluded_mem
) {
6131 assert(num_tasks_can_use_secluded_mem
> 0);
6133 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
6134 task
->task_can_use_secluded_mem
= FALSE
;
6139 task_set_could_use_secluded_mem(
6141 boolean_t could_use_secluded_mem
)
6143 task
->task_could_use_secluded_mem
= could_use_secluded_mem
;
6147 task_set_could_also_use_secluded_mem(
6149 boolean_t could_also_use_secluded_mem
)
6151 task
->task_could_also_use_secluded_mem
= could_also_use_secluded_mem
;
6155 task_can_use_secluded_mem(
6158 if (task
->task_can_use_secluded_mem
) {
6159 assert(task
->task_could_use_secluded_mem
);
6160 assert(num_tasks_can_use_secluded_mem
> 0);
6163 if (task
->task_could_also_use_secluded_mem
&&
6164 num_tasks_can_use_secluded_mem
> 0) {
6165 assert(num_tasks_can_use_secluded_mem
> 0);
6172 task_could_use_secluded_mem(
6175 return task
->task_could_use_secluded_mem
;
6177 #endif /* CONFIG_SECLUDED_MEMORY */
6180 task_io_user_clients(task_t task
)
6182 return (&task
->io_user_clients
);
6186 task_copy_fields_for_exec(task_t dst_task
, task_t src_task
)
6188 dst_task
->vtimers
= src_task
->vtimers
;
6191 #if DEVELOPMENT || DEBUG
6192 int vm_region_footprint
= 0;
6193 #endif /* DEVELOPMENT || DEBUG */
6196 task_self_region_footprint(void)
6198 #if DEVELOPMENT || DEBUG
6199 if (vm_region_footprint
) {
6200 /* system-wide override */
6203 #endif /* DEVELOPMENT || DEBUG */
6204 return current_task()->task_region_footprint
;
6208 task_self_region_footprint_set(
6213 curtask
= current_task();
6216 curtask
->task_region_footprint
= TRUE
;
6218 curtask
->task_region_footprint
= FALSE
;
6220 task_unlock(curtask
);