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>
164 #include <libkern/section_keywords.h>
167 #include <atm/atm_internal.h>
170 #include <kern/sfi.h> /* picks up ledger.h */
173 #include <security/mac_mach_internal.h>
177 extern int kpc_force_all_ctrs(task_t
, int);
182 lck_attr_t task_lck_attr
;
183 lck_grp_t task_lck_grp
;
184 lck_grp_attr_t task_lck_grp_attr
;
186 extern int exc_via_corpse_forking
;
187 extern int corpse_for_fatal_memkill
;
188 extern boolean_t
proc_send_synchronous_EXC_RESOURCE(void *p
);
190 /* Flag set by core audio when audio is playing. Used to stifle EXC_RESOURCE generation when active. */
191 int audio_active
= 0;
193 zinfo_usage_store_t tasks_tkm_private
;
194 zinfo_usage_store_t tasks_tkm_shared
;
196 /* A container to accumulate statistics for expired tasks */
197 expired_task_statistics_t dead_task_statistics
;
198 lck_spin_t dead_task_statistics_lock
;
200 ledger_template_t task_ledger_template
= NULL
;
202 SECURITY_READ_ONLY_LATE(struct _task_ledger_indices
) task_ledgers
__attribute__((used
)) =
210 .alternate_accounting
= -1,
211 .alternate_accounting_compressed
= -1,
213 .phys_footprint
= -1,
214 .internal_compressed
= -1,
215 .purgeable_volatile
= -1,
216 .purgeable_nonvolatile
= -1,
217 .purgeable_volatile_compressed
= -1,
218 .purgeable_nonvolatile_compressed
= -1,
219 .network_volatile
= -1,
220 .network_nonvolatile
= -1,
221 .network_volatile_compressed
= -1,
222 .network_nonvolatile_compressed
= -1,
223 .platform_idle_wakeups
= -1,
224 .interrupt_wakeups
= -1,
226 .sfi_wait_times
= { 0 /* initialized at runtime */},
227 #endif /* !CONFIG_EMBEDDED */
228 .cpu_time_billed_to_me
= -1,
229 .cpu_time_billed_to_others
= -1,
230 .physical_writes
= -1,
231 .logical_writes
= -1,
232 .energy_billed_to_me
= -1,
233 .energy_billed_to_others
= -1
236 /* System sleep state */
237 boolean_t tasks_suspend_state
;
240 void init_task_ledgers(void);
241 void task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
242 void task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
243 void task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
);
244 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void);
245 void __attribute__((noinline
)) PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
);
246 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
);
248 kern_return_t
task_suspend_internal(task_t
);
249 kern_return_t
task_resume_internal(task_t
);
250 static kern_return_t
task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
);
252 extern kern_return_t
iokit_task_terminate(task_t task
);
254 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
*);
255 extern void bsd_copythreadname(void *dst_uth
, void *src_uth
);
256 extern kern_return_t
thread_resume(thread_t thread
);
258 // Warn tasks when they hit 80% of their memory limit.
259 #define PHYS_FOOTPRINT_WARNING_LEVEL 80
261 #define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT 150 /* wakeups per second */
262 #define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL 300 /* in seconds. */
265 * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
267 * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
268 * stacktraces, aka micro-stackshots)
270 #define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER 70
272 int task_wakeups_monitor_interval
; /* In seconds. Time period over which wakeups rate is observed */
273 int task_wakeups_monitor_rate
; /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
275 int task_wakeups_monitor_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
277 int disable_exc_resource
; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
279 ledger_amount_t max_task_footprint
= 0; /* Per-task limit on physical memory consumption in bytes */
280 int max_task_footprint_warning_level
= 0; /* Per-task limit warning percentage */
281 int max_task_footprint_mb
= 0; /* Per-task limit on physical memory consumption in megabytes */
283 /* I/O Monitor Limits */
284 #define IOMON_DEFAULT_LIMIT (20480ull) /* MB of logical/physical I/O */
285 #define IOMON_DEFAULT_INTERVAL (86400ull) /* in seconds */
287 uint64_t task_iomon_limit_mb
; /* Per-task I/O monitor limit in MBs */
288 uint64_t task_iomon_interval_secs
; /* Per-task I/O monitor interval in secs */
290 #define IO_TELEMETRY_DEFAULT_LIMIT (10ll * 1024ll * 1024ll)
291 int64_t io_telemetry_limit
; /* Threshold to take a microstackshot (0 indicated I/O telemetry is turned off) */
292 int64_t global_logical_writes_count
= 0; /* Global count for logical writes */
293 static boolean_t
global_update_logical_writes(int64_t);
295 #define TASK_MAX_THREAD_LIMIT 256
298 int pmap_ledgers_panic
= 1;
299 int pmap_ledgers_panic_leeway
= 3;
300 #endif /* MACH_ASSERT */
302 int task_max
= CONFIG_TASK_MAX
; /* Max number of tasks */
305 int hwm_user_cores
= 0; /* high watermark violations generate user core files */
309 extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
310 extern int proc_pid(struct proc
*p
);
311 extern int proc_selfpid(void);
312 extern struct proc
*current_proc(void);
313 extern char *proc_name_address(struct proc
*p
);
314 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
315 extern int kevent_proc_copy_uptrs(void *proc
, uint64_t *buf
, int bufsize
);
316 extern void workq_proc_suspended(struct proc
*p
);
317 extern void workq_proc_resumed(struct proc
*p
);
319 #if CONFIG_MEMORYSTATUS
320 extern void proc_memstat_terminated(struct proc
* p
, boolean_t set
);
321 extern void memorystatus_on_ledger_footprint_exceeded(int warning
, boolean_t memlimit_is_active
, boolean_t memlimit_is_fatal
);
322 extern void memorystatus_log_exception(const int max_footprint_mb
, boolean_t memlimit_is_active
, boolean_t memlimit_is_fatal
);
323 extern boolean_t
memorystatus_allowed_vm_map_fork(task_t task
);
325 #if DEVELOPMENT || DEBUG
326 extern void memorystatus_abort_vm_map_fork(task_t
);
329 #endif /* CONFIG_MEMORYSTATUS */
331 #endif /* MACH_BSD */
333 #if DEVELOPMENT || DEBUG
334 int exc_resource_threads_enabled
;
335 #endif /* DEVELOPMENT || DEBUG */
337 #if (DEVELOPMENT || DEBUG) && TASK_EXC_GUARD_DELIVER_CORPSE
338 uint32_t task_exc_guard_default
= TASK_EXC_GUARD_MP_DELIVER
| TASK_EXC_GUARD_MP_CORPSE
|
339 TASK_EXC_GUARD_VM_DELIVER
| TASK_EXC_GUARD_VM_CORPSE
;
341 uint32_t task_exc_guard_default
= 0;
346 static void task_hold_locked(task_t task
);
347 static void task_wait_locked(task_t task
, boolean_t until_not_runnable
);
348 static void task_release_locked(task_t task
);
350 static void task_synchronizer_destroy_all(task_t task
);
357 boolean_t is_64bit_data
)
359 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
361 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
366 * Switching to/from 64-bit address spaces
369 if (!task_has_64Bit_addr(task
)) {
370 task_set_64Bit_addr(task
);
373 if (task_has_64Bit_addr(task
)) {
374 task_clear_64Bit_addr(task
);
379 * Switching to/from 64-bit register state.
382 if (task_has_64Bit_data(task
))
385 task_set_64Bit_data(task
);
387 if ( !task_has_64Bit_data(task
))
390 task_clear_64Bit_data(task
);
393 /* FIXME: On x86, the thread save state flavor can diverge from the
394 * task's 64-bit feature flag due to the 32-bit/64-bit register save
395 * state dichotomy. Since we can be pre-empted in this interval,
396 * certain routines may observe the thread as being in an inconsistent
397 * state with respect to its task's 64-bitness.
400 #if defined(__x86_64__) || defined(__arm64__)
401 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
402 thread_mtx_lock(thread
);
403 machine_thread_switch_addrmode(thread
);
404 thread_mtx_unlock(thread
);
406 #if defined(__arm64__)
407 /* specifically, if running on H9 */
408 if (thread
== current_thread()) {
411 spl_t spl
= splsched();
413 * This call tell that the current thread changed it's 32bitness.
414 * Other thread were no more on core when 32bitness was changed,
415 * but current_thread() is on core and the previous call to
416 * machine_thread_going_on_core() gave 32bitness which is now wrong.
418 * This is needed for bring-up, a different callback should be used
421 * TODO: Remove this callout when we no longer support 32-bit code on H9
424 urgency
= thread_get_urgency(thread
, &arg1
, &arg2
);
425 machine_thread_going_on_core(thread
, urgency
, 0, 0, mach_approximate_time());
426 thread_unlock(thread
);
429 #endif /* defined(__arm64__) */
431 #endif /* defined(__x86_64__) || defined(__arm64__) */
438 task_get_64bit_data(task_t task
)
440 return task_has_64Bit_data(task
);
444 task_set_platform_binary(
446 boolean_t is_platform
)
450 task
->t_flags
|= TF_PLATFORM
;
452 task
->t_flags
&= ~(TF_PLATFORM
);
458 * Set or clear per-task TF_CA_CLIENT_WI flag according to specified argument.
459 * Returns "false" if flag is already set, and "true" in other cases.
462 task_set_ca_client_wi(
464 boolean_t set_or_clear
)
469 /* Tasks can have only one CA_CLIENT work interval */
470 if (task
->t_flags
& TF_CA_CLIENT_WI
)
473 task
->t_flags
|= TF_CA_CLIENT_WI
;
475 task
->t_flags
&= ~TF_CA_CLIENT_WI
;
484 mach_vm_address_t addr
,
488 task
->all_image_info_addr
= addr
;
489 task
->all_image_info_size
= size
;
494 task_atm_reset(__unused task_t task
) {
497 if (task
->atm_context
!= NULL
) {
498 atm_task_descriptor_destroy(task
->atm_context
);
499 task
->atm_context
= NULL
;
506 task_bank_reset(__unused task_t task
) {
508 if (task
->bank_context
!= NULL
) {
509 bank_task_destroy(task
);
514 * NOTE: This should only be called when the P_LINTRANSIT
515 * flag is set (the proc_trans lock is held) on the
516 * proc associated with the task.
519 task_bank_init(__unused task_t task
) {
521 if (task
->bank_context
!= NULL
) {
522 panic("Task bank init called with non null bank context for task: %p and bank_context: %p", task
, task
->bank_context
);
524 bank_task_initialize(task
);
528 task_set_did_exec_flag(task_t task
)
530 task
->t_procflags
|= TPF_DID_EXEC
;
534 task_clear_exec_copy_flag(task_t task
)
536 task
->t_procflags
&= ~TPF_EXEC_COPY
;
540 * This wait event is t_procflags instead of t_flags because t_flags is volatile
542 * TODO: store the flags in the same place as the event
543 * rdar://problem/28501994
546 task_get_return_wait_event(task_t task
)
548 return (event_t
)&task
->t_procflags
;
552 task_clear_return_wait(task_t task
)
556 task
->t_flags
&= ~TF_LRETURNWAIT
;
558 if (task
->t_flags
& TF_LRETURNWAITER
) {
559 thread_wakeup(task_get_return_wait_event(task
));
560 task
->t_flags
&= ~TF_LRETURNWAITER
;
566 void __attribute__((noreturn
))
567 task_wait_to_return(void)
571 task
= current_task();
574 if (task
->t_flags
& TF_LRETURNWAIT
) {
576 task
->t_flags
|= TF_LRETURNWAITER
;
577 assert_wait(task_get_return_wait_event(task
), THREAD_UNINT
);
580 thread_block(THREAD_CONTINUE_NULL
);
583 } while (task
->t_flags
& TF_LRETURNWAIT
);
590 * Before jumping to userspace and allowing this process to execute any code,
591 * notify any interested parties.
593 mac_proc_notify_exec_complete(current_proc());
596 thread_bootstrap_return();
599 #ifdef CONFIG_32BIT_TELEMETRY
601 task_consume_32bit_log_flag(task_t task
)
603 if ((task
->t_procflags
& TPF_LOG_32BIT_TELEMETRY
) != 0) {
604 task
->t_procflags
&= ~TPF_LOG_32BIT_TELEMETRY
;
612 task_set_32bit_log_flag(task_t task
)
614 task
->t_procflags
|= TPF_LOG_32BIT_TELEMETRY
;
616 #endif /* CONFIG_32BIT_TELEMETRY */
619 task_is_exec_copy(task_t task
)
621 return task_is_exec_copy_internal(task
);
625 task_did_exec(task_t task
)
627 return task_did_exec_internal(task
);
631 task_is_active(task_t task
)
637 task_is_halting(task_t task
)
639 return task
->halting
;
642 #if TASK_REFERENCE_LEAK_DEBUG
643 #include <kern/btlog.h>
645 static btlog_t
*task_ref_btlog
;
646 #define TASK_REF_OP_INCR 0x1
647 #define TASK_REF_OP_DECR 0x2
649 #define TASK_REF_NUM_RECORDS 100000
650 #define TASK_REF_BTDEPTH 7
653 task_reference_internal(task_t task
)
655 void * bt
[TASK_REF_BTDEPTH
];
658 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
660 (void)hw_atomic_add(&(task
)->ref_count
, 1);
661 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_INCR
,
666 task_deallocate_internal(task_t task
)
668 void * bt
[TASK_REF_BTDEPTH
];
671 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
673 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_DECR
,
675 return hw_atomic_sub(&(task
)->ref_count
, 1);
678 #endif /* TASK_REFERENCE_LEAK_DEBUG */
684 lck_grp_attr_setdefault(&task_lck_grp_attr
);
685 lck_grp_init(&task_lck_grp
, "task", &task_lck_grp_attr
);
686 lck_attr_setdefault(&task_lck_attr
);
687 lck_mtx_init(&tasks_threads_lock
, &task_lck_grp
, &task_lck_attr
);
688 lck_mtx_init(&tasks_corpse_lock
, &task_lck_grp
, &task_lck_attr
);
692 task_max
* sizeof(struct task
),
693 TASK_CHUNK
* sizeof(struct task
),
696 zone_change(task_zone
, Z_NOENCRYPT
, TRUE
);
700 #endif /* CONFIG_EMBEDDED */
703 * Configure per-task memory limit.
704 * The boot-arg is interpreted as Megabytes,
705 * and takes precedence over the device tree.
706 * Setting the boot-arg to 0 disables task limits.
708 if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb
,
709 sizeof (max_task_footprint_mb
))) {
711 * No limit was found in boot-args, so go look in the device tree.
713 if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb
,
714 sizeof(max_task_footprint_mb
))) {
716 * No limit was found in device tree.
718 max_task_footprint_mb
= 0;
722 if (max_task_footprint_mb
!= 0) {
723 #if CONFIG_MEMORYSTATUS
724 if (max_task_footprint_mb
< 50) {
725 printf("Warning: max_task_pmem %d below minimum.\n",
726 max_task_footprint_mb
);
727 max_task_footprint_mb
= 50;
729 printf("Limiting task physical memory footprint to %d MB\n",
730 max_task_footprint_mb
);
732 max_task_footprint
= (ledger_amount_t
)max_task_footprint_mb
* 1024 * 1024; // Convert MB to bytes
735 * Configure the per-task memory limit warning level.
736 * This is computed as a percentage.
738 max_task_footprint_warning_level
= 0;
740 if (max_mem
< 0x40000000) {
742 * On devices with < 1GB of memory:
743 * -- set warnings to 50MB below the per-task limit.
745 if (max_task_footprint_mb
> 50) {
746 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 50) * 100) / max_task_footprint_mb
;
750 * On devices with >= 1GB of memory:
751 * -- set warnings to 100MB below the per-task limit.
753 if (max_task_footprint_mb
> 100) {
754 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 100) * 100) / max_task_footprint_mb
;
759 * Never allow warning level to land below the default.
761 if (max_task_footprint_warning_level
< PHYS_FOOTPRINT_WARNING_LEVEL
) {
762 max_task_footprint_warning_level
= PHYS_FOOTPRINT_WARNING_LEVEL
;
765 printf("Limiting task physical memory warning to %d%%\n", max_task_footprint_warning_level
);
768 printf("Warning: max_task_pmem specified, but jetsam not configured; ignoring.\n");
769 #endif /* CONFIG_MEMORYSTATUS */
772 #if DEVELOPMENT || DEBUG
773 if (!PE_parse_boot_argn("exc_resource_threads",
774 &exc_resource_threads_enabled
,
775 sizeof(exc_resource_threads_enabled
))) {
776 exc_resource_threads_enabled
= 1;
778 PE_parse_boot_argn("task_exc_guard_default",
779 &task_exc_guard_default
,
780 sizeof(task_exc_guard_default
));
781 #endif /* DEVELOPMENT || DEBUG */
784 if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores
,
785 sizeof (hwm_user_cores
))) {
790 proc_init_cpumon_params();
792 if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate
, sizeof (task_wakeups_monitor_rate
))) {
793 task_wakeups_monitor_rate
= TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT
;
796 if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval
, sizeof (task_wakeups_monitor_interval
))) {
797 task_wakeups_monitor_interval
= TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL
;
800 if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct
,
801 sizeof (task_wakeups_monitor_ustackshots_trigger_pct
))) {
802 task_wakeups_monitor_ustackshots_trigger_pct
= TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER
;
805 if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource
,
806 sizeof (disable_exc_resource
))) {
807 disable_exc_resource
= 0;
810 if (!PE_parse_boot_argn("task_iomon_limit_mb", &task_iomon_limit_mb
, sizeof (task_iomon_limit_mb
))) {
811 task_iomon_limit_mb
= IOMON_DEFAULT_LIMIT
;
814 if (!PE_parse_boot_argn("task_iomon_interval_secs", &task_iomon_interval_secs
, sizeof (task_iomon_interval_secs
))) {
815 task_iomon_interval_secs
= IOMON_DEFAULT_INTERVAL
;
818 if (!PE_parse_boot_argn("io_telemetry_limit", &io_telemetry_limit
, sizeof (io_telemetry_limit
))) {
819 io_telemetry_limit
= IO_TELEMETRY_DEFAULT_LIMIT
;
823 * If we have coalitions, coalition_init() will call init_task_ledgers() as it
824 * sets up the ledgers for the default coalition. If we don't have coalitions,
825 * then we have to call it now.
827 #if CONFIG_COALITIONS
828 assert(task_ledger_template
);
829 #else /* CONFIG_COALITIONS */
831 #endif /* CONFIG_COALITIONS */
833 #if TASK_REFERENCE_LEAK_DEBUG
834 task_ref_btlog
= btlog_create(TASK_REF_NUM_RECORDS
, TASK_REF_BTDEPTH
, TRUE
/* caller_will_remove_entries_for_element? */);
835 assert(task_ref_btlog
);
839 * Create the kernel task as the first task.
842 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, TRUE
, TRUE
, TF_NONE
, TPF_NONE
, &kernel_task
) != KERN_SUCCESS
)
844 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, FALSE
, FALSE
, TF_NONE
, TPF_NONE
, &kernel_task
) != KERN_SUCCESS
)
846 panic("task_init\n");
849 vm_map_deallocate(kernel_task
->map
);
850 kernel_task
->map
= kernel_map
;
851 lck_spin_init(&dead_task_statistics_lock
, &task_lck_grp
, &task_lck_attr
);
855 * Create a task running in the kernel address space. It may
856 * have its own map of size mem_size and may have ipc privileges.
860 __unused task_t parent_task
,
861 __unused vm_offset_t map_base
,
862 __unused vm_size_t map_size
,
863 __unused task_t
*child_task
)
865 return (KERN_INVALID_ARGUMENT
);
871 __unused ledger_port_array_t ledger_ports
,
872 __unused mach_msg_type_number_t num_ledger_ports
,
873 __unused boolean_t inherit_memory
,
874 __unused task_t
*child_task
) /* OUT */
876 if (parent_task
== TASK_NULL
)
877 return(KERN_INVALID_ARGUMENT
);
880 * No longer supported: too many calls assume that a task has a valid
883 return(KERN_FAILURE
);
887 host_security_create_task_token(
888 host_security_t host_security
,
890 __unused security_token_t sec_token
,
891 __unused audit_token_t audit_token
,
892 __unused host_priv_t host_priv
,
893 __unused ledger_port_array_t ledger_ports
,
894 __unused mach_msg_type_number_t num_ledger_ports
,
895 __unused boolean_t inherit_memory
,
896 __unused task_t
*child_task
) /* OUT */
898 if (parent_task
== TASK_NULL
)
899 return(KERN_INVALID_ARGUMENT
);
901 if (host_security
== HOST_NULL
)
902 return(KERN_INVALID_SECURITY
);
905 * No longer supported.
907 return(KERN_FAILURE
);
915 * Physical footprint: This is the sum of:
916 * + (internal - alternate_accounting)
917 * + (internal_compressed - alternate_accounting_compressed)
919 * + purgeable_nonvolatile
920 * + purgeable_nonvolatile_compressed
924 * The task's anonymous memory, which on iOS is always resident.
926 * internal_compressed
927 * Amount of this task's internal memory which is held by the compressor.
928 * Such memory is no longer actually resident for the task [i.e., resident in its pmap],
929 * and could be either decompressed back into memory, or paged out to storage, depending
930 * on our implementation.
933 * IOKit mappings: The total size of all IOKit mappings in this task, regardless of
934 clean/dirty or internal/external state].
936 * alternate_accounting
937 * The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
938 * are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
942 init_task_ledgers(void)
946 assert(task_ledger_template
== NULL
);
947 assert(kernel_task
== TASK_NULL
);
950 PE_parse_boot_argn("pmap_ledgers_panic",
952 sizeof (pmap_ledgers_panic
));
953 PE_parse_boot_argn("pmap_ledgers_panic_leeway",
954 &pmap_ledgers_panic_leeway
,
955 sizeof (pmap_ledgers_panic_leeway
));
956 #endif /* MACH_ASSERT */
958 if ((t
= ledger_template_create("Per-task ledger")) == NULL
)
959 panic("couldn't create task ledger template");
961 task_ledgers
.cpu_time
= ledger_entry_add(t
, "cpu_time", "sched", "ns");
962 task_ledgers
.tkm_private
= ledger_entry_add(t
, "tkm_private",
964 task_ledgers
.tkm_shared
= ledger_entry_add(t
, "tkm_shared", "physmem",
966 task_ledgers
.phys_mem
= ledger_entry_add(t
, "phys_mem", "physmem",
968 task_ledgers
.wired_mem
= ledger_entry_add(t
, "wired_mem", "physmem",
970 task_ledgers
.internal
= ledger_entry_add(t
, "internal", "physmem",
972 task_ledgers
.iokit_mapped
= ledger_entry_add(t
, "iokit_mapped", "mappings",
974 task_ledgers
.alternate_accounting
= ledger_entry_add(t
, "alternate_accounting", "physmem",
976 task_ledgers
.alternate_accounting_compressed
= ledger_entry_add(t
, "alternate_accounting_compressed", "physmem",
978 task_ledgers
.page_table
= ledger_entry_add(t
, "page_table", "physmem",
980 task_ledgers
.phys_footprint
= ledger_entry_add(t
, "phys_footprint", "physmem",
982 task_ledgers
.internal_compressed
= ledger_entry_add(t
, "internal_compressed", "physmem",
984 task_ledgers
.purgeable_volatile
= ledger_entry_add(t
, "purgeable_volatile", "physmem", "bytes");
985 task_ledgers
.purgeable_nonvolatile
= ledger_entry_add(t
, "purgeable_nonvolatile", "physmem", "bytes");
986 task_ledgers
.purgeable_volatile_compressed
= ledger_entry_add(t
, "purgeable_volatile_compress", "physmem", "bytes");
987 task_ledgers
.purgeable_nonvolatile_compressed
= ledger_entry_add(t
, "purgeable_nonvolatile_compress", "physmem", "bytes");
989 task_ledgers
.network_volatile
= ledger_entry_add(t
, "network_volatile", "physmem", "bytes");
990 task_ledgers
.network_nonvolatile
= ledger_entry_add(t
, "network_nonvolatile", "physmem", "bytes");
991 task_ledgers
.network_volatile_compressed
= ledger_entry_add(t
, "network_volatile_compressed", "physmem", "bytes");
992 task_ledgers
.network_nonvolatile_compressed
= ledger_entry_add(t
, "network_nonvolatile_compressed", "physmem", "bytes");
994 task_ledgers
.platform_idle_wakeups
= ledger_entry_add(t
, "platform_idle_wakeups", "power",
996 task_ledgers
.interrupt_wakeups
= ledger_entry_add(t
, "interrupt_wakeups", "power",
1000 sfi_class_id_t class_id
, ledger_alias
;
1001 for (class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1002 task_ledgers
.sfi_wait_times
[class_id
] = -1;
1005 /* don't account for UNSPECIFIED */
1006 for (class_id
= SFI_CLASS_UNSPECIFIED
+ 1; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1007 ledger_alias
= sfi_get_ledger_alias_for_class(class_id
);
1008 if (ledger_alias
!= SFI_CLASS_UNSPECIFIED
) {
1009 /* Check to see if alias has been registered yet */
1010 if (task_ledgers
.sfi_wait_times
[ledger_alias
] != -1) {
1011 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
];
1013 /* Otherwise, initialize it first */
1014 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
] = sfi_ledger_entry_add(t
, ledger_alias
);
1017 task_ledgers
.sfi_wait_times
[class_id
] = sfi_ledger_entry_add(t
, class_id
);
1020 if (task_ledgers
.sfi_wait_times
[class_id
] < 0) {
1021 panic("couldn't create entries for task ledger template for SFI class 0x%x", class_id
);
1025 assert(task_ledgers
.sfi_wait_times
[MAX_SFI_CLASS_ID
-1] != -1);
1026 #endif /* CONFIG_SCHED_SFI */
1028 task_ledgers
.cpu_time_billed_to_me
= ledger_entry_add(t
, "cpu_time_billed_to_me", "sched", "ns");
1029 task_ledgers
.cpu_time_billed_to_others
= ledger_entry_add(t
, "cpu_time_billed_to_others", "sched", "ns");
1030 task_ledgers
.physical_writes
= ledger_entry_add(t
, "physical_writes", "res", "bytes");
1031 task_ledgers
.logical_writes
= ledger_entry_add(t
, "logical_writes", "res", "bytes");
1032 task_ledgers
.energy_billed_to_me
= ledger_entry_add(t
, "energy_billed_to_me", "power", "nj");
1033 task_ledgers
.energy_billed_to_others
= ledger_entry_add(t
, "energy_billed_to_others", "power", "nj");
1035 if ((task_ledgers
.cpu_time
< 0) ||
1036 (task_ledgers
.tkm_private
< 0) ||
1037 (task_ledgers
.tkm_shared
< 0) ||
1038 (task_ledgers
.phys_mem
< 0) ||
1039 (task_ledgers
.wired_mem
< 0) ||
1040 (task_ledgers
.internal
< 0) ||
1041 (task_ledgers
.iokit_mapped
< 0) ||
1042 (task_ledgers
.alternate_accounting
< 0) ||
1043 (task_ledgers
.alternate_accounting_compressed
< 0) ||
1044 (task_ledgers
.page_table
< 0) ||
1045 (task_ledgers
.phys_footprint
< 0) ||
1046 (task_ledgers
.internal_compressed
< 0) ||
1047 (task_ledgers
.purgeable_volatile
< 0) ||
1048 (task_ledgers
.purgeable_nonvolatile
< 0) ||
1049 (task_ledgers
.purgeable_volatile_compressed
< 0) ||
1050 (task_ledgers
.purgeable_nonvolatile_compressed
< 0) ||
1051 (task_ledgers
.network_volatile
< 0) ||
1052 (task_ledgers
.network_nonvolatile
< 0) ||
1053 (task_ledgers
.network_volatile_compressed
< 0) ||
1054 (task_ledgers
.network_nonvolatile_compressed
< 0) ||
1055 (task_ledgers
.platform_idle_wakeups
< 0) ||
1056 (task_ledgers
.interrupt_wakeups
< 0) ||
1057 (task_ledgers
.cpu_time_billed_to_me
< 0) || (task_ledgers
.cpu_time_billed_to_others
< 0) ||
1058 (task_ledgers
.physical_writes
< 0) ||
1059 (task_ledgers
.logical_writes
< 0) ||
1060 (task_ledgers
.energy_billed_to_me
< 0) ||
1061 (task_ledgers
.energy_billed_to_others
< 0)
1063 panic("couldn't create entries for task ledger template");
1066 ledger_track_credit_only(t
, task_ledgers
.phys_footprint
);
1067 ledger_track_credit_only(t
, task_ledgers
.page_table
);
1068 ledger_track_credit_only(t
, task_ledgers
.internal
);
1069 ledger_track_credit_only(t
, task_ledgers
.internal_compressed
);
1070 ledger_track_credit_only(t
, task_ledgers
.iokit_mapped
);
1071 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting
);
1072 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting_compressed
);
1073 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile
);
1074 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile
);
1075 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile_compressed
);
1076 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
1078 ledger_track_credit_only(t
, task_ledgers
.network_volatile
);
1079 ledger_track_credit_only(t
, task_ledgers
.network_nonvolatile
);
1080 ledger_track_credit_only(t
, task_ledgers
.network_volatile_compressed
);
1081 ledger_track_credit_only(t
, task_ledgers
.network_nonvolatile_compressed
);
1083 ledger_track_maximum(t
, task_ledgers
.phys_footprint
, 60);
1085 if (pmap_ledgers_panic
) {
1086 ledger_panic_on_negative(t
, task_ledgers
.phys_footprint
);
1087 ledger_panic_on_negative(t
, task_ledgers
.page_table
);
1088 ledger_panic_on_negative(t
, task_ledgers
.internal
);
1089 ledger_panic_on_negative(t
, task_ledgers
.internal_compressed
);
1090 ledger_panic_on_negative(t
, task_ledgers
.iokit_mapped
);
1091 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting
);
1092 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting_compressed
);
1093 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile
);
1094 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile
);
1095 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile_compressed
);
1096 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
1098 ledger_panic_on_negative(t
, task_ledgers
.network_volatile
);
1099 ledger_panic_on_negative(t
, task_ledgers
.network_nonvolatile
);
1100 ledger_panic_on_negative(t
, task_ledgers
.network_volatile_compressed
);
1101 ledger_panic_on_negative(t
, task_ledgers
.network_nonvolatile_compressed
);
1103 #endif /* MACH_ASSERT */
1105 #if CONFIG_MEMORYSTATUS
1106 ledger_set_callback(t
, task_ledgers
.phys_footprint
, task_footprint_exceeded
, NULL
, NULL
);
1107 #endif /* CONFIG_MEMORYSTATUS */
1109 ledger_set_callback(t
, task_ledgers
.interrupt_wakeups
,
1110 task_wakeups_rate_exceeded
, NULL
, NULL
);
1111 ledger_set_callback(t
, task_ledgers
.physical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_PHYSICAL_WRITES
, NULL
);
1112 ledger_set_callback(t
, task_ledgers
.logical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_LOGICAL_WRITES
, NULL
);
1114 ledger_template_complete(t
);
1115 task_ledger_template
= t
;
1119 task_create_internal(
1121 coalition_t
*parent_coalitions __unused
,
1122 boolean_t inherit_memory
,
1123 __unused boolean_t is_64bit
,
1124 boolean_t is_64bit_data
,
1126 uint32_t t_procflags
,
1127 task_t
*child_task
) /* OUT */
1130 vm_shared_region_t shared_region
;
1131 ledger_t ledger
= NULL
;
1133 new_task
= (task_t
) zalloc(task_zone
);
1135 if (new_task
== TASK_NULL
)
1136 return(KERN_RESOURCE_SHORTAGE
);
1138 /* one ref for just being alive; one for our caller */
1139 new_task
->ref_count
= 2;
1141 /* allocate with active entries */
1142 assert(task_ledger_template
!= NULL
);
1143 if ((ledger
= ledger_instantiate(task_ledger_template
,
1144 LEDGER_CREATE_ACTIVE_ENTRIES
)) == NULL
) {
1145 zfree(task_zone
, new_task
);
1146 return(KERN_RESOURCE_SHORTAGE
);
1150 new_task
->ledger
= ledger
;
1152 #if defined(CONFIG_SCHED_MULTIQ)
1153 new_task
->sched_group
= sched_group_create();
1156 /* if inherit_memory is true, parent_task MUST not be NULL */
1157 if (!(t_flags
& TF_CORPSE_FORK
) && inherit_memory
)
1158 new_task
->map
= vm_map_fork(ledger
, parent_task
->map
, 0);
1160 new_task
->map
= vm_map_create(pmap_create(ledger
, 0, is_64bit
),
1161 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
1162 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
1164 /* Inherit memlock limit from parent */
1166 vm_map_set_user_wire_limit(new_task
->map
, (vm_size_t
)parent_task
->map
->user_wire_limit
);
1168 lck_mtx_init(&new_task
->lock
, &task_lck_grp
, &task_lck_attr
);
1169 queue_init(&new_task
->threads
);
1170 new_task
->suspend_count
= 0;
1171 new_task
->thread_count
= 0;
1172 new_task
->active_thread_count
= 0;
1173 new_task
->user_stop_count
= 0;
1174 new_task
->legacy_stop_count
= 0;
1175 new_task
->active
= TRUE
;
1176 new_task
->halting
= FALSE
;
1177 new_task
->priv_flags
= 0;
1178 new_task
->t_flags
= t_flags
;
1179 new_task
->t_procflags
= t_procflags
;
1180 new_task
->importance
= 0;
1181 new_task
->crashed_thread_id
= 0;
1182 new_task
->exec_token
= 0;
1184 new_task
->task_exc_guard
= task_exc_guard_default
;
1187 new_task
->atm_context
= NULL
;
1189 new_task
->bank_context
= NULL
;
1192 new_task
->bsd_info
= NULL
;
1193 new_task
->corpse_info
= NULL
;
1194 #endif /* MACH_BSD */
1197 new_task
->crash_label
= NULL
;
1200 #if CONFIG_MEMORYSTATUS
1201 if (max_task_footprint
!= 0) {
1202 ledger_set_limit(ledger
, task_ledgers
.phys_footprint
, max_task_footprint
, PHYS_FOOTPRINT_WARNING_LEVEL
);
1204 #endif /* CONFIG_MEMORYSTATUS */
1206 if (task_wakeups_monitor_rate
!= 0) {
1207 uint32_t flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
1208 int32_t rate
; // Ignored because of WAKEMON_SET_DEFAULTS
1209 task_wakeups_monitor_ctl(new_task
, &flags
, &rate
);
1212 #if CONFIG_IO_ACCOUNTING
1213 uint32_t flags
= IOMON_ENABLE
;
1214 task_io_monitor_ctl(new_task
, &flags
);
1215 #endif /* CONFIG_IO_ACCOUNTING */
1217 machine_task_init(new_task
, parent_task
, inherit_memory
);
1219 new_task
->task_debug
= NULL
;
1221 #if DEVELOPMENT || DEBUG
1222 new_task
->task_unnested
= FALSE
;
1223 new_task
->task_disconnected_count
= 0;
1225 queue_init(&new_task
->semaphore_list
);
1226 new_task
->semaphores_owned
= 0;
1228 ipc_task_init(new_task
, parent_task
);
1230 new_task
->vtimers
= 0;
1232 new_task
->shared_region
= NULL
;
1234 new_task
->affinity_space
= NULL
;
1236 new_task
->t_kpc
= 0;
1238 new_task
->pidsuspended
= FALSE
;
1239 new_task
->frozen
= FALSE
;
1240 new_task
->changing_freeze_state
= FALSE
;
1241 new_task
->rusage_cpu_flags
= 0;
1242 new_task
->rusage_cpu_percentage
= 0;
1243 new_task
->rusage_cpu_interval
= 0;
1244 new_task
->rusage_cpu_deadline
= 0;
1245 new_task
->rusage_cpu_callt
= NULL
;
1247 new_task
->suspends_outstanding
= 0;
1251 new_task
->hv_task_target
= NULL
;
1252 #endif /* HYPERVISOR */
1255 queue_init(&new_task
->task_watchers
);
1256 new_task
->num_taskwatchers
= 0;
1257 new_task
->watchapplying
= 0;
1258 #endif /* CONFIG_EMBEDDED */
1260 new_task
->mem_notify_reserved
= 0;
1261 new_task
->memlimit_attrs_reserved
= 0;
1263 new_task
->requested_policy
= default_task_requested_policy
;
1264 new_task
->effective_policy
= default_task_effective_policy
;
1266 task_importance_init_from_parent(new_task
, parent_task
);
1268 if (parent_task
!= TASK_NULL
) {
1269 new_task
->sec_token
= parent_task
->sec_token
;
1270 new_task
->audit_token
= parent_task
->audit_token
;
1272 /* inherit the parent's shared region */
1273 shared_region
= vm_shared_region_get(parent_task
);
1274 vm_shared_region_set(new_task
, shared_region
);
1276 if(task_has_64Bit_addr(parent_task
)) {
1277 task_set_64Bit_addr(new_task
);
1280 if(task_has_64Bit_data(parent_task
)) {
1281 task_set_64Bit_data(new_task
);
1284 new_task
->all_image_info_addr
= parent_task
->all_image_info_addr
;
1285 new_task
->all_image_info_size
= parent_task
->all_image_info_size
;
1287 if (inherit_memory
&& parent_task
->affinity_space
)
1288 task_affinity_create(parent_task
, new_task
);
1290 new_task
->pset_hint
= parent_task
->pset_hint
= task_choose_pset(parent_task
);
1292 new_task
->priority
= BASEPRI_DEFAULT
;
1293 new_task
->max_priority
= MAXPRI_USER
;
1295 task_policy_create(new_task
, parent_task
);
1297 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
1298 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
1301 task_set_64Bit_addr(new_task
);
1306 task_set_64Bit_data(new_task
);
1309 new_task
->all_image_info_addr
= (mach_vm_address_t
)0;
1310 new_task
->all_image_info_size
= (mach_vm_size_t
)0;
1312 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
1314 if (kernel_task
== TASK_NULL
) {
1315 new_task
->priority
= BASEPRI_KERNEL
;
1316 new_task
->max_priority
= MAXPRI_KERNEL
;
1318 new_task
->priority
= BASEPRI_DEFAULT
;
1319 new_task
->max_priority
= MAXPRI_USER
;
1323 bzero(new_task
->coalition
, sizeof(new_task
->coalition
));
1324 for (int i
= 0; i
< COALITION_NUM_TYPES
; i
++)
1325 queue_chain_init(new_task
->task_coalition
[i
]);
1327 /* Allocate I/O Statistics */
1328 new_task
->task_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
1329 assert(new_task
->task_io_stats
!= NULL
);
1330 bzero(new_task
->task_io_stats
, sizeof(struct io_stat_info
));
1332 bzero(&(new_task
->cpu_time_eqos_stats
), sizeof(new_task
->cpu_time_eqos_stats
));
1333 bzero(&(new_task
->cpu_time_rqos_stats
), sizeof(new_task
->cpu_time_rqos_stats
));
1335 bzero(&new_task
->extmod_statistics
, sizeof(new_task
->extmod_statistics
));
1337 /* Copy resource acc. info from Parent for Corpe Forked task. */
1338 if (parent_task
!= NULL
&& (t_flags
& TF_CORPSE_FORK
)) {
1339 task_rollup_accounting_info(new_task
, parent_task
);
1341 /* Initialize to zero for standard fork/spawn case */
1342 new_task
->total_user_time
= 0;
1343 new_task
->total_system_time
= 0;
1344 new_task
->total_ptime
= 0;
1345 new_task
->total_runnable_time
= 0;
1346 new_task
->faults
= 0;
1347 new_task
->pageins
= 0;
1348 new_task
->cow_faults
= 0;
1349 new_task
->messages_sent
= 0;
1350 new_task
->messages_received
= 0;
1351 new_task
->syscalls_mach
= 0;
1352 new_task
->syscalls_unix
= 0;
1353 new_task
->c_switch
= 0;
1354 new_task
->p_switch
= 0;
1355 new_task
->ps_switch
= 0;
1356 new_task
->low_mem_notified_warn
= 0;
1357 new_task
->low_mem_notified_critical
= 0;
1358 new_task
->purged_memory_warn
= 0;
1359 new_task
->purged_memory_critical
= 0;
1360 new_task
->low_mem_privileged_listener
= 0;
1361 new_task
->memlimit_is_active
= 0;
1362 new_task
->memlimit_is_fatal
= 0;
1363 new_task
->memlimit_active_exc_resource
= 0;
1364 new_task
->memlimit_inactive_exc_resource
= 0;
1365 new_task
->task_timer_wakeups_bin_1
= 0;
1366 new_task
->task_timer_wakeups_bin_2
= 0;
1367 new_task
->task_gpu_ns
= 0;
1368 new_task
->task_immediate_writes
= 0;
1369 new_task
->task_deferred_writes
= 0;
1370 new_task
->task_invalidated_writes
= 0;
1371 new_task
->task_metadata_writes
= 0;
1372 new_task
->task_energy
= 0;
1374 memset(&new_task
->task_monotonic
, 0, sizeof(new_task
->task_monotonic
));
1375 #endif /* MONOTONIC */
1379 #if CONFIG_COALITIONS
1380 if (!(t_flags
& TF_CORPSE_FORK
)) {
1381 /* TODO: there is no graceful failure path here... */
1382 if (parent_coalitions
&& parent_coalitions
[COALITION_TYPE_RESOURCE
]) {
1383 coalitions_adopt_task(parent_coalitions
, new_task
);
1384 } else if (parent_task
&& parent_task
->coalition
[COALITION_TYPE_RESOURCE
]) {
1386 * all tasks at least have a resource coalition, so
1387 * if the parent has one then inherit all coalitions
1388 * the parent is a part of
1390 coalitions_adopt_task(parent_task
->coalition
, new_task
);
1392 /* TODO: assert that new_task will be PID 1 (launchd) */
1393 coalitions_adopt_init_task(new_task
);
1396 * on exec, we need to transfer the coalition roles from the
1397 * parent task to the exec copy task.
1399 if (parent_task
&& (t_procflags
& TPF_EXEC_COPY
)) {
1400 int coal_roles
[COALITION_NUM_TYPES
];
1401 task_coalition_roles(parent_task
, coal_roles
);
1402 (void)coalitions_set_roles(new_task
->coalition
, new_task
, coal_roles
);
1405 coalitions_adopt_corpse_task(new_task
);
1408 if (new_task
->coalition
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
1409 panic("created task is not a member of a resource coalition");
1411 #endif /* CONFIG_COALITIONS */
1413 new_task
->dispatchqueue_offset
= 0;
1414 if (parent_task
!= NULL
) {
1415 new_task
->dispatchqueue_offset
= parent_task
->dispatchqueue_offset
;
1418 new_task
->task_volatile_objects
= 0;
1419 new_task
->task_nonvolatile_objects
= 0;
1420 new_task
->task_purgeable_disowning
= FALSE
;
1421 new_task
->task_purgeable_disowned
= FALSE
;
1422 queue_init(&new_task
->task_objq
);
1423 task_objq_lock_init(new_task
);
1426 new_task
->task_legacy_footprint
= FALSE
;
1427 #endif /* __arm64__ */
1428 new_task
->task_region_footprint
= FALSE
;
1429 new_task
->task_has_crossed_thread_limit
= FALSE
;
1430 new_task
->task_thread_limit
= 0;
1431 #if CONFIG_SECLUDED_MEMORY
1432 new_task
->task_can_use_secluded_mem
= FALSE
;
1433 new_task
->task_could_use_secluded_mem
= FALSE
;
1434 new_task
->task_could_also_use_secluded_mem
= FALSE
;
1435 new_task
->task_suppressed_secluded
= FALSE
;
1436 #endif /* CONFIG_SECLUDED_MEMORY */
1439 * t_flags is set up above. But since we don't
1440 * support darkwake mode being set that way
1441 * currently, we clear it out here explicitly.
1443 new_task
->t_flags
&= ~(TF_DARKWAKE_MODE
);
1445 queue_init(&new_task
->io_user_clients
);
1447 ipc_task_enable(new_task
);
1449 lck_mtx_lock(&tasks_threads_lock
);
1450 queue_enter(&tasks
, new_task
, task_t
, tasks
);
1452 if (tasks_suspend_state
) {
1453 task_suspend_internal(new_task
);
1455 lck_mtx_unlock(&tasks_threads_lock
);
1457 *child_task
= new_task
;
1458 return(KERN_SUCCESS
);
1462 * task_rollup_accounting_info
1464 * Roll up accounting stats. Used to rollup stats
1465 * for exec copy task and corpse fork.
1468 task_rollup_accounting_info(task_t to_task
, task_t from_task
)
1470 assert(from_task
!= to_task
);
1472 to_task
->total_user_time
= from_task
->total_user_time
;
1473 to_task
->total_system_time
= from_task
->total_system_time
;
1474 to_task
->total_ptime
= from_task
->total_ptime
;
1475 to_task
->total_runnable_time
= from_task
->total_runnable_time
;
1476 to_task
->faults
= from_task
->faults
;
1477 to_task
->pageins
= from_task
->pageins
;
1478 to_task
->cow_faults
= from_task
->cow_faults
;
1479 to_task
->messages_sent
= from_task
->messages_sent
;
1480 to_task
->messages_received
= from_task
->messages_received
;
1481 to_task
->syscalls_mach
= from_task
->syscalls_mach
;
1482 to_task
->syscalls_unix
= from_task
->syscalls_unix
;
1483 to_task
->c_switch
= from_task
->c_switch
;
1484 to_task
->p_switch
= from_task
->p_switch
;
1485 to_task
->ps_switch
= from_task
->ps_switch
;
1486 to_task
->extmod_statistics
= from_task
->extmod_statistics
;
1487 to_task
->low_mem_notified_warn
= from_task
->low_mem_notified_warn
;
1488 to_task
->low_mem_notified_critical
= from_task
->low_mem_notified_critical
;
1489 to_task
->purged_memory_warn
= from_task
->purged_memory_warn
;
1490 to_task
->purged_memory_critical
= from_task
->purged_memory_critical
;
1491 to_task
->low_mem_privileged_listener
= from_task
->low_mem_privileged_listener
;
1492 *to_task
->task_io_stats
= *from_task
->task_io_stats
;
1493 to_task
->cpu_time_eqos_stats
= from_task
->cpu_time_eqos_stats
;
1494 to_task
->cpu_time_rqos_stats
= from_task
->cpu_time_rqos_stats
;
1495 to_task
->task_timer_wakeups_bin_1
= from_task
->task_timer_wakeups_bin_1
;
1496 to_task
->task_timer_wakeups_bin_2
= from_task
->task_timer_wakeups_bin_2
;
1497 to_task
->task_gpu_ns
= from_task
->task_gpu_ns
;
1498 to_task
->task_immediate_writes
= from_task
->task_immediate_writes
;
1499 to_task
->task_deferred_writes
= from_task
->task_deferred_writes
;
1500 to_task
->task_invalidated_writes
= from_task
->task_invalidated_writes
;
1501 to_task
->task_metadata_writes
= from_task
->task_metadata_writes
;
1502 to_task
->task_energy
= from_task
->task_energy
;
1504 /* Skip ledger roll up for memory accounting entries */
1505 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time
);
1506 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.platform_idle_wakeups
);
1507 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.interrupt_wakeups
);
1508 #if CONFIG_SCHED_SFI
1509 for (sfi_class_id_t class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1510 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.sfi_wait_times
[class_id
]);
1513 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_me
);
1514 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_others
);
1515 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.physical_writes
);
1516 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.logical_writes
);
1517 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.energy_billed_to_me
);
1518 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.energy_billed_to_others
);
1521 int task_dropped_imp_count
= 0;
1526 * Drop a reference on a task.
1532 ledger_amount_t credit
, debit
, interrupt_wakeups
, platform_idle_wakeups
;
1535 if (task
== TASK_NULL
)
1538 refs
= task_deallocate_internal(task
);
1540 #if IMPORTANCE_INHERITANCE
1544 atomic_load_explicit(&task
->ref_count
, memory_order_acquire
);
1548 * If last ref potentially comes from the task's importance,
1549 * disconnect it. But more task refs may be added before
1550 * that completes, so wait for the reference to go to zero
1551 * naturually (it may happen on a recursive task_deallocate()
1552 * from the ipc_importance_disconnect_task() call).
1554 if (IIT_NULL
!= task
->task_imp_base
)
1555 ipc_importance_disconnect_task(task
);
1562 atomic_load_explicit(&task
->ref_count
, memory_order_acquire
);
1564 #endif /* IMPORTANCE_INHERITANCE */
1566 lck_mtx_lock(&tasks_threads_lock
);
1567 queue_remove(&terminated_tasks
, task
, task_t
, tasks
);
1568 terminated_tasks_count
--;
1569 lck_mtx_unlock(&tasks_threads_lock
);
1572 * remove the reference on atm descriptor
1574 task_atm_reset(task
);
1577 * remove the reference on bank context
1579 task_bank_reset(task
);
1581 if (task
->task_io_stats
)
1582 kfree(task
->task_io_stats
, sizeof(struct io_stat_info
));
1585 * Give the machine dependent code a chance
1586 * to perform cleanup before ripping apart
1589 machine_task_terminate(task
);
1591 ipc_task_terminate(task
);
1593 /* let iokit know */
1594 iokit_task_terminate(task
);
1596 if (task
->affinity_space
)
1597 task_affinity_deallocate(task
);
1600 if (task
->ledger
!= NULL
&&
1601 task
->map
!= NULL
&&
1602 task
->map
->pmap
!= NULL
&&
1603 task
->map
->pmap
->ledger
!= NULL
) {
1604 assert(task
->ledger
== task
->map
->pmap
->ledger
);
1606 #endif /* MACH_ASSERT */
1608 vm_purgeable_disown(task
);
1609 assert(task
->task_purgeable_disowned
);
1610 if (task
->task_volatile_objects
!= 0 ||
1611 task
->task_nonvolatile_objects
!= 0) {
1612 panic("task_deallocate(%p): "
1613 "volatile_objects=%d nonvolatile_objects=%d\n",
1615 task
->task_volatile_objects
,
1616 task
->task_nonvolatile_objects
);
1619 vm_map_deallocate(task
->map
);
1620 is_release(task
->itk_space
);
1622 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
1623 &interrupt_wakeups
, &debit
);
1624 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
1625 &platform_idle_wakeups
, &debit
);
1627 #if defined(CONFIG_SCHED_MULTIQ)
1628 sched_group_destroy(task
->sched_group
);
1631 /* Accumulate statistics for dead tasks */
1632 lck_spin_lock(&dead_task_statistics_lock
);
1633 dead_task_statistics
.total_user_time
+= task
->total_user_time
;
1634 dead_task_statistics
.total_system_time
+= task
->total_system_time
;
1636 dead_task_statistics
.task_interrupt_wakeups
+= interrupt_wakeups
;
1637 dead_task_statistics
.task_platform_idle_wakeups
+= platform_idle_wakeups
;
1639 dead_task_statistics
.task_timer_wakeups_bin_1
+= task
->task_timer_wakeups_bin_1
;
1640 dead_task_statistics
.task_timer_wakeups_bin_2
+= task
->task_timer_wakeups_bin_2
;
1641 dead_task_statistics
.total_ptime
+= task
->total_ptime
;
1642 dead_task_statistics
.total_pset_switches
+= task
->ps_switch
;
1643 dead_task_statistics
.task_gpu_ns
+= task
->task_gpu_ns
;
1644 dead_task_statistics
.task_energy
+= task
->task_energy
;
1646 lck_spin_unlock(&dead_task_statistics_lock
);
1647 lck_mtx_destroy(&task
->lock
, &task_lck_grp
);
1649 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_private
, &credit
,
1651 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_private
.alloc
);
1652 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_private
.free
);
1654 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_shared
, &credit
,
1656 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_shared
.alloc
);
1657 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_shared
.free
);
1659 ledger_dereference(task
->ledger
);
1661 #if TASK_REFERENCE_LEAK_DEBUG
1662 btlog_remove_entries_for_element(task_ref_btlog
, task
);
1665 #if CONFIG_COALITIONS
1666 task_release_coalitions(task
);
1667 #endif /* CONFIG_COALITIONS */
1669 bzero(task
->coalition
, sizeof(task
->coalition
));
1672 /* clean up collected information since last reference to task is gone */
1673 if (task
->corpse_info
) {
1674 void *corpse_info_kernel
= kcdata_memory_get_begin_addr(task
->corpse_info
);
1675 task_crashinfo_destroy(task
->corpse_info
);
1676 task
->corpse_info
= NULL
;
1677 if (corpse_info_kernel
) {
1678 kfree(corpse_info_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1684 if (task
->crash_label
) {
1685 mac_exc_free_label(task
->crash_label
);
1686 task
->crash_label
= NULL
;
1690 assert(queue_empty(&task
->task_objq
));
1692 zfree(task_zone
, task
);
1696 * task_name_deallocate:
1698 * Drop a reference on a task name.
1701 task_name_deallocate(
1702 task_name_t task_name
)
1704 return(task_deallocate((task_t
)task_name
));
1708 * task_inspect_deallocate:
1710 * Drop a task inspection reference.
1713 task_inspect_deallocate(
1714 task_inspect_t task_inspect
)
1716 return(task_deallocate((task_t
)task_inspect
));
1720 * task_suspension_token_deallocate:
1722 * Drop a reference on a task suspension token.
1725 task_suspension_token_deallocate(
1726 task_suspension_token_t token
)
1728 return(task_deallocate((task_t
)token
));
1733 * task_collect_crash_info:
1735 * collect crash info from bsd and mach based data
1738 task_collect_crash_info(
1741 struct label
*crash_label
,
1745 kern_return_t kr
= KERN_SUCCESS
;
1747 kcdata_descriptor_t crash_data
= NULL
;
1748 kcdata_descriptor_t crash_data_release
= NULL
;
1749 mach_msg_type_number_t size
= CORPSEINFO_ALLOCATION_SIZE
;
1750 mach_vm_offset_t crash_data_ptr
= 0;
1751 void *crash_data_kernel
= NULL
;
1752 void *crash_data_kernel_release
= NULL
;
1754 struct label
*label
, *free_label
;
1757 if (!corpses_enabled()) {
1758 return KERN_NOT_SUPPORTED
;
1762 free_label
= label
= mac_exc_create_label();
1767 assert(is_corpse_fork
|| task
->bsd_info
!= NULL
);
1768 if (task
->corpse_info
== NULL
&& (is_corpse_fork
|| task
->bsd_info
!= NULL
)) {
1770 /* Set the crash label, used by the exception delivery mac hook */
1771 free_label
= task
->crash_label
; // Most likely NULL.
1772 task
->crash_label
= label
;
1773 mac_exc_update_task_crash_label(task
, crash_label
);
1777 crash_data_kernel
= (void *) kalloc(CORPSEINFO_ALLOCATION_SIZE
);
1778 if (crash_data_kernel
== NULL
) {
1779 kr
= KERN_RESOURCE_SHORTAGE
;
1782 bzero(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1783 crash_data_ptr
= (mach_vm_offset_t
) crash_data_kernel
;
1785 /* Do not get a corpse ref for corpse fork */
1786 crash_data
= task_crashinfo_alloc_init((mach_vm_address_t
)crash_data_ptr
, size
,
1787 is_corpse_fork
? 0 : CORPSE_CRASHINFO_HAS_REF
,
1788 KCFLAG_USE_MEMCOPY
);
1791 crash_data_release
= task
->corpse_info
;
1792 crash_data_kernel_release
= kcdata_memory_get_begin_addr(crash_data_release
);
1793 task
->corpse_info
= crash_data
;
1798 kfree(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1802 if (crash_data_release
!= NULL
) {
1803 task_crashinfo_destroy(crash_data_release
);
1805 if (crash_data_kernel_release
!= NULL
) {
1806 kfree(crash_data_kernel_release
, CORPSEINFO_ALLOCATION_SIZE
);
1814 if (free_label
!= NULL
) {
1815 mac_exc_free_label(free_label
);
1822 * task_deliver_crash_notification:
1824 * Makes outcall to registered host port for a corpse.
1827 task_deliver_crash_notification(
1830 exception_type_t etype
,
1831 mach_exception_subcode_t subcode
)
1833 kcdata_descriptor_t crash_info
= task
->corpse_info
;
1834 thread_t th_iter
= NULL
;
1835 kern_return_t kr
= KERN_SUCCESS
;
1836 wait_interrupt_t wsave
;
1837 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1838 ipc_port_t task_port
, old_notify
;
1840 if (crash_info
== NULL
)
1841 return KERN_FAILURE
;
1844 if (task_is_a_corpse_fork(task
)) {
1845 /* Populate code with EXC_{RESOURCE,GUARD} for corpse fork */
1849 /* Populate code with EXC_CRASH for corpses */
1850 code
[0] = EXC_CRASH
;
1852 /* Update the code[1] if the boot-arg corpse_for_fatal_memkill is set */
1853 if (corpse_for_fatal_memkill
) {
1858 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1860 if (th_iter
->corpse_dup
== FALSE
) {
1861 ipc_thread_reset(th_iter
);
1866 /* Arm the no-sender notification for taskport */
1867 task_reference(task
);
1868 task_port
= convert_task_to_port(task
);
1870 assert(ip_active(task_port
));
1871 ipc_port_nsrequest(task_port
, task_port
->ip_mscount
, ipc_port_make_sonce_locked(task_port
), &old_notify
);
1873 assert(IP_NULL
== old_notify
);
1875 wsave
= thread_interrupt_level(THREAD_UNINT
);
1876 kr
= exception_triage_thread(EXC_CORPSE_NOTIFY
, code
, EXCEPTION_CODE_MAX
, thread
);
1877 if (kr
!= KERN_SUCCESS
) {
1878 printf("Failed to send exception EXC_CORPSE_NOTIFY. error code: %d for pid %d\n", kr
, task_pid(task
));
1881 (void)thread_interrupt_level(wsave
);
1884 * Drop the send right on task port, will fire the
1885 * no-sender notification if exception deliver failed.
1887 ipc_port_release_send(task_port
);
1894 * Terminate the specified task. See comments on thread_terminate
1895 * (kern/thread.c) about problems with terminating the "current task."
1902 if (task
== TASK_NULL
)
1903 return (KERN_INVALID_ARGUMENT
);
1906 return (KERN_FAILURE
);
1908 return (task_terminate_internal(task
));
1912 extern int proc_pid(struct proc
*);
1913 extern void proc_name_kdp(task_t t
, char *buf
, int size
);
1914 #endif /* MACH_ASSERT */
1916 #define VM_MAP_PARTIAL_REAP 0x54 /* 0x150 */
1918 __unused
task_partial_reap(task_t task
, __unused
int pid
)
1920 unsigned int reclaimed_resident
= 0;
1921 unsigned int reclaimed_compressed
= 0;
1922 uint64_t task_page_count
;
1924 task_page_count
= (get_task_phys_footprint(task
) / PAGE_SIZE_64
);
1926 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_START
),
1927 pid
, task_page_count
, 0, 0, 0);
1929 vm_map_partial_reap(task
->map
, &reclaimed_resident
, &reclaimed_compressed
);
1931 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_END
),
1932 pid
, reclaimed_resident
, reclaimed_compressed
, 0, 0);
1936 task_mark_corpse(task_t task
)
1938 kern_return_t kr
= KERN_SUCCESS
;
1939 thread_t self_thread
;
1941 wait_interrupt_t wsave
;
1943 struct label
*crash_label
= NULL
;
1946 assert(task
!= kernel_task
);
1947 assert(task
== current_task());
1948 assert(!task_is_a_corpse(task
));
1951 crash_label
= mac_exc_create_label_for_proc((struct proc
*)task
->bsd_info
);
1954 kr
= task_collect_crash_info(task
,
1959 if (kr
!= KERN_SUCCESS
) {
1963 self_thread
= current_thread();
1965 wsave
= thread_interrupt_level(THREAD_UNINT
);
1968 task_set_corpse_pending_report(task
);
1969 task_set_corpse(task
);
1970 task
->crashed_thread_id
= thread_tid(self_thread
);
1972 kr
= task_start_halt_locked(task
, TRUE
);
1973 assert(kr
== KERN_SUCCESS
);
1975 ipc_task_reset(task
);
1976 /* Remove the naked send right for task port, needed to arm no sender notification */
1977 task_set_special_port(task
, TASK_KERNEL_PORT
, IPC_PORT_NULL
);
1978 ipc_task_enable(task
);
1981 /* terminate the ipc space */
1982 ipc_space_terminate(task
->itk_space
);
1984 /* Add it to global corpse task list */
1985 task_add_to_corpse_task_list(task
);
1987 task_start_halt(task
);
1988 thread_terminate_internal(self_thread
);
1990 (void) thread_interrupt_level(wsave
);
1991 assert(task
->halting
== TRUE
);
1995 mac_exc_free_label(crash_label
);
2003 * Clears the corpse pending bit on task.
2004 * Removes inspection bit on the threads.
2007 task_clear_corpse(task_t task
)
2009 thread_t th_iter
= NULL
;
2012 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
2014 thread_mtx_lock(th_iter
);
2015 th_iter
->inspection
= FALSE
;
2016 thread_mtx_unlock(th_iter
);
2019 thread_terminate_crashed_threads();
2020 /* remove the pending corpse report flag */
2021 task_clear_corpse_pending_report(task
);
2029 * Called whenever the Mach port system detects no-senders on
2030 * the task port of a corpse.
2031 * Each notification that comes in should terminate the task (corpse).
2034 task_port_notify(mach_msg_header_t
*msg
)
2036 mach_no_senders_notification_t
*notification
= (void *)msg
;
2037 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
2040 assert(ip_active(port
));
2041 assert(IKOT_TASK
== ip_kotype(port
));
2042 task
= (task_t
) port
->ip_kobject
;
2044 assert(task_is_a_corpse(task
));
2046 /* Remove the task from global corpse task list */
2047 task_remove_from_corpse_task_list(task
);
2049 task_clear_corpse(task
);
2050 task_terminate_internal(task
);
2054 * task_wait_till_threads_terminate_locked
2056 * Wait till all the threads in the task are terminated.
2057 * Might release the task lock and re-acquire it.
2060 task_wait_till_threads_terminate_locked(task_t task
)
2062 /* wait for all the threads in the task to terminate */
2063 while (task
->active_thread_count
!= 0) {
2064 assert_wait((event_t
)&task
->active_thread_count
, THREAD_UNINT
);
2066 thread_block(THREAD_CONTINUE_NULL
);
2073 * task_duplicate_map_and_threads
2075 * Copy vmmap of source task.
2076 * Copy active threads from source task to destination task.
2077 * Source task would be suspended during the copy.
2080 task_duplicate_map_and_threads(
2084 thread_t
*thread_ret
,
2085 uint64_t **udata_buffer
,
2089 kern_return_t kr
= KERN_SUCCESS
;
2091 thread_t thread
, self
, thread_return
= THREAD_NULL
;
2092 thread_t new_thread
= THREAD_NULL
, first_thread
= THREAD_NULL
;
2093 thread_t
*thread_array
;
2094 uint32_t active_thread_count
= 0, array_count
= 0, i
;
2096 uint64_t *buffer
= NULL
;
2098 int est_knotes
= 0, num_knotes
= 0;
2100 self
= current_thread();
2103 * Suspend the task to copy thread state, use the internal
2104 * variant so that no user-space process can resume
2105 * the task from under us
2107 kr
= task_suspend_internal(task
);
2108 if (kr
!= KERN_SUCCESS
) {
2112 if (task
->map
->disable_vmentry_reuse
== TRUE
) {
2114 * Quite likely GuardMalloc (or some debugging tool)
2115 * is being used on this task. And it has gone through
2116 * its limit. Making a corpse will likely encounter
2117 * a lot of VM entries that will need COW.
2121 #if DEVELOPMENT || DEBUG
2122 memorystatus_abort_vm_map_fork(task
);
2124 task_resume_internal(task
);
2125 return KERN_FAILURE
;
2128 /* Check with VM if vm_map_fork is allowed for this task */
2129 if (memorystatus_allowed_vm_map_fork(task
)) {
2131 /* Setup new task's vmmap, switch from parent task's map to it COW map */
2132 oldmap
= new_task
->map
;
2133 new_task
->map
= vm_map_fork(new_task
->ledger
,
2135 (VM_MAP_FORK_SHARE_IF_INHERIT_NONE
|
2136 VM_MAP_FORK_PRESERVE_PURGEABLE
|
2137 VM_MAP_FORK_CORPSE_FOOTPRINT
));
2138 vm_map_deallocate(oldmap
);
2140 /* copy ledgers that impact the memory footprint */
2141 vm_map_copy_footprint_ledgers(task
, new_task
);
2143 /* Get all the udata pointers from kqueue */
2144 est_knotes
= kevent_proc_copy_uptrs(p
, NULL
, 0);
2145 if (est_knotes
> 0) {
2146 buf_size
= (est_knotes
+ 32) * sizeof(uint64_t);
2147 buffer
= (uint64_t *) kalloc(buf_size
);
2148 num_knotes
= kevent_proc_copy_uptrs(p
, buffer
, buf_size
);
2149 if (num_knotes
> est_knotes
+ 32) {
2150 num_knotes
= est_knotes
+ 32;
2155 active_thread_count
= task
->active_thread_count
;
2156 if (active_thread_count
== 0) {
2157 if (buffer
!= NULL
) {
2158 kfree(buffer
, buf_size
);
2160 task_resume_internal(task
);
2161 return KERN_FAILURE
;
2164 thread_array
= (thread_t
*) kalloc(sizeof(thread_t
) * active_thread_count
);
2166 /* Iterate all the threads and drop the task lock before calling thread_create_with_continuation */
2168 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2169 /* Skip inactive threads */
2170 active
= thread
->active
;
2175 if (array_count
>= active_thread_count
) {
2179 thread_array
[array_count
++] = thread
;
2180 thread_reference(thread
);
2184 for (i
= 0; i
< array_count
; i
++) {
2186 kr
= thread_create_with_continuation(new_task
, &new_thread
, (thread_continue_t
)thread_corpse_continue
);
2187 if (kr
!= KERN_SUCCESS
) {
2191 /* Equivalent of current thread in corpse */
2192 if (thread_array
[i
] == self
) {
2193 thread_return
= new_thread
;
2194 new_task
->crashed_thread_id
= thread_tid(new_thread
);
2195 } else if (first_thread
== NULL
) {
2196 first_thread
= new_thread
;
2198 /* drop the extra ref returned by thread_create_with_continuation */
2199 thread_deallocate(new_thread
);
2202 kr
= thread_dup2(thread_array
[i
], new_thread
);
2203 if (kr
!= KERN_SUCCESS
) {
2204 thread_mtx_lock(new_thread
);
2205 new_thread
->corpse_dup
= TRUE
;
2206 thread_mtx_unlock(new_thread
);
2210 /* Copy thread name */
2211 bsd_copythreadname(new_thread
->uthread
, thread_array
[i
]->uthread
);
2212 new_thread
->thread_tag
= thread_array
[i
]->thread_tag
;
2213 thread_copy_resource_info(new_thread
, thread_array
[i
]);
2216 /* return the first thread if we couldn't find the equivalent of current */
2217 if (thread_return
== THREAD_NULL
) {
2218 thread_return
= first_thread
;
2220 else if (first_thread
!= THREAD_NULL
) {
2221 /* drop the extra ref returned by thread_create_with_continuation */
2222 thread_deallocate(first_thread
);
2225 task_resume_internal(task
);
2227 for (i
= 0; i
< array_count
; i
++) {
2228 thread_deallocate(thread_array
[i
]);
2230 kfree(thread_array
, sizeof(thread_t
) * active_thread_count
);
2232 if (kr
== KERN_SUCCESS
) {
2233 *thread_ret
= thread_return
;
2234 *udata_buffer
= buffer
;
2236 *num_udata
= num_knotes
;
2238 if (thread_return
!= THREAD_NULL
) {
2239 thread_deallocate(thread_return
);
2241 if (buffer
!= NULL
) {
2242 kfree(buffer
, buf_size
);
2249 #if CONFIG_SECLUDED_MEMORY
2250 extern void task_set_can_use_secluded_mem_locked(
2252 boolean_t can_use_secluded_mem
);
2253 #endif /* CONFIG_SECLUDED_MEMORY */
2256 task_terminate_internal(
2259 thread_t thread
, self
;
2261 boolean_t interrupt_save
;
2264 assert(task
!= kernel_task
);
2266 self
= current_thread();
2267 self_task
= self
->task
;
2270 * Get the task locked and make sure that we are not racing
2271 * with someone else trying to terminate us.
2273 if (task
== self_task
)
2276 if (task
< self_task
) {
2278 task_lock(self_task
);
2281 task_lock(self_task
);
2285 #if CONFIG_SECLUDED_MEMORY
2286 if (task
->task_can_use_secluded_mem
) {
2287 task_set_can_use_secluded_mem_locked(task
, FALSE
);
2289 task
->task_could_use_secluded_mem
= FALSE
;
2290 task
->task_could_also_use_secluded_mem
= FALSE
;
2292 if (task
->task_suppressed_secluded
) {
2293 stop_secluded_suppression(task
);
2295 #endif /* CONFIG_SECLUDED_MEMORY */
2297 if (!task
->active
) {
2299 * Task is already being terminated.
2300 * Just return an error. If we are dying, this will
2301 * just get us to our AST special handler and that
2302 * will get us to finalize the termination of ourselves.
2305 if (self_task
!= task
)
2306 task_unlock(self_task
);
2308 return (KERN_FAILURE
);
2311 if (task_corpse_pending_report(task
)) {
2313 * Task is marked for reporting as corpse.
2314 * Just return an error. This will
2315 * just get us to our AST special handler and that
2316 * will get us to finish the path to death
2319 if (self_task
!= task
)
2320 task_unlock(self_task
);
2322 return (KERN_FAILURE
);
2325 if (self_task
!= task
)
2326 task_unlock(self_task
);
2329 * Make sure the current thread does not get aborted out of
2330 * the waits inside these operations.
2332 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
2335 * Indicate that we want all the threads to stop executing
2336 * at user space by holding the task (we would have held
2337 * each thread independently in thread_terminate_internal -
2338 * but this way we may be more likely to already find it
2339 * held there). Mark the task inactive, and prevent
2340 * further task operations via the task port.
2342 task_hold_locked(task
);
2343 task
->active
= FALSE
;
2344 ipc_task_disable(task
);
2346 #if CONFIG_TELEMETRY
2348 * Notify telemetry that this task is going away.
2350 telemetry_task_ctl_locked(task
, TF_TELEMETRY
, 0);
2354 * Terminate each thread in the task.
2356 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2357 thread_terminate_internal(thread
);
2361 if (task
->bsd_info
!= NULL
&& !task_is_exec_copy(task
)) {
2362 pid
= proc_pid(task
->bsd_info
);
2364 #endif /* MACH_BSD */
2368 proc_set_task_policy(task
, TASK_POLICY_ATTRIBUTE
,
2369 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
2371 /* Early object reap phase */
2373 // PR-17045188: Revisit implementation
2374 // task_partial_reap(task, pid);
2378 * remove all task watchers
2380 task_removewatchers(task
);
2382 #endif /* CONFIG_EMBEDDED */
2385 * Destroy all synchronizers owned by the task.
2387 task_synchronizer_destroy_all(task
);
2390 * Destroy the IPC space, leaving just a reference for it.
2392 ipc_space_terminate(task
->itk_space
);
2395 /* if some ledgers go negative on tear-down again... */
2396 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2397 task_ledgers
.phys_footprint
);
2398 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2399 task_ledgers
.internal
);
2400 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2401 task_ledgers
.internal_compressed
);
2402 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2403 task_ledgers
.iokit_mapped
);
2404 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2405 task_ledgers
.alternate_accounting
);
2406 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2407 task_ledgers
.alternate_accounting_compressed
);
2411 * If the current thread is a member of the task
2412 * being terminated, then the last reference to
2413 * the task will not be dropped until the thread
2414 * is finally reaped. To avoid incurring the
2415 * expense of removing the address space regions
2416 * at reap time, we do it explictly here.
2419 vm_map_lock(task
->map
);
2420 vm_map_disable_hole_optimization(task
->map
);
2421 vm_map_unlock(task
->map
);
2425 * Identify the pmap's process, in case the pmap ledgers drift
2426 * and we have to report it.
2429 if (task
->bsd_info
&& !task_is_exec_copy(task
)) {
2430 pid
= proc_pid(task
->bsd_info
);
2431 proc_name_kdp(task
, procname
, sizeof (procname
));
2434 strlcpy(procname
, "<unknown>", sizeof (procname
));
2436 pmap_set_process(task
->map
->pmap
, pid
, procname
);
2437 #endif /* MACH_ASSERT */
2439 vm_map_remove(task
->map
,
2440 task
->map
->min_offset
,
2441 task
->map
->max_offset
,
2445 * + remove immutable mappings
2446 * + allow gaps in range
2448 (VM_MAP_REMOVE_NO_UNNESTING
|
2449 VM_MAP_REMOVE_IMMUTABLE
|
2450 VM_MAP_REMOVE_GAPS_OK
));
2452 /* release our shared region */
2453 vm_shared_region_set(task
, NULL
);
2456 lck_mtx_lock(&tasks_threads_lock
);
2457 queue_remove(&tasks
, task
, task_t
, tasks
);
2458 queue_enter(&terminated_tasks
, task
, task_t
, tasks
);
2460 terminated_tasks_count
++;
2461 lck_mtx_unlock(&tasks_threads_lock
);
2464 * We no longer need to guard against being aborted, so restore
2465 * the previous interruptible state.
2467 thread_interrupt_level(interrupt_save
);
2470 /* force the task to release all ctrs */
2471 if (task
->t_kpc
& TASK_KPC_FORCED_ALL_CTRS
)
2472 kpc_force_all_ctrs(task
, 0);
2475 #if CONFIG_COALITIONS
2477 * Leave our coalitions. (drop activation but not reference)
2479 coalitions_remove_task(task
);
2483 * Get rid of the task active reference on itself.
2485 task_deallocate(task
);
2487 return (KERN_SUCCESS
);
2491 tasks_system_suspend(boolean_t suspend
)
2495 lck_mtx_lock(&tasks_threads_lock
);
2496 assert(tasks_suspend_state
!= suspend
);
2497 tasks_suspend_state
= suspend
;
2498 queue_iterate(&tasks
, task
, task_t
, tasks
) {
2499 if (task
== kernel_task
) {
2502 suspend
? task_suspend_internal(task
) : task_resume_internal(task
);
2504 lck_mtx_unlock(&tasks_threads_lock
);
2510 * Shut the current task down (except for the current thread) in
2511 * preparation for dramatic changes to the task (probably exec).
2512 * We hold the task and mark all other threads in the task for
2516 task_start_halt(task_t task
)
2518 kern_return_t kr
= KERN_SUCCESS
;
2520 kr
= task_start_halt_locked(task
, FALSE
);
2525 static kern_return_t
2526 task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
)
2528 thread_t thread
, self
;
2529 uint64_t dispatchqueue_offset
;
2531 assert(task
!= kernel_task
);
2533 self
= current_thread();
2535 if (task
!= self
->task
&& !task_is_a_corpse_fork(task
))
2536 return (KERN_INVALID_ARGUMENT
);
2538 if (task
->halting
|| !task
->active
|| !self
->active
) {
2540 * Task or current thread is already being terminated.
2541 * Hurry up and return out of the current kernel context
2542 * so that we run our AST special handler to terminate
2545 return (KERN_FAILURE
);
2548 task
->halting
= TRUE
;
2551 * Mark all the threads to keep them from starting any more
2552 * user-level execution. The thread_terminate_internal code
2553 * would do this on a thread by thread basis anyway, but this
2554 * gives us a better chance of not having to wait there.
2556 task_hold_locked(task
);
2557 dispatchqueue_offset
= get_dispatchqueue_offset_from_proc(task
->bsd_info
);
2560 * Terminate all the other threads in the task.
2562 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
)
2564 if (should_mark_corpse
) {
2565 thread_mtx_lock(thread
);
2566 thread
->inspection
= TRUE
;
2567 thread_mtx_unlock(thread
);
2570 thread_terminate_internal(thread
);
2572 task
->dispatchqueue_offset
= dispatchqueue_offset
;
2574 task_release_locked(task
);
2576 return KERN_SUCCESS
;
2581 * task_complete_halt:
2583 * Complete task halt by waiting for threads to terminate, then clean
2584 * up task resources (VM, port namespace, etc...) and then let the
2585 * current thread go in the (practically empty) task context.
2587 * Note: task->halting flag is not cleared in order to avoid creation
2588 * of new thread in old exec'ed task.
2591 task_complete_halt(task_t task
)
2594 assert(task
->halting
);
2595 assert(task
== current_task());
2598 * Wait for the other threads to get shut down.
2599 * When the last other thread is reaped, we'll be
2602 if (task
->thread_count
> 1) {
2603 assert_wait((event_t
)&task
->halting
, THREAD_UNINT
);
2605 thread_block(THREAD_CONTINUE_NULL
);
2611 * Give the machine dependent code a chance
2612 * to perform cleanup of task-level resources
2613 * associated with the current thread before
2614 * ripping apart the task.
2616 machine_task_terminate(task
);
2619 * Destroy all synchronizers owned by the task.
2621 task_synchronizer_destroy_all(task
);
2624 * Destroy the contents of the IPC space, leaving just
2625 * a reference for it.
2627 ipc_space_clean(task
->itk_space
);
2630 * Clean out the address space, as we are going to be
2631 * getting a new one.
2633 vm_map_remove(task
->map
, task
->map
->min_offset
,
2634 task
->map
->max_offset
,
2638 * + remove immutable mappings
2639 * + allow gaps in the range
2641 (VM_MAP_REMOVE_NO_UNNESTING
|
2642 VM_MAP_REMOVE_IMMUTABLE
|
2643 VM_MAP_REMOVE_GAPS_OK
));
2646 * Kick out any IOKitUser handles to the task. At best they're stale,
2647 * at worst someone is racing a SUID exec.
2649 iokit_task_terminate(task
);
2655 * Suspend execution of the specified task.
2656 * This is a recursive-style suspension of the task, a count of
2657 * suspends is maintained.
2659 * CONDITIONS: the task is locked and active.
2667 assert(task
->active
);
2669 if (task
->suspend_count
++ > 0)
2672 if (task
->bsd_info
) {
2673 workq_proc_suspended(task
->bsd_info
);
2677 * Iterate through all the threads and hold them.
2679 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2680 thread_mtx_lock(thread
);
2681 thread_hold(thread
);
2682 thread_mtx_unlock(thread
);
2689 * Same as the internal routine above, except that is must lock
2690 * and verify that the task is active. This differs from task_suspend
2691 * in that it places a kernel hold on the task rather than just a
2692 * user-level hold. This keeps users from over resuming and setting
2693 * it running out from under the kernel.
2695 * CONDITIONS: the caller holds a reference on the task
2701 if (task
== TASK_NULL
)
2702 return (KERN_INVALID_ARGUMENT
);
2706 if (!task
->active
) {
2709 return (KERN_FAILURE
);
2712 task_hold_locked(task
);
2715 return (KERN_SUCCESS
);
2721 boolean_t until_not_runnable
)
2723 if (task
== TASK_NULL
)
2724 return (KERN_INVALID_ARGUMENT
);
2728 if (!task
->active
) {
2731 return (KERN_FAILURE
);
2734 task_wait_locked(task
, until_not_runnable
);
2737 return (KERN_SUCCESS
);
2743 * Wait for all threads in task to stop.
2746 * Called with task locked, active, and held.
2751 boolean_t until_not_runnable
)
2753 thread_t thread
, self
;
2755 assert(task
->active
);
2756 assert(task
->suspend_count
> 0);
2758 self
= current_thread();
2761 * Iterate through all the threads and wait for them to
2762 * stop. Do not wait for the current thread if it is within
2765 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2767 thread_wait(thread
, until_not_runnable
);
2772 * task_release_locked:
2774 * Release a kernel hold on a task.
2776 * CONDITIONS: the task is locked and active
2779 task_release_locked(
2784 assert(task
->active
);
2785 assert(task
->suspend_count
> 0);
2787 if (--task
->suspend_count
> 0)
2790 if (task
->bsd_info
) {
2791 workq_proc_resumed(task
->bsd_info
);
2794 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2795 thread_mtx_lock(thread
);
2796 thread_release(thread
);
2797 thread_mtx_unlock(thread
);
2804 * Same as the internal routine above, except that it must lock
2805 * and verify that the task is active.
2807 * CONDITIONS: The caller holds a reference to the task
2813 if (task
== TASK_NULL
)
2814 return (KERN_INVALID_ARGUMENT
);
2818 if (!task
->active
) {
2821 return (KERN_FAILURE
);
2824 task_release_locked(task
);
2827 return (KERN_SUCCESS
);
2833 thread_act_array_t
*threads_out
,
2834 mach_msg_type_number_t
*count
)
2836 mach_msg_type_number_t actual
;
2837 thread_t
*thread_list
;
2839 vm_size_t size
, size_needed
;
2843 if (task
== TASK_NULL
)
2844 return (KERN_INVALID_ARGUMENT
);
2846 size
= 0; addr
= NULL
;
2850 if (!task
->active
) {
2856 return (KERN_FAILURE
);
2859 actual
= task
->thread_count
;
2861 /* do we have the memory we need? */
2862 size_needed
= actual
* sizeof (mach_port_t
);
2863 if (size_needed
<= size
)
2866 /* unlock the task and allocate more memory */
2872 assert(size_needed
> 0);
2875 addr
= kalloc(size
);
2877 return (KERN_RESOURCE_SHORTAGE
);
2880 /* OK, have memory and the task is locked & active */
2881 thread_list
= (thread_t
*)addr
;
2885 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
2886 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
2887 thread_reference_internal(thread
);
2888 thread_list
[j
++] = thread
;
2891 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
2894 size_needed
= actual
* sizeof (mach_port_t
);
2896 /* can unlock task now that we've got the thread refs */
2900 /* no threads, so return null pointer and deallocate memory */
2902 *threads_out
= NULL
;
2909 /* if we allocated too much, must copy */
2911 if (size_needed
< size
) {
2914 newaddr
= kalloc(size_needed
);
2916 for (i
= 0; i
< actual
; ++i
)
2917 thread_deallocate(thread_list
[i
]);
2919 return (KERN_RESOURCE_SHORTAGE
);
2922 bcopy(addr
, newaddr
, size_needed
);
2924 thread_list
= (thread_t
*)newaddr
;
2927 *threads_out
= thread_list
;
2930 /* do the conversion that Mig should handle */
2932 for (i
= 0; i
< actual
; ++i
)
2933 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
2936 return (KERN_SUCCESS
);
2939 #define TASK_HOLD_NORMAL 0
2940 #define TASK_HOLD_PIDSUSPEND 1
2941 #define TASK_HOLD_LEGACY 2
2942 #define TASK_HOLD_LEGACY_ALL 3
2944 static kern_return_t
2949 if (!task
->active
&& !task_is_a_corpse(task
)) {
2950 return (KERN_FAILURE
);
2953 /* Return success for corpse task */
2954 if (task_is_a_corpse(task
)) {
2955 return KERN_SUCCESS
;
2958 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2959 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_SUSPEND
) | DBG_FUNC_NONE
,
2960 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2961 task
->user_stop_count
, task
->user_stop_count
+ 1, 0);
2964 current_task()->suspends_outstanding
++;
2967 if (mode
== TASK_HOLD_LEGACY
)
2968 task
->legacy_stop_count
++;
2970 if (task
->user_stop_count
++ > 0) {
2972 * If the stop count was positive, the task is
2973 * already stopped and we can exit.
2975 return (KERN_SUCCESS
);
2979 * Put a kernel-level hold on the threads in the task (all
2980 * user-level task suspensions added together represent a
2981 * single kernel-level hold). We then wait for the threads
2982 * to stop executing user code.
2984 task_hold_locked(task
);
2985 task_wait_locked(task
, FALSE
);
2987 return (KERN_SUCCESS
);
2990 static kern_return_t
2995 boolean_t release
= FALSE
;
2997 if (!task
->active
&& !task_is_a_corpse(task
)) {
2998 return (KERN_FAILURE
);
3001 /* Return success for corpse task */
3002 if (task_is_a_corpse(task
)) {
3003 return KERN_SUCCESS
;
3006 if (mode
== TASK_HOLD_PIDSUSPEND
) {
3007 if (task
->pidsuspended
== FALSE
) {
3008 return (KERN_FAILURE
);
3010 task
->pidsuspended
= FALSE
;
3013 if (task
->user_stop_count
> (task
->pidsuspended
? 1 : 0)) {
3015 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3016 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_RESUME
) | DBG_FUNC_NONE
,
3017 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
3018 task
->user_stop_count
, mode
, task
->legacy_stop_count
);
3022 * This is obviously not robust; if we suspend one task and then resume a different one,
3023 * we'll fly under the radar. This is only meant to catch the common case of a crashed
3024 * or buggy suspender.
3026 current_task()->suspends_outstanding
--;
3029 if (mode
== TASK_HOLD_LEGACY_ALL
) {
3030 if (task
->legacy_stop_count
>= task
->user_stop_count
) {
3031 task
->user_stop_count
= 0;
3034 task
->user_stop_count
-= task
->legacy_stop_count
;
3036 task
->legacy_stop_count
= 0;
3038 if (mode
== TASK_HOLD_LEGACY
&& task
->legacy_stop_count
> 0)
3039 task
->legacy_stop_count
--;
3040 if (--task
->user_stop_count
== 0)
3045 return (KERN_FAILURE
);
3049 * Release the task if necessary.
3052 task_release_locked(task
);
3054 return (KERN_SUCCESS
);
3061 * Implement an (old-fashioned) user-level suspension on a task.
3063 * Because the user isn't expecting to have to manage a suspension
3064 * token, we'll track it for him in the kernel in the form of a naked
3065 * send right to the task's resume port. All such send rights
3066 * account for a single suspension against the task (unlike task_suspend2()
3067 * where each caller gets a unique suspension count represented by a
3068 * unique send-once right).
3071 * The caller holds a reference to the task
3078 mach_port_t port
, send
, old_notify
;
3079 mach_port_name_t name
;
3081 if (task
== TASK_NULL
|| task
== kernel_task
)
3082 return (KERN_INVALID_ARGUMENT
);
3087 * Claim a send right on the task resume port, and request a no-senders
3088 * notification on that port (if none outstanding).
3090 if (task
->itk_resume
== IP_NULL
) {
3091 task
->itk_resume
= ipc_port_alloc_kernel();
3092 if (!IP_VALID(task
->itk_resume
))
3093 panic("failed to create resume port");
3094 ipc_kobject_set(task
->itk_resume
, (ipc_kobject_t
)task
, IKOT_TASK_RESUME
);
3097 port
= task
->itk_resume
;
3099 assert(ip_active(port
));
3101 send
= ipc_port_make_send_locked(port
);
3102 assert(IP_VALID(send
));
3104 if (port
->ip_nsrequest
== IP_NULL
) {
3105 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
3106 assert(old_notify
== IP_NULL
);
3113 * place a legacy hold on the task.
3115 kr
= place_task_hold(task
, TASK_HOLD_LEGACY
);
3116 if (kr
!= KERN_SUCCESS
) {
3118 ipc_port_release_send(send
);
3125 * Copyout the send right into the calling task's IPC space. It won't know it is there,
3126 * but we'll look it up when calling a traditional resume. Any IPC operations that
3127 * deallocate the send right will auto-release the suspension.
3129 if ((kr
= ipc_kmsg_copyout_object(current_task()->itk_space
, (ipc_object_t
)send
,
3130 MACH_MSG_TYPE_MOVE_SEND
, &name
)) != KERN_SUCCESS
) {
3131 printf("warning: %s(%d) failed to copyout suspension token for pid %d with error: %d\n",
3132 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
3133 task_pid(task
), kr
);
3142 * Release a user hold on a task.
3145 * The caller holds a reference to the task
3152 mach_port_name_t resume_port_name
;
3153 ipc_entry_t resume_port_entry
;
3154 ipc_space_t space
= current_task()->itk_space
;
3156 if (task
== TASK_NULL
|| task
== kernel_task
)
3157 return (KERN_INVALID_ARGUMENT
);
3159 /* release a legacy task hold */
3161 kr
= release_task_hold(task
, TASK_HOLD_LEGACY
);
3164 is_write_lock(space
);
3165 if (is_active(space
) && IP_VALID(task
->itk_resume
) &&
3166 ipc_hash_lookup(space
, (ipc_object_t
)task
->itk_resume
, &resume_port_name
, &resume_port_entry
) == TRUE
) {
3168 * We found a suspension token in the caller's IPC space. Release a send right to indicate that
3169 * we are holding one less legacy hold on the task from this caller. If the release failed,
3170 * go ahead and drop all the rights, as someone either already released our holds or the task
3173 if (kr
== KERN_SUCCESS
)
3174 ipc_right_dealloc(space
, resume_port_name
, resume_port_entry
);
3176 ipc_right_destroy(space
, resume_port_name
, resume_port_entry
, FALSE
, 0);
3177 /* space unlocked */
3179 is_write_unlock(space
);
3180 if (kr
== KERN_SUCCESS
)
3181 printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
3182 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
3190 * Suspend the target task.
3191 * Making/holding a token/reference/port is the callers responsibility.
3194 task_suspend_internal(task_t task
)
3198 if (task
== TASK_NULL
|| task
== kernel_task
)
3199 return (KERN_INVALID_ARGUMENT
);
3202 kr
= place_task_hold(task
, TASK_HOLD_NORMAL
);
3208 * Suspend the target task, and return a suspension token. The token
3209 * represents a reference on the suspended task.
3214 task_suspension_token_t
*suspend_token
)
3218 kr
= task_suspend_internal(task
);
3219 if (kr
!= KERN_SUCCESS
) {
3220 *suspend_token
= TASK_NULL
;
3225 * Take a reference on the target task and return that to the caller
3226 * as a "suspension token," which can be converted into an SO right to
3227 * the now-suspended task's resume port.
3229 task_reference_internal(task
);
3230 *suspend_token
= task
;
3232 return (KERN_SUCCESS
);
3237 * (reference/token/port management is caller's responsibility).
3240 task_resume_internal(
3241 task_suspension_token_t task
)
3245 if (task
== TASK_NULL
|| task
== kernel_task
)
3246 return (KERN_INVALID_ARGUMENT
);
3249 kr
= release_task_hold(task
, TASK_HOLD_NORMAL
);
3255 * Resume the task using a suspension token. Consumes the token's ref.
3259 task_suspension_token_t task
)
3263 kr
= task_resume_internal(task
);
3264 task_suspension_token_deallocate(task
);
3270 task_suspension_notify(mach_msg_header_t
*request_header
)
3272 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
3273 task_t task
= convert_port_to_task_suspension_token(port
);
3274 mach_msg_type_number_t not_count
;
3276 if (task
== TASK_NULL
|| task
== kernel_task
)
3277 return TRUE
; /* nothing to do */
3279 switch (request_header
->msgh_id
) {
3281 case MACH_NOTIFY_SEND_ONCE
:
3282 /* release the hold held by this specific send-once right */
3284 release_task_hold(task
, TASK_HOLD_NORMAL
);
3288 case MACH_NOTIFY_NO_SENDERS
:
3289 not_count
= ((mach_no_senders_notification_t
*)request_header
)->not_count
;
3293 if (port
->ip_mscount
== not_count
) {
3295 /* release all the [remaining] outstanding legacy holds */
3296 assert(port
->ip_nsrequest
== IP_NULL
);
3298 release_task_hold(task
, TASK_HOLD_LEGACY_ALL
);
3301 } else if (port
->ip_nsrequest
== IP_NULL
) {
3302 ipc_port_t old_notify
;
3305 /* new send rights, re-arm notification at current make-send count */
3306 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
3307 assert(old_notify
== IP_NULL
);
3319 task_suspension_token_deallocate(task
); /* drop token reference */
3324 task_pidsuspend_locked(task_t task
)
3328 if (task
->pidsuspended
) {
3333 task
->pidsuspended
= TRUE
;
3335 kr
= place_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3336 if (kr
!= KERN_SUCCESS
) {
3337 task
->pidsuspended
= FALSE
;
3347 * Suspends a task by placing a hold on its threads.
3350 * The caller holds a reference to the task
3358 if (task
== TASK_NULL
|| task
== kernel_task
)
3359 return (KERN_INVALID_ARGUMENT
);
3363 kr
= task_pidsuspend_locked(task
);
3372 * Resumes a previously suspended task.
3375 * The caller holds a reference to the task
3383 if (task
== TASK_NULL
|| task
== kernel_task
)
3384 return (KERN_INVALID_ARGUMENT
);
3390 while (task
->changing_freeze_state
) {
3392 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3394 thread_block(THREAD_CONTINUE_NULL
);
3398 task
->changing_freeze_state
= TRUE
;
3401 kr
= release_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3409 if (kr
== KERN_SUCCESS
)
3410 task
->frozen
= FALSE
;
3411 task
->changing_freeze_state
= FALSE
;
3412 thread_wakeup(&task
->changing_freeze_state
);
3421 #if DEVELOPMENT || DEBUG
3423 extern void IOSleep(int);
3426 task_disconnect_page_mappings(task_t task
)
3430 if (task
== TASK_NULL
|| task
== kernel_task
)
3431 return (KERN_INVALID_ARGUMENT
);
3434 * this function is used to strip all of the mappings from
3435 * the pmap for the specified task to force the task to
3436 * re-fault all of the pages it is actively using... this
3437 * allows us to approximate the true working set of the
3438 * specified task. We only engage if at least 1 of the
3439 * threads in the task is runnable, but we want to continuously
3440 * sweep (at least for a while - I've arbitrarily set the limit at
3441 * 100 sweeps to be re-looked at as we gain experience) to get a better
3442 * view into what areas within a page are being visited (as opposed to only
3443 * seeing the first fault of a page after the task becomes
3444 * runnable)... in the future I may
3445 * try to block until awakened by a thread in this task
3446 * being made runnable, but for now we'll periodically poll from the
3447 * user level debug tool driving the sysctl
3449 for (n
= 0; n
< 100; n
++) {
3452 boolean_t do_unnest
;
3460 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3462 if (thread
->state
& TH_RUN
) {
3468 task
->task_disconnected_count
++;
3470 if (task
->task_unnested
== FALSE
) {
3471 if (runnable
== TRUE
) {
3472 task
->task_unnested
= TRUE
;
3478 if (runnable
== FALSE
)
3481 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_START
,
3482 task
, do_unnest
, task
->task_disconnected_count
, 0, 0);
3484 page_count
= vm_map_disconnect_page_mappings(task
->map
, do_unnest
);
3486 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_END
,
3487 task
, page_count
, 0, 0, 0);
3492 return (KERN_SUCCESS
);
3506 * The caller holds a reference to the task
3508 extern void vm_wake_compactor_swapper(void);
3509 extern queue_head_t c_swapout_list_head
;
3514 uint32_t *purgeable_count
,
3515 uint32_t *wired_count
,
3516 uint32_t *clean_count
,
3517 uint32_t *dirty_count
,
3518 uint32_t dirty_budget
,
3519 uint32_t *shared_count
,
3520 int *freezer_error_code
,
3521 boolean_t eval_only
)
3523 kern_return_t kr
= KERN_SUCCESS
;
3525 if (task
== TASK_NULL
|| task
== kernel_task
)
3526 return (KERN_INVALID_ARGUMENT
);
3530 while (task
->changing_freeze_state
) {
3532 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3534 thread_block(THREAD_CONTINUE_NULL
);
3540 return (KERN_FAILURE
);
3542 task
->changing_freeze_state
= TRUE
;
3546 kr
= vm_map_freeze(task
->map
,
3558 if ((kr
== KERN_SUCCESS
) && (eval_only
== FALSE
)) {
3559 task
->frozen
= TRUE
;
3562 task
->changing_freeze_state
= FALSE
;
3563 thread_wakeup(&task
->changing_freeze_state
);
3567 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
&&
3568 (eval_only
== FALSE
)) {
3569 vm_wake_compactor_swapper();
3571 * We do an explicit wakeup of the swapout thread here
3572 * because the compact_and_swap routines don't have
3573 * knowledge about these kind of "per-task packed c_segs"
3574 * and so will not be evaluating whether we need to do
3577 thread_wakeup((event_t
)&c_swapout_list_head
);
3586 * Thaw a currently frozen task.
3589 * The caller holds a reference to the task
3595 if (task
== TASK_NULL
|| task
== kernel_task
)
3596 return (KERN_INVALID_ARGUMENT
);
3600 while (task
->changing_freeze_state
) {
3602 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3604 thread_block(THREAD_CONTINUE_NULL
);
3608 if (!task
->frozen
) {
3610 return (KERN_FAILURE
);
3612 task
->frozen
= FALSE
;
3616 return (KERN_SUCCESS
);
3619 #endif /* CONFIG_FREEZE */
3622 host_security_set_task_token(
3623 host_security_t host_security
,
3625 security_token_t sec_token
,
3626 audit_token_t audit_token
,
3627 host_priv_t host_priv
)
3629 ipc_port_t host_port
;
3632 if (task
== TASK_NULL
)
3633 return(KERN_INVALID_ARGUMENT
);
3635 if (host_security
== HOST_NULL
)
3636 return(KERN_INVALID_SECURITY
);
3639 task
->sec_token
= sec_token
;
3640 task
->audit_token
= audit_token
;
3644 if (host_priv
!= HOST_PRIV_NULL
) {
3645 kr
= host_get_host_priv_port(host_priv
, &host_port
);
3647 kr
= host_get_host_port(host_priv_self(), &host_port
);
3649 assert(kr
== KERN_SUCCESS
);
3650 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
3655 task_send_trace_memory(
3657 __unused
uint32_t pid
,
3658 __unused
uint64_t uniqueid
)
3660 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
3661 if (target_task
== TASK_NULL
)
3662 return (KERN_INVALID_ARGUMENT
);
3665 kr
= atm_send_proc_inspect_notification(target_task
,
3673 * This routine was added, pretty much exclusively, for registering the
3674 * RPC glue vector for in-kernel short circuited tasks. Rather than
3675 * removing it completely, I have only disabled that feature (which was
3676 * the only feature at the time). It just appears that we are going to
3677 * want to add some user data to tasks in the future (i.e. bsd info,
3678 * task names, etc...), so I left it in the formal task interface.
3683 task_flavor_t flavor
,
3684 __unused task_info_t task_info_in
, /* pointer to IN array */
3685 __unused mach_msg_type_number_t task_info_count
)
3687 if (task
== TASK_NULL
)
3688 return(KERN_INVALID_ARGUMENT
);
3693 case TASK_TRACE_MEMORY_INFO
:
3695 if (task_info_count
!= TASK_TRACE_MEMORY_INFO_COUNT
)
3696 return (KERN_INVALID_ARGUMENT
);
3698 assert(task_info_in
!= NULL
);
3699 task_trace_memory_info_t mem_info
;
3700 mem_info
= (task_trace_memory_info_t
) task_info_in
;
3701 kern_return_t kr
= atm_register_trace_memory(task
,
3702 mem_info
->user_memory_address
,
3703 mem_info
->buffer_size
);
3709 return (KERN_INVALID_ARGUMENT
);
3711 return (KERN_SUCCESS
);
3714 int radar_20146450
= 1;
3718 task_flavor_t flavor
,
3719 task_info_t task_info_out
,
3720 mach_msg_type_number_t
*task_info_count
)
3722 kern_return_t error
= KERN_SUCCESS
;
3723 mach_msg_type_number_t original_task_info_count
;
3725 if (task
== TASK_NULL
)
3726 return (KERN_INVALID_ARGUMENT
);
3728 original_task_info_count
= *task_info_count
;
3731 if ((task
!= current_task()) && (!task
->active
)) {
3733 return (KERN_INVALID_ARGUMENT
);
3738 case TASK_BASIC_INFO_32
:
3739 case TASK_BASIC2_INFO_32
:
3740 #if defined(__arm__) || defined(__arm64__)
3741 case TASK_BASIC_INFO_64
:
3744 task_basic_info_32_t basic_info
;
3749 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
) {
3750 error
= KERN_INVALID_ARGUMENT
;
3754 basic_info
= (task_basic_info_32_t
)task_info_out
;
3756 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3757 basic_info
->virtual_size
= (typeof(basic_info
->virtual_size
))map
->size
;
3758 if (flavor
== TASK_BASIC2_INFO_32
) {
3760 * The "BASIC2" flavor gets the maximum resident
3761 * size instead of the current resident size...
3763 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
3765 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
3767 basic_info
->resident_size
*= PAGE_SIZE
;
3769 basic_info
->policy
= ((task
!= kernel_task
)?
3770 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
= TASK_BASIC_INFO_32_COUNT
;
3787 #if defined(__arm__) || defined(__arm64__)
3788 case TASK_BASIC_INFO_64_2
:
3790 task_basic_info_64_2_t basic_info
;
3795 if (*task_info_count
< TASK_BASIC_INFO_64_2_COUNT
) {
3796 error
= KERN_INVALID_ARGUMENT
;
3800 basic_info
= (task_basic_info_64_2_t
)task_info_out
;
3802 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3803 basic_info
->virtual_size
= map
->size
;
3804 basic_info
->resident_size
=
3805 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
3808 basic_info
->policy
= ((task
!= kernel_task
)?
3809 POLICY_TIMESHARE
: POLICY_RR
);
3810 basic_info
->suspend_count
= task
->user_stop_count
;
3812 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3813 basic_info
->user_time
.seconds
=
3814 (typeof(basic_info
->user_time
.seconds
))secs
;
3815 basic_info
->user_time
.microseconds
= usecs
;
3817 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3818 basic_info
->system_time
.seconds
=
3819 (typeof(basic_info
->system_time
.seconds
))secs
;
3820 basic_info
->system_time
.microseconds
= usecs
;
3822 *task_info_count
= TASK_BASIC_INFO_64_2_COUNT
;
3826 #else /* defined(__arm__) || defined(__arm64__) */
3827 case TASK_BASIC_INFO_64
:
3829 task_basic_info_64_t basic_info
;
3834 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
) {
3835 error
= KERN_INVALID_ARGUMENT
;
3839 basic_info
= (task_basic_info_64_t
)task_info_out
;
3841 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3842 basic_info
->virtual_size
= map
->size
;
3843 basic_info
->resident_size
=
3844 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
3847 basic_info
->policy
= ((task
!= kernel_task
)?
3848 POLICY_TIMESHARE
: POLICY_RR
);
3849 basic_info
->suspend_count
= task
->user_stop_count
;
3851 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3852 basic_info
->user_time
.seconds
=
3853 (typeof(basic_info
->user_time
.seconds
))secs
;
3854 basic_info
->user_time
.microseconds
= usecs
;
3856 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3857 basic_info
->system_time
.seconds
=
3858 (typeof(basic_info
->system_time
.seconds
))secs
;
3859 basic_info
->system_time
.microseconds
= usecs
;
3861 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
3864 #endif /* defined(__arm__) || defined(__arm64__) */
3866 case MACH_TASK_BASIC_INFO
:
3868 mach_task_basic_info_t basic_info
;
3873 if (*task_info_count
< MACH_TASK_BASIC_INFO_COUNT
) {
3874 error
= KERN_INVALID_ARGUMENT
;
3878 basic_info
= (mach_task_basic_info_t
)task_info_out
;
3880 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
3882 basic_info
->virtual_size
= map
->size
;
3884 basic_info
->resident_size
=
3885 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
));
3886 basic_info
->resident_size
*= PAGE_SIZE_64
;
3888 basic_info
->resident_size_max
=
3889 (mach_vm_size_t
)(pmap_resident_max(map
->pmap
));
3890 basic_info
->resident_size_max
*= PAGE_SIZE_64
;
3892 basic_info
->policy
= ((task
!= kernel_task
) ?
3893 POLICY_TIMESHARE
: POLICY_RR
);
3895 basic_info
->suspend_count
= task
->user_stop_count
;
3897 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3898 basic_info
->user_time
.seconds
=
3899 (typeof(basic_info
->user_time
.seconds
))secs
;
3900 basic_info
->user_time
.microseconds
= usecs
;
3902 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3903 basic_info
->system_time
.seconds
=
3904 (typeof(basic_info
->system_time
.seconds
))secs
;
3905 basic_info
->system_time
.microseconds
= usecs
;
3907 *task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
3911 case TASK_THREAD_TIMES_INFO
:
3913 task_thread_times_info_t times_info
;
3916 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
3917 error
= KERN_INVALID_ARGUMENT
;
3921 times_info
= (task_thread_times_info_t
) task_info_out
;
3922 times_info
->user_time
.seconds
= 0;
3923 times_info
->user_time
.microseconds
= 0;
3924 times_info
->system_time
.seconds
= 0;
3925 times_info
->system_time
.microseconds
= 0;
3928 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3929 time_value_t user_time
, system_time
;
3931 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3934 thread_read_times(thread
, &user_time
, &system_time
, NULL
);
3936 time_value_add(×_info
->user_time
, &user_time
);
3937 time_value_add(×_info
->system_time
, &system_time
);
3940 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
3944 case TASK_ABSOLUTETIME_INFO
:
3946 task_absolutetime_info_t info
;
3949 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
) {
3950 error
= KERN_INVALID_ARGUMENT
;
3954 info
= (task_absolutetime_info_t
)task_info_out
;
3955 info
->threads_user
= info
->threads_system
= 0;
3958 info
->total_user
= task
->total_user_time
;
3959 info
->total_system
= task
->total_system_time
;
3961 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3965 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3969 thread_lock(thread
);
3971 tval
= timer_grab(&thread
->user_timer
);
3972 info
->threads_user
+= tval
;
3973 info
->total_user
+= tval
;
3975 tval
= timer_grab(&thread
->system_timer
);
3976 if (thread
->precise_user_kernel_time
) {
3977 info
->threads_system
+= tval
;
3978 info
->total_system
+= tval
;
3980 /* system_timer may represent either sys or user */
3981 info
->threads_user
+= tval
;
3982 info
->total_user
+= tval
;
3985 thread_unlock(thread
);
3990 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
3994 case TASK_DYLD_INFO
:
3996 task_dyld_info_t info
;
3999 * We added the format field to TASK_DYLD_INFO output. For
4000 * temporary backward compatibility, accept the fact that
4001 * clients may ask for the old version - distinquished by the
4002 * size of the expected result structure.
4004 #define TASK_LEGACY_DYLD_INFO_COUNT \
4005 offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
4007 if (*task_info_count
< TASK_LEGACY_DYLD_INFO_COUNT
) {
4008 error
= KERN_INVALID_ARGUMENT
;
4012 info
= (task_dyld_info_t
)task_info_out
;
4013 info
->all_image_info_addr
= task
->all_image_info_addr
;
4014 info
->all_image_info_size
= task
->all_image_info_size
;
4016 /* only set format on output for those expecting it */
4017 if (*task_info_count
>= TASK_DYLD_INFO_COUNT
) {
4018 info
->all_image_info_format
= task_has_64Bit_addr(task
) ?
4019 TASK_DYLD_ALL_IMAGE_INFO_64
:
4020 TASK_DYLD_ALL_IMAGE_INFO_32
;
4021 *task_info_count
= TASK_DYLD_INFO_COUNT
;
4023 *task_info_count
= TASK_LEGACY_DYLD_INFO_COUNT
;
4028 case TASK_EXTMOD_INFO
:
4030 task_extmod_info_t info
;
4033 if (*task_info_count
< TASK_EXTMOD_INFO_COUNT
) {
4034 error
= KERN_INVALID_ARGUMENT
;
4038 info
= (task_extmod_info_t
)task_info_out
;
4040 p
= get_bsdtask_info(task
);
4042 proc_getexecutableuuid(p
, info
->task_uuid
, sizeof(info
->task_uuid
));
4044 bzero(info
->task_uuid
, sizeof(info
->task_uuid
));
4046 info
->extmod_statistics
= task
->extmod_statistics
;
4047 *task_info_count
= TASK_EXTMOD_INFO_COUNT
;
4052 case TASK_KERNELMEMORY_INFO
:
4054 task_kernelmemory_info_t tkm_info
;
4055 ledger_amount_t credit
, debit
;
4057 if (*task_info_count
< TASK_KERNELMEMORY_INFO_COUNT
) {
4058 error
= KERN_INVALID_ARGUMENT
;
4062 tkm_info
= (task_kernelmemory_info_t
) task_info_out
;
4063 tkm_info
->total_palloc
= 0;
4064 tkm_info
->total_pfree
= 0;
4065 tkm_info
->total_salloc
= 0;
4066 tkm_info
->total_sfree
= 0;
4068 if (task
== kernel_task
) {
4070 * All shared allocs/frees from other tasks count against
4071 * the kernel private memory usage. If we are looking up
4072 * info for the kernel task, gather from everywhere.
4076 /* start by accounting for all the terminated tasks against the kernel */
4077 tkm_info
->total_palloc
= tasks_tkm_private
.alloc
+ tasks_tkm_shared
.alloc
;
4078 tkm_info
->total_pfree
= tasks_tkm_private
.free
+ tasks_tkm_shared
.free
;
4080 /* count all other task/thread shared alloc/free against the kernel */
4081 lck_mtx_lock(&tasks_threads_lock
);
4083 /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
4084 queue_iterate(&tasks
, task
, task_t
, tasks
) {
4085 if (task
== kernel_task
) {
4086 if (ledger_get_entries(task
->ledger
,
4087 task_ledgers
.tkm_private
, &credit
,
4088 &debit
) == KERN_SUCCESS
) {
4089 tkm_info
->total_palloc
+= credit
;
4090 tkm_info
->total_pfree
+= debit
;
4093 if (!ledger_get_entries(task
->ledger
,
4094 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
4095 tkm_info
->total_palloc
+= credit
;
4096 tkm_info
->total_pfree
+= debit
;
4099 lck_mtx_unlock(&tasks_threads_lock
);
4101 if (!ledger_get_entries(task
->ledger
,
4102 task_ledgers
.tkm_private
, &credit
, &debit
)) {
4103 tkm_info
->total_palloc
= credit
;
4104 tkm_info
->total_pfree
= debit
;
4106 if (!ledger_get_entries(task
->ledger
,
4107 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
4108 tkm_info
->total_salloc
= credit
;
4109 tkm_info
->total_sfree
= debit
;
4114 *task_info_count
= TASK_KERNELMEMORY_INFO_COUNT
;
4115 return KERN_SUCCESS
;
4119 case TASK_SCHED_FIFO_INFO
:
4122 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
) {
4123 error
= KERN_INVALID_ARGUMENT
;
4127 error
= KERN_INVALID_POLICY
;
4132 case TASK_SCHED_RR_INFO
:
4134 policy_rr_base_t rr_base
;
4135 uint32_t quantum_time
;
4136 uint64_t quantum_ns
;
4138 if (*task_info_count
< POLICY_RR_BASE_COUNT
) {
4139 error
= KERN_INVALID_ARGUMENT
;
4143 rr_base
= (policy_rr_base_t
) task_info_out
;
4145 if (task
!= kernel_task
) {
4146 error
= KERN_INVALID_POLICY
;
4150 rr_base
->base_priority
= task
->priority
;
4152 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
4153 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
4155 rr_base
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
4157 *task_info_count
= POLICY_RR_BASE_COUNT
;
4162 case TASK_SCHED_TIMESHARE_INFO
:
4164 policy_timeshare_base_t ts_base
;
4166 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
) {
4167 error
= KERN_INVALID_ARGUMENT
;
4171 ts_base
= (policy_timeshare_base_t
) task_info_out
;
4173 if (task
== kernel_task
) {
4174 error
= KERN_INVALID_POLICY
;
4178 ts_base
->base_priority
= task
->priority
;
4180 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
4184 case TASK_SECURITY_TOKEN
:
4186 security_token_t
*sec_token_p
;
4188 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
4189 error
= KERN_INVALID_ARGUMENT
;
4193 sec_token_p
= (security_token_t
*) task_info_out
;
4195 *sec_token_p
= task
->sec_token
;
4197 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
4201 case TASK_AUDIT_TOKEN
:
4203 audit_token_t
*audit_token_p
;
4205 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
4206 error
= KERN_INVALID_ARGUMENT
;
4210 audit_token_p
= (audit_token_t
*) task_info_out
;
4212 *audit_token_p
= task
->audit_token
;
4214 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
4218 case TASK_SCHED_INFO
:
4219 error
= KERN_INVALID_ARGUMENT
;
4222 case TASK_EVENTS_INFO
:
4224 task_events_info_t events_info
;
4227 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
4228 error
= KERN_INVALID_ARGUMENT
;
4232 events_info
= (task_events_info_t
) task_info_out
;
4235 events_info
->faults
= task
->faults
;
4236 events_info
->pageins
= task
->pageins
;
4237 events_info
->cow_faults
= task
->cow_faults
;
4238 events_info
->messages_sent
= task
->messages_sent
;
4239 events_info
->messages_received
= task
->messages_received
;
4240 events_info
->syscalls_mach
= task
->syscalls_mach
;
4241 events_info
->syscalls_unix
= task
->syscalls_unix
;
4243 events_info
->csw
= task
->c_switch
;
4245 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4246 events_info
->csw
+= thread
->c_switch
;
4247 events_info
->syscalls_mach
+= thread
->syscalls_mach
;
4248 events_info
->syscalls_unix
+= thread
->syscalls_unix
;
4252 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
4255 case TASK_AFFINITY_TAG_INFO
:
4257 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
) {
4258 error
= KERN_INVALID_ARGUMENT
;
4262 error
= task_affinity_info(task
, task_info_out
, task_info_count
);
4265 case TASK_POWER_INFO
:
4267 if (*task_info_count
< TASK_POWER_INFO_COUNT
) {
4268 error
= KERN_INVALID_ARGUMENT
;
4272 task_power_info_locked(task
, (task_power_info_t
)task_info_out
, NULL
, NULL
);
4276 case TASK_POWER_INFO_V2
:
4278 if (*task_info_count
< TASK_POWER_INFO_V2_COUNT_OLD
) {
4279 error
= KERN_INVALID_ARGUMENT
;
4282 task_power_info_v2_t tpiv2
= (task_power_info_v2_t
) task_info_out
;
4283 task_power_info_locked(task
, &tpiv2
->cpu_energy
, &tpiv2
->gpu_energy
, tpiv2
);
4288 case TASK_VM_INFO_PURGEABLE
:
4290 task_vm_info_t vm_info
;
4293 if (*task_info_count
< TASK_VM_INFO_REV0_COUNT
) {
4294 error
= KERN_INVALID_ARGUMENT
;
4298 vm_info
= (task_vm_info_t
)task_info_out
;
4300 if (task
== kernel_task
) {
4305 vm_map_lock_read(map
);
4308 vm_info
->virtual_size
= (typeof(vm_info
->virtual_size
))map
->size
;
4309 vm_info
->region_count
= map
->hdr
.nentries
;
4310 vm_info
->page_size
= vm_map_page_size(map
);
4312 vm_info
->resident_size
= pmap_resident_count(map
->pmap
);
4313 vm_info
->resident_size
*= PAGE_SIZE
;
4314 vm_info
->resident_size_peak
= pmap_resident_max(map
->pmap
);
4315 vm_info
->resident_size_peak
*= PAGE_SIZE
;
4317 #define _VM_INFO(_name) \
4318 vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
4321 _VM_INFO(device_peak
);
4323 _VM_INFO(external_peak
);
4325 _VM_INFO(internal_peak
);
4327 _VM_INFO(reusable_peak
);
4328 _VM_INFO(compressed
);
4329 _VM_INFO(compressed_peak
);
4330 _VM_INFO(compressed_lifetime
);
4332 vm_info
->purgeable_volatile_pmap
= 0;
4333 vm_info
->purgeable_volatile_resident
= 0;
4334 vm_info
->purgeable_volatile_virtual
= 0;
4335 if (task
== kernel_task
) {
4337 * We do not maintain the detailed stats for the
4338 * kernel_pmap, so just count everything as
4341 vm_info
->internal
= vm_info
->resident_size
;
4343 * ... but since the memory held by the VM compressor
4344 * in the kernel address space ought to be attributed
4345 * to user-space tasks, we subtract it from "internal"
4346 * to give memory reporting tools a more accurate idea
4347 * of what the kernel itself is actually using, instead
4348 * of making it look like the kernel is leaking memory
4349 * when the system is under memory pressure.
4351 vm_info
->internal
-= (VM_PAGE_COMPRESSOR_COUNT
*
4354 mach_vm_size_t volatile_virtual_size
;
4355 mach_vm_size_t volatile_resident_size
;
4356 mach_vm_size_t volatile_compressed_size
;
4357 mach_vm_size_t volatile_pmap_size
;
4358 mach_vm_size_t volatile_compressed_pmap_size
;
4361 if (flavor
== TASK_VM_INFO_PURGEABLE
) {
4362 kr
= vm_map_query_volatile(
4364 &volatile_virtual_size
,
4365 &volatile_resident_size
,
4366 &volatile_compressed_size
,
4367 &volatile_pmap_size
,
4368 &volatile_compressed_pmap_size
);
4369 if (kr
== KERN_SUCCESS
) {
4370 vm_info
->purgeable_volatile_pmap
=
4372 if (radar_20146450
) {
4373 vm_info
->compressed
-=
4374 volatile_compressed_pmap_size
;
4376 vm_info
->purgeable_volatile_resident
=
4377 volatile_resident_size
;
4378 vm_info
->purgeable_volatile_virtual
=
4379 volatile_virtual_size
;
4383 *task_info_count
= TASK_VM_INFO_REV0_COUNT
;
4385 if (original_task_info_count
>= TASK_VM_INFO_REV1_COUNT
) {
4386 vm_info
->phys_footprint
=
4387 (mach_vm_size_t
) get_task_phys_footprint(task
);
4388 *task_info_count
= TASK_VM_INFO_REV1_COUNT
;
4390 if (original_task_info_count
>= TASK_VM_INFO_REV2_COUNT
) {
4391 vm_info
->min_address
= map
->min_offset
;
4392 vm_info
->max_address
= map
->max_offset
;
4393 *task_info_count
= TASK_VM_INFO_REV2_COUNT
;
4396 if (task
!= kernel_task
) {
4397 vm_map_unlock_read(map
);
4403 case TASK_WAIT_STATE_INFO
:
4406 * Deprecated flavor. Currently allowing some results until all users
4407 * stop calling it. The results may not be accurate.
4409 task_wait_state_info_t wait_state_info
;
4410 uint64_t total_sfi_ledger_val
= 0;
4412 if (*task_info_count
< TASK_WAIT_STATE_INFO_COUNT
) {
4413 error
= KERN_INVALID_ARGUMENT
;
4417 wait_state_info
= (task_wait_state_info_t
) task_info_out
;
4419 wait_state_info
->total_wait_state_time
= 0;
4420 bzero(wait_state_info
->_reserved
, sizeof(wait_state_info
->_reserved
));
4422 #if CONFIG_SCHED_SFI
4423 int i
, prev_lentry
= -1;
4424 int64_t val_credit
, val_debit
;
4426 for (i
= 0; i
< MAX_SFI_CLASS_ID
; i
++){
4429 * checking with prev_lentry != entry ensures adjacent classes
4430 * which share the same ledger do not add wait times twice.
4431 * Note: Use ledger() call to get data for each individual sfi class.
4433 if (prev_lentry
!= task_ledgers
.sfi_wait_times
[i
] &&
4434 KERN_SUCCESS
== ledger_get_entries(task
->ledger
,
4435 task_ledgers
.sfi_wait_times
[i
], &val_credit
, &val_debit
)) {
4436 total_sfi_ledger_val
+= val_credit
;
4438 prev_lentry
= task_ledgers
.sfi_wait_times
[i
];
4441 #endif /* CONFIG_SCHED_SFI */
4442 wait_state_info
->total_wait_sfi_state_time
= total_sfi_ledger_val
;
4443 *task_info_count
= TASK_WAIT_STATE_INFO_COUNT
;
4447 case TASK_VM_INFO_PURGEABLE_ACCOUNT
:
4449 #if DEVELOPMENT || DEBUG
4450 pvm_account_info_t acnt_info
;
4452 if (*task_info_count
< PVM_ACCOUNT_INFO_COUNT
) {
4453 error
= KERN_INVALID_ARGUMENT
;
4457 if (task_info_out
== NULL
) {
4458 error
= KERN_INVALID_ARGUMENT
;
4462 acnt_info
= (pvm_account_info_t
) task_info_out
;
4464 error
= vm_purgeable_account(task
, acnt_info
);
4466 *task_info_count
= PVM_ACCOUNT_INFO_COUNT
;
4469 #else /* DEVELOPMENT || DEBUG */
4470 error
= KERN_NOT_SUPPORTED
;
4472 #endif /* DEVELOPMENT || DEBUG */
4474 case TASK_FLAGS_INFO
:
4476 task_flags_info_t flags_info
;
4478 if (*task_info_count
< TASK_FLAGS_INFO_COUNT
) {
4479 error
= KERN_INVALID_ARGUMENT
;
4483 flags_info
= (task_flags_info_t
)task_info_out
;
4485 /* only publish the 64-bit flag of the task */
4486 flags_info
->flags
= task
->t_flags
& (TF_64B_ADDR
| TF_64B_DATA
);
4488 *task_info_count
= TASK_FLAGS_INFO_COUNT
;
4492 case TASK_DEBUG_INFO_INTERNAL
:
4494 #if DEVELOPMENT || DEBUG
4495 task_debug_info_internal_t dbg_info
;
4496 if (*task_info_count
< TASK_DEBUG_INFO_INTERNAL_COUNT
) {
4497 error
= KERN_NOT_SUPPORTED
;
4501 if (task_info_out
== NULL
) {
4502 error
= KERN_INVALID_ARGUMENT
;
4505 dbg_info
= (task_debug_info_internal_t
) task_info_out
;
4506 dbg_info
->ipc_space_size
= 0;
4507 if (task
->itk_space
){
4508 dbg_info
->ipc_space_size
= task
->itk_space
->is_table_size
;
4511 dbg_info
->suspend_count
= task
->suspend_count
;
4513 error
= KERN_SUCCESS
;
4514 *task_info_count
= TASK_DEBUG_INFO_INTERNAL_COUNT
;
4516 #else /* DEVELOPMENT || DEBUG */
4517 error
= KERN_NOT_SUPPORTED
;
4519 #endif /* DEVELOPMENT || DEBUG */
4522 error
= KERN_INVALID_ARGUMENT
;
4530 * task_info_from_user
4532 * When calling task_info from user space,
4533 * this function will be executed as mig server side
4534 * instead of calling directly into task_info.
4535 * This gives the possibility to perform more security
4536 * checks on task_port.
4538 * In the case of TASK_DYLD_INFO, we require the more
4539 * privileged task_port not the less-privileged task_name_port.
4543 task_info_from_user(
4544 mach_port_t task_port
,
4545 task_flavor_t flavor
,
4546 task_info_t task_info_out
,
4547 mach_msg_type_number_t
*task_info_count
)
4552 if (flavor
== TASK_DYLD_INFO
)
4553 task
= convert_port_to_task(task_port
);
4555 task
= convert_port_to_task_name(task_port
);
4557 ret
= task_info(task
, flavor
, task_info_out
, task_info_count
);
4559 task_deallocate(task
);
4567 * Returns power stats for the task.
4568 * Note: Called with task locked.
4571 task_power_info_locked(
4573 task_power_info_t info
,
4574 gpu_energy_data_t ginfo
,
4575 task_power_info_v2_t infov2
)
4578 ledger_amount_t tmp
;
4580 task_lock_assert_owned(task
);
4582 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
4583 (ledger_amount_t
*)&info
->task_interrupt_wakeups
, &tmp
);
4584 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
4585 (ledger_amount_t
*)&info
->task_platform_idle_wakeups
, &tmp
);
4587 info
->task_timer_wakeups_bin_1
= task
->task_timer_wakeups_bin_1
;
4588 info
->task_timer_wakeups_bin_2
= task
->task_timer_wakeups_bin_2
;
4590 info
->total_user
= task
->total_user_time
;
4591 info
->total_system
= task
->total_system_time
;
4595 infov2
->task_energy
= task
->task_energy
;
4600 ginfo
->task_gpu_utilisation
= task
->task_gpu_ns
;
4604 infov2
->task_ptime
= task
->total_ptime
;
4605 infov2
->task_pset_switches
= task
->ps_switch
;
4608 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4612 if (thread
->options
& TH_OPT_IDLE_THREAD
)
4616 thread_lock(thread
);
4618 info
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
4619 info
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
4623 infov2
->task_energy
+= ml_energy_stat(thread
);
4627 tval
= timer_grab(&thread
->user_timer
);
4628 info
->total_user
+= tval
;
4631 tval
= timer_grab(&thread
->ptime
);
4632 infov2
->task_ptime
+= tval
;
4633 infov2
->task_pset_switches
+= thread
->ps_switch
;
4636 tval
= timer_grab(&thread
->system_timer
);
4637 if (thread
->precise_user_kernel_time
) {
4638 info
->total_system
+= tval
;
4640 /* system_timer may represent either sys or user */
4641 info
->total_user
+= tval
;
4645 ginfo
->task_gpu_utilisation
+= ml_gpu_stat(thread
);
4647 thread_unlock(thread
);
4653 * task_gpu_utilisation
4655 * Returns the total gpu time used by the all the threads of the task
4656 * (both dead and alive)
4659 task_gpu_utilisation(
4662 uint64_t gpu_time
= 0;
4663 #if !CONFIG_EMBEDDED
4667 gpu_time
+= task
->task_gpu_ns
;
4669 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4672 thread_lock(thread
);
4673 gpu_time
+= ml_gpu_stat(thread
);
4674 thread_unlock(thread
);
4679 #else /* CONFIG_EMBEDDED */
4680 /* silence compiler warning */
4682 #endif /* !CONFIG_EMBEDDED */
4689 * Returns the total energy used by the all the threads of the task
4690 * (both dead and alive)
4696 uint64_t energy
= 0;
4700 energy
+= task
->task_energy
;
4702 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4705 thread_lock(thread
);
4706 energy
+= ml_energy_stat(thread
);
4707 thread_unlock(thread
);
4718 __unused task_t task
)
4724 /* This function updates the cpu time in the arrays for each
4725 * effective and requested QoS class
4728 task_update_cpu_time_qos_stats(
4730 uint64_t *eqos_stats
,
4731 uint64_t *rqos_stats
)
4733 if (!eqos_stats
&& !rqos_stats
) {
4739 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4740 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
4744 thread_update_qos_cpu_time(thread
);
4748 eqos_stats
[THREAD_QOS_DEFAULT
] += task
->cpu_time_eqos_stats
.cpu_time_qos_default
;
4749 eqos_stats
[THREAD_QOS_MAINTENANCE
] += task
->cpu_time_eqos_stats
.cpu_time_qos_maintenance
;
4750 eqos_stats
[THREAD_QOS_BACKGROUND
] += task
->cpu_time_eqos_stats
.cpu_time_qos_background
;
4751 eqos_stats
[THREAD_QOS_UTILITY
] += task
->cpu_time_eqos_stats
.cpu_time_qos_utility
;
4752 eqos_stats
[THREAD_QOS_LEGACY
] += task
->cpu_time_eqos_stats
.cpu_time_qos_legacy
;
4753 eqos_stats
[THREAD_QOS_USER_INITIATED
] += task
->cpu_time_eqos_stats
.cpu_time_qos_user_initiated
;
4754 eqos_stats
[THREAD_QOS_USER_INTERACTIVE
] += task
->cpu_time_eqos_stats
.cpu_time_qos_user_interactive
;
4758 rqos_stats
[THREAD_QOS_DEFAULT
] += task
->cpu_time_rqos_stats
.cpu_time_qos_default
;
4759 rqos_stats
[THREAD_QOS_MAINTENANCE
] += task
->cpu_time_rqos_stats
.cpu_time_qos_maintenance
;
4760 rqos_stats
[THREAD_QOS_BACKGROUND
] += task
->cpu_time_rqos_stats
.cpu_time_qos_background
;
4761 rqos_stats
[THREAD_QOS_UTILITY
] += task
->cpu_time_rqos_stats
.cpu_time_qos_utility
;
4762 rqos_stats
[THREAD_QOS_LEGACY
] += task
->cpu_time_rqos_stats
.cpu_time_qos_legacy
;
4763 rqos_stats
[THREAD_QOS_USER_INITIATED
] += task
->cpu_time_rqos_stats
.cpu_time_qos_user_initiated
;
4764 rqos_stats
[THREAD_QOS_USER_INTERACTIVE
] += task
->cpu_time_rqos_stats
.cpu_time_qos_user_interactive
;
4773 task_purgable_info_t
*stats
)
4775 if (task
== TASK_NULL
|| stats
== NULL
)
4776 return KERN_INVALID_ARGUMENT
;
4777 /* Take task reference */
4778 task_reference(task
);
4779 vm_purgeable_stats((vm_purgeable_info_t
)stats
, task
);
4780 /* Drop task reference */
4781 task_deallocate(task
);
4782 return KERN_SUCCESS
;
4795 task
->vtimers
|= which
;
4799 case TASK_VTIMER_USER
:
4800 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4802 thread_lock(thread
);
4803 if (thread
->precise_user_kernel_time
)
4804 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
4806 thread
->vtimer_user_save
= timer_grab(&thread
->system_timer
);
4807 thread_unlock(thread
);
4812 case TASK_VTIMER_PROF
:
4813 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4815 thread_lock(thread
);
4816 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
4817 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
4818 thread_unlock(thread
);
4823 case TASK_VTIMER_RLIM
:
4824 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4826 thread_lock(thread
);
4827 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
4828 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
4829 thread_unlock(thread
);
4843 assert(task
== current_task());
4847 task
->vtimers
&= ~which
;
4857 uint32_t *microsecs
)
4859 thread_t thread
= current_thread();
4861 clock_sec_t secs
= 0;
4864 assert(task
== current_task());
4866 spl_t s
= splsched();
4867 thread_lock(thread
);
4869 if ((task
->vtimers
& which
) != (uint32_t)which
) {
4870 thread_unlock(thread
);
4877 case TASK_VTIMER_USER
:
4878 if (thread
->precise_user_kernel_time
) {
4879 tdelt
= (uint32_t)timer_delta(&thread
->user_timer
,
4880 &thread
->vtimer_user_save
);
4882 tdelt
= (uint32_t)timer_delta(&thread
->system_timer
,
4883 &thread
->vtimer_user_save
);
4885 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4888 case TASK_VTIMER_PROF
:
4889 tsum
= timer_grab(&thread
->user_timer
);
4890 tsum
+= timer_grab(&thread
->system_timer
);
4891 tdelt
= (uint32_t)(tsum
- thread
->vtimer_prof_save
);
4892 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4893 /* if the time delta is smaller than a usec, ignore */
4894 if (*microsecs
!= 0)
4895 thread
->vtimer_prof_save
= tsum
;
4898 case TASK_VTIMER_RLIM
:
4899 tsum
= timer_grab(&thread
->user_timer
);
4900 tsum
+= timer_grab(&thread
->system_timer
);
4901 tdelt
= (uint32_t)(tsum
- thread
->vtimer_rlim_save
);
4902 thread
->vtimer_rlim_save
= tsum
;
4903 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4907 thread_unlock(thread
);
4914 * Change the assigned processor set for the task
4918 __unused task_t task
,
4919 __unused processor_set_t new_pset
,
4920 __unused boolean_t assign_threads
)
4922 return(KERN_FAILURE
);
4926 * task_assign_default:
4928 * Version of task_assign to assign to default processor set.
4931 task_assign_default(
4933 boolean_t assign_threads
)
4935 return (task_assign(task
, &pset0
, assign_threads
));
4939 * task_get_assignment
4941 * Return name of processor set that task is assigned to.
4944 task_get_assignment(
4946 processor_set_t
*pset
)
4948 if (!task
|| !task
->active
)
4949 return KERN_FAILURE
;
4953 return KERN_SUCCESS
;
4957 get_task_dispatchqueue_offset(
4960 return task
->dispatchqueue_offset
;
4966 * Set scheduling policy and parameters, both base and limit, for
4967 * the given task. Policy must be a policy which is enabled for the
4968 * processor set. Change contained threads if requested.
4972 __unused task_t task
,
4973 __unused policy_t policy_id
,
4974 __unused policy_base_t base
,
4975 __unused mach_msg_type_number_t count
,
4976 __unused boolean_t set_limit
,
4977 __unused boolean_t change
)
4979 return(KERN_FAILURE
);
4985 * Set scheduling policy and parameters, both base and limit, for
4986 * the given task. Policy can be any policy implemented by the
4987 * processor set, whether enabled or not. Change contained threads
4992 __unused task_t task
,
4993 __unused processor_set_t pset
,
4994 __unused policy_t policy_id
,
4995 __unused policy_base_t base
,
4996 __unused mach_msg_type_number_t base_count
,
4997 __unused policy_limit_t limit
,
4998 __unused mach_msg_type_number_t limit_count
,
4999 __unused boolean_t change
)
5001 return(KERN_FAILURE
);
5006 __unused task_t task
,
5007 __unused vm_offset_t pc
,
5008 __unused vm_offset_t endpc
)
5010 return KERN_FAILURE
;
5014 task_synchronizer_destroy_all(task_t task
)
5017 * Destroy owned semaphores
5019 semaphore_destroy_all(task
);
5023 * Install default (machine-dependent) initial thread state
5024 * on the task. Subsequent thread creation will have this initial
5025 * state set on the thread by machine_thread_inherit_taskwide().
5026 * Flavors and structures are exactly the same as those to thread_set_state()
5032 thread_state_t state
,
5033 mach_msg_type_number_t state_count
)
5037 if (task
== TASK_NULL
) {
5038 return (KERN_INVALID_ARGUMENT
);
5043 if (!task
->active
) {
5045 return (KERN_FAILURE
);
5048 ret
= machine_task_set_state(task
, flavor
, state
, state_count
);
5055 * Examine the default (machine-dependent) initial thread state
5056 * on the task, as set by task_set_state(). Flavors and structures
5057 * are exactly the same as those passed to thread_get_state().
5063 thread_state_t state
,
5064 mach_msg_type_number_t
*state_count
)
5068 if (task
== TASK_NULL
) {
5069 return (KERN_INVALID_ARGUMENT
);
5074 if (!task
->active
) {
5076 return (KERN_FAILURE
);
5079 ret
= machine_task_get_state(task
, flavor
, state
, state_count
);
5086 static kern_return_t
__attribute__((noinline
,not_tail_called
))
5087 PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(
5088 mach_exception_code_t code
,
5089 mach_exception_subcode_t subcode
,
5093 if (1 == proc_selfpid())
5094 return KERN_NOT_SUPPORTED
; // initproc is immune
5096 mach_exception_data_type_t codes
[EXCEPTION_CODE_MAX
] = {
5100 task_t task
= current_task();
5103 /* (See jetsam-related comments below) */
5105 proc_memstat_terminated(task
->bsd_info
, TRUE
);
5106 kr
= task_enqueue_exception_with_corpse(task
, EXC_GUARD
, codes
, 2, reason
);
5107 proc_memstat_terminated(task
->bsd_info
, FALSE
);
5112 task_violated_guard(
5113 mach_exception_code_t code
,
5114 mach_exception_subcode_t subcode
,
5117 return PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(code
, subcode
, reason
);
5121 #if CONFIG_MEMORYSTATUS
5124 task_get_memlimit_is_active(task_t task
)
5126 assert (task
!= NULL
);
5128 if (task
->memlimit_is_active
== 1) {
5136 task_set_memlimit_is_active(task_t task
, boolean_t memlimit_is_active
)
5138 assert (task
!= NULL
);
5140 if (memlimit_is_active
) {
5141 task
->memlimit_is_active
= 1;
5143 task
->memlimit_is_active
= 0;
5148 task_get_memlimit_is_fatal(task_t task
)
5150 assert(task
!= NULL
);
5152 if (task
->memlimit_is_fatal
== 1) {
5160 task_set_memlimit_is_fatal(task_t task
, boolean_t memlimit_is_fatal
)
5162 assert (task
!= NULL
);
5164 if (memlimit_is_fatal
) {
5165 task
->memlimit_is_fatal
= 1;
5167 task
->memlimit_is_fatal
= 0;
5172 task_has_triggered_exc_resource(task_t task
, boolean_t memlimit_is_active
)
5174 boolean_t triggered
= FALSE
;
5176 assert(task
== current_task());
5179 * Returns true, if task has already triggered an exc_resource exception.
5182 if (memlimit_is_active
) {
5183 triggered
= (task
->memlimit_active_exc_resource
? TRUE
: FALSE
);
5185 triggered
= (task
->memlimit_inactive_exc_resource
? TRUE
: FALSE
);
5192 task_mark_has_triggered_exc_resource(task_t task
, boolean_t memlimit_is_active
)
5194 assert(task
== current_task());
5197 * We allow one exc_resource per process per active/inactive limit.
5198 * The limit's fatal attribute does not come into play.
5201 if (memlimit_is_active
) {
5202 task
->memlimit_active_exc_resource
= 1;
5204 task
->memlimit_inactive_exc_resource
= 1;
5208 #define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
5210 void __attribute__((noinline
))
5211 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
)
5213 task_t task
= current_task();
5215 const char *procname
= "unknown";
5216 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5217 boolean_t send_sync_exc_resource
= FALSE
;
5220 pid
= proc_selfpid();
5224 * Cannot have ReportCrash analyzing
5225 * a suspended initproc.
5230 if (task
->bsd_info
!= NULL
) {
5231 procname
= proc_name_address(current_task()->bsd_info
);
5232 send_sync_exc_resource
= proc_send_synchronous_EXC_RESOURCE(current_task()->bsd_info
);
5236 if (hwm_user_cores
) {
5238 uint64_t starttime
, end
;
5239 clock_sec_t secs
= 0;
5240 uint32_t microsecs
= 0;
5242 starttime
= mach_absolute_time();
5244 * Trigger a coredump of this process. Don't proceed unless we know we won't
5245 * be filling up the disk; and ignore the core size resource limit for this
5248 if ((error
= coredump(current_task()->bsd_info
, HWM_USERCORE_MINSPACE
, COREDUMP_IGNORE_ULIMIT
)) != 0) {
5249 printf("couldn't take coredump of %s[%d]: %d\n", procname
, pid
, error
);
5252 * coredump() leaves the task suspended.
5254 task_resume_internal(current_task());
5256 end
= mach_absolute_time();
5257 absolutetime_to_microtime(end
- starttime
, &secs
, µsecs
);
5258 printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
5259 proc_name_address(current_task()->bsd_info
), pid
, (int)secs
, microsecs
);
5261 #endif /* CONFIG_COREDUMP */
5263 if (disable_exc_resource
) {
5264 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
5265 "supressed by a boot-arg.\n", procname
, pid
, max_footprint_mb
);
5270 * A task that has triggered an EXC_RESOURCE, should not be
5271 * jetsammed when the device is under memory pressure. Here
5272 * we set the P_MEMSTAT_TERMINATED flag so that the process
5273 * will be skipped if the memorystatus_thread wakes up.
5275 proc_memstat_terminated(current_task()->bsd_info
, TRUE
);
5277 code
[0] = code
[1] = 0;
5278 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_MEMORY
);
5279 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_HIGH_WATERMARK
);
5280 EXC_RESOURCE_HWM_ENCODE_LIMIT(code
[0], max_footprint_mb
);
5283 * Do not generate a corpse fork if the violation is a fatal one
5284 * or the process wants synchronous EXC_RESOURCE exceptions.
5286 if (is_fatal
|| send_sync_exc_resource
|| exc_via_corpse_forking
== 0) {
5287 /* Do not send a EXC_RESOURCE if corpse_for_fatal_memkill is set */
5288 if (send_sync_exc_resource
|| corpse_for_fatal_memkill
== 0) {
5290 * Use the _internal_ variant so that no user-space
5291 * process can resume our task from under us.
5293 task_suspend_internal(task
);
5294 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5295 task_resume_internal(task
);
5299 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
5300 "supressed due to audio playback.\n", procname
, pid
, max_footprint_mb
);
5302 task_enqueue_exception_with_corpse(task
, EXC_RESOURCE
,
5303 code
, EXCEPTION_CODE_MAX
, NULL
);
5308 * After the EXC_RESOURCE has been handled, we must clear the
5309 * P_MEMSTAT_TERMINATED flag so that the process can again be
5310 * considered for jetsam if the memorystatus_thread wakes up.
5312 proc_memstat_terminated(current_task()->bsd_info
, FALSE
); /* clear the flag */
5316 * Callback invoked when a task exceeds its physical footprint limit.
5319 task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
5321 ledger_amount_t max_footprint
, max_footprint_mb
;
5323 boolean_t is_warning
;
5324 boolean_t memlimit_is_active
;
5325 boolean_t memlimit_is_fatal
;
5327 if (warning
== LEDGER_WARNING_DIPPED_BELOW
) {
5329 * Task memory limits only provide a warning on the way up.
5332 } else if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
5334 * This task is in danger of violating a memory limit,
5335 * It has exceeded a percentage level of the limit.
5340 * The task has exceeded the physical footprint limit.
5341 * This is not a warning but a true limit violation.
5346 task
= current_task();
5348 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &max_footprint
);
5349 max_footprint_mb
= max_footprint
>> 20;
5351 memlimit_is_active
= task_get_memlimit_is_active(task
);
5352 memlimit_is_fatal
= task_get_memlimit_is_fatal(task
);
5355 * If this is an actual violation (not a warning), then generate EXC_RESOURCE exception.
5356 * We only generate the exception once per process per memlimit (active/inactive limit).
5357 * To enforce this, we monitor state based on the memlimit's active/inactive attribute
5358 * and we disable it by marking that memlimit as exception triggered.
5360 if ((is_warning
== FALSE
) && (!task_has_triggered_exc_resource(task
, memlimit_is_active
))) {
5361 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND((int)max_footprint_mb
, memlimit_is_fatal
);
5362 memorystatus_log_exception((int)max_footprint_mb
, memlimit_is_active
, memlimit_is_fatal
);
5363 task_mark_has_triggered_exc_resource(task
, memlimit_is_active
);
5366 memorystatus_on_ledger_footprint_exceeded(is_warning
, memlimit_is_active
, memlimit_is_fatal
);
5369 extern int proc_check_footprint_priv(void);
5372 task_set_phys_footprint_limit(
5377 kern_return_t error
;
5379 boolean_t memlimit_is_active
;
5380 boolean_t memlimit_is_fatal
;
5382 if ((error
= proc_check_footprint_priv())) {
5383 return (KERN_NO_ACCESS
);
5387 * This call should probably be obsoleted.
5388 * But for now, we default to current state.
5390 memlimit_is_active
= task_get_memlimit_is_active(task
);
5391 memlimit_is_fatal
= task_get_memlimit_is_fatal(task
);
5393 return task_set_phys_footprint_limit_internal(task
, new_limit_mb
, old_limit_mb
, memlimit_is_active
, memlimit_is_fatal
);
5397 task_convert_phys_footprint_limit(
5399 int *converted_limit_mb
)
5401 if (limit_mb
== -1) {
5405 if (max_task_footprint
!= 0) {
5406 *converted_limit_mb
= (int)(max_task_footprint
/ 1024 / 1024); /* bytes to MB */
5408 *converted_limit_mb
= (int)(LEDGER_LIMIT_INFINITY
>> 20);
5411 /* nothing to convert */
5412 *converted_limit_mb
= limit_mb
;
5414 return (KERN_SUCCESS
);
5419 task_set_phys_footprint_limit_internal(
5423 boolean_t memlimit_is_active
,
5424 boolean_t memlimit_is_fatal
)
5426 ledger_amount_t old
;
5428 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &old
);
5431 * Check that limit >> 20 will not give an "unexpected" 32-bit
5432 * result. There are, however, implicit assumptions that -1 mb limit
5433 * equates to LEDGER_LIMIT_INFINITY.
5435 assert(((old
& 0xFFF0000000000000LL
) == 0) || (old
== LEDGER_LIMIT_INFINITY
));
5438 *old_limit_mb
= (int)(old
>> 20);
5441 if (new_limit_mb
== -1) {
5443 * Caller wishes to remove the limit.
5445 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
5446 max_task_footprint
? max_task_footprint
: LEDGER_LIMIT_INFINITY
,
5447 max_task_footprint
? max_task_footprint_warning_level
: 0);
5450 task_set_memlimit_is_active(task
, memlimit_is_active
);
5451 task_set_memlimit_is_fatal(task
, memlimit_is_fatal
);
5454 return (KERN_SUCCESS
);
5457 #ifdef CONFIG_NOMONITORS
5458 return (KERN_SUCCESS
);
5459 #endif /* CONFIG_NOMONITORS */
5463 if ((memlimit_is_active
== task_get_memlimit_is_active(task
)) &&
5464 (memlimit_is_fatal
== task_get_memlimit_is_fatal(task
)) &&
5465 (((ledger_amount_t
)new_limit_mb
<< 20) == old
)) {
5467 * memlimit state is not changing
5470 return(KERN_SUCCESS
);
5473 task_set_memlimit_is_active(task
, memlimit_is_active
);
5474 task_set_memlimit_is_fatal(task
, memlimit_is_fatal
);
5476 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
5477 (ledger_amount_t
)new_limit_mb
<< 20, PHYS_FOOTPRINT_WARNING_LEVEL
);
5479 if (task
== current_task()) {
5480 ledger_check_new_balance(current_thread(), task
->ledger
,
5481 task_ledgers
.phys_footprint
);
5486 return (KERN_SUCCESS
);
5490 task_get_phys_footprint_limit(
5494 ledger_amount_t limit
;
5496 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &limit
);
5498 * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
5499 * result. There are, however, implicit assumptions that -1 mb limit
5500 * equates to LEDGER_LIMIT_INFINITY.
5502 assert(((limit
& 0xFFF0000000000000LL
) == 0) || (limit
== LEDGER_LIMIT_INFINITY
));
5503 *limit_mb
= (int)(limit
>> 20);
5505 return (KERN_SUCCESS
);
5507 #else /* CONFIG_MEMORYSTATUS */
5509 task_set_phys_footprint_limit(
5510 __unused task_t task
,
5511 __unused
int new_limit_mb
,
5512 __unused
int *old_limit_mb
)
5514 return (KERN_FAILURE
);
5518 task_get_phys_footprint_limit(
5519 __unused task_t task
,
5520 __unused
int *limit_mb
)
5522 return (KERN_FAILURE
);
5524 #endif /* CONFIG_MEMORYSTATUS */
5527 task_set_thread_limit(task_t task
, uint16_t thread_limit
)
5529 assert(task
!= kernel_task
);
5530 if (thread_limit
<= TASK_MAX_THREAD_LIMIT
) {
5532 task
->task_thread_limit
= thread_limit
;
5538 * We need to export some functions to other components that
5539 * are currently implemented in macros within the osfmk
5540 * component. Just export them as functions of the same name.
5542 boolean_t
is_kerneltask(task_t t
)
5544 if (t
== kernel_task
)
5550 boolean_t
is_corpsetask(task_t t
)
5552 return (task_is_a_corpse(t
));
5556 task_t
current_task(void);
5557 task_t
current_task(void)
5559 return (current_task_fast());
5562 #undef task_reference
5563 void task_reference(task_t task
);
5568 if (task
!= TASK_NULL
)
5569 task_reference_internal(task
);
5572 /* defined in bsd/kern/kern_prot.c */
5573 extern int get_audit_token_pid(audit_token_t
*audit_token
);
5575 int task_pid(task_t task
)
5578 return get_audit_token_pid(&task
->audit_token
);
5584 * This routine finds a thread in a task by its unique id
5585 * Returns a referenced thread or THREAD_NULL if the thread was not found
5587 * TODO: This is super inefficient - it's an O(threads in task) list walk!
5588 * We should make a tid hash, or transition all tid clients to thread ports
5590 * Precondition: No locks held (will take task lock)
5593 task_findtid(task_t task
, uint64_t tid
)
5595 thread_t self
= current_thread();
5596 thread_t found_thread
= THREAD_NULL
;
5597 thread_t iter_thread
= THREAD_NULL
;
5599 /* Short-circuit the lookup if we're looking up ourselves */
5600 if (tid
== self
->thread_id
|| tid
== TID_NULL
) {
5601 assert(self
->task
== task
);
5603 thread_reference(self
);
5610 queue_iterate(&task
->threads
, iter_thread
, thread_t
, task_threads
) {
5611 if (iter_thread
->thread_id
== tid
) {
5612 found_thread
= iter_thread
;
5613 thread_reference(found_thread
);
5620 return (found_thread
);
5623 int pid_from_task(task_t task
)
5627 if (task
->bsd_info
) {
5628 pid
= proc_pid(task
->bsd_info
);
5630 pid
= task_pid(task
);
5637 * Control the CPU usage monitor for a task.
5640 task_cpu_usage_monitor_ctl(task_t task
, uint32_t *flags
)
5642 int error
= KERN_SUCCESS
;
5644 if (*flags
& CPUMON_MAKE_FATAL
) {
5645 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_CPUMON
;
5647 error
= KERN_INVALID_ARGUMENT
;
5654 * Control the wakeups monitor for a task.
5657 task_wakeups_monitor_ctl(task_t task
, uint32_t *flags
, int32_t *rate_hz
)
5659 ledger_t ledger
= task
->ledger
;
5662 if (*flags
& WAKEMON_GET_PARAMS
) {
5663 ledger_amount_t limit
;
5666 ledger_get_limit(ledger
, task_ledgers
.interrupt_wakeups
, &limit
);
5667 ledger_get_period(ledger
, task_ledgers
.interrupt_wakeups
, &period
);
5669 if (limit
!= LEDGER_LIMIT_INFINITY
) {
5671 * An active limit means the wakeups monitor is enabled.
5673 *rate_hz
= (int32_t)(limit
/ (int64_t)(period
/ NSEC_PER_SEC
));
5674 *flags
= WAKEMON_ENABLE
;
5675 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
5676 *flags
|= WAKEMON_MAKE_FATAL
;
5679 *flags
= WAKEMON_DISABLE
;
5684 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
5687 return KERN_SUCCESS
;
5690 if (*flags
& WAKEMON_ENABLE
) {
5691 if (*flags
& WAKEMON_SET_DEFAULTS
) {
5692 *rate_hz
= task_wakeups_monitor_rate
;
5695 #ifndef CONFIG_NOMONITORS
5696 if (*flags
& WAKEMON_MAKE_FATAL
) {
5697 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
5699 #endif /* CONFIG_NOMONITORS */
5701 if (*rate_hz
<= 0) {
5703 return KERN_INVALID_ARGUMENT
;
5706 #ifndef CONFIG_NOMONITORS
5707 ledger_set_limit(ledger
, task_ledgers
.interrupt_wakeups
, *rate_hz
* task_wakeups_monitor_interval
,
5708 task_wakeups_monitor_ustackshots_trigger_pct
);
5709 ledger_set_period(ledger
, task_ledgers
.interrupt_wakeups
, task_wakeups_monitor_interval
* NSEC_PER_SEC
);
5710 ledger_enable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
5711 #endif /* CONFIG_NOMONITORS */
5712 } else if (*flags
& WAKEMON_DISABLE
) {
5714 * Caller wishes to disable wakeups monitor on the task.
5716 * Disable telemetry if it was triggered by the wakeups monitor, and
5717 * remove the limit & callback on the wakeups ledger entry.
5719 #if CONFIG_TELEMETRY
5720 telemetry_task_ctl_locked(task
, TF_WAKEMON_WARNING
, 0);
5722 ledger_disable_refill(ledger
, task_ledgers
.interrupt_wakeups
);
5723 ledger_disable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
5727 return KERN_SUCCESS
;
5731 task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
5733 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
5734 #if CONFIG_TELEMETRY
5736 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
5737 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
5739 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 1);
5744 #if CONFIG_TELEMETRY
5746 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
5747 * exceeded the limit, turn telemetry off for the task.
5749 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 0);
5753 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS();
5757 void __attribute__((noinline
))
5758 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
5760 task_t task
= current_task();
5762 const char *procname
= "unknown";
5765 #ifdef EXC_RESOURCE_MONITORS
5766 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5767 #endif /* EXC_RESOURCE_MONITORS */
5768 struct ledger_entry_info lei
;
5771 pid
= proc_selfpid();
5772 if (task
->bsd_info
!= NULL
)
5773 procname
= proc_name_address(current_task()->bsd_info
);
5776 ledger_get_entry_info(task
->ledger
, task_ledgers
.interrupt_wakeups
, &lei
);
5779 * Disable the exception notification so we don't overwhelm
5780 * the listener with an endless stream of redundant exceptions.
5781 * TODO: detect whether another thread is already reporting the violation.
5783 uint32_t flags
= WAKEMON_DISABLE
;
5784 task_wakeups_monitor_ctl(task
, &flags
, NULL
);
5786 fatal
= task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
5787 trace_resource_violation(RMON_CPUWAKES_VIOLATED
, &lei
);
5788 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught waking the CPU %llu times "
5789 "over ~%llu seconds, averaging %llu wakes / second and "
5790 "violating a %slimit of %llu wakes over %llu seconds.\n",
5792 lei
.lei_balance
, lei
.lei_last_refill
/ NSEC_PER_SEC
,
5793 lei
.lei_last_refill
== 0 ? 0 :
5794 (NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
),
5795 fatal
? "FATAL " : "",
5796 lei
.lei_limit
, lei
.lei_refill_period
/ NSEC_PER_SEC
);
5798 kr
= send_resource_violation(send_cpu_wakes_violation
, task
, &lei
,
5799 fatal
? kRNFatalLimitFlag
: 0);
5801 printf("send_resource_violation(CPU wakes, ...): error %#x\n", kr
);
5804 #ifdef EXC_RESOURCE_MONITORS
5805 if (disable_exc_resource
) {
5806 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5807 "supressed by a boot-arg\n", procname
, pid
);
5811 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5812 "supressed due to audio playback\n", procname
, pid
);
5815 if (lei
.lei_last_refill
== 0) {
5816 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5817 "supressed due to lei.lei_last_refill = 0 \n", procname
, pid
);
5820 code
[0] = code
[1] = 0;
5821 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_WAKEUPS
);
5822 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_WAKEUPS_MONITOR
);
5823 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code
[0],
5824 NSEC_PER_SEC
* lei
.lei_limit
/ lei
.lei_refill_period
);
5825 EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code
[0],
5826 lei
.lei_last_refill
);
5827 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code
[1],
5828 NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
);
5829 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5830 #endif /* EXC_RESOURCE_MONITORS */
5833 task_terminate_internal(task
);
5838 global_update_logical_writes(int64_t io_delta
)
5840 int64_t old_count
, new_count
;
5841 boolean_t needs_telemetry
;
5844 new_count
= old_count
= global_logical_writes_count
;
5845 new_count
+= io_delta
;
5846 if (new_count
>= io_telemetry_limit
) {
5848 needs_telemetry
= TRUE
;
5850 needs_telemetry
= FALSE
;
5852 } while(!OSCompareAndSwap64(old_count
, new_count
, &global_logical_writes_count
));
5853 return needs_telemetry
;
5856 void task_update_logical_writes(task_t task
, uint32_t io_size
, int flags
, void *vp
)
5858 int64_t io_delta
= 0;
5859 boolean_t needs_telemetry
= FALSE
;
5861 if ((!task
) || (!io_size
) || (!vp
))
5864 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_DATA_WRITE
)) | DBG_FUNC_NONE
,
5865 task_pid(task
), io_size
, flags
, (uintptr_t)VM_KERNEL_ADDRPERM(vp
), 0);
5866 DTRACE_IO4(logical_writes
, struct task
*, task
, uint32_t, io_size
, int, flags
, vnode
*, vp
);
5868 case TASK_WRITE_IMMEDIATE
:
5869 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_immediate_writes
));
5870 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5872 case TASK_WRITE_DEFERRED
:
5873 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_deferred_writes
));
5874 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5876 case TASK_WRITE_INVALIDATED
:
5877 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_invalidated_writes
));
5878 ledger_debit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5880 case TASK_WRITE_METADATA
:
5881 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_metadata_writes
));
5882 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5886 io_delta
= (flags
== TASK_WRITE_INVALIDATED
) ? ((int64_t)io_size
* -1ll) : ((int64_t)io_size
);
5887 if (io_telemetry_limit
!= 0) {
5888 /* If io_telemetry_limit is 0, disable global updates and I/O telemetry */
5889 needs_telemetry
= global_update_logical_writes(io_delta
);
5890 if (needs_telemetry
) {
5891 act_set_io_telemetry_ast(current_thread());
5897 * Control the I/O monitor for a task.
5900 task_io_monitor_ctl(task_t task
, uint32_t *flags
)
5902 ledger_t ledger
= task
->ledger
;
5905 if (*flags
& IOMON_ENABLE
) {
5906 /* Configure the physical I/O ledger */
5907 ledger_set_limit(ledger
, task_ledgers
.physical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5908 ledger_set_period(ledger
, task_ledgers
.physical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5910 /* Configure the logical I/O ledger */
5911 ledger_set_limit(ledger
, task_ledgers
.logical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5912 ledger_set_period(ledger
, task_ledgers
.logical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5914 } else if (*flags
& IOMON_DISABLE
) {
5916 * Caller wishes to disable I/O monitor on the task.
5918 ledger_disable_refill(ledger
, task_ledgers
.physical_writes
);
5919 ledger_disable_callback(ledger
, task_ledgers
.physical_writes
);
5920 ledger_disable_refill(ledger
, task_ledgers
.logical_writes
);
5921 ledger_disable_callback(ledger
, task_ledgers
.logical_writes
);
5925 return KERN_SUCCESS
;
5929 task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
)
5932 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO((int)param0
);
5936 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
)
5939 task_t task
= current_task();
5940 #ifdef EXC_RESOURCE_MONITORS
5941 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5942 #endif /* EXC_RESOURCE_MONITORS */
5943 struct ledger_entry_info lei
;
5947 pid
= proc_selfpid();
5950 * Get the ledger entry info. We need to do this before disabling the exception
5951 * to get correct values for all fields.
5954 case FLAVOR_IO_PHYSICAL_WRITES
:
5955 ledger_get_entry_info(task
->ledger
, task_ledgers
.physical_writes
, &lei
);
5957 case FLAVOR_IO_LOGICAL_WRITES
:
5958 ledger_get_entry_info(task
->ledger
, task_ledgers
.logical_writes
, &lei
);
5964 * Disable the exception notification so we don't overwhelm
5965 * the listener with an endless stream of redundant exceptions.
5966 * TODO: detect whether another thread is already reporting the violation.
5968 uint32_t flags
= IOMON_DISABLE
;
5969 task_io_monitor_ctl(task
, &flags
);
5971 if (flavor
== FLAVOR_IO_LOGICAL_WRITES
) {
5972 trace_resource_violation(RMON_LOGWRITES_VIOLATED
, &lei
);
5974 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",
5975 pid
, flavor
, (lei
.lei_balance
/ (1024 * 1024)), (lei
.lei_limit
/ (1024 * 1024)), (lei
.lei_refill_period
/ NSEC_PER_SEC
));
5977 kr
= send_resource_violation(send_disk_writes_violation
, task
, &lei
, kRNFlagsNone
);
5979 printf("send_resource_violation(disk_writes, ...): error %#x\n", kr
);
5982 #ifdef EXC_RESOURCE_MONITORS
5983 code
[0] = code
[1] = 0;
5984 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_IO
);
5985 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], flavor
);
5986 EXC_RESOURCE_IO_ENCODE_INTERVAL(code
[0], (lei
.lei_refill_period
/ NSEC_PER_SEC
));
5987 EXC_RESOURCE_IO_ENCODE_LIMIT(code
[0], (lei
.lei_limit
/ (1024 * 1024)));
5988 EXC_RESOURCE_IO_ENCODE_OBSERVED(code
[1], (lei
.lei_balance
/ (1024 * 1024)));
5989 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5990 #endif /* EXC_RESOURCE_MONITORS */
5993 /* Placeholders for the task set/get voucher interfaces */
5995 task_get_mach_voucher(
5997 mach_voucher_selector_t __unused which
,
5998 ipc_voucher_t
*voucher
)
6000 if (TASK_NULL
== task
)
6001 return KERN_INVALID_TASK
;
6004 return KERN_SUCCESS
;
6008 task_set_mach_voucher(
6010 ipc_voucher_t __unused voucher
)
6012 if (TASK_NULL
== task
)
6013 return KERN_INVALID_TASK
;
6015 return KERN_SUCCESS
;
6019 task_swap_mach_voucher(
6021 ipc_voucher_t new_voucher
,
6022 ipc_voucher_t
*in_out_old_voucher
)
6024 if (TASK_NULL
== task
)
6025 return KERN_INVALID_TASK
;
6027 *in_out_old_voucher
= new_voucher
;
6028 return KERN_SUCCESS
;
6031 void task_set_gpu_denied(task_t task
, boolean_t denied
)
6036 task
->t_flags
|= TF_GPU_DENIED
;
6038 task
->t_flags
&= ~TF_GPU_DENIED
;
6044 boolean_t
task_is_gpu_denied(task_t task
)
6046 /* We don't need the lock to read this flag */
6047 return (task
->t_flags
& TF_GPU_DENIED
) ? TRUE
: FALSE
;
6051 uint64_t get_task_memory_region_count(task_t task
)
6054 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
6055 return((uint64_t)get_map_nentries(map
));
6059 kdebug_trace_dyld_internal(uint32_t base_code
,
6060 struct dyld_kernel_image_info
*info
)
6062 static_assert(sizeof(info
->uuid
) >= 16);
6064 #if defined(__LP64__)
6065 uint64_t *uuid
= (uint64_t *)&(info
->uuid
);
6067 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6068 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
), uuid
[0],
6069 uuid
[1], info
->load_addr
,
6070 (uint64_t)info
->fsid
.val
[0] | ((uint64_t)info
->fsid
.val
[1] << 32),
6072 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6073 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 1),
6074 (uint64_t)info
->fsobjid
.fid_objno
|
6075 ((uint64_t)info
->fsobjid
.fid_generation
<< 32),
6077 #else /* defined(__LP64__) */
6078 uint32_t *uuid
= (uint32_t *)&(info
->uuid
);
6080 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6081 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 2), uuid
[0],
6082 uuid
[1], uuid
[2], uuid
[3], 0);
6083 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6084 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 3),
6085 (uint32_t)info
->load_addr
, info
->fsid
.val
[0], info
->fsid
.val
[1],
6086 info
->fsobjid
.fid_objno
, 0);
6087 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6088 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 4),
6089 info
->fsobjid
.fid_generation
, 0, 0, 0, 0);
6090 #endif /* !defined(__LP64__) */
6093 static kern_return_t
6094 kdebug_trace_dyld(task_t task
, uint32_t base_code
,
6095 vm_map_copy_t infos_copy
, mach_msg_type_number_t infos_len
)
6098 dyld_kernel_image_info_array_t infos
;
6099 vm_map_offset_t map_data
;
6103 return KERN_INVALID_ADDRESS
;
6106 if (!kdebug_enable
||
6107 !kdebug_debugid_enabled(KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, 0)))
6109 vm_map_copy_discard(infos_copy
);
6110 return KERN_SUCCESS
;
6113 if (task
== NULL
|| task
!= current_task()) {
6114 return KERN_INVALID_TASK
;
6117 kr
= vm_map_copyout(ipc_kernel_map
, &map_data
, (vm_map_copy_t
)infos_copy
);
6118 if (kr
!= KERN_SUCCESS
) {
6122 infos
= CAST_DOWN(dyld_kernel_image_info_array_t
, map_data
);
6124 for (mach_msg_type_number_t i
= 0; i
< infos_len
; i
++) {
6125 kdebug_trace_dyld_internal(base_code
, &(infos
[i
]));
6128 data
= CAST_DOWN(vm_offset_t
, map_data
);
6129 mach_vm_deallocate(ipc_kernel_map
, data
, infos_len
* sizeof(infos
[0]));
6130 return KERN_SUCCESS
;
6134 task_register_dyld_image_infos(task_t task
,
6135 dyld_kernel_image_info_array_t infos_copy
,
6136 mach_msg_type_number_t infos_len
)
6138 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_MAP_A
,
6139 (vm_map_copy_t
)infos_copy
, infos_len
);
6143 task_unregister_dyld_image_infos(task_t task
,
6144 dyld_kernel_image_info_array_t infos_copy
,
6145 mach_msg_type_number_t infos_len
)
6147 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_UNMAP_A
,
6148 (vm_map_copy_t
)infos_copy
, infos_len
);
6152 task_get_dyld_image_infos(__unused task_t task
,
6153 __unused dyld_kernel_image_info_array_t
* dyld_images
,
6154 __unused mach_msg_type_number_t
* dyld_imagesCnt
)
6156 return KERN_NOT_SUPPORTED
;
6160 task_register_dyld_shared_cache_image_info(task_t task
,
6161 dyld_kernel_image_info_t cache_img
,
6162 __unused boolean_t no_cache
,
6163 __unused boolean_t private_cache
)
6165 if (task
== NULL
|| task
!= current_task()) {
6166 return KERN_INVALID_TASK
;
6169 kdebug_trace_dyld_internal(DBG_DYLD_UUID_SHARED_CACHE_A
, &cache_img
);
6170 return KERN_SUCCESS
;
6174 task_register_dyld_set_dyld_state(__unused task_t task
,
6175 __unused
uint8_t dyld_state
)
6177 return KERN_NOT_SUPPORTED
;
6181 task_register_dyld_get_process_state(__unused task_t task
,
6182 __unused dyld_kernel_process_info_t
* dyld_process_state
)
6184 return KERN_NOT_SUPPORTED
;
6188 task_inspect(task_inspect_t task_insp
, task_inspect_flavor_t flavor
,
6189 task_inspect_info_t info_out
, mach_msg_type_number_t
*size_in_out
)
6192 task_t task
= (task_t
)task_insp
;
6193 kern_return_t kr
= KERN_SUCCESS
;
6194 mach_msg_type_number_t size
;
6196 if (task
== TASK_NULL
) {
6197 return KERN_INVALID_ARGUMENT
;
6200 size
= *size_in_out
;
6203 case TASK_INSPECT_BASIC_COUNTS
: {
6204 struct task_inspect_basic_counts
*bc
;
6205 uint64_t task_counts
[MT_CORE_NFIXED
];
6207 if (size
< TASK_INSPECT_BASIC_COUNTS_COUNT
) {
6208 kr
= KERN_INVALID_ARGUMENT
;
6212 mt_fixed_task_counts(task
, task_counts
);
6213 bc
= (struct task_inspect_basic_counts
*)info_out
;
6214 #ifdef MT_CORE_INSTRS
6215 bc
->instructions
= task_counts
[MT_CORE_INSTRS
];
6216 #else /* defined(MT_CORE_INSTRS) */
6217 bc
->instructions
= 0;
6218 #endif /* !defined(MT_CORE_INSTRS) */
6219 bc
->cycles
= task_counts
[MT_CORE_CYCLES
];
6220 size
= TASK_INSPECT_BASIC_COUNTS_COUNT
;
6224 kr
= KERN_INVALID_ARGUMENT
;
6228 if (kr
== KERN_SUCCESS
) {
6229 *size_in_out
= size
;
6232 #else /* MONOTONIC */
6233 #pragma unused(task_insp, flavor, info_out, size_in_out)
6234 return KERN_NOT_SUPPORTED
;
6235 #endif /* !MONOTONIC */
6238 #if CONFIG_SECLUDED_MEMORY
6239 int num_tasks_can_use_secluded_mem
= 0;
6242 task_set_can_use_secluded_mem(
6244 boolean_t can_use_secluded_mem
)
6246 if (!task
->task_could_use_secluded_mem
) {
6250 task_set_can_use_secluded_mem_locked(task
, can_use_secluded_mem
);
6255 task_set_can_use_secluded_mem_locked(
6257 boolean_t can_use_secluded_mem
)
6259 assert(task
->task_could_use_secluded_mem
);
6260 if (can_use_secluded_mem
&&
6261 secluded_for_apps
&& /* global boot-arg */
6262 !task
->task_can_use_secluded_mem
) {
6263 assert(num_tasks_can_use_secluded_mem
>= 0);
6265 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
6266 task
->task_can_use_secluded_mem
= TRUE
;
6267 } else if (!can_use_secluded_mem
&&
6268 task
->task_can_use_secluded_mem
) {
6269 assert(num_tasks_can_use_secluded_mem
> 0);
6271 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
6272 task
->task_can_use_secluded_mem
= FALSE
;
6277 task_set_could_use_secluded_mem(
6279 boolean_t could_use_secluded_mem
)
6281 task
->task_could_use_secluded_mem
= could_use_secluded_mem
;
6285 task_set_could_also_use_secluded_mem(
6287 boolean_t could_also_use_secluded_mem
)
6289 task
->task_could_also_use_secluded_mem
= could_also_use_secluded_mem
;
6293 task_can_use_secluded_mem(
6297 if (task
->task_can_use_secluded_mem
) {
6298 assert(task
->task_could_use_secluded_mem
);
6299 assert(num_tasks_can_use_secluded_mem
> 0);
6302 if (task
->task_could_also_use_secluded_mem
&&
6303 num_tasks_can_use_secluded_mem
> 0) {
6304 assert(num_tasks_can_use_secluded_mem
> 0);
6309 * If a single task is using more than some amount of
6310 * memory, allow it to dip into secluded and also begin
6311 * suppression of secluded memory until the tasks exits.
6313 if (is_alloc
&& secluded_shutoff_trigger
!= 0) {
6314 uint64_t phys_used
= get_task_phys_footprint(task
);
6315 if (phys_used
> secluded_shutoff_trigger
) {
6316 start_secluded_suppression(task
);
6325 task_could_use_secluded_mem(
6328 return task
->task_could_use_secluded_mem
;
6330 #endif /* CONFIG_SECLUDED_MEMORY */
6333 task_io_user_clients(task_t task
)
6335 return (&task
->io_user_clients
);
6339 task_copy_fields_for_exec(task_t dst_task
, task_t src_task
)
6341 dst_task
->vtimers
= src_task
->vtimers
;
6344 #if DEVELOPMENT || DEBUG
6345 int vm_region_footprint
= 0;
6346 #endif /* DEVELOPMENT || DEBUG */
6349 task_self_region_footprint(void)
6351 #if DEVELOPMENT || DEBUG
6352 if (vm_region_footprint
) {
6353 /* system-wide override */
6356 #endif /* DEVELOPMENT || DEBUG */
6357 return current_task()->task_region_footprint
;
6361 task_self_region_footprint_set(
6366 curtask
= current_task();
6369 curtask
->task_region_footprint
= TRUE
;
6371 curtask
->task_region_footprint
= FALSE
;
6373 task_unlock(curtask
);
6377 task_set_darkwake_mode(task_t task
, boolean_t set_mode
)
6384 task
->t_flags
|= TF_DARKWAKE_MODE
;
6386 task
->t_flags
&= ~(TF_DARKWAKE_MODE
);
6393 task_get_darkwake_mode(task_t task
)
6396 return ((task
->t_flags
& TF_DARKWAKE_MODE
) != 0);
6401 task_set_legacy_footprint(
6406 task
->task_legacy_footprint
= new_val
;
6409 #endif /* __arm64__ */