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,
235 .pages_grabbed_kern
= -1,
236 .pages_grabbed_iopl
= -1,
237 .pages_grabbed_upl
= -1};
239 /* System sleep state */
240 boolean_t tasks_suspend_state
;
243 void init_task_ledgers(void);
244 void task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
245 void task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
246 void task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
);
247 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void);
248 void __attribute__((noinline
)) PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
);
249 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
);
251 kern_return_t
task_suspend_internal(task_t
);
252 kern_return_t
task_resume_internal(task_t
);
253 static kern_return_t
task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
);
255 extern kern_return_t
iokit_task_terminate(task_t task
);
257 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
*);
258 extern void bsd_copythreadname(void *dst_uth
, void *src_uth
);
259 extern kern_return_t
thread_resume(thread_t thread
);
261 // Warn tasks when they hit 80% of their memory limit.
262 #define PHYS_FOOTPRINT_WARNING_LEVEL 80
264 #define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT 150 /* wakeups per second */
265 #define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL 300 /* in seconds. */
268 * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
270 * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
271 * stacktraces, aka micro-stackshots)
273 #define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER 70
275 int task_wakeups_monitor_interval
; /* In seconds. Time period over which wakeups rate is observed */
276 int task_wakeups_monitor_rate
; /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
278 int task_wakeups_monitor_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
280 int disable_exc_resource
; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
282 ledger_amount_t max_task_footprint
= 0; /* Per-task limit on physical memory consumption in bytes */
283 int max_task_footprint_warning_level
= 0; /* Per-task limit warning percentage */
284 int max_task_footprint_mb
= 0; /* Per-task limit on physical memory consumption in megabytes */
286 /* I/O Monitor Limits */
287 #define IOMON_DEFAULT_LIMIT (20480ull) /* MB of logical/physical I/O */
288 #define IOMON_DEFAULT_INTERVAL (86400ull) /* in seconds */
290 uint64_t task_iomon_limit_mb
; /* Per-task I/O monitor limit in MBs */
291 uint64_t task_iomon_interval_secs
; /* Per-task I/O monitor interval in secs */
293 #define IO_TELEMETRY_DEFAULT_LIMIT (10ll * 1024ll * 1024ll)
294 int64_t io_telemetry_limit
; /* Threshold to take a microstackshot (0 indicated I/O telemetry is turned off) */
295 int64_t global_logical_writes_count
= 0; /* Global count for logical writes */
296 static boolean_t
global_update_logical_writes(int64_t);
298 #define TASK_MAX_THREAD_LIMIT 256
301 int pmap_ledgers_panic
= 1;
302 int pmap_ledgers_panic_leeway
= 3;
303 #endif /* MACH_ASSERT */
305 int task_max
= CONFIG_TASK_MAX
; /* Max number of tasks */
308 int hwm_user_cores
= 0; /* high watermark violations generate user core files */
312 extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
313 extern int proc_pid(struct proc
*p
);
314 extern int proc_selfpid(void);
315 extern struct proc
*current_proc(void);
316 extern char *proc_name_address(struct proc
*p
);
317 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
318 extern int kevent_proc_copy_uptrs(void *proc
, uint64_t *buf
, int bufsize
);
319 extern void workq_proc_suspended(struct proc
*p
);
320 extern void workq_proc_resumed(struct proc
*p
);
322 #if CONFIG_MEMORYSTATUS
323 extern void proc_memstat_terminated(struct proc
* p
, boolean_t set
);
324 extern void memorystatus_on_ledger_footprint_exceeded(int warning
, boolean_t memlimit_is_active
, boolean_t memlimit_is_fatal
);
325 extern void memorystatus_log_exception(const int max_footprint_mb
, boolean_t memlimit_is_active
, boolean_t memlimit_is_fatal
);
326 extern boolean_t
memorystatus_allowed_vm_map_fork(task_t task
);
328 #if DEVELOPMENT || DEBUG
329 extern void memorystatus_abort_vm_map_fork(task_t
);
332 #endif /* CONFIG_MEMORYSTATUS */
334 #endif /* MACH_BSD */
336 #if DEVELOPMENT || DEBUG
337 int exc_resource_threads_enabled
;
338 #endif /* DEVELOPMENT || DEBUG */
340 #if (DEVELOPMENT || DEBUG) && TASK_EXC_GUARD_DELIVER_CORPSE
341 uint32_t task_exc_guard_default
= TASK_EXC_GUARD_MP_DELIVER
| TASK_EXC_GUARD_MP_CORPSE
|
342 TASK_EXC_GUARD_VM_DELIVER
| TASK_EXC_GUARD_VM_CORPSE
;
344 uint32_t task_exc_guard_default
= 0;
349 static void task_hold_locked(task_t task
);
350 static void task_wait_locked(task_t task
, boolean_t until_not_runnable
);
351 static void task_release_locked(task_t task
);
353 static void task_synchronizer_destroy_all(task_t task
);
360 boolean_t is_64bit_data
)
362 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
364 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
369 * Switching to/from 64-bit address spaces
372 if (!task_has_64Bit_addr(task
)) {
373 task_set_64Bit_addr(task
);
376 if (task_has_64Bit_addr(task
)) {
377 task_clear_64Bit_addr(task
);
382 * Switching to/from 64-bit register state.
385 if (task_has_64Bit_data(task
)) {
389 task_set_64Bit_data(task
);
391 if (!task_has_64Bit_data(task
)) {
395 task_clear_64Bit_data(task
);
398 /* FIXME: On x86, the thread save state flavor can diverge from the
399 * task's 64-bit feature flag due to the 32-bit/64-bit register save
400 * state dichotomy. Since we can be pre-empted in this interval,
401 * certain routines may observe the thread as being in an inconsistent
402 * state with respect to its task's 64-bitness.
405 #if defined(__x86_64__) || defined(__arm64__)
406 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
407 thread_mtx_lock(thread
);
408 machine_thread_switch_addrmode(thread
);
409 thread_mtx_unlock(thread
);
411 #if defined(__arm64__)
412 /* specifically, if running on H9 */
413 if (thread
== current_thread()) {
416 spl_t spl
= splsched();
418 * This call tell that the current thread changed it's 32bitness.
419 * Other thread were no more on core when 32bitness was changed,
420 * but current_thread() is on core and the previous call to
421 * machine_thread_going_on_core() gave 32bitness which is now wrong.
423 * This is needed for bring-up, a different callback should be used
426 * TODO: Remove this callout when we no longer support 32-bit code on H9
429 urgency
= thread_get_urgency(thread
, &arg1
, &arg2
);
430 machine_thread_going_on_core(thread
, urgency
, 0, 0, mach_approximate_time());
431 thread_unlock(thread
);
434 #endif /* defined(__arm64__) */
436 #endif /* defined(__x86_64__) || defined(__arm64__) */
443 task_get_64bit_data(task_t task
)
445 return task_has_64Bit_data(task
);
449 task_set_platform_binary(
451 boolean_t is_platform
)
455 task
->t_flags
|= TF_PLATFORM
;
457 task
->t_flags
&= ~(TF_PLATFORM
);
463 * Set or clear per-task TF_CA_CLIENT_WI flag according to specified argument.
464 * Returns "false" if flag is already set, and "true" in other cases.
467 task_set_ca_client_wi(
469 boolean_t set_or_clear
)
474 /* Tasks can have only one CA_CLIENT work interval */
475 if (task
->t_flags
& TF_CA_CLIENT_WI
) {
478 task
->t_flags
|= TF_CA_CLIENT_WI
;
481 task
->t_flags
&= ~TF_CA_CLIENT_WI
;
490 mach_vm_address_t addr
,
494 task
->all_image_info_addr
= addr
;
495 task
->all_image_info_size
= size
;
500 task_atm_reset(__unused task_t task
)
503 if (task
->atm_context
!= NULL
) {
504 atm_task_descriptor_destroy(task
->atm_context
);
505 task
->atm_context
= NULL
;
511 task_bank_reset(__unused task_t task
)
513 if (task
->bank_context
!= NULL
) {
514 bank_task_destroy(task
);
519 * NOTE: This should only be called when the P_LINTRANSIT
520 * flag is set (the proc_trans lock is held) on the
521 * proc associated with the task.
524 task_bank_init(__unused task_t task
)
526 if (task
->bank_context
!= NULL
) {
527 panic("Task bank init called with non null bank context for task: %p and bank_context: %p", task
, task
->bank_context
);
529 bank_task_initialize(task
);
533 task_set_did_exec_flag(task_t task
)
535 task
->t_procflags
|= TPF_DID_EXEC
;
539 task_clear_exec_copy_flag(task_t task
)
541 task
->t_procflags
&= ~TPF_EXEC_COPY
;
545 * This wait event is t_procflags instead of t_flags because t_flags is volatile
547 * TODO: store the flags in the same place as the event
548 * rdar://problem/28501994
551 task_get_return_wait_event(task_t task
)
553 return (event_t
)&task
->t_procflags
;
557 task_clear_return_wait(task_t task
)
561 task
->t_flags
&= ~TF_LRETURNWAIT
;
563 if (task
->t_flags
& TF_LRETURNWAITER
) {
564 thread_wakeup(task_get_return_wait_event(task
));
565 task
->t_flags
&= ~TF_LRETURNWAITER
;
571 void __attribute__((noreturn
))
572 task_wait_to_return(void)
576 task
= current_task();
579 if (task
->t_flags
& TF_LRETURNWAIT
) {
581 task
->t_flags
|= TF_LRETURNWAITER
;
582 assert_wait(task_get_return_wait_event(task
), THREAD_UNINT
);
585 thread_block(THREAD_CONTINUE_NULL
);
588 } while (task
->t_flags
& TF_LRETURNWAIT
);
595 * Before jumping to userspace and allowing this process to execute any code,
596 * notify any interested parties.
598 mac_proc_notify_exec_complete(current_proc());
601 thread_bootstrap_return();
604 #ifdef CONFIG_32BIT_TELEMETRY
606 task_consume_32bit_log_flag(task_t task
)
608 if ((task
->t_procflags
& TPF_LOG_32BIT_TELEMETRY
) != 0) {
609 task
->t_procflags
&= ~TPF_LOG_32BIT_TELEMETRY
;
617 task_set_32bit_log_flag(task_t task
)
619 task
->t_procflags
|= TPF_LOG_32BIT_TELEMETRY
;
621 #endif /* CONFIG_32BIT_TELEMETRY */
624 task_is_exec_copy(task_t task
)
626 return task_is_exec_copy_internal(task
);
630 task_did_exec(task_t task
)
632 return task_did_exec_internal(task
);
636 task_is_active(task_t task
)
642 task_is_halting(task_t task
)
644 return task
->halting
;
647 #if TASK_REFERENCE_LEAK_DEBUG
648 #include <kern/btlog.h>
650 static btlog_t
*task_ref_btlog
;
651 #define TASK_REF_OP_INCR 0x1
652 #define TASK_REF_OP_DECR 0x2
654 #define TASK_REF_NUM_RECORDS 100000
655 #define TASK_REF_BTDEPTH 7
658 task_reference_internal(task_t task
)
660 void * bt
[TASK_REF_BTDEPTH
];
663 os_ref_retain(&task
->ref_count
);
665 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
666 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_INCR
,
671 task_deallocate_internal(task_t task
)
673 void * bt
[TASK_REF_BTDEPTH
];
676 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
677 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_DECR
,
680 return os_ref_release(&task
->ref_count
);
683 #endif /* TASK_REFERENCE_LEAK_DEBUG */
688 lck_grp_attr_setdefault(&task_lck_grp_attr
);
689 lck_grp_init(&task_lck_grp
, "task", &task_lck_grp_attr
);
690 lck_attr_setdefault(&task_lck_attr
);
691 lck_mtx_init(&tasks_threads_lock
, &task_lck_grp
, &task_lck_attr
);
692 lck_mtx_init(&tasks_corpse_lock
, &task_lck_grp
, &task_lck_attr
);
696 task_max
* sizeof(struct task
),
697 TASK_CHUNK
* sizeof(struct task
),
700 zone_change(task_zone
, Z_NOENCRYPT
, TRUE
);
704 #endif /* CONFIG_EMBEDDED */
707 * Configure per-task memory limit.
708 * The boot-arg is interpreted as Megabytes,
709 * and takes precedence over the device tree.
710 * Setting the boot-arg to 0 disables task limits.
712 if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb
,
713 sizeof(max_task_footprint_mb
))) {
715 * No limit was found in boot-args, so go look in the device tree.
717 if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb
,
718 sizeof(max_task_footprint_mb
))) {
720 * No limit was found in device tree.
722 max_task_footprint_mb
= 0;
726 if (max_task_footprint_mb
!= 0) {
727 #if CONFIG_MEMORYSTATUS
728 if (max_task_footprint_mb
< 50) {
729 printf("Warning: max_task_pmem %d below minimum.\n",
730 max_task_footprint_mb
);
731 max_task_footprint_mb
= 50;
733 printf("Limiting task physical memory footprint to %d MB\n",
734 max_task_footprint_mb
);
736 max_task_footprint
= (ledger_amount_t
)max_task_footprint_mb
* 1024 * 1024; // Convert MB to bytes
739 * Configure the per-task memory limit warning level.
740 * This is computed as a percentage.
742 max_task_footprint_warning_level
= 0;
744 if (max_mem
< 0x40000000) {
746 * On devices with < 1GB of memory:
747 * -- set warnings to 50MB below the per-task limit.
749 if (max_task_footprint_mb
> 50) {
750 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 50) * 100) / max_task_footprint_mb
;
754 * On devices with >= 1GB of memory:
755 * -- set warnings to 100MB below the per-task limit.
757 if (max_task_footprint_mb
> 100) {
758 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 100) * 100) / max_task_footprint_mb
;
763 * Never allow warning level to land below the default.
765 if (max_task_footprint_warning_level
< PHYS_FOOTPRINT_WARNING_LEVEL
) {
766 max_task_footprint_warning_level
= PHYS_FOOTPRINT_WARNING_LEVEL
;
769 printf("Limiting task physical memory warning to %d%%\n", max_task_footprint_warning_level
);
772 printf("Warning: max_task_pmem specified, but jetsam not configured; ignoring.\n");
773 #endif /* CONFIG_MEMORYSTATUS */
776 #if DEVELOPMENT || DEBUG
777 if (!PE_parse_boot_argn("exc_resource_threads",
778 &exc_resource_threads_enabled
,
779 sizeof(exc_resource_threads_enabled
))) {
780 exc_resource_threads_enabled
= 1;
782 PE_parse_boot_argn("task_exc_guard_default",
783 &task_exc_guard_default
,
784 sizeof(task_exc_guard_default
));
785 #endif /* DEVELOPMENT || DEBUG */
788 if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores
,
789 sizeof(hwm_user_cores
))) {
794 proc_init_cpumon_params();
796 if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate
, sizeof(task_wakeups_monitor_rate
))) {
797 task_wakeups_monitor_rate
= TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT
;
800 if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval
, sizeof(task_wakeups_monitor_interval
))) {
801 task_wakeups_monitor_interval
= TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL
;
804 if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct
,
805 sizeof(task_wakeups_monitor_ustackshots_trigger_pct
))) {
806 task_wakeups_monitor_ustackshots_trigger_pct
= TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER
;
809 if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource
,
810 sizeof(disable_exc_resource
))) {
811 disable_exc_resource
= 0;
814 if (!PE_parse_boot_argn("task_iomon_limit_mb", &task_iomon_limit_mb
, sizeof(task_iomon_limit_mb
))) {
815 task_iomon_limit_mb
= IOMON_DEFAULT_LIMIT
;
818 if (!PE_parse_boot_argn("task_iomon_interval_secs", &task_iomon_interval_secs
, sizeof(task_iomon_interval_secs
))) {
819 task_iomon_interval_secs
= IOMON_DEFAULT_INTERVAL
;
822 if (!PE_parse_boot_argn("io_telemetry_limit", &io_telemetry_limit
, sizeof(io_telemetry_limit
))) {
823 io_telemetry_limit
= IO_TELEMETRY_DEFAULT_LIMIT
;
827 * If we have coalitions, coalition_init() will call init_task_ledgers() as it
828 * sets up the ledgers for the default coalition. If we don't have coalitions,
829 * then we have to call it now.
831 #if CONFIG_COALITIONS
832 assert(task_ledger_template
);
833 #else /* CONFIG_COALITIONS */
835 #endif /* CONFIG_COALITIONS */
837 #if TASK_REFERENCE_LEAK_DEBUG
838 task_ref_btlog
= btlog_create(TASK_REF_NUM_RECORDS
, TASK_REF_BTDEPTH
, TRUE
/* caller_will_remove_entries_for_element? */);
839 assert(task_ref_btlog
);
843 * Create the kernel task as the first task.
846 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, TRUE
, TRUE
, TF_NONE
, TPF_NONE
, &kernel_task
) != KERN_SUCCESS
)
848 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, FALSE
, FALSE
, TF_NONE
, TPF_NONE
, &kernel_task
) != KERN_SUCCESS
)
850 { panic("task_init\n");}
853 vm_map_deallocate(kernel_task
->map
);
854 kernel_task
->map
= kernel_map
;
855 lck_spin_init(&dead_task_statistics_lock
, &task_lck_grp
, &task_lck_attr
);
859 * Create a task running in the kernel address space. It may
860 * have its own map of size mem_size and may have ipc privileges.
864 __unused task_t parent_task
,
865 __unused vm_offset_t map_base
,
866 __unused vm_size_t map_size
,
867 __unused task_t
*child_task
)
869 return KERN_INVALID_ARGUMENT
;
875 __unused ledger_port_array_t ledger_ports
,
876 __unused mach_msg_type_number_t num_ledger_ports
,
877 __unused boolean_t inherit_memory
,
878 __unused task_t
*child_task
) /* OUT */
880 if (parent_task
== TASK_NULL
) {
881 return KERN_INVALID_ARGUMENT
;
885 * No longer supported: too many calls assume that a task has a valid
892 host_security_create_task_token(
893 host_security_t host_security
,
895 __unused security_token_t sec_token
,
896 __unused audit_token_t audit_token
,
897 __unused host_priv_t host_priv
,
898 __unused ledger_port_array_t ledger_ports
,
899 __unused mach_msg_type_number_t num_ledger_ports
,
900 __unused boolean_t inherit_memory
,
901 __unused task_t
*child_task
) /* OUT */
903 if (parent_task
== TASK_NULL
) {
904 return KERN_INVALID_ARGUMENT
;
907 if (host_security
== HOST_NULL
) {
908 return KERN_INVALID_SECURITY
;
912 * No longer supported.
922 * Physical footprint: This is the sum of:
923 * + (internal - alternate_accounting)
924 * + (internal_compressed - alternate_accounting_compressed)
926 * + purgeable_nonvolatile
927 * + purgeable_nonvolatile_compressed
931 * The task's anonymous memory, which on iOS is always resident.
933 * internal_compressed
934 * Amount of this task's internal memory which is held by the compressor.
935 * Such memory is no longer actually resident for the task [i.e., resident in its pmap],
936 * and could be either decompressed back into memory, or paged out to storage, depending
937 * on our implementation.
940 * IOKit mappings: The total size of all IOKit mappings in this task, regardless of
941 * clean/dirty or internal/external state].
943 * alternate_accounting
944 * The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
945 * are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
949 * pages_grabbed counts all page grabs in a task. It is also broken out into three subtypes
950 * which track UPL, IOPL and Kernel page grabs.
953 init_task_ledgers(void)
957 assert(task_ledger_template
== NULL
);
958 assert(kernel_task
== TASK_NULL
);
961 PE_parse_boot_argn("pmap_ledgers_panic",
963 sizeof(pmap_ledgers_panic
));
964 PE_parse_boot_argn("pmap_ledgers_panic_leeway",
965 &pmap_ledgers_panic_leeway
,
966 sizeof(pmap_ledgers_panic_leeway
));
967 #endif /* MACH_ASSERT */
969 if ((t
= ledger_template_create("Per-task ledger")) == NULL
) {
970 panic("couldn't create task ledger template");
973 task_ledgers
.cpu_time
= ledger_entry_add(t
, "cpu_time", "sched", "ns");
974 task_ledgers
.tkm_private
= ledger_entry_add(t
, "tkm_private",
976 task_ledgers
.tkm_shared
= ledger_entry_add(t
, "tkm_shared", "physmem",
978 task_ledgers
.phys_mem
= ledger_entry_add(t
, "phys_mem", "physmem",
980 task_ledgers
.wired_mem
= ledger_entry_add(t
, "wired_mem", "physmem",
982 task_ledgers
.internal
= ledger_entry_add(t
, "internal", "physmem",
984 task_ledgers
.iokit_mapped
= ledger_entry_add(t
, "iokit_mapped", "mappings",
986 task_ledgers
.alternate_accounting
= ledger_entry_add(t
, "alternate_accounting", "physmem",
988 task_ledgers
.alternate_accounting_compressed
= ledger_entry_add(t
, "alternate_accounting_compressed", "physmem",
990 task_ledgers
.page_table
= ledger_entry_add(t
, "page_table", "physmem",
992 task_ledgers
.phys_footprint
= ledger_entry_add(t
, "phys_footprint", "physmem",
994 task_ledgers
.internal_compressed
= ledger_entry_add(t
, "internal_compressed", "physmem",
996 task_ledgers
.purgeable_volatile
= ledger_entry_add(t
, "purgeable_volatile", "physmem", "bytes");
997 task_ledgers
.purgeable_nonvolatile
= ledger_entry_add(t
, "purgeable_nonvolatile", "physmem", "bytes");
998 task_ledgers
.purgeable_volatile_compressed
= ledger_entry_add(t
, "purgeable_volatile_compress", "physmem", "bytes");
999 task_ledgers
.purgeable_nonvolatile_compressed
= ledger_entry_add(t
, "purgeable_nonvolatile_compress", "physmem", "bytes");
1000 task_ledgers
.pages_grabbed
= ledger_entry_add(t
, "pages_grabbed", "physmem", "count");
1001 task_ledgers
.pages_grabbed_kern
= ledger_entry_add(t
, "pages_grabbed_kern", "physmem", "count");
1002 task_ledgers
.pages_grabbed_iopl
= ledger_entry_add(t
, "pages_grabbed_iopl", "physmem", "count");
1003 task_ledgers
.pages_grabbed_upl
= ledger_entry_add(t
, "pages_grabbed_upl", "physmem", "count");
1005 task_ledgers
.network_volatile
= ledger_entry_add(t
, "network_volatile", "physmem", "bytes");
1006 task_ledgers
.network_nonvolatile
= ledger_entry_add(t
, "network_nonvolatile", "physmem", "bytes");
1007 task_ledgers
.network_volatile_compressed
= ledger_entry_add(t
, "network_volatile_compressed", "physmem", "bytes");
1008 task_ledgers
.network_nonvolatile_compressed
= ledger_entry_add(t
, "network_nonvolatile_compressed", "physmem", "bytes");
1010 task_ledgers
.platform_idle_wakeups
= ledger_entry_add(t
, "platform_idle_wakeups", "power",
1012 task_ledgers
.interrupt_wakeups
= ledger_entry_add(t
, "interrupt_wakeups", "power",
1015 #if CONFIG_SCHED_SFI
1016 sfi_class_id_t class_id
, ledger_alias
;
1017 for (class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1018 task_ledgers
.sfi_wait_times
[class_id
] = -1;
1021 /* don't account for UNSPECIFIED */
1022 for (class_id
= SFI_CLASS_UNSPECIFIED
+ 1; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1023 ledger_alias
= sfi_get_ledger_alias_for_class(class_id
);
1024 if (ledger_alias
!= SFI_CLASS_UNSPECIFIED
) {
1025 /* Check to see if alias has been registered yet */
1026 if (task_ledgers
.sfi_wait_times
[ledger_alias
] != -1) {
1027 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
];
1029 /* Otherwise, initialize it first */
1030 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
] = sfi_ledger_entry_add(t
, ledger_alias
);
1033 task_ledgers
.sfi_wait_times
[class_id
] = sfi_ledger_entry_add(t
, class_id
);
1036 if (task_ledgers
.sfi_wait_times
[class_id
] < 0) {
1037 panic("couldn't create entries for task ledger template for SFI class 0x%x", class_id
);
1041 assert(task_ledgers
.sfi_wait_times
[MAX_SFI_CLASS_ID
- 1] != -1);
1042 #endif /* CONFIG_SCHED_SFI */
1044 task_ledgers
.cpu_time_billed_to_me
= ledger_entry_add(t
, "cpu_time_billed_to_me", "sched", "ns");
1045 task_ledgers
.cpu_time_billed_to_others
= ledger_entry_add(t
, "cpu_time_billed_to_others", "sched", "ns");
1046 task_ledgers
.physical_writes
= ledger_entry_add(t
, "physical_writes", "res", "bytes");
1047 task_ledgers
.logical_writes
= ledger_entry_add(t
, "logical_writes", "res", "bytes");
1048 task_ledgers
.energy_billed_to_me
= ledger_entry_add(t
, "energy_billed_to_me", "power", "nj");
1049 task_ledgers
.energy_billed_to_others
= ledger_entry_add(t
, "energy_billed_to_others", "power", "nj");
1051 if ((task_ledgers
.cpu_time
< 0) ||
1052 (task_ledgers
.tkm_private
< 0) ||
1053 (task_ledgers
.tkm_shared
< 0) ||
1054 (task_ledgers
.phys_mem
< 0) ||
1055 (task_ledgers
.wired_mem
< 0) ||
1056 (task_ledgers
.internal
< 0) ||
1057 (task_ledgers
.iokit_mapped
< 0) ||
1058 (task_ledgers
.alternate_accounting
< 0) ||
1059 (task_ledgers
.alternate_accounting_compressed
< 0) ||
1060 (task_ledgers
.page_table
< 0) ||
1061 (task_ledgers
.phys_footprint
< 0) ||
1062 (task_ledgers
.internal_compressed
< 0) ||
1063 (task_ledgers
.purgeable_volatile
< 0) ||
1064 (task_ledgers
.purgeable_nonvolatile
< 0) ||
1065 (task_ledgers
.purgeable_volatile_compressed
< 0) ||
1066 (task_ledgers
.purgeable_nonvolatile_compressed
< 0) ||
1067 (task_ledgers
.network_volatile
< 0) ||
1068 (task_ledgers
.network_nonvolatile
< 0) ||
1069 (task_ledgers
.network_volatile_compressed
< 0) ||
1070 (task_ledgers
.network_nonvolatile_compressed
< 0) ||
1071 (task_ledgers
.platform_idle_wakeups
< 0) ||
1072 (task_ledgers
.interrupt_wakeups
< 0) ||
1073 (task_ledgers
.cpu_time_billed_to_me
< 0) || (task_ledgers
.cpu_time_billed_to_others
< 0) ||
1074 (task_ledgers
.physical_writes
< 0) ||
1075 (task_ledgers
.logical_writes
< 0) ||
1076 (task_ledgers
.energy_billed_to_me
< 0) ||
1077 (task_ledgers
.energy_billed_to_others
< 0)
1079 panic("couldn't create entries for task ledger template");
1082 ledger_track_credit_only(t
, task_ledgers
.phys_footprint
);
1083 ledger_track_credit_only(t
, task_ledgers
.page_table
);
1084 ledger_track_credit_only(t
, task_ledgers
.internal
);
1085 ledger_track_credit_only(t
, task_ledgers
.internal_compressed
);
1086 ledger_track_credit_only(t
, task_ledgers
.iokit_mapped
);
1087 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting
);
1088 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting_compressed
);
1089 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile
);
1090 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile
);
1091 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile_compressed
);
1092 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
1093 ledger_track_credit_only(t
, task_ledgers
.pages_grabbed
);
1094 ledger_track_credit_only(t
, task_ledgers
.pages_grabbed_kern
);
1095 ledger_track_credit_only(t
, task_ledgers
.pages_grabbed_iopl
);
1096 ledger_track_credit_only(t
, task_ledgers
.pages_grabbed_upl
);
1098 ledger_track_credit_only(t
, task_ledgers
.network_volatile
);
1099 ledger_track_credit_only(t
, task_ledgers
.network_nonvolatile
);
1100 ledger_track_credit_only(t
, task_ledgers
.network_volatile_compressed
);
1101 ledger_track_credit_only(t
, task_ledgers
.network_nonvolatile_compressed
);
1103 ledger_track_maximum(t
, task_ledgers
.phys_footprint
, 60);
1105 if (pmap_ledgers_panic
) {
1106 ledger_panic_on_negative(t
, task_ledgers
.phys_footprint
);
1107 ledger_panic_on_negative(t
, task_ledgers
.page_table
);
1108 ledger_panic_on_negative(t
, task_ledgers
.internal
);
1109 ledger_panic_on_negative(t
, task_ledgers
.internal_compressed
);
1110 ledger_panic_on_negative(t
, task_ledgers
.iokit_mapped
);
1111 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting
);
1112 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting_compressed
);
1113 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile
);
1114 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile
);
1115 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile_compressed
);
1116 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
1118 ledger_panic_on_negative(t
, task_ledgers
.network_volatile
);
1119 ledger_panic_on_negative(t
, task_ledgers
.network_nonvolatile
);
1120 ledger_panic_on_negative(t
, task_ledgers
.network_volatile_compressed
);
1121 ledger_panic_on_negative(t
, task_ledgers
.network_nonvolatile_compressed
);
1123 #endif /* MACH_ASSERT */
1125 #if CONFIG_MEMORYSTATUS
1126 ledger_set_callback(t
, task_ledgers
.phys_footprint
, task_footprint_exceeded
, NULL
, NULL
);
1127 #endif /* CONFIG_MEMORYSTATUS */
1129 ledger_set_callback(t
, task_ledgers
.interrupt_wakeups
,
1130 task_wakeups_rate_exceeded
, NULL
, NULL
);
1131 ledger_set_callback(t
, task_ledgers
.physical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_PHYSICAL_WRITES
, NULL
);
1132 ledger_set_callback(t
, task_ledgers
.logical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_LOGICAL_WRITES
, NULL
);
1134 ledger_template_complete(t
);
1135 task_ledger_template
= t
;
1138 os_refgrp_decl(static, task_refgrp
, "task", NULL
);
1141 task_create_internal(
1143 coalition_t
*parent_coalitions __unused
,
1144 boolean_t inherit_memory
,
1145 __unused boolean_t is_64bit
,
1146 boolean_t is_64bit_data
,
1148 uint32_t t_procflags
,
1149 task_t
*child_task
) /* OUT */
1152 vm_shared_region_t shared_region
;
1153 ledger_t ledger
= NULL
;
1155 new_task
= (task_t
) zalloc(task_zone
);
1157 if (new_task
== TASK_NULL
) {
1158 return KERN_RESOURCE_SHORTAGE
;
1161 /* one ref for just being alive; one for our caller */
1162 os_ref_init_count(&new_task
->ref_count
, &task_refgrp
, 2);
1164 /* allocate with active entries */
1165 assert(task_ledger_template
!= NULL
);
1166 if ((ledger
= ledger_instantiate(task_ledger_template
,
1167 LEDGER_CREATE_ACTIVE_ENTRIES
)) == NULL
) {
1168 zfree(task_zone
, new_task
);
1169 return KERN_RESOURCE_SHORTAGE
;
1173 new_task
->ledger
= ledger
;
1175 #if defined(CONFIG_SCHED_MULTIQ)
1176 new_task
->sched_group
= sched_group_create();
1179 /* if inherit_memory is true, parent_task MUST not be NULL */
1180 if (!(t_flags
& TF_CORPSE_FORK
) && inherit_memory
) {
1181 new_task
->map
= vm_map_fork(ledger
, parent_task
->map
, 0);
1183 new_task
->map
= vm_map_create(pmap_create(ledger
, 0, is_64bit
),
1184 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
1185 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
1188 /* Inherit memlock limit from parent */
1190 vm_map_set_user_wire_limit(new_task
->map
, (vm_size_t
)parent_task
->map
->user_wire_limit
);
1193 lck_mtx_init(&new_task
->lock
, &task_lck_grp
, &task_lck_attr
);
1194 queue_init(&new_task
->threads
);
1195 new_task
->suspend_count
= 0;
1196 new_task
->thread_count
= 0;
1197 new_task
->active_thread_count
= 0;
1198 new_task
->user_stop_count
= 0;
1199 new_task
->legacy_stop_count
= 0;
1200 new_task
->active
= TRUE
;
1201 new_task
->halting
= FALSE
;
1202 new_task
->priv_flags
= 0;
1203 new_task
->t_flags
= t_flags
;
1204 new_task
->t_procflags
= t_procflags
;
1205 new_task
->importance
= 0;
1206 new_task
->crashed_thread_id
= 0;
1207 new_task
->exec_token
= 0;
1209 new_task
->task_exc_guard
= task_exc_guard_default
;
1212 new_task
->atm_context
= NULL
;
1214 new_task
->bank_context
= NULL
;
1217 new_task
->bsd_info
= NULL
;
1218 new_task
->corpse_info
= NULL
;
1219 #endif /* MACH_BSD */
1222 new_task
->crash_label
= NULL
;
1225 #if CONFIG_MEMORYSTATUS
1226 if (max_task_footprint
!= 0) {
1227 ledger_set_limit(ledger
, task_ledgers
.phys_footprint
, max_task_footprint
, PHYS_FOOTPRINT_WARNING_LEVEL
);
1229 #endif /* CONFIG_MEMORYSTATUS */
1231 if (task_wakeups_monitor_rate
!= 0) {
1232 uint32_t flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
1233 int32_t rate
; // Ignored because of WAKEMON_SET_DEFAULTS
1234 task_wakeups_monitor_ctl(new_task
, &flags
, &rate
);
1237 #if CONFIG_IO_ACCOUNTING
1238 uint32_t flags
= IOMON_ENABLE
;
1239 task_io_monitor_ctl(new_task
, &flags
);
1240 #endif /* CONFIG_IO_ACCOUNTING */
1242 machine_task_init(new_task
, parent_task
, inherit_memory
);
1244 new_task
->task_debug
= NULL
;
1246 #if DEVELOPMENT || DEBUG
1247 new_task
->task_unnested
= FALSE
;
1248 new_task
->task_disconnected_count
= 0;
1250 queue_init(&new_task
->semaphore_list
);
1251 new_task
->semaphores_owned
= 0;
1253 ipc_task_init(new_task
, parent_task
);
1255 new_task
->vtimers
= 0;
1257 new_task
->shared_region
= NULL
;
1259 new_task
->affinity_space
= NULL
;
1261 new_task
->t_kpc
= 0;
1263 new_task
->pidsuspended
= FALSE
;
1264 new_task
->frozen
= FALSE
;
1265 new_task
->changing_freeze_state
= FALSE
;
1266 new_task
->rusage_cpu_flags
= 0;
1267 new_task
->rusage_cpu_percentage
= 0;
1268 new_task
->rusage_cpu_interval
= 0;
1269 new_task
->rusage_cpu_deadline
= 0;
1270 new_task
->rusage_cpu_callt
= NULL
;
1272 new_task
->suspends_outstanding
= 0;
1276 new_task
->hv_task_target
= NULL
;
1277 #endif /* HYPERVISOR */
1280 queue_init(&new_task
->task_watchers
);
1281 new_task
->num_taskwatchers
= 0;
1282 new_task
->watchapplying
= 0;
1283 #endif /* CONFIG_EMBEDDED */
1285 new_task
->mem_notify_reserved
= 0;
1286 new_task
->memlimit_attrs_reserved
= 0;
1288 new_task
->requested_policy
= default_task_requested_policy
;
1289 new_task
->effective_policy
= default_task_effective_policy
;
1291 task_importance_init_from_parent(new_task
, parent_task
);
1293 if (parent_task
!= TASK_NULL
) {
1294 new_task
->sec_token
= parent_task
->sec_token
;
1295 new_task
->audit_token
= parent_task
->audit_token
;
1297 /* inherit the parent's shared region */
1298 shared_region
= vm_shared_region_get(parent_task
);
1299 vm_shared_region_set(new_task
, shared_region
);
1301 if (task_has_64Bit_addr(parent_task
)) {
1302 task_set_64Bit_addr(new_task
);
1305 if (task_has_64Bit_data(parent_task
)) {
1306 task_set_64Bit_data(new_task
);
1309 new_task
->all_image_info_addr
= parent_task
->all_image_info_addr
;
1310 new_task
->all_image_info_size
= parent_task
->all_image_info_size
;
1312 if (inherit_memory
&& parent_task
->affinity_space
) {
1313 task_affinity_create(parent_task
, new_task
);
1316 new_task
->pset_hint
= parent_task
->pset_hint
= task_choose_pset(parent_task
);
1318 #if DEBUG || DEVELOPMENT
1319 if (parent_task
->t_flags
& TF_NO_SMT
) {
1320 new_task
->t_flags
|= TF_NO_SMT
;
1324 new_task
->priority
= BASEPRI_DEFAULT
;
1325 new_task
->max_priority
= MAXPRI_USER
;
1327 task_policy_create(new_task
, parent_task
);
1329 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
1330 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
1333 task_set_64Bit_addr(new_task
);
1337 if (is_64bit_data
) {
1338 task_set_64Bit_data(new_task
);
1341 new_task
->all_image_info_addr
= (mach_vm_address_t
)0;
1342 new_task
->all_image_info_size
= (mach_vm_size_t
)0;
1344 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
1346 if (kernel_task
== TASK_NULL
) {
1347 new_task
->priority
= BASEPRI_KERNEL
;
1348 new_task
->max_priority
= MAXPRI_KERNEL
;
1350 new_task
->priority
= BASEPRI_DEFAULT
;
1351 new_task
->max_priority
= MAXPRI_USER
;
1355 bzero(new_task
->coalition
, sizeof(new_task
->coalition
));
1356 for (int i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1357 queue_chain_init(new_task
->task_coalition
[i
]);
1360 /* Allocate I/O Statistics */
1361 new_task
->task_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
1362 assert(new_task
->task_io_stats
!= NULL
);
1363 bzero(new_task
->task_io_stats
, sizeof(struct io_stat_info
));
1365 bzero(&(new_task
->cpu_time_eqos_stats
), sizeof(new_task
->cpu_time_eqos_stats
));
1366 bzero(&(new_task
->cpu_time_rqos_stats
), sizeof(new_task
->cpu_time_rqos_stats
));
1368 bzero(&new_task
->extmod_statistics
, sizeof(new_task
->extmod_statistics
));
1370 /* Copy resource acc. info from Parent for Corpe Forked task. */
1371 if (parent_task
!= NULL
&& (t_flags
& TF_CORPSE_FORK
)) {
1372 task_rollup_accounting_info(new_task
, parent_task
);
1374 /* Initialize to zero for standard fork/spawn case */
1375 new_task
->total_user_time
= 0;
1376 new_task
->total_system_time
= 0;
1377 new_task
->total_ptime
= 0;
1378 new_task
->total_runnable_time
= 0;
1379 new_task
->faults
= 0;
1380 new_task
->pageins
= 0;
1381 new_task
->cow_faults
= 0;
1382 new_task
->messages_sent
= 0;
1383 new_task
->messages_received
= 0;
1384 new_task
->syscalls_mach
= 0;
1385 new_task
->syscalls_unix
= 0;
1386 new_task
->c_switch
= 0;
1387 new_task
->p_switch
= 0;
1388 new_task
->ps_switch
= 0;
1389 new_task
->low_mem_notified_warn
= 0;
1390 new_task
->low_mem_notified_critical
= 0;
1391 new_task
->purged_memory_warn
= 0;
1392 new_task
->purged_memory_critical
= 0;
1393 new_task
->low_mem_privileged_listener
= 0;
1394 new_task
->memlimit_is_active
= 0;
1395 new_task
->memlimit_is_fatal
= 0;
1396 new_task
->memlimit_active_exc_resource
= 0;
1397 new_task
->memlimit_inactive_exc_resource
= 0;
1398 new_task
->task_timer_wakeups_bin_1
= 0;
1399 new_task
->task_timer_wakeups_bin_2
= 0;
1400 new_task
->task_gpu_ns
= 0;
1401 new_task
->task_immediate_writes
= 0;
1402 new_task
->task_deferred_writes
= 0;
1403 new_task
->task_invalidated_writes
= 0;
1404 new_task
->task_metadata_writes
= 0;
1405 new_task
->task_energy
= 0;
1407 memset(&new_task
->task_monotonic
, 0, sizeof(new_task
->task_monotonic
));
1408 #endif /* MONOTONIC */
1412 #if CONFIG_COALITIONS
1413 if (!(t_flags
& TF_CORPSE_FORK
)) {
1414 /* TODO: there is no graceful failure path here... */
1415 if (parent_coalitions
&& parent_coalitions
[COALITION_TYPE_RESOURCE
]) {
1416 coalitions_adopt_task(parent_coalitions
, new_task
);
1417 } else if (parent_task
&& parent_task
->coalition
[COALITION_TYPE_RESOURCE
]) {
1419 * all tasks at least have a resource coalition, so
1420 * if the parent has one then inherit all coalitions
1421 * the parent is a part of
1423 coalitions_adopt_task(parent_task
->coalition
, new_task
);
1425 /* TODO: assert that new_task will be PID 1 (launchd) */
1426 coalitions_adopt_init_task(new_task
);
1429 * on exec, we need to transfer the coalition roles from the
1430 * parent task to the exec copy task.
1432 if (parent_task
&& (t_procflags
& TPF_EXEC_COPY
)) {
1433 int coal_roles
[COALITION_NUM_TYPES
];
1434 task_coalition_roles(parent_task
, coal_roles
);
1435 (void)coalitions_set_roles(new_task
->coalition
, new_task
, coal_roles
);
1438 coalitions_adopt_corpse_task(new_task
);
1441 if (new_task
->coalition
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
1442 panic("created task is not a member of a resource coalition");
1444 #endif /* CONFIG_COALITIONS */
1446 new_task
->dispatchqueue_offset
= 0;
1447 if (parent_task
!= NULL
) {
1448 new_task
->dispatchqueue_offset
= parent_task
->dispatchqueue_offset
;
1451 new_task
->task_volatile_objects
= 0;
1452 new_task
->task_nonvolatile_objects
= 0;
1453 new_task
->task_purgeable_disowning
= FALSE
;
1454 new_task
->task_purgeable_disowned
= FALSE
;
1455 queue_init(&new_task
->task_objq
);
1456 task_objq_lock_init(new_task
);
1459 new_task
->task_legacy_footprint
= FALSE
;
1460 #endif /* __arm64__ */
1461 new_task
->task_region_footprint
= FALSE
;
1462 new_task
->task_has_crossed_thread_limit
= FALSE
;
1463 new_task
->task_thread_limit
= 0;
1464 #if CONFIG_SECLUDED_MEMORY
1465 new_task
->task_can_use_secluded_mem
= FALSE
;
1466 new_task
->task_could_use_secluded_mem
= FALSE
;
1467 new_task
->task_could_also_use_secluded_mem
= FALSE
;
1468 new_task
->task_suppressed_secluded
= FALSE
;
1469 #endif /* CONFIG_SECLUDED_MEMORY */
1472 * t_flags is set up above. But since we don't
1473 * support darkwake mode being set that way
1474 * currently, we clear it out here explicitly.
1476 new_task
->t_flags
&= ~(TF_DARKWAKE_MODE
);
1478 queue_init(&new_task
->io_user_clients
);
1480 ipc_task_enable(new_task
);
1482 lck_mtx_lock(&tasks_threads_lock
);
1483 queue_enter(&tasks
, new_task
, task_t
, tasks
);
1485 if (tasks_suspend_state
) {
1486 task_suspend_internal(new_task
);
1488 lck_mtx_unlock(&tasks_threads_lock
);
1490 *child_task
= new_task
;
1491 return KERN_SUCCESS
;
1495 * task_rollup_accounting_info
1497 * Roll up accounting stats. Used to rollup stats
1498 * for exec copy task and corpse fork.
1501 task_rollup_accounting_info(task_t to_task
, task_t from_task
)
1503 assert(from_task
!= to_task
);
1505 to_task
->total_user_time
= from_task
->total_user_time
;
1506 to_task
->total_system_time
= from_task
->total_system_time
;
1507 to_task
->total_ptime
= from_task
->total_ptime
;
1508 to_task
->total_runnable_time
= from_task
->total_runnable_time
;
1509 to_task
->faults
= from_task
->faults
;
1510 to_task
->pageins
= from_task
->pageins
;
1511 to_task
->cow_faults
= from_task
->cow_faults
;
1512 to_task
->messages_sent
= from_task
->messages_sent
;
1513 to_task
->messages_received
= from_task
->messages_received
;
1514 to_task
->syscalls_mach
= from_task
->syscalls_mach
;
1515 to_task
->syscalls_unix
= from_task
->syscalls_unix
;
1516 to_task
->c_switch
= from_task
->c_switch
;
1517 to_task
->p_switch
= from_task
->p_switch
;
1518 to_task
->ps_switch
= from_task
->ps_switch
;
1519 to_task
->extmod_statistics
= from_task
->extmod_statistics
;
1520 to_task
->low_mem_notified_warn
= from_task
->low_mem_notified_warn
;
1521 to_task
->low_mem_notified_critical
= from_task
->low_mem_notified_critical
;
1522 to_task
->purged_memory_warn
= from_task
->purged_memory_warn
;
1523 to_task
->purged_memory_critical
= from_task
->purged_memory_critical
;
1524 to_task
->low_mem_privileged_listener
= from_task
->low_mem_privileged_listener
;
1525 *to_task
->task_io_stats
= *from_task
->task_io_stats
;
1526 to_task
->cpu_time_eqos_stats
= from_task
->cpu_time_eqos_stats
;
1527 to_task
->cpu_time_rqos_stats
= from_task
->cpu_time_rqos_stats
;
1528 to_task
->task_timer_wakeups_bin_1
= from_task
->task_timer_wakeups_bin_1
;
1529 to_task
->task_timer_wakeups_bin_2
= from_task
->task_timer_wakeups_bin_2
;
1530 to_task
->task_gpu_ns
= from_task
->task_gpu_ns
;
1531 to_task
->task_immediate_writes
= from_task
->task_immediate_writes
;
1532 to_task
->task_deferred_writes
= from_task
->task_deferred_writes
;
1533 to_task
->task_invalidated_writes
= from_task
->task_invalidated_writes
;
1534 to_task
->task_metadata_writes
= from_task
->task_metadata_writes
;
1535 to_task
->task_energy
= from_task
->task_energy
;
1537 /* Skip ledger roll up for memory accounting entries */
1538 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time
);
1539 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.platform_idle_wakeups
);
1540 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.interrupt_wakeups
);
1541 #if CONFIG_SCHED_SFI
1542 for (sfi_class_id_t class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1543 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.sfi_wait_times
[class_id
]);
1546 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_me
);
1547 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_others
);
1548 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.physical_writes
);
1549 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.logical_writes
);
1550 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.energy_billed_to_me
);
1551 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.energy_billed_to_others
);
1554 int task_dropped_imp_count
= 0;
1559 * Drop a reference on a task.
1565 ledger_amount_t credit
, debit
, interrupt_wakeups
, platform_idle_wakeups
;
1566 os_ref_count_t refs
;
1568 if (task
== TASK_NULL
) {
1572 refs
= task_deallocate_internal(task
);
1574 #if IMPORTANCE_INHERITANCE
1577 * If last ref potentially comes from the task's importance,
1578 * disconnect it. But more task refs may be added before
1579 * that completes, so wait for the reference to go to zero
1580 * naturally (it may happen on a recursive task_deallocate()
1581 * from the ipc_importance_disconnect_task() call).
1583 if (IIT_NULL
!= task
->task_imp_base
) {
1584 ipc_importance_disconnect_task(task
);
1588 #endif /* IMPORTANCE_INHERITANCE */
1594 lck_mtx_lock(&tasks_threads_lock
);
1595 queue_remove(&terminated_tasks
, task
, task_t
, tasks
);
1596 terminated_tasks_count
--;
1597 lck_mtx_unlock(&tasks_threads_lock
);
1600 * remove the reference on atm descriptor
1602 task_atm_reset(task
);
1605 * remove the reference on bank context
1607 task_bank_reset(task
);
1609 if (task
->task_io_stats
) {
1610 kfree(task
->task_io_stats
, sizeof(struct io_stat_info
));
1614 * Give the machine dependent code a chance
1615 * to perform cleanup before ripping apart
1618 machine_task_terminate(task
);
1620 ipc_task_terminate(task
);
1622 /* let iokit know */
1623 iokit_task_terminate(task
);
1625 if (task
->affinity_space
) {
1626 task_affinity_deallocate(task
);
1630 if (task
->ledger
!= NULL
&&
1631 task
->map
!= NULL
&&
1632 task
->map
->pmap
!= NULL
&&
1633 task
->map
->pmap
->ledger
!= NULL
) {
1634 assert(task
->ledger
== task
->map
->pmap
->ledger
);
1636 #endif /* MACH_ASSERT */
1638 vm_purgeable_disown(task
);
1639 assert(task
->task_purgeable_disowned
);
1640 if (task
->task_volatile_objects
!= 0 ||
1641 task
->task_nonvolatile_objects
!= 0) {
1642 panic("task_deallocate(%p): "
1643 "volatile_objects=%d nonvolatile_objects=%d\n",
1645 task
->task_volatile_objects
,
1646 task
->task_nonvolatile_objects
);
1649 vm_map_deallocate(task
->map
);
1650 is_release(task
->itk_space
);
1652 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
1653 &interrupt_wakeups
, &debit
);
1654 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
1655 &platform_idle_wakeups
, &debit
);
1657 #if defined(CONFIG_SCHED_MULTIQ)
1658 sched_group_destroy(task
->sched_group
);
1661 /* Accumulate statistics for dead tasks */
1662 lck_spin_lock(&dead_task_statistics_lock
);
1663 dead_task_statistics
.total_user_time
+= task
->total_user_time
;
1664 dead_task_statistics
.total_system_time
+= task
->total_system_time
;
1666 dead_task_statistics
.task_interrupt_wakeups
+= interrupt_wakeups
;
1667 dead_task_statistics
.task_platform_idle_wakeups
+= platform_idle_wakeups
;
1669 dead_task_statistics
.task_timer_wakeups_bin_1
+= task
->task_timer_wakeups_bin_1
;
1670 dead_task_statistics
.task_timer_wakeups_bin_2
+= task
->task_timer_wakeups_bin_2
;
1671 dead_task_statistics
.total_ptime
+= task
->total_ptime
;
1672 dead_task_statistics
.total_pset_switches
+= task
->ps_switch
;
1673 dead_task_statistics
.task_gpu_ns
+= task
->task_gpu_ns
;
1674 dead_task_statistics
.task_energy
+= task
->task_energy
;
1676 lck_spin_unlock(&dead_task_statistics_lock
);
1677 lck_mtx_destroy(&task
->lock
, &task_lck_grp
);
1679 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_private
, &credit
,
1681 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_private
.alloc
);
1682 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_private
.free
);
1684 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_shared
, &credit
,
1686 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_shared
.alloc
);
1687 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_shared
.free
);
1689 ledger_dereference(task
->ledger
);
1691 #if TASK_REFERENCE_LEAK_DEBUG
1692 btlog_remove_entries_for_element(task_ref_btlog
, task
);
1695 #if CONFIG_COALITIONS
1696 task_release_coalitions(task
);
1697 #endif /* CONFIG_COALITIONS */
1699 bzero(task
->coalition
, sizeof(task
->coalition
));
1702 /* clean up collected information since last reference to task is gone */
1703 if (task
->corpse_info
) {
1704 void *corpse_info_kernel
= kcdata_memory_get_begin_addr(task
->corpse_info
);
1705 task_crashinfo_destroy(task
->corpse_info
);
1706 task
->corpse_info
= NULL
;
1707 if (corpse_info_kernel
) {
1708 kfree(corpse_info_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1714 if (task
->crash_label
) {
1715 mac_exc_free_label(task
->crash_label
);
1716 task
->crash_label
= NULL
;
1720 assert(queue_empty(&task
->task_objq
));
1722 zfree(task_zone
, task
);
1726 * task_name_deallocate:
1728 * Drop a reference on a task name.
1731 task_name_deallocate(
1732 task_name_t task_name
)
1734 return task_deallocate((task_t
)task_name
);
1738 * task_inspect_deallocate:
1740 * Drop a task inspection reference.
1743 task_inspect_deallocate(
1744 task_inspect_t task_inspect
)
1746 return task_deallocate((task_t
)task_inspect
);
1750 * task_suspension_token_deallocate:
1752 * Drop a reference on a task suspension token.
1755 task_suspension_token_deallocate(
1756 task_suspension_token_t token
)
1758 return task_deallocate((task_t
)token
);
1763 * task_collect_crash_info:
1765 * collect crash info from bsd and mach based data
1768 task_collect_crash_info(
1771 struct label
*crash_label
,
1775 kern_return_t kr
= KERN_SUCCESS
;
1777 kcdata_descriptor_t crash_data
= NULL
;
1778 kcdata_descriptor_t crash_data_release
= NULL
;
1779 mach_msg_type_number_t size
= CORPSEINFO_ALLOCATION_SIZE
;
1780 mach_vm_offset_t crash_data_ptr
= 0;
1781 void *crash_data_kernel
= NULL
;
1782 void *crash_data_kernel_release
= NULL
;
1784 struct label
*label
, *free_label
;
1787 if (!corpses_enabled()) {
1788 return KERN_NOT_SUPPORTED
;
1792 free_label
= label
= mac_exc_create_label();
1797 assert(is_corpse_fork
|| task
->bsd_info
!= NULL
);
1798 if (task
->corpse_info
== NULL
&& (is_corpse_fork
|| task
->bsd_info
!= NULL
)) {
1800 /* Set the crash label, used by the exception delivery mac hook */
1801 free_label
= task
->crash_label
; // Most likely NULL.
1802 task
->crash_label
= label
;
1803 mac_exc_update_task_crash_label(task
, crash_label
);
1807 crash_data_kernel
= (void *) kalloc(CORPSEINFO_ALLOCATION_SIZE
);
1808 if (crash_data_kernel
== NULL
) {
1809 kr
= KERN_RESOURCE_SHORTAGE
;
1812 bzero(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1813 crash_data_ptr
= (mach_vm_offset_t
) crash_data_kernel
;
1815 /* Do not get a corpse ref for corpse fork */
1816 crash_data
= task_crashinfo_alloc_init((mach_vm_address_t
)crash_data_ptr
, size
,
1817 is_corpse_fork
? 0 : CORPSE_CRASHINFO_HAS_REF
,
1818 KCFLAG_USE_MEMCOPY
);
1821 crash_data_release
= task
->corpse_info
;
1822 crash_data_kernel_release
= kcdata_memory_get_begin_addr(crash_data_release
);
1823 task
->corpse_info
= crash_data
;
1828 kfree(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1832 if (crash_data_release
!= NULL
) {
1833 task_crashinfo_destroy(crash_data_release
);
1835 if (crash_data_kernel_release
!= NULL
) {
1836 kfree(crash_data_kernel_release
, CORPSEINFO_ALLOCATION_SIZE
);
1844 if (free_label
!= NULL
) {
1845 mac_exc_free_label(free_label
);
1852 * task_deliver_crash_notification:
1854 * Makes outcall to registered host port for a corpse.
1857 task_deliver_crash_notification(
1860 exception_type_t etype
,
1861 mach_exception_subcode_t subcode
)
1863 kcdata_descriptor_t crash_info
= task
->corpse_info
;
1864 thread_t th_iter
= NULL
;
1865 kern_return_t kr
= KERN_SUCCESS
;
1866 wait_interrupt_t wsave
;
1867 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1868 ipc_port_t task_port
, old_notify
;
1870 if (crash_info
== NULL
) {
1871 return KERN_FAILURE
;
1875 if (task_is_a_corpse_fork(task
)) {
1876 /* Populate code with EXC_{RESOURCE,GUARD} for corpse fork */
1880 /* Populate code with EXC_CRASH for corpses */
1881 code
[0] = EXC_CRASH
;
1883 /* Update the code[1] if the boot-arg corpse_for_fatal_memkill is set */
1884 if (corpse_for_fatal_memkill
) {
1889 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1891 if (th_iter
->corpse_dup
== FALSE
) {
1892 ipc_thread_reset(th_iter
);
1897 /* Arm the no-sender notification for taskport */
1898 task_reference(task
);
1899 task_port
= convert_task_to_port(task
);
1901 assert(ip_active(task_port
));
1902 ipc_port_nsrequest(task_port
, task_port
->ip_mscount
, ipc_port_make_sonce_locked(task_port
), &old_notify
);
1904 assert(IP_NULL
== old_notify
);
1906 wsave
= thread_interrupt_level(THREAD_UNINT
);
1907 kr
= exception_triage_thread(EXC_CORPSE_NOTIFY
, code
, EXCEPTION_CODE_MAX
, thread
);
1908 if (kr
!= KERN_SUCCESS
) {
1909 printf("Failed to send exception EXC_CORPSE_NOTIFY. error code: %d for pid %d\n", kr
, task_pid(task
));
1912 (void)thread_interrupt_level(wsave
);
1915 * Drop the send right on task port, will fire the
1916 * no-sender notification if exception deliver failed.
1918 ipc_port_release_send(task_port
);
1925 * Terminate the specified task. See comments on thread_terminate
1926 * (kern/thread.c) about problems with terminating the "current task."
1933 if (task
== TASK_NULL
) {
1934 return KERN_INVALID_ARGUMENT
;
1937 if (task
->bsd_info
) {
1938 return KERN_FAILURE
;
1941 return task_terminate_internal(task
);
1945 extern int proc_pid(struct proc
*);
1946 extern void proc_name_kdp(task_t t
, char *buf
, int size
);
1947 #endif /* MACH_ASSERT */
1949 #define VM_MAP_PARTIAL_REAP 0x54 /* 0x150 */
1951 __unused
task_partial_reap(task_t task
, __unused
int pid
)
1953 unsigned int reclaimed_resident
= 0;
1954 unsigned int reclaimed_compressed
= 0;
1955 uint64_t task_page_count
;
1957 task_page_count
= (get_task_phys_footprint(task
) / PAGE_SIZE_64
);
1959 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_START
),
1960 pid
, task_page_count
, 0, 0, 0);
1962 vm_map_partial_reap(task
->map
, &reclaimed_resident
, &reclaimed_compressed
);
1964 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_END
),
1965 pid
, reclaimed_resident
, reclaimed_compressed
, 0, 0);
1969 task_mark_corpse(task_t task
)
1971 kern_return_t kr
= KERN_SUCCESS
;
1972 thread_t self_thread
;
1974 wait_interrupt_t wsave
;
1976 struct label
*crash_label
= NULL
;
1979 assert(task
!= kernel_task
);
1980 assert(task
== current_task());
1981 assert(!task_is_a_corpse(task
));
1984 crash_label
= mac_exc_create_label_for_proc((struct proc
*)task
->bsd_info
);
1987 kr
= task_collect_crash_info(task
,
1992 if (kr
!= KERN_SUCCESS
) {
1996 self_thread
= current_thread();
1998 wsave
= thread_interrupt_level(THREAD_UNINT
);
2001 task_set_corpse_pending_report(task
);
2002 task_set_corpse(task
);
2003 task
->crashed_thread_id
= thread_tid(self_thread
);
2005 kr
= task_start_halt_locked(task
, TRUE
);
2006 assert(kr
== KERN_SUCCESS
);
2008 ipc_task_reset(task
);
2009 /* Remove the naked send right for task port, needed to arm no sender notification */
2010 task_set_special_port(task
, TASK_KERNEL_PORT
, IPC_PORT_NULL
);
2011 ipc_task_enable(task
);
2014 /* terminate the ipc space */
2015 ipc_space_terminate(task
->itk_space
);
2017 /* Add it to global corpse task list */
2018 task_add_to_corpse_task_list(task
);
2020 task_start_halt(task
);
2021 thread_terminate_internal(self_thread
);
2023 (void) thread_interrupt_level(wsave
);
2024 assert(task
->halting
== TRUE
);
2028 mac_exc_free_label(crash_label
);
2036 * Clears the corpse pending bit on task.
2037 * Removes inspection bit on the threads.
2040 task_clear_corpse(task_t task
)
2042 thread_t th_iter
= NULL
;
2045 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
2047 thread_mtx_lock(th_iter
);
2048 th_iter
->inspection
= FALSE
;
2049 thread_mtx_unlock(th_iter
);
2052 thread_terminate_crashed_threads();
2053 /* remove the pending corpse report flag */
2054 task_clear_corpse_pending_report(task
);
2062 * Called whenever the Mach port system detects no-senders on
2063 * the task port of a corpse.
2064 * Each notification that comes in should terminate the task (corpse).
2067 task_port_notify(mach_msg_header_t
*msg
)
2069 mach_no_senders_notification_t
*notification
= (void *)msg
;
2070 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
2073 assert(ip_active(port
));
2074 assert(IKOT_TASK
== ip_kotype(port
));
2075 task
= (task_t
) port
->ip_kobject
;
2077 assert(task_is_a_corpse(task
));
2079 /* Remove the task from global corpse task list */
2080 task_remove_from_corpse_task_list(task
);
2082 task_clear_corpse(task
);
2083 task_terminate_internal(task
);
2087 * task_wait_till_threads_terminate_locked
2089 * Wait till all the threads in the task are terminated.
2090 * Might release the task lock and re-acquire it.
2093 task_wait_till_threads_terminate_locked(task_t task
)
2095 /* wait for all the threads in the task to terminate */
2096 while (task
->active_thread_count
!= 0) {
2097 assert_wait((event_t
)&task
->active_thread_count
, THREAD_UNINT
);
2099 thread_block(THREAD_CONTINUE_NULL
);
2106 * task_duplicate_map_and_threads
2108 * Copy vmmap of source task.
2109 * Copy active threads from source task to destination task.
2110 * Source task would be suspended during the copy.
2113 task_duplicate_map_and_threads(
2117 thread_t
*thread_ret
,
2118 uint64_t **udata_buffer
,
2122 kern_return_t kr
= KERN_SUCCESS
;
2124 thread_t thread
, self
, thread_return
= THREAD_NULL
;
2125 thread_t new_thread
= THREAD_NULL
, first_thread
= THREAD_NULL
;
2126 thread_t
*thread_array
;
2127 uint32_t active_thread_count
= 0, array_count
= 0, i
;
2129 uint64_t *buffer
= NULL
;
2131 int est_knotes
= 0, num_knotes
= 0;
2133 self
= current_thread();
2136 * Suspend the task to copy thread state, use the internal
2137 * variant so that no user-space process can resume
2138 * the task from under us
2140 kr
= task_suspend_internal(task
);
2141 if (kr
!= KERN_SUCCESS
) {
2145 if (task
->map
->disable_vmentry_reuse
== TRUE
) {
2147 * Quite likely GuardMalloc (or some debugging tool)
2148 * is being used on this task. And it has gone through
2149 * its limit. Making a corpse will likely encounter
2150 * a lot of VM entries that will need COW.
2154 #if DEVELOPMENT || DEBUG
2155 memorystatus_abort_vm_map_fork(task
);
2157 task_resume_internal(task
);
2158 return KERN_FAILURE
;
2161 /* Check with VM if vm_map_fork is allowed for this task */
2162 if (memorystatus_allowed_vm_map_fork(task
)) {
2163 /* Setup new task's vmmap, switch from parent task's map to it COW map */
2164 oldmap
= new_task
->map
;
2165 new_task
->map
= vm_map_fork(new_task
->ledger
,
2167 (VM_MAP_FORK_SHARE_IF_INHERIT_NONE
|
2168 VM_MAP_FORK_PRESERVE_PURGEABLE
|
2169 VM_MAP_FORK_CORPSE_FOOTPRINT
));
2170 vm_map_deallocate(oldmap
);
2172 /* copy ledgers that impact the memory footprint */
2173 vm_map_copy_footprint_ledgers(task
, new_task
);
2175 /* Get all the udata pointers from kqueue */
2176 est_knotes
= kevent_proc_copy_uptrs(p
, NULL
, 0);
2177 if (est_knotes
> 0) {
2178 buf_size
= (est_knotes
+ 32) * sizeof(uint64_t);
2179 buffer
= (uint64_t *) kalloc(buf_size
);
2180 num_knotes
= kevent_proc_copy_uptrs(p
, buffer
, buf_size
);
2181 if (num_knotes
> est_knotes
+ 32) {
2182 num_knotes
= est_knotes
+ 32;
2187 active_thread_count
= task
->active_thread_count
;
2188 if (active_thread_count
== 0) {
2189 if (buffer
!= NULL
) {
2190 kfree(buffer
, buf_size
);
2192 task_resume_internal(task
);
2193 return KERN_FAILURE
;
2196 thread_array
= (thread_t
*) kalloc(sizeof(thread_t
) * active_thread_count
);
2198 /* Iterate all the threads and drop the task lock before calling thread_create_with_continuation */
2200 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2201 /* Skip inactive threads */
2202 active
= thread
->active
;
2207 if (array_count
>= active_thread_count
) {
2211 thread_array
[array_count
++] = thread
;
2212 thread_reference(thread
);
2216 for (i
= 0; i
< array_count
; i
++) {
2217 kr
= thread_create_with_continuation(new_task
, &new_thread
, (thread_continue_t
)thread_corpse_continue
);
2218 if (kr
!= KERN_SUCCESS
) {
2222 /* Equivalent of current thread in corpse */
2223 if (thread_array
[i
] == self
) {
2224 thread_return
= new_thread
;
2225 new_task
->crashed_thread_id
= thread_tid(new_thread
);
2226 } else if (first_thread
== NULL
) {
2227 first_thread
= new_thread
;
2229 /* drop the extra ref returned by thread_create_with_continuation */
2230 thread_deallocate(new_thread
);
2233 kr
= thread_dup2(thread_array
[i
], new_thread
);
2234 if (kr
!= KERN_SUCCESS
) {
2235 thread_mtx_lock(new_thread
);
2236 new_thread
->corpse_dup
= TRUE
;
2237 thread_mtx_unlock(new_thread
);
2241 /* Copy thread name */
2242 bsd_copythreadname(new_thread
->uthread
, thread_array
[i
]->uthread
);
2243 new_thread
->thread_tag
= thread_array
[i
]->thread_tag
;
2244 thread_copy_resource_info(new_thread
, thread_array
[i
]);
2247 /* return the first thread if we couldn't find the equivalent of current */
2248 if (thread_return
== THREAD_NULL
) {
2249 thread_return
= first_thread
;
2250 } else if (first_thread
!= THREAD_NULL
) {
2251 /* drop the extra ref returned by thread_create_with_continuation */
2252 thread_deallocate(first_thread
);
2255 task_resume_internal(task
);
2257 for (i
= 0; i
< array_count
; i
++) {
2258 thread_deallocate(thread_array
[i
]);
2260 kfree(thread_array
, sizeof(thread_t
) * active_thread_count
);
2262 if (kr
== KERN_SUCCESS
) {
2263 *thread_ret
= thread_return
;
2264 *udata_buffer
= buffer
;
2266 *num_udata
= num_knotes
;
2268 if (thread_return
!= THREAD_NULL
) {
2269 thread_deallocate(thread_return
);
2271 if (buffer
!= NULL
) {
2272 kfree(buffer
, buf_size
);
2279 #if CONFIG_SECLUDED_MEMORY
2280 extern void task_set_can_use_secluded_mem_locked(
2282 boolean_t can_use_secluded_mem
);
2283 #endif /* CONFIG_SECLUDED_MEMORY */
2286 task_terminate_internal(
2289 thread_t thread
, self
;
2291 boolean_t interrupt_save
;
2294 assert(task
!= kernel_task
);
2296 self
= current_thread();
2297 self_task
= self
->task
;
2300 * Get the task locked and make sure that we are not racing
2301 * with someone else trying to terminate us.
2303 if (task
== self_task
) {
2305 } else if (task
< self_task
) {
2307 task_lock(self_task
);
2309 task_lock(self_task
);
2313 #if CONFIG_SECLUDED_MEMORY
2314 if (task
->task_can_use_secluded_mem
) {
2315 task_set_can_use_secluded_mem_locked(task
, FALSE
);
2317 task
->task_could_use_secluded_mem
= FALSE
;
2318 task
->task_could_also_use_secluded_mem
= FALSE
;
2320 if (task
->task_suppressed_secluded
) {
2321 stop_secluded_suppression(task
);
2323 #endif /* CONFIG_SECLUDED_MEMORY */
2325 if (!task
->active
) {
2327 * Task is already being terminated.
2328 * Just return an error. If we are dying, this will
2329 * just get us to our AST special handler and that
2330 * will get us to finalize the termination of ourselves.
2333 if (self_task
!= task
) {
2334 task_unlock(self_task
);
2337 return KERN_FAILURE
;
2340 if (task_corpse_pending_report(task
)) {
2342 * Task is marked for reporting as corpse.
2343 * Just return an error. This will
2344 * just get us to our AST special handler and that
2345 * will get us to finish the path to death
2348 if (self_task
!= task
) {
2349 task_unlock(self_task
);
2352 return KERN_FAILURE
;
2355 if (self_task
!= task
) {
2356 task_unlock(self_task
);
2360 * Make sure the current thread does not get aborted out of
2361 * the waits inside these operations.
2363 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
2366 * Indicate that we want all the threads to stop executing
2367 * at user space by holding the task (we would have held
2368 * each thread independently in thread_terminate_internal -
2369 * but this way we may be more likely to already find it
2370 * held there). Mark the task inactive, and prevent
2371 * further task operations via the task port.
2373 task_hold_locked(task
);
2374 task
->active
= FALSE
;
2375 ipc_task_disable(task
);
2377 #if CONFIG_TELEMETRY
2379 * Notify telemetry that this task is going away.
2381 telemetry_task_ctl_locked(task
, TF_TELEMETRY
, 0);
2385 * Terminate each thread in the task.
2387 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2388 thread_terminate_internal(thread
);
2392 if (task
->bsd_info
!= NULL
&& !task_is_exec_copy(task
)) {
2393 pid
= proc_pid(task
->bsd_info
);
2395 #endif /* MACH_BSD */
2399 proc_set_task_policy(task
, TASK_POLICY_ATTRIBUTE
,
2400 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
2402 /* Early object reap phase */
2404 // PR-17045188: Revisit implementation
2405 // task_partial_reap(task, pid);
2409 * remove all task watchers
2411 task_removewatchers(task
);
2413 #endif /* CONFIG_EMBEDDED */
2416 * Destroy all synchronizers owned by the task.
2418 task_synchronizer_destroy_all(task
);
2421 * Destroy the IPC space, leaving just a reference for it.
2423 ipc_space_terminate(task
->itk_space
);
2426 /* if some ledgers go negative on tear-down again... */
2427 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2428 task_ledgers
.phys_footprint
);
2429 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2430 task_ledgers
.internal
);
2431 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2432 task_ledgers
.internal_compressed
);
2433 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2434 task_ledgers
.iokit_mapped
);
2435 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2436 task_ledgers
.alternate_accounting
);
2437 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2438 task_ledgers
.alternate_accounting_compressed
);
2442 * If the current thread is a member of the task
2443 * being terminated, then the last reference to
2444 * the task will not be dropped until the thread
2445 * is finally reaped. To avoid incurring the
2446 * expense of removing the address space regions
2447 * at reap time, we do it explictly here.
2450 vm_map_lock(task
->map
);
2451 vm_map_disable_hole_optimization(task
->map
);
2452 vm_map_unlock(task
->map
);
2456 * Identify the pmap's process, in case the pmap ledgers drift
2457 * and we have to report it.
2460 if (task
->bsd_info
&& !task_is_exec_copy(task
)) {
2461 pid
= proc_pid(task
->bsd_info
);
2462 proc_name_kdp(task
, procname
, sizeof(procname
));
2465 strlcpy(procname
, "<unknown>", sizeof(procname
));
2467 pmap_set_process(task
->map
->pmap
, pid
, procname
);
2468 #endif /* MACH_ASSERT */
2470 vm_map_remove(task
->map
,
2471 task
->map
->min_offset
,
2472 task
->map
->max_offset
,
2476 * + remove immutable mappings
2477 * + allow gaps in range
2479 (VM_MAP_REMOVE_NO_UNNESTING
|
2480 VM_MAP_REMOVE_IMMUTABLE
|
2481 VM_MAP_REMOVE_GAPS_OK
));
2483 /* release our shared region */
2484 vm_shared_region_set(task
, NULL
);
2487 lck_mtx_lock(&tasks_threads_lock
);
2488 queue_remove(&tasks
, task
, task_t
, tasks
);
2489 queue_enter(&terminated_tasks
, task
, task_t
, tasks
);
2491 terminated_tasks_count
++;
2492 lck_mtx_unlock(&tasks_threads_lock
);
2495 * We no longer need to guard against being aborted, so restore
2496 * the previous interruptible state.
2498 thread_interrupt_level(interrupt_save
);
2501 /* force the task to release all ctrs */
2502 if (task
->t_kpc
& TASK_KPC_FORCED_ALL_CTRS
) {
2503 kpc_force_all_ctrs(task
, 0);
2507 #if CONFIG_COALITIONS
2509 * Leave our coalitions. (drop activation but not reference)
2511 coalitions_remove_task(task
);
2515 * Get rid of the task active reference on itself.
2517 task_deallocate(task
);
2519 return KERN_SUCCESS
;
2523 tasks_system_suspend(boolean_t suspend
)
2527 lck_mtx_lock(&tasks_threads_lock
);
2528 assert(tasks_suspend_state
!= suspend
);
2529 tasks_suspend_state
= suspend
;
2530 queue_iterate(&tasks
, task
, task_t
, tasks
) {
2531 if (task
== kernel_task
) {
2534 suspend
? task_suspend_internal(task
) : task_resume_internal(task
);
2536 lck_mtx_unlock(&tasks_threads_lock
);
2542 * Shut the current task down (except for the current thread) in
2543 * preparation for dramatic changes to the task (probably exec).
2544 * We hold the task and mark all other threads in the task for
2548 task_start_halt(task_t task
)
2550 kern_return_t kr
= KERN_SUCCESS
;
2552 kr
= task_start_halt_locked(task
, FALSE
);
2557 static kern_return_t
2558 task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
)
2560 thread_t thread
, self
;
2561 uint64_t dispatchqueue_offset
;
2563 assert(task
!= kernel_task
);
2565 self
= current_thread();
2567 if (task
!= self
->task
&& !task_is_a_corpse_fork(task
)) {
2568 return KERN_INVALID_ARGUMENT
;
2571 if (task
->halting
|| !task
->active
|| !self
->active
) {
2573 * Task or current thread is already being terminated.
2574 * Hurry up and return out of the current kernel context
2575 * so that we run our AST special handler to terminate
2578 return KERN_FAILURE
;
2581 task
->halting
= TRUE
;
2584 * Mark all the threads to keep them from starting any more
2585 * user-level execution. The thread_terminate_internal code
2586 * would do this on a thread by thread basis anyway, but this
2587 * gives us a better chance of not having to wait there.
2589 task_hold_locked(task
);
2590 dispatchqueue_offset
= get_dispatchqueue_offset_from_proc(task
->bsd_info
);
2593 * Terminate all the other threads in the task.
2595 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
)
2597 if (should_mark_corpse
) {
2598 thread_mtx_lock(thread
);
2599 thread
->inspection
= TRUE
;
2600 thread_mtx_unlock(thread
);
2602 if (thread
!= self
) {
2603 thread_terminate_internal(thread
);
2606 task
->dispatchqueue_offset
= dispatchqueue_offset
;
2608 task_release_locked(task
);
2610 return KERN_SUCCESS
;
2615 * task_complete_halt:
2617 * Complete task halt by waiting for threads to terminate, then clean
2618 * up task resources (VM, port namespace, etc...) and then let the
2619 * current thread go in the (practically empty) task context.
2621 * Note: task->halting flag is not cleared in order to avoid creation
2622 * of new thread in old exec'ed task.
2625 task_complete_halt(task_t task
)
2628 assert(task
->halting
);
2629 assert(task
== current_task());
2632 * Wait for the other threads to get shut down.
2633 * When the last other thread is reaped, we'll be
2636 if (task
->thread_count
> 1) {
2637 assert_wait((event_t
)&task
->halting
, THREAD_UNINT
);
2639 thread_block(THREAD_CONTINUE_NULL
);
2645 * Give the machine dependent code a chance
2646 * to perform cleanup of task-level resources
2647 * associated with the current thread before
2648 * ripping apart the task.
2650 machine_task_terminate(task
);
2653 * Destroy all synchronizers owned by the task.
2655 task_synchronizer_destroy_all(task
);
2658 * Destroy the contents of the IPC space, leaving just
2659 * a reference for it.
2661 ipc_space_clean(task
->itk_space
);
2664 * Clean out the address space, as we are going to be
2665 * getting a new one.
2667 vm_map_remove(task
->map
, task
->map
->min_offset
,
2668 task
->map
->max_offset
,
2672 * + remove immutable mappings
2673 * + allow gaps in the range
2675 (VM_MAP_REMOVE_NO_UNNESTING
|
2676 VM_MAP_REMOVE_IMMUTABLE
|
2677 VM_MAP_REMOVE_GAPS_OK
));
2680 * Kick out any IOKitUser handles to the task. At best they're stale,
2681 * at worst someone is racing a SUID exec.
2683 iokit_task_terminate(task
);
2689 * Suspend execution of the specified task.
2690 * This is a recursive-style suspension of the task, a count of
2691 * suspends is maintained.
2693 * CONDITIONS: the task is locked and active.
2701 assert(task
->active
);
2703 if (task
->suspend_count
++ > 0) {
2707 if (task
->bsd_info
) {
2708 workq_proc_suspended(task
->bsd_info
);
2712 * Iterate through all the threads and hold them.
2714 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2715 thread_mtx_lock(thread
);
2716 thread_hold(thread
);
2717 thread_mtx_unlock(thread
);
2724 * Same as the internal routine above, except that is must lock
2725 * and verify that the task is active. This differs from task_suspend
2726 * in that it places a kernel hold on the task rather than just a
2727 * user-level hold. This keeps users from over resuming and setting
2728 * it running out from under the kernel.
2730 * CONDITIONS: the caller holds a reference on the task
2736 if (task
== TASK_NULL
) {
2737 return KERN_INVALID_ARGUMENT
;
2742 if (!task
->active
) {
2745 return KERN_FAILURE
;
2748 task_hold_locked(task
);
2751 return KERN_SUCCESS
;
2757 boolean_t until_not_runnable
)
2759 if (task
== TASK_NULL
) {
2760 return KERN_INVALID_ARGUMENT
;
2765 if (!task
->active
) {
2768 return KERN_FAILURE
;
2771 task_wait_locked(task
, until_not_runnable
);
2774 return KERN_SUCCESS
;
2780 * Wait for all threads in task to stop.
2783 * Called with task locked, active, and held.
2788 boolean_t until_not_runnable
)
2790 thread_t thread
, self
;
2792 assert(task
->active
);
2793 assert(task
->suspend_count
> 0);
2795 self
= current_thread();
2798 * Iterate through all the threads and wait for them to
2799 * stop. Do not wait for the current thread if it is within
2802 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2803 if (thread
!= self
) {
2804 thread_wait(thread
, until_not_runnable
);
2810 * task_release_locked:
2812 * Release a kernel hold on a task.
2814 * CONDITIONS: the task is locked and active
2817 task_release_locked(
2822 assert(task
->active
);
2823 assert(task
->suspend_count
> 0);
2825 if (--task
->suspend_count
> 0) {
2829 if (task
->bsd_info
) {
2830 workq_proc_resumed(task
->bsd_info
);
2833 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2834 thread_mtx_lock(thread
);
2835 thread_release(thread
);
2836 thread_mtx_unlock(thread
);
2843 * Same as the internal routine above, except that it must lock
2844 * and verify that the task is active.
2846 * CONDITIONS: The caller holds a reference to the task
2852 if (task
== TASK_NULL
) {
2853 return KERN_INVALID_ARGUMENT
;
2858 if (!task
->active
) {
2861 return KERN_FAILURE
;
2864 task_release_locked(task
);
2867 return KERN_SUCCESS
;
2873 thread_act_array_t
*threads_out
,
2874 mach_msg_type_number_t
*count
)
2876 mach_msg_type_number_t actual
;
2877 thread_t
*thread_list
;
2879 vm_size_t size
, size_needed
;
2883 if (task
== TASK_NULL
) {
2884 return KERN_INVALID_ARGUMENT
;
2887 size
= 0; addr
= NULL
;
2891 if (!task
->active
) {
2898 return KERN_FAILURE
;
2901 actual
= task
->thread_count
;
2903 /* do we have the memory we need? */
2904 size_needed
= actual
* sizeof(mach_port_t
);
2905 if (size_needed
<= size
) {
2909 /* unlock the task and allocate more memory */
2916 assert(size_needed
> 0);
2919 addr
= kalloc(size
);
2921 return KERN_RESOURCE_SHORTAGE
;
2925 /* OK, have memory and the task is locked & active */
2926 thread_list
= (thread_t
*)addr
;
2930 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
2931 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
2932 thread_reference_internal(thread
);
2933 thread_list
[j
++] = thread
;
2936 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
2939 size_needed
= actual
* sizeof(mach_port_t
);
2941 /* can unlock task now that we've got the thread refs */
2945 /* no threads, so return null pointer and deallocate memory */
2947 *threads_out
= NULL
;
2954 /* if we allocated too much, must copy */
2956 if (size_needed
< size
) {
2959 newaddr
= kalloc(size_needed
);
2961 for (i
= 0; i
< actual
; ++i
) {
2962 thread_deallocate(thread_list
[i
]);
2965 return KERN_RESOURCE_SHORTAGE
;
2968 bcopy(addr
, newaddr
, size_needed
);
2970 thread_list
= (thread_t
*)newaddr
;
2973 *threads_out
= thread_list
;
2976 /* do the conversion that Mig should handle */
2978 for (i
= 0; i
< actual
; ++i
) {
2979 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
2983 return KERN_SUCCESS
;
2986 #define TASK_HOLD_NORMAL 0
2987 #define TASK_HOLD_PIDSUSPEND 1
2988 #define TASK_HOLD_LEGACY 2
2989 #define TASK_HOLD_LEGACY_ALL 3
2991 static kern_return_t
2996 if (!task
->active
&& !task_is_a_corpse(task
)) {
2997 return KERN_FAILURE
;
3000 /* Return success for corpse task */
3001 if (task_is_a_corpse(task
)) {
3002 return KERN_SUCCESS
;
3005 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3006 MACHDBG_CODE(DBG_MACH_IPC
, MACH_TASK_SUSPEND
) | DBG_FUNC_NONE
,
3007 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
3008 task
->user_stop_count
, task
->user_stop_count
+ 1, 0);
3011 current_task()->suspends_outstanding
++;
3014 if (mode
== TASK_HOLD_LEGACY
) {
3015 task
->legacy_stop_count
++;
3018 if (task
->user_stop_count
++ > 0) {
3020 * If the stop count was positive, the task is
3021 * already stopped and we can exit.
3023 return KERN_SUCCESS
;
3027 * Put a kernel-level hold on the threads in the task (all
3028 * user-level task suspensions added together represent a
3029 * single kernel-level hold). We then wait for the threads
3030 * to stop executing user code.
3032 task_hold_locked(task
);
3033 task_wait_locked(task
, FALSE
);
3035 return KERN_SUCCESS
;
3038 static kern_return_t
3043 boolean_t release
= FALSE
;
3045 if (!task
->active
&& !task_is_a_corpse(task
)) {
3046 return KERN_FAILURE
;
3049 /* Return success for corpse task */
3050 if (task_is_a_corpse(task
)) {
3051 return KERN_SUCCESS
;
3054 if (mode
== TASK_HOLD_PIDSUSPEND
) {
3055 if (task
->pidsuspended
== FALSE
) {
3056 return KERN_FAILURE
;
3058 task
->pidsuspended
= FALSE
;
3061 if (task
->user_stop_count
> (task
->pidsuspended
? 1 : 0)) {
3062 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3063 MACHDBG_CODE(DBG_MACH_IPC
, MACH_TASK_RESUME
) | DBG_FUNC_NONE
,
3064 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
3065 task
->user_stop_count
, mode
, task
->legacy_stop_count
);
3069 * This is obviously not robust; if we suspend one task and then resume a different one,
3070 * we'll fly under the radar. This is only meant to catch the common case of a crashed
3071 * or buggy suspender.
3073 current_task()->suspends_outstanding
--;
3076 if (mode
== TASK_HOLD_LEGACY_ALL
) {
3077 if (task
->legacy_stop_count
>= task
->user_stop_count
) {
3078 task
->user_stop_count
= 0;
3081 task
->user_stop_count
-= task
->legacy_stop_count
;
3083 task
->legacy_stop_count
= 0;
3085 if (mode
== TASK_HOLD_LEGACY
&& task
->legacy_stop_count
> 0) {
3086 task
->legacy_stop_count
--;
3088 if (--task
->user_stop_count
== 0) {
3093 return KERN_FAILURE
;
3097 * Release the task if necessary.
3100 task_release_locked(task
);
3103 return KERN_SUCCESS
;
3110 * Implement an (old-fashioned) user-level suspension on a task.
3112 * Because the user isn't expecting to have to manage a suspension
3113 * token, we'll track it for him in the kernel in the form of a naked
3114 * send right to the task's resume port. All such send rights
3115 * account for a single suspension against the task (unlike task_suspend2()
3116 * where each caller gets a unique suspension count represented by a
3117 * unique send-once right).
3120 * The caller holds a reference to the task
3127 mach_port_t port
, send
, old_notify
;
3128 mach_port_name_t name
;
3130 if (task
== TASK_NULL
|| task
== kernel_task
) {
3131 return KERN_INVALID_ARGUMENT
;
3137 * Claim a send right on the task resume port, and request a no-senders
3138 * notification on that port (if none outstanding).
3140 if (task
->itk_resume
== IP_NULL
) {
3141 task
->itk_resume
= ipc_port_alloc_kernel();
3142 if (!IP_VALID(task
->itk_resume
)) {
3143 panic("failed to create resume port");
3145 ipc_kobject_set(task
->itk_resume
, (ipc_kobject_t
)task
, IKOT_TASK_RESUME
);
3148 port
= task
->itk_resume
;
3150 assert(ip_active(port
));
3152 send
= ipc_port_make_send_locked(port
);
3153 assert(IP_VALID(send
));
3155 if (port
->ip_nsrequest
== IP_NULL
) {
3156 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
3157 assert(old_notify
== IP_NULL
);
3164 * place a legacy hold on the task.
3166 kr
= place_task_hold(task
, TASK_HOLD_LEGACY
);
3167 if (kr
!= KERN_SUCCESS
) {
3169 ipc_port_release_send(send
);
3176 * Copyout the send right into the calling task's IPC space. It won't know it is there,
3177 * but we'll look it up when calling a traditional resume. Any IPC operations that
3178 * deallocate the send right will auto-release the suspension.
3180 if ((kr
= ipc_kmsg_copyout_object(current_task()->itk_space
, (ipc_object_t
)send
,
3181 MACH_MSG_TYPE_MOVE_SEND
, &name
)) != KERN_SUCCESS
) {
3182 printf("warning: %s(%d) failed to copyout suspension token for pid %d with error: %d\n",
3183 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
3184 task_pid(task
), kr
);
3193 * Release a user hold on a task.
3196 * The caller holds a reference to the task
3203 mach_port_name_t resume_port_name
;
3204 ipc_entry_t resume_port_entry
;
3205 ipc_space_t space
= current_task()->itk_space
;
3207 if (task
== TASK_NULL
|| task
== kernel_task
) {
3208 return KERN_INVALID_ARGUMENT
;
3211 /* release a legacy task hold */
3213 kr
= release_task_hold(task
, TASK_HOLD_LEGACY
);
3216 is_write_lock(space
);
3217 if (is_active(space
) && IP_VALID(task
->itk_resume
) &&
3218 ipc_hash_lookup(space
, (ipc_object_t
)task
->itk_resume
, &resume_port_name
, &resume_port_entry
) == TRUE
) {
3220 * We found a suspension token in the caller's IPC space. Release a send right to indicate that
3221 * we are holding one less legacy hold on the task from this caller. If the release failed,
3222 * go ahead and drop all the rights, as someone either already released our holds or the task
3225 if (kr
== KERN_SUCCESS
) {
3226 ipc_right_dealloc(space
, resume_port_name
, resume_port_entry
);
3228 ipc_right_destroy(space
, resume_port_name
, resume_port_entry
, FALSE
, 0);
3230 /* space unlocked */
3232 is_write_unlock(space
);
3233 if (kr
== KERN_SUCCESS
) {
3234 printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
3235 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
3244 * Suspend the target task.
3245 * Making/holding a token/reference/port is the callers responsibility.
3248 task_suspend_internal(task_t task
)
3252 if (task
== TASK_NULL
|| task
== kernel_task
) {
3253 return KERN_INVALID_ARGUMENT
;
3257 kr
= place_task_hold(task
, TASK_HOLD_NORMAL
);
3263 * Suspend the target task, and return a suspension token. The token
3264 * represents a reference on the suspended task.
3269 task_suspension_token_t
*suspend_token
)
3273 kr
= task_suspend_internal(task
);
3274 if (kr
!= KERN_SUCCESS
) {
3275 *suspend_token
= TASK_NULL
;
3280 * Take a reference on the target task and return that to the caller
3281 * as a "suspension token," which can be converted into an SO right to
3282 * the now-suspended task's resume port.
3284 task_reference_internal(task
);
3285 *suspend_token
= task
;
3287 return KERN_SUCCESS
;
3292 * (reference/token/port management is caller's responsibility).
3295 task_resume_internal(
3296 task_suspension_token_t task
)
3300 if (task
== TASK_NULL
|| task
== kernel_task
) {
3301 return KERN_INVALID_ARGUMENT
;
3305 kr
= release_task_hold(task
, TASK_HOLD_NORMAL
);
3311 * Resume the task using a suspension token. Consumes the token's ref.
3315 task_suspension_token_t task
)
3319 kr
= task_resume_internal(task
);
3320 task_suspension_token_deallocate(task
);
3326 task_suspension_notify(mach_msg_header_t
*request_header
)
3328 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
3329 task_t task
= convert_port_to_task_suspension_token(port
);
3330 mach_msg_type_number_t not_count
;
3332 if (task
== TASK_NULL
|| task
== kernel_task
) {
3333 return TRUE
; /* nothing to do */
3335 switch (request_header
->msgh_id
) {
3336 case MACH_NOTIFY_SEND_ONCE
:
3337 /* release the hold held by this specific send-once right */
3339 release_task_hold(task
, TASK_HOLD_NORMAL
);
3343 case MACH_NOTIFY_NO_SENDERS
:
3344 not_count
= ((mach_no_senders_notification_t
*)request_header
)->not_count
;
3348 if (port
->ip_mscount
== not_count
) {
3349 /* release all the [remaining] outstanding legacy holds */
3350 assert(port
->ip_nsrequest
== IP_NULL
);
3352 release_task_hold(task
, TASK_HOLD_LEGACY_ALL
);
3354 } else if (port
->ip_nsrequest
== IP_NULL
) {
3355 ipc_port_t old_notify
;
3358 /* new send rights, re-arm notification at current make-send count */
3359 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
3360 assert(old_notify
== IP_NULL
);
3372 task_suspension_token_deallocate(task
); /* drop token reference */
3377 task_pidsuspend_locked(task_t task
)
3381 if (task
->pidsuspended
) {
3386 task
->pidsuspended
= TRUE
;
3388 kr
= place_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3389 if (kr
!= KERN_SUCCESS
) {
3390 task
->pidsuspended
= FALSE
;
3400 * Suspends a task by placing a hold on its threads.
3403 * The caller holds a reference to the task
3411 if (task
== TASK_NULL
|| task
== kernel_task
) {
3412 return KERN_INVALID_ARGUMENT
;
3417 kr
= task_pidsuspend_locked(task
);
3426 * Resumes a previously suspended task.
3429 * The caller holds a reference to the task
3437 if (task
== TASK_NULL
|| task
== kernel_task
) {
3438 return KERN_INVALID_ARGUMENT
;
3445 while (task
->changing_freeze_state
) {
3446 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3448 thread_block(THREAD_CONTINUE_NULL
);
3452 task
->changing_freeze_state
= TRUE
;
3455 kr
= release_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3463 if (kr
== KERN_SUCCESS
) {
3464 task
->frozen
= FALSE
;
3466 task
->changing_freeze_state
= FALSE
;
3467 thread_wakeup(&task
->changing_freeze_state
);
3476 #if DEVELOPMENT || DEBUG
3478 extern void IOSleep(int);
3481 task_disconnect_page_mappings(task_t task
)
3485 if (task
== TASK_NULL
|| task
== kernel_task
) {
3486 return KERN_INVALID_ARGUMENT
;
3490 * this function is used to strip all of the mappings from
3491 * the pmap for the specified task to force the task to
3492 * re-fault all of the pages it is actively using... this
3493 * allows us to approximate the true working set of the
3494 * specified task. We only engage if at least 1 of the
3495 * threads in the task is runnable, but we want to continuously
3496 * sweep (at least for a while - I've arbitrarily set the limit at
3497 * 100 sweeps to be re-looked at as we gain experience) to get a better
3498 * view into what areas within a page are being visited (as opposed to only
3499 * seeing the first fault of a page after the task becomes
3500 * runnable)... in the future I may
3501 * try to block until awakened by a thread in this task
3502 * being made runnable, but for now we'll periodically poll from the
3503 * user level debug tool driving the sysctl
3505 for (n
= 0; n
< 100; n
++) {
3508 boolean_t do_unnest
;
3516 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3517 if (thread
->state
& TH_RUN
) {
3523 task
->task_disconnected_count
++;
3526 if (task
->task_unnested
== FALSE
) {
3527 if (runnable
== TRUE
) {
3528 task
->task_unnested
= TRUE
;
3534 if (runnable
== FALSE
) {
3538 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_START
,
3539 task
, do_unnest
, task
->task_disconnected_count
, 0, 0);
3541 page_count
= vm_map_disconnect_page_mappings(task
->map
, do_unnest
);
3543 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_END
,
3544 task
, page_count
, 0, 0, 0);
3550 return KERN_SUCCESS
;
3564 * The caller holds a reference to the task
3566 extern void vm_wake_compactor_swapper(void);
3567 extern queue_head_t c_swapout_list_head
;
3572 uint32_t *purgeable_count
,
3573 uint32_t *wired_count
,
3574 uint32_t *clean_count
,
3575 uint32_t *dirty_count
,
3576 uint32_t dirty_budget
,
3577 uint32_t *shared_count
,
3578 int *freezer_error_code
,
3579 boolean_t eval_only
)
3581 kern_return_t kr
= KERN_SUCCESS
;
3583 if (task
== TASK_NULL
|| task
== kernel_task
) {
3584 return KERN_INVALID_ARGUMENT
;
3589 while (task
->changing_freeze_state
) {
3590 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3592 thread_block(THREAD_CONTINUE_NULL
);
3598 return KERN_FAILURE
;
3600 task
->changing_freeze_state
= TRUE
;
3604 kr
= vm_map_freeze(task
->map
,
3616 if ((kr
== KERN_SUCCESS
) && (eval_only
== FALSE
)) {
3617 task
->frozen
= TRUE
;
3620 task
->changing_freeze_state
= FALSE
;
3621 thread_wakeup(&task
->changing_freeze_state
);
3625 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
&&
3626 (eval_only
== FALSE
)) {
3627 vm_wake_compactor_swapper();
3629 * We do an explicit wakeup of the swapout thread here
3630 * because the compact_and_swap routines don't have
3631 * knowledge about these kind of "per-task packed c_segs"
3632 * and so will not be evaluating whether we need to do
3635 thread_wakeup((event_t
)&c_swapout_list_head
);
3644 * Thaw a currently frozen task.
3647 * The caller holds a reference to the task
3653 if (task
== TASK_NULL
|| task
== kernel_task
) {
3654 return KERN_INVALID_ARGUMENT
;
3659 while (task
->changing_freeze_state
) {
3660 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3662 thread_block(THREAD_CONTINUE_NULL
);
3666 if (!task
->frozen
) {
3668 return KERN_FAILURE
;
3670 task
->frozen
= FALSE
;
3674 return KERN_SUCCESS
;
3677 #endif /* CONFIG_FREEZE */
3680 host_security_set_task_token(
3681 host_security_t host_security
,
3683 security_token_t sec_token
,
3684 audit_token_t audit_token
,
3685 host_priv_t host_priv
)
3687 ipc_port_t host_port
;
3690 if (task
== TASK_NULL
) {
3691 return KERN_INVALID_ARGUMENT
;
3694 if (host_security
== HOST_NULL
) {
3695 return KERN_INVALID_SECURITY
;
3699 task
->sec_token
= sec_token
;
3700 task
->audit_token
= audit_token
;
3704 if (host_priv
!= HOST_PRIV_NULL
) {
3705 kr
= host_get_host_priv_port(host_priv
, &host_port
);
3707 kr
= host_get_host_port(host_priv_self(), &host_port
);
3709 assert(kr
== KERN_SUCCESS
);
3710 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
3715 task_send_trace_memory(
3716 __unused task_t target_task
,
3717 __unused
uint32_t pid
,
3718 __unused
uint64_t uniqueid
)
3720 return KERN_INVALID_ARGUMENT
;
3724 * This routine was added, pretty much exclusively, for registering the
3725 * RPC glue vector for in-kernel short circuited tasks. Rather than
3726 * removing it completely, I have only disabled that feature (which was
3727 * the only feature at the time). It just appears that we are going to
3728 * want to add some user data to tasks in the future (i.e. bsd info,
3729 * task names, etc...), so I left it in the formal task interface.
3734 task_flavor_t flavor
,
3735 __unused task_info_t task_info_in
, /* pointer to IN array */
3736 __unused mach_msg_type_number_t task_info_count
)
3738 if (task
== TASK_NULL
) {
3739 return KERN_INVALID_ARGUMENT
;
3744 case TASK_TRACE_MEMORY_INFO
:
3746 if (task_info_count
!= TASK_TRACE_MEMORY_INFO_COUNT
) {
3747 return KERN_INVALID_ARGUMENT
;
3750 assert(task_info_in
!= NULL
);
3751 task_trace_memory_info_t mem_info
;
3752 mem_info
= (task_trace_memory_info_t
) task_info_in
;
3753 kern_return_t kr
= atm_register_trace_memory(task
,
3754 mem_info
->user_memory_address
,
3755 mem_info
->buffer_size
);
3761 return KERN_INVALID_ARGUMENT
;
3763 return KERN_SUCCESS
;
3766 int radar_20146450
= 1;
3770 task_flavor_t flavor
,
3771 task_info_t task_info_out
,
3772 mach_msg_type_number_t
*task_info_count
)
3774 kern_return_t error
= KERN_SUCCESS
;
3775 mach_msg_type_number_t original_task_info_count
;
3777 if (task
== TASK_NULL
) {
3778 return KERN_INVALID_ARGUMENT
;
3781 original_task_info_count
= *task_info_count
;
3784 if ((task
!= current_task()) && (!task
->active
)) {
3786 return KERN_INVALID_ARGUMENT
;
3790 case TASK_BASIC_INFO_32
:
3791 case TASK_BASIC2_INFO_32
:
3792 #if defined(__arm__) || defined(__arm64__)
3793 case TASK_BASIC_INFO_64
:
3796 task_basic_info_32_t basic_info
;
3801 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
) {
3802 error
= KERN_INVALID_ARGUMENT
;
3806 basic_info
= (task_basic_info_32_t
)task_info_out
;
3808 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3809 basic_info
->virtual_size
= (typeof(basic_info
->virtual_size
))map
->size
;
3810 if (flavor
== TASK_BASIC2_INFO_32
) {
3812 * The "BASIC2" flavor gets the maximum resident
3813 * size instead of the current resident size...
3815 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
3817 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
3819 basic_info
->resident_size
*= PAGE_SIZE
;
3821 basic_info
->policy
= ((task
!= kernel_task
)?
3822 POLICY_TIMESHARE
: POLICY_RR
);
3823 basic_info
->suspend_count
= task
->user_stop_count
;
3825 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3826 basic_info
->user_time
.seconds
=
3827 (typeof(basic_info
->user_time
.seconds
))secs
;
3828 basic_info
->user_time
.microseconds
= usecs
;
3830 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3831 basic_info
->system_time
.seconds
=
3832 (typeof(basic_info
->system_time
.seconds
))secs
;
3833 basic_info
->system_time
.microseconds
= usecs
;
3835 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
3839 #if defined(__arm__) || defined(__arm64__)
3840 case TASK_BASIC_INFO_64_2
:
3842 task_basic_info_64_2_t basic_info
;
3847 if (*task_info_count
< TASK_BASIC_INFO_64_2_COUNT
) {
3848 error
= KERN_INVALID_ARGUMENT
;
3852 basic_info
= (task_basic_info_64_2_t
)task_info_out
;
3854 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3855 basic_info
->virtual_size
= map
->size
;
3856 basic_info
->resident_size
=
3857 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
3860 basic_info
->policy
= ((task
!= kernel_task
)?
3861 POLICY_TIMESHARE
: POLICY_RR
);
3862 basic_info
->suspend_count
= task
->user_stop_count
;
3864 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3865 basic_info
->user_time
.seconds
=
3866 (typeof(basic_info
->user_time
.seconds
))secs
;
3867 basic_info
->user_time
.microseconds
= usecs
;
3869 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3870 basic_info
->system_time
.seconds
=
3871 (typeof(basic_info
->system_time
.seconds
))secs
;
3872 basic_info
->system_time
.microseconds
= usecs
;
3874 *task_info_count
= TASK_BASIC_INFO_64_2_COUNT
;
3878 #else /* defined(__arm__) || defined(__arm64__) */
3879 case TASK_BASIC_INFO_64
:
3881 task_basic_info_64_t basic_info
;
3886 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
) {
3887 error
= KERN_INVALID_ARGUMENT
;
3891 basic_info
= (task_basic_info_64_t
)task_info_out
;
3893 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3894 basic_info
->virtual_size
= map
->size
;
3895 basic_info
->resident_size
=
3896 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
3899 basic_info
->policy
= ((task
!= kernel_task
)?
3900 POLICY_TIMESHARE
: POLICY_RR
);
3901 basic_info
->suspend_count
= task
->user_stop_count
;
3903 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3904 basic_info
->user_time
.seconds
=
3905 (typeof(basic_info
->user_time
.seconds
))secs
;
3906 basic_info
->user_time
.microseconds
= usecs
;
3908 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3909 basic_info
->system_time
.seconds
=
3910 (typeof(basic_info
->system_time
.seconds
))secs
;
3911 basic_info
->system_time
.microseconds
= usecs
;
3913 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
3916 #endif /* defined(__arm__) || defined(__arm64__) */
3918 case MACH_TASK_BASIC_INFO
:
3920 mach_task_basic_info_t basic_info
;
3925 if (*task_info_count
< MACH_TASK_BASIC_INFO_COUNT
) {
3926 error
= KERN_INVALID_ARGUMENT
;
3930 basic_info
= (mach_task_basic_info_t
)task_info_out
;
3932 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
3934 basic_info
->virtual_size
= map
->size
;
3936 basic_info
->resident_size
=
3937 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
));
3938 basic_info
->resident_size
*= PAGE_SIZE_64
;
3940 basic_info
->resident_size_max
=
3941 (mach_vm_size_t
)(pmap_resident_max(map
->pmap
));
3942 basic_info
->resident_size_max
*= PAGE_SIZE_64
;
3944 basic_info
->policy
= ((task
!= kernel_task
) ?
3945 POLICY_TIMESHARE
: POLICY_RR
);
3947 basic_info
->suspend_count
= task
->user_stop_count
;
3949 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3950 basic_info
->user_time
.seconds
=
3951 (typeof(basic_info
->user_time
.seconds
))secs
;
3952 basic_info
->user_time
.microseconds
= usecs
;
3954 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3955 basic_info
->system_time
.seconds
=
3956 (typeof(basic_info
->system_time
.seconds
))secs
;
3957 basic_info
->system_time
.microseconds
= usecs
;
3959 *task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
3963 case TASK_THREAD_TIMES_INFO
:
3965 task_thread_times_info_t times_info
;
3968 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
3969 error
= KERN_INVALID_ARGUMENT
;
3973 times_info
= (task_thread_times_info_t
) task_info_out
;
3974 times_info
->user_time
.seconds
= 0;
3975 times_info
->user_time
.microseconds
= 0;
3976 times_info
->system_time
.seconds
= 0;
3977 times_info
->system_time
.microseconds
= 0;
3980 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3981 time_value_t user_time
, system_time
;
3983 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
3987 thread_read_times(thread
, &user_time
, &system_time
, NULL
);
3989 time_value_add(×_info
->user_time
, &user_time
);
3990 time_value_add(×_info
->system_time
, &system_time
);
3993 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
3997 case TASK_ABSOLUTETIME_INFO
:
3999 task_absolutetime_info_t info
;
4002 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
) {
4003 error
= KERN_INVALID_ARGUMENT
;
4007 info
= (task_absolutetime_info_t
)task_info_out
;
4008 info
->threads_user
= info
->threads_system
= 0;
4011 info
->total_user
= task
->total_user_time
;
4012 info
->total_system
= task
->total_system_time
;
4014 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4018 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
4023 thread_lock(thread
);
4025 tval
= timer_grab(&thread
->user_timer
);
4026 info
->threads_user
+= tval
;
4027 info
->total_user
+= tval
;
4029 tval
= timer_grab(&thread
->system_timer
);
4030 if (thread
->precise_user_kernel_time
) {
4031 info
->threads_system
+= tval
;
4032 info
->total_system
+= tval
;
4034 /* system_timer may represent either sys or user */
4035 info
->threads_user
+= tval
;
4036 info
->total_user
+= tval
;
4039 thread_unlock(thread
);
4044 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
4048 case TASK_DYLD_INFO
:
4050 task_dyld_info_t info
;
4053 * We added the format field to TASK_DYLD_INFO output. For
4054 * temporary backward compatibility, accept the fact that
4055 * clients may ask for the old version - distinquished by the
4056 * size of the expected result structure.
4058 #define TASK_LEGACY_DYLD_INFO_COUNT \
4059 offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
4061 if (*task_info_count
< TASK_LEGACY_DYLD_INFO_COUNT
) {
4062 error
= KERN_INVALID_ARGUMENT
;
4066 info
= (task_dyld_info_t
)task_info_out
;
4067 info
->all_image_info_addr
= task
->all_image_info_addr
;
4068 info
->all_image_info_size
= task
->all_image_info_size
;
4070 /* only set format on output for those expecting it */
4071 if (*task_info_count
>= TASK_DYLD_INFO_COUNT
) {
4072 info
->all_image_info_format
= task_has_64Bit_addr(task
) ?
4073 TASK_DYLD_ALL_IMAGE_INFO_64
:
4074 TASK_DYLD_ALL_IMAGE_INFO_32
;
4075 *task_info_count
= TASK_DYLD_INFO_COUNT
;
4077 *task_info_count
= TASK_LEGACY_DYLD_INFO_COUNT
;
4082 case TASK_EXTMOD_INFO
:
4084 task_extmod_info_t info
;
4087 if (*task_info_count
< TASK_EXTMOD_INFO_COUNT
) {
4088 error
= KERN_INVALID_ARGUMENT
;
4092 info
= (task_extmod_info_t
)task_info_out
;
4094 p
= get_bsdtask_info(task
);
4096 proc_getexecutableuuid(p
, info
->task_uuid
, sizeof(info
->task_uuid
));
4098 bzero(info
->task_uuid
, sizeof(info
->task_uuid
));
4100 info
->extmod_statistics
= task
->extmod_statistics
;
4101 *task_info_count
= TASK_EXTMOD_INFO_COUNT
;
4106 case TASK_KERNELMEMORY_INFO
:
4108 task_kernelmemory_info_t tkm_info
;
4109 ledger_amount_t credit
, debit
;
4111 if (*task_info_count
< TASK_KERNELMEMORY_INFO_COUNT
) {
4112 error
= KERN_INVALID_ARGUMENT
;
4116 tkm_info
= (task_kernelmemory_info_t
) task_info_out
;
4117 tkm_info
->total_palloc
= 0;
4118 tkm_info
->total_pfree
= 0;
4119 tkm_info
->total_salloc
= 0;
4120 tkm_info
->total_sfree
= 0;
4122 if (task
== kernel_task
) {
4124 * All shared allocs/frees from other tasks count against
4125 * the kernel private memory usage. If we are looking up
4126 * info for the kernel task, gather from everywhere.
4130 /* start by accounting for all the terminated tasks against the kernel */
4131 tkm_info
->total_palloc
= tasks_tkm_private
.alloc
+ tasks_tkm_shared
.alloc
;
4132 tkm_info
->total_pfree
= tasks_tkm_private
.free
+ tasks_tkm_shared
.free
;
4134 /* count all other task/thread shared alloc/free against the kernel */
4135 lck_mtx_lock(&tasks_threads_lock
);
4137 /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
4138 queue_iterate(&tasks
, task
, task_t
, tasks
) {
4139 if (task
== kernel_task
) {
4140 if (ledger_get_entries(task
->ledger
,
4141 task_ledgers
.tkm_private
, &credit
,
4142 &debit
) == KERN_SUCCESS
) {
4143 tkm_info
->total_palloc
+= credit
;
4144 tkm_info
->total_pfree
+= debit
;
4147 if (!ledger_get_entries(task
->ledger
,
4148 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
4149 tkm_info
->total_palloc
+= credit
;
4150 tkm_info
->total_pfree
+= debit
;
4153 lck_mtx_unlock(&tasks_threads_lock
);
4155 if (!ledger_get_entries(task
->ledger
,
4156 task_ledgers
.tkm_private
, &credit
, &debit
)) {
4157 tkm_info
->total_palloc
= credit
;
4158 tkm_info
->total_pfree
= debit
;
4160 if (!ledger_get_entries(task
->ledger
,
4161 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
4162 tkm_info
->total_salloc
= credit
;
4163 tkm_info
->total_sfree
= debit
;
4168 *task_info_count
= TASK_KERNELMEMORY_INFO_COUNT
;
4169 return KERN_SUCCESS
;
4173 case TASK_SCHED_FIFO_INFO
:
4175 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
) {
4176 error
= KERN_INVALID_ARGUMENT
;
4180 error
= KERN_INVALID_POLICY
;
4185 case TASK_SCHED_RR_INFO
:
4187 policy_rr_base_t rr_base
;
4188 uint32_t quantum_time
;
4189 uint64_t quantum_ns
;
4191 if (*task_info_count
< POLICY_RR_BASE_COUNT
) {
4192 error
= KERN_INVALID_ARGUMENT
;
4196 rr_base
= (policy_rr_base_t
) task_info_out
;
4198 if (task
!= kernel_task
) {
4199 error
= KERN_INVALID_POLICY
;
4203 rr_base
->base_priority
= task
->priority
;
4205 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
4206 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
4208 rr_base
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
4210 *task_info_count
= POLICY_RR_BASE_COUNT
;
4215 case TASK_SCHED_TIMESHARE_INFO
:
4217 policy_timeshare_base_t ts_base
;
4219 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
) {
4220 error
= KERN_INVALID_ARGUMENT
;
4224 ts_base
= (policy_timeshare_base_t
) task_info_out
;
4226 if (task
== kernel_task
) {
4227 error
= KERN_INVALID_POLICY
;
4231 ts_base
->base_priority
= task
->priority
;
4233 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
4237 case TASK_SECURITY_TOKEN
:
4239 security_token_t
*sec_token_p
;
4241 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
4242 error
= KERN_INVALID_ARGUMENT
;
4246 sec_token_p
= (security_token_t
*) task_info_out
;
4248 *sec_token_p
= task
->sec_token
;
4250 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
4254 case TASK_AUDIT_TOKEN
:
4256 audit_token_t
*audit_token_p
;
4258 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
4259 error
= KERN_INVALID_ARGUMENT
;
4263 audit_token_p
= (audit_token_t
*) task_info_out
;
4265 *audit_token_p
= task
->audit_token
;
4267 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
4271 case TASK_SCHED_INFO
:
4272 error
= KERN_INVALID_ARGUMENT
;
4275 case TASK_EVENTS_INFO
:
4277 task_events_info_t events_info
;
4280 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
4281 error
= KERN_INVALID_ARGUMENT
;
4285 events_info
= (task_events_info_t
) task_info_out
;
4288 events_info
->faults
= task
->faults
;
4289 events_info
->pageins
= task
->pageins
;
4290 events_info
->cow_faults
= task
->cow_faults
;
4291 events_info
->messages_sent
= task
->messages_sent
;
4292 events_info
->messages_received
= task
->messages_received
;
4293 events_info
->syscalls_mach
= task
->syscalls_mach
;
4294 events_info
->syscalls_unix
= task
->syscalls_unix
;
4296 events_info
->csw
= task
->c_switch
;
4298 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4299 events_info
->csw
+= thread
->c_switch
;
4300 events_info
->syscalls_mach
+= thread
->syscalls_mach
;
4301 events_info
->syscalls_unix
+= thread
->syscalls_unix
;
4305 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
4308 case TASK_AFFINITY_TAG_INFO
:
4310 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
) {
4311 error
= KERN_INVALID_ARGUMENT
;
4315 error
= task_affinity_info(task
, task_info_out
, task_info_count
);
4318 case TASK_POWER_INFO
:
4320 if (*task_info_count
< TASK_POWER_INFO_COUNT
) {
4321 error
= KERN_INVALID_ARGUMENT
;
4325 task_power_info_locked(task
, (task_power_info_t
)task_info_out
, NULL
, NULL
);
4329 case TASK_POWER_INFO_V2
:
4331 if (*task_info_count
< TASK_POWER_INFO_V2_COUNT_OLD
) {
4332 error
= KERN_INVALID_ARGUMENT
;
4335 task_power_info_v2_t tpiv2
= (task_power_info_v2_t
) task_info_out
;
4336 task_power_info_locked(task
, &tpiv2
->cpu_energy
, &tpiv2
->gpu_energy
, tpiv2
);
4341 case TASK_VM_INFO_PURGEABLE
:
4343 task_vm_info_t vm_info
;
4346 if (*task_info_count
< TASK_VM_INFO_REV0_COUNT
) {
4347 error
= KERN_INVALID_ARGUMENT
;
4351 vm_info
= (task_vm_info_t
)task_info_out
;
4353 if (task
== kernel_task
) {
4358 vm_map_lock_read(map
);
4361 vm_info
->virtual_size
= (typeof(vm_info
->virtual_size
))map
->size
;
4362 vm_info
->region_count
= map
->hdr
.nentries
;
4363 vm_info
->page_size
= vm_map_page_size(map
);
4365 vm_info
->resident_size
= pmap_resident_count(map
->pmap
);
4366 vm_info
->resident_size
*= PAGE_SIZE
;
4367 vm_info
->resident_size_peak
= pmap_resident_max(map
->pmap
);
4368 vm_info
->resident_size_peak
*= PAGE_SIZE
;
4370 #define _VM_INFO(_name) \
4371 vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
4374 _VM_INFO(device_peak
);
4376 _VM_INFO(external_peak
);
4378 _VM_INFO(internal_peak
);
4380 _VM_INFO(reusable_peak
);
4381 _VM_INFO(compressed
);
4382 _VM_INFO(compressed_peak
);
4383 _VM_INFO(compressed_lifetime
);
4385 vm_info
->purgeable_volatile_pmap
= 0;
4386 vm_info
->purgeable_volatile_resident
= 0;
4387 vm_info
->purgeable_volatile_virtual
= 0;
4388 if (task
== kernel_task
) {
4390 * We do not maintain the detailed stats for the
4391 * kernel_pmap, so just count everything as
4394 vm_info
->internal
= vm_info
->resident_size
;
4396 * ... but since the memory held by the VM compressor
4397 * in the kernel address space ought to be attributed
4398 * to user-space tasks, we subtract it from "internal"
4399 * to give memory reporting tools a more accurate idea
4400 * of what the kernel itself is actually using, instead
4401 * of making it look like the kernel is leaking memory
4402 * when the system is under memory pressure.
4404 vm_info
->internal
-= (VM_PAGE_COMPRESSOR_COUNT
*
4407 mach_vm_size_t volatile_virtual_size
;
4408 mach_vm_size_t volatile_resident_size
;
4409 mach_vm_size_t volatile_compressed_size
;
4410 mach_vm_size_t volatile_pmap_size
;
4411 mach_vm_size_t volatile_compressed_pmap_size
;
4414 if (flavor
== TASK_VM_INFO_PURGEABLE
) {
4415 kr
= vm_map_query_volatile(
4417 &volatile_virtual_size
,
4418 &volatile_resident_size
,
4419 &volatile_compressed_size
,
4420 &volatile_pmap_size
,
4421 &volatile_compressed_pmap_size
);
4422 if (kr
== KERN_SUCCESS
) {
4423 vm_info
->purgeable_volatile_pmap
=
4425 if (radar_20146450
) {
4426 vm_info
->compressed
-=
4427 volatile_compressed_pmap_size
;
4429 vm_info
->purgeable_volatile_resident
=
4430 volatile_resident_size
;
4431 vm_info
->purgeable_volatile_virtual
=
4432 volatile_virtual_size
;
4436 *task_info_count
= TASK_VM_INFO_REV0_COUNT
;
4438 if (original_task_info_count
>= TASK_VM_INFO_REV1_COUNT
) {
4439 vm_info
->phys_footprint
=
4440 (mach_vm_size_t
) get_task_phys_footprint(task
);
4441 *task_info_count
= TASK_VM_INFO_REV1_COUNT
;
4443 if (original_task_info_count
>= TASK_VM_INFO_REV2_COUNT
) {
4444 vm_info
->min_address
= map
->min_offset
;
4445 vm_info
->max_address
= map
->max_offset
;
4446 *task_info_count
= TASK_VM_INFO_REV2_COUNT
;
4449 if (task
!= kernel_task
) {
4450 vm_map_unlock_read(map
);
4456 case TASK_WAIT_STATE_INFO
:
4459 * Deprecated flavor. Currently allowing some results until all users
4460 * stop calling it. The results may not be accurate.
4462 task_wait_state_info_t wait_state_info
;
4463 uint64_t total_sfi_ledger_val
= 0;
4465 if (*task_info_count
< TASK_WAIT_STATE_INFO_COUNT
) {
4466 error
= KERN_INVALID_ARGUMENT
;
4470 wait_state_info
= (task_wait_state_info_t
) task_info_out
;
4472 wait_state_info
->total_wait_state_time
= 0;
4473 bzero(wait_state_info
->_reserved
, sizeof(wait_state_info
->_reserved
));
4475 #if CONFIG_SCHED_SFI
4476 int i
, prev_lentry
= -1;
4477 int64_t val_credit
, val_debit
;
4479 for (i
= 0; i
< MAX_SFI_CLASS_ID
; i
++) {
4482 * checking with prev_lentry != entry ensures adjacent classes
4483 * which share the same ledger do not add wait times twice.
4484 * Note: Use ledger() call to get data for each individual sfi class.
4486 if (prev_lentry
!= task_ledgers
.sfi_wait_times
[i
] &&
4487 KERN_SUCCESS
== ledger_get_entries(task
->ledger
,
4488 task_ledgers
.sfi_wait_times
[i
], &val_credit
, &val_debit
)) {
4489 total_sfi_ledger_val
+= val_credit
;
4491 prev_lentry
= task_ledgers
.sfi_wait_times
[i
];
4494 #endif /* CONFIG_SCHED_SFI */
4495 wait_state_info
->total_wait_sfi_state_time
= total_sfi_ledger_val
;
4496 *task_info_count
= TASK_WAIT_STATE_INFO_COUNT
;
4500 case TASK_VM_INFO_PURGEABLE_ACCOUNT
:
4502 #if DEVELOPMENT || DEBUG
4503 pvm_account_info_t acnt_info
;
4505 if (*task_info_count
< PVM_ACCOUNT_INFO_COUNT
) {
4506 error
= KERN_INVALID_ARGUMENT
;
4510 if (task_info_out
== NULL
) {
4511 error
= KERN_INVALID_ARGUMENT
;
4515 acnt_info
= (pvm_account_info_t
) task_info_out
;
4517 error
= vm_purgeable_account(task
, acnt_info
);
4519 *task_info_count
= PVM_ACCOUNT_INFO_COUNT
;
4522 #else /* DEVELOPMENT || DEBUG */
4523 error
= KERN_NOT_SUPPORTED
;
4525 #endif /* DEVELOPMENT || DEBUG */
4527 case TASK_FLAGS_INFO
:
4529 task_flags_info_t flags_info
;
4531 if (*task_info_count
< TASK_FLAGS_INFO_COUNT
) {
4532 error
= KERN_INVALID_ARGUMENT
;
4536 flags_info
= (task_flags_info_t
)task_info_out
;
4538 /* only publish the 64-bit flag of the task */
4539 flags_info
->flags
= task
->t_flags
& (TF_64B_ADDR
| TF_64B_DATA
);
4541 *task_info_count
= TASK_FLAGS_INFO_COUNT
;
4545 case TASK_DEBUG_INFO_INTERNAL
:
4547 #if DEVELOPMENT || DEBUG
4548 task_debug_info_internal_t dbg_info
;
4549 if (*task_info_count
< TASK_DEBUG_INFO_INTERNAL_COUNT
) {
4550 error
= KERN_NOT_SUPPORTED
;
4554 if (task_info_out
== NULL
) {
4555 error
= KERN_INVALID_ARGUMENT
;
4558 dbg_info
= (task_debug_info_internal_t
) task_info_out
;
4559 dbg_info
->ipc_space_size
= 0;
4560 if (task
->itk_space
) {
4561 dbg_info
->ipc_space_size
= task
->itk_space
->is_table_size
;
4564 dbg_info
->suspend_count
= task
->suspend_count
;
4566 error
= KERN_SUCCESS
;
4567 *task_info_count
= TASK_DEBUG_INFO_INTERNAL_COUNT
;
4569 #else /* DEVELOPMENT || DEBUG */
4570 error
= KERN_NOT_SUPPORTED
;
4572 #endif /* DEVELOPMENT || DEBUG */
4575 error
= KERN_INVALID_ARGUMENT
;
4583 * task_info_from_user
4585 * When calling task_info from user space,
4586 * this function will be executed as mig server side
4587 * instead of calling directly into task_info.
4588 * This gives the possibility to perform more security
4589 * checks on task_port.
4591 * In the case of TASK_DYLD_INFO, we require the more
4592 * privileged task_port not the less-privileged task_name_port.
4596 task_info_from_user(
4597 mach_port_t task_port
,
4598 task_flavor_t flavor
,
4599 task_info_t task_info_out
,
4600 mach_msg_type_number_t
*task_info_count
)
4605 if (flavor
== TASK_DYLD_INFO
) {
4606 task
= convert_port_to_task(task_port
);
4608 task
= convert_port_to_task_name(task_port
);
4611 ret
= task_info(task
, flavor
, task_info_out
, task_info_count
);
4613 task_deallocate(task
);
4621 * Returns power stats for the task.
4622 * Note: Called with task locked.
4625 task_power_info_locked(
4627 task_power_info_t info
,
4628 gpu_energy_data_t ginfo
,
4629 task_power_info_v2_t infov2
)
4632 ledger_amount_t tmp
;
4634 task_lock_assert_owned(task
);
4636 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
4637 (ledger_amount_t
*)&info
->task_interrupt_wakeups
, &tmp
);
4638 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
4639 (ledger_amount_t
*)&info
->task_platform_idle_wakeups
, &tmp
);
4641 info
->task_timer_wakeups_bin_1
= task
->task_timer_wakeups_bin_1
;
4642 info
->task_timer_wakeups_bin_2
= task
->task_timer_wakeups_bin_2
;
4644 info
->total_user
= task
->total_user_time
;
4645 info
->total_system
= task
->total_system_time
;
4649 infov2
->task_energy
= task
->task_energy
;
4654 ginfo
->task_gpu_utilisation
= task
->task_gpu_ns
;
4658 infov2
->task_ptime
= task
->total_ptime
;
4659 infov2
->task_pset_switches
= task
->ps_switch
;
4662 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4666 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
4671 thread_lock(thread
);
4673 info
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
4674 info
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
4678 infov2
->task_energy
+= ml_energy_stat(thread
);
4682 tval
= timer_grab(&thread
->user_timer
);
4683 info
->total_user
+= tval
;
4686 tval
= timer_grab(&thread
->ptime
);
4687 infov2
->task_ptime
+= tval
;
4688 infov2
->task_pset_switches
+= thread
->ps_switch
;
4691 tval
= timer_grab(&thread
->system_timer
);
4692 if (thread
->precise_user_kernel_time
) {
4693 info
->total_system
+= tval
;
4695 /* system_timer may represent either sys or user */
4696 info
->total_user
+= tval
;
4700 ginfo
->task_gpu_utilisation
+= ml_gpu_stat(thread
);
4702 thread_unlock(thread
);
4708 * task_gpu_utilisation
4710 * Returns the total gpu time used by the all the threads of the task
4711 * (both dead and alive)
4714 task_gpu_utilisation(
4717 uint64_t gpu_time
= 0;
4718 #if !CONFIG_EMBEDDED
4722 gpu_time
+= task
->task_gpu_ns
;
4724 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4727 thread_lock(thread
);
4728 gpu_time
+= ml_gpu_stat(thread
);
4729 thread_unlock(thread
);
4734 #else /* CONFIG_EMBEDDED */
4735 /* silence compiler warning */
4737 #endif /* !CONFIG_EMBEDDED */
4744 * Returns the total energy used by the all the threads of the task
4745 * (both dead and alive)
4751 uint64_t energy
= 0;
4755 energy
+= task
->task_energy
;
4757 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4760 thread_lock(thread
);
4761 energy
+= ml_energy_stat(thread
);
4762 thread_unlock(thread
);
4773 __unused task_t task
)
4779 /* This function updates the cpu time in the arrays for each
4780 * effective and requested QoS class
4783 task_update_cpu_time_qos_stats(
4785 uint64_t *eqos_stats
,
4786 uint64_t *rqos_stats
)
4788 if (!eqos_stats
&& !rqos_stats
) {
4794 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4795 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
4799 thread_update_qos_cpu_time(thread
);
4803 eqos_stats
[THREAD_QOS_DEFAULT
] += task
->cpu_time_eqos_stats
.cpu_time_qos_default
;
4804 eqos_stats
[THREAD_QOS_MAINTENANCE
] += task
->cpu_time_eqos_stats
.cpu_time_qos_maintenance
;
4805 eqos_stats
[THREAD_QOS_BACKGROUND
] += task
->cpu_time_eqos_stats
.cpu_time_qos_background
;
4806 eqos_stats
[THREAD_QOS_UTILITY
] += task
->cpu_time_eqos_stats
.cpu_time_qos_utility
;
4807 eqos_stats
[THREAD_QOS_LEGACY
] += task
->cpu_time_eqos_stats
.cpu_time_qos_legacy
;
4808 eqos_stats
[THREAD_QOS_USER_INITIATED
] += task
->cpu_time_eqos_stats
.cpu_time_qos_user_initiated
;
4809 eqos_stats
[THREAD_QOS_USER_INTERACTIVE
] += task
->cpu_time_eqos_stats
.cpu_time_qos_user_interactive
;
4813 rqos_stats
[THREAD_QOS_DEFAULT
] += task
->cpu_time_rqos_stats
.cpu_time_qos_default
;
4814 rqos_stats
[THREAD_QOS_MAINTENANCE
] += task
->cpu_time_rqos_stats
.cpu_time_qos_maintenance
;
4815 rqos_stats
[THREAD_QOS_BACKGROUND
] += task
->cpu_time_rqos_stats
.cpu_time_qos_background
;
4816 rqos_stats
[THREAD_QOS_UTILITY
] += task
->cpu_time_rqos_stats
.cpu_time_qos_utility
;
4817 rqos_stats
[THREAD_QOS_LEGACY
] += task
->cpu_time_rqos_stats
.cpu_time_qos_legacy
;
4818 rqos_stats
[THREAD_QOS_USER_INITIATED
] += task
->cpu_time_rqos_stats
.cpu_time_qos_user_initiated
;
4819 rqos_stats
[THREAD_QOS_USER_INTERACTIVE
] += task
->cpu_time_rqos_stats
.cpu_time_qos_user_interactive
;
4828 task_purgable_info_t
*stats
)
4830 if (task
== TASK_NULL
|| stats
== NULL
) {
4831 return KERN_INVALID_ARGUMENT
;
4833 /* Take task reference */
4834 task_reference(task
);
4835 vm_purgeable_stats((vm_purgeable_info_t
)stats
, task
);
4836 /* Drop task reference */
4837 task_deallocate(task
);
4838 return KERN_SUCCESS
;
4851 task
->vtimers
|= which
;
4854 case TASK_VTIMER_USER
:
4855 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4857 thread_lock(thread
);
4858 if (thread
->precise_user_kernel_time
) {
4859 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
4861 thread
->vtimer_user_save
= timer_grab(&thread
->system_timer
);
4863 thread_unlock(thread
);
4868 case TASK_VTIMER_PROF
:
4869 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4871 thread_lock(thread
);
4872 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
4873 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
4874 thread_unlock(thread
);
4879 case TASK_VTIMER_RLIM
:
4880 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4882 thread_lock(thread
);
4883 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
4884 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
4885 thread_unlock(thread
);
4899 assert(task
== current_task());
4903 task
->vtimers
&= ~which
;
4913 uint32_t *microsecs
)
4915 thread_t thread
= current_thread();
4917 clock_sec_t secs
= 0;
4920 assert(task
== current_task());
4922 spl_t s
= splsched();
4923 thread_lock(thread
);
4925 if ((task
->vtimers
& which
) != (uint32_t)which
) {
4926 thread_unlock(thread
);
4932 case TASK_VTIMER_USER
:
4933 if (thread
->precise_user_kernel_time
) {
4934 tdelt
= (uint32_t)timer_delta(&thread
->user_timer
,
4935 &thread
->vtimer_user_save
);
4937 tdelt
= (uint32_t)timer_delta(&thread
->system_timer
,
4938 &thread
->vtimer_user_save
);
4940 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4943 case TASK_VTIMER_PROF
:
4944 tsum
= timer_grab(&thread
->user_timer
);
4945 tsum
+= timer_grab(&thread
->system_timer
);
4946 tdelt
= (uint32_t)(tsum
- thread
->vtimer_prof_save
);
4947 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4948 /* if the time delta is smaller than a usec, ignore */
4949 if (*microsecs
!= 0) {
4950 thread
->vtimer_prof_save
= tsum
;
4954 case TASK_VTIMER_RLIM
:
4955 tsum
= timer_grab(&thread
->user_timer
);
4956 tsum
+= timer_grab(&thread
->system_timer
);
4957 tdelt
= (uint32_t)(tsum
- thread
->vtimer_rlim_save
);
4958 thread
->vtimer_rlim_save
= tsum
;
4959 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4963 thread_unlock(thread
);
4970 * Change the assigned processor set for the task
4974 __unused task_t task
,
4975 __unused processor_set_t new_pset
,
4976 __unused boolean_t assign_threads
)
4978 return KERN_FAILURE
;
4982 * task_assign_default:
4984 * Version of task_assign to assign to default processor set.
4987 task_assign_default(
4989 boolean_t assign_threads
)
4991 return task_assign(task
, &pset0
, assign_threads
);
4995 * task_get_assignment
4997 * Return name of processor set that task is assigned to.
5000 task_get_assignment(
5002 processor_set_t
*pset
)
5004 if (!task
|| !task
->active
) {
5005 return KERN_FAILURE
;
5010 return KERN_SUCCESS
;
5014 get_task_dispatchqueue_offset(
5017 return task
->dispatchqueue_offset
;
5023 * Set scheduling policy and parameters, both base and limit, for
5024 * the given task. Policy must be a policy which is enabled for the
5025 * processor set. Change contained threads if requested.
5029 __unused task_t task
,
5030 __unused policy_t policy_id
,
5031 __unused policy_base_t base
,
5032 __unused mach_msg_type_number_t count
,
5033 __unused boolean_t set_limit
,
5034 __unused boolean_t change
)
5036 return KERN_FAILURE
;
5042 * Set scheduling policy and parameters, both base and limit, for
5043 * the given task. Policy can be any policy implemented by the
5044 * processor set, whether enabled or not. Change contained threads
5049 __unused task_t task
,
5050 __unused processor_set_t pset
,
5051 __unused policy_t policy_id
,
5052 __unused policy_base_t base
,
5053 __unused mach_msg_type_number_t base_count
,
5054 __unused policy_limit_t limit
,
5055 __unused mach_msg_type_number_t limit_count
,
5056 __unused boolean_t change
)
5058 return KERN_FAILURE
;
5063 __unused task_t task
,
5064 __unused vm_offset_t pc
,
5065 __unused vm_offset_t endpc
)
5067 return KERN_FAILURE
;
5071 task_synchronizer_destroy_all(task_t task
)
5074 * Destroy owned semaphores
5076 semaphore_destroy_all(task
);
5080 * Install default (machine-dependent) initial thread state
5081 * on the task. Subsequent thread creation will have this initial
5082 * state set on the thread by machine_thread_inherit_taskwide().
5083 * Flavors and structures are exactly the same as those to thread_set_state()
5089 thread_state_t state
,
5090 mach_msg_type_number_t state_count
)
5094 if (task
== TASK_NULL
) {
5095 return KERN_INVALID_ARGUMENT
;
5100 if (!task
->active
) {
5102 return KERN_FAILURE
;
5105 ret
= machine_task_set_state(task
, flavor
, state
, state_count
);
5112 * Examine the default (machine-dependent) initial thread state
5113 * on the task, as set by task_set_state(). Flavors and structures
5114 * are exactly the same as those passed to thread_get_state().
5120 thread_state_t state
,
5121 mach_msg_type_number_t
*state_count
)
5125 if (task
== TASK_NULL
) {
5126 return KERN_INVALID_ARGUMENT
;
5131 if (!task
->active
) {
5133 return KERN_FAILURE
;
5136 ret
= machine_task_get_state(task
, flavor
, state
, state_count
);
5143 static kern_return_t
__attribute__((noinline
, not_tail_called
))
5144 PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(
5145 mach_exception_code_t code
,
5146 mach_exception_subcode_t subcode
,
5150 if (1 == proc_selfpid()) {
5151 return KERN_NOT_SUPPORTED
; // initproc is immune
5154 mach_exception_data_type_t codes
[EXCEPTION_CODE_MAX
] = {
5158 task_t task
= current_task();
5161 /* (See jetsam-related comments below) */
5163 proc_memstat_terminated(task
->bsd_info
, TRUE
);
5164 kr
= task_enqueue_exception_with_corpse(task
, EXC_GUARD
, codes
, 2, reason
);
5165 proc_memstat_terminated(task
->bsd_info
, FALSE
);
5170 task_violated_guard(
5171 mach_exception_code_t code
,
5172 mach_exception_subcode_t subcode
,
5175 return PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(code
, subcode
, reason
);
5179 #if CONFIG_MEMORYSTATUS
5182 task_get_memlimit_is_active(task_t task
)
5184 assert(task
!= NULL
);
5186 if (task
->memlimit_is_active
== 1) {
5194 task_set_memlimit_is_active(task_t task
, boolean_t memlimit_is_active
)
5196 assert(task
!= NULL
);
5198 if (memlimit_is_active
) {
5199 task
->memlimit_is_active
= 1;
5201 task
->memlimit_is_active
= 0;
5206 task_get_memlimit_is_fatal(task_t task
)
5208 assert(task
!= NULL
);
5210 if (task
->memlimit_is_fatal
== 1) {
5218 task_set_memlimit_is_fatal(task_t task
, boolean_t memlimit_is_fatal
)
5220 assert(task
!= NULL
);
5222 if (memlimit_is_fatal
) {
5223 task
->memlimit_is_fatal
= 1;
5225 task
->memlimit_is_fatal
= 0;
5230 task_has_triggered_exc_resource(task_t task
, boolean_t memlimit_is_active
)
5232 boolean_t triggered
= FALSE
;
5234 assert(task
== current_task());
5237 * Returns true, if task has already triggered an exc_resource exception.
5240 if (memlimit_is_active
) {
5241 triggered
= (task
->memlimit_active_exc_resource
? TRUE
: FALSE
);
5243 triggered
= (task
->memlimit_inactive_exc_resource
? TRUE
: FALSE
);
5250 task_mark_has_triggered_exc_resource(task_t task
, boolean_t memlimit_is_active
)
5252 assert(task
== current_task());
5255 * We allow one exc_resource per process per active/inactive limit.
5256 * The limit's fatal attribute does not come into play.
5259 if (memlimit_is_active
) {
5260 task
->memlimit_active_exc_resource
= 1;
5262 task
->memlimit_inactive_exc_resource
= 1;
5266 #define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
5268 void __attribute__((noinline
))
5269 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
)
5271 task_t task
= current_task();
5273 const char *procname
= "unknown";
5274 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5275 boolean_t send_sync_exc_resource
= FALSE
;
5278 pid
= proc_selfpid();
5282 * Cannot have ReportCrash analyzing
5283 * a suspended initproc.
5288 if (task
->bsd_info
!= NULL
) {
5289 procname
= proc_name_address(current_task()->bsd_info
);
5290 send_sync_exc_resource
= proc_send_synchronous_EXC_RESOURCE(current_task()->bsd_info
);
5294 if (hwm_user_cores
) {
5296 uint64_t starttime
, end
;
5297 clock_sec_t secs
= 0;
5298 uint32_t microsecs
= 0;
5300 starttime
= mach_absolute_time();
5302 * Trigger a coredump of this process. Don't proceed unless we know we won't
5303 * be filling up the disk; and ignore the core size resource limit for this
5306 if ((error
= coredump(current_task()->bsd_info
, HWM_USERCORE_MINSPACE
, COREDUMP_IGNORE_ULIMIT
)) != 0) {
5307 printf("couldn't take coredump of %s[%d]: %d\n", procname
, pid
, error
);
5310 * coredump() leaves the task suspended.
5312 task_resume_internal(current_task());
5314 end
= mach_absolute_time();
5315 absolutetime_to_microtime(end
- starttime
, &secs
, µsecs
);
5316 printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
5317 proc_name_address(current_task()->bsd_info
), pid
, (int)secs
, microsecs
);
5319 #endif /* CONFIG_COREDUMP */
5321 if (disable_exc_resource
) {
5322 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
5323 "supressed by a boot-arg.\n", procname
, pid
, max_footprint_mb
);
5328 * A task that has triggered an EXC_RESOURCE, should not be
5329 * jetsammed when the device is under memory pressure. Here
5330 * we set the P_MEMSTAT_TERMINATED flag so that the process
5331 * will be skipped if the memorystatus_thread wakes up.
5333 proc_memstat_terminated(current_task()->bsd_info
, TRUE
);
5335 code
[0] = code
[1] = 0;
5336 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_MEMORY
);
5337 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_HIGH_WATERMARK
);
5338 EXC_RESOURCE_HWM_ENCODE_LIMIT(code
[0], max_footprint_mb
);
5341 * Do not generate a corpse fork if the violation is a fatal one
5342 * or the process wants synchronous EXC_RESOURCE exceptions.
5344 if (is_fatal
|| send_sync_exc_resource
|| exc_via_corpse_forking
== 0) {
5345 /* Do not send a EXC_RESOURCE if corpse_for_fatal_memkill is set */
5346 if (send_sync_exc_resource
|| corpse_for_fatal_memkill
== 0) {
5348 * Use the _internal_ variant so that no user-space
5349 * process can resume our task from under us.
5351 task_suspend_internal(task
);
5352 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5353 task_resume_internal(task
);
5357 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
5358 "supressed due to audio playback.\n", procname
, pid
, max_footprint_mb
);
5360 task_enqueue_exception_with_corpse(task
, EXC_RESOURCE
,
5361 code
, EXCEPTION_CODE_MAX
, NULL
);
5366 * After the EXC_RESOURCE has been handled, we must clear the
5367 * P_MEMSTAT_TERMINATED flag so that the process can again be
5368 * considered for jetsam if the memorystatus_thread wakes up.
5370 proc_memstat_terminated(current_task()->bsd_info
, FALSE
); /* clear the flag */
5374 * Callback invoked when a task exceeds its physical footprint limit.
5377 task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
5379 ledger_amount_t max_footprint
, max_footprint_mb
;
5381 boolean_t is_warning
;
5382 boolean_t memlimit_is_active
;
5383 boolean_t memlimit_is_fatal
;
5385 if (warning
== LEDGER_WARNING_DIPPED_BELOW
) {
5387 * Task memory limits only provide a warning on the way up.
5390 } else if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
5392 * This task is in danger of violating a memory limit,
5393 * It has exceeded a percentage level of the limit.
5398 * The task has exceeded the physical footprint limit.
5399 * This is not a warning but a true limit violation.
5404 task
= current_task();
5406 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &max_footprint
);
5407 max_footprint_mb
= max_footprint
>> 20;
5409 memlimit_is_active
= task_get_memlimit_is_active(task
);
5410 memlimit_is_fatal
= task_get_memlimit_is_fatal(task
);
5413 * If this is an actual violation (not a warning), then generate EXC_RESOURCE exception.
5414 * We only generate the exception once per process per memlimit (active/inactive limit).
5415 * To enforce this, we monitor state based on the memlimit's active/inactive attribute
5416 * and we disable it by marking that memlimit as exception triggered.
5418 if ((is_warning
== FALSE
) && (!task_has_triggered_exc_resource(task
, memlimit_is_active
))) {
5419 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND((int)max_footprint_mb
, memlimit_is_fatal
);
5420 memorystatus_log_exception((int)max_footprint_mb
, memlimit_is_active
, memlimit_is_fatal
);
5421 task_mark_has_triggered_exc_resource(task
, memlimit_is_active
);
5424 memorystatus_on_ledger_footprint_exceeded(is_warning
, memlimit_is_active
, memlimit_is_fatal
);
5427 extern int proc_check_footprint_priv(void);
5430 task_set_phys_footprint_limit(
5435 kern_return_t error
;
5437 boolean_t memlimit_is_active
;
5438 boolean_t memlimit_is_fatal
;
5440 if ((error
= proc_check_footprint_priv())) {
5441 return KERN_NO_ACCESS
;
5445 * This call should probably be obsoleted.
5446 * But for now, we default to current state.
5448 memlimit_is_active
= task_get_memlimit_is_active(task
);
5449 memlimit_is_fatal
= task_get_memlimit_is_fatal(task
);
5451 return task_set_phys_footprint_limit_internal(task
, new_limit_mb
, old_limit_mb
, memlimit_is_active
, memlimit_is_fatal
);
5455 task_convert_phys_footprint_limit(
5457 int *converted_limit_mb
)
5459 if (limit_mb
== -1) {
5463 if (max_task_footprint
!= 0) {
5464 *converted_limit_mb
= (int)(max_task_footprint
/ 1024 / 1024); /* bytes to MB */
5466 *converted_limit_mb
= (int)(LEDGER_LIMIT_INFINITY
>> 20);
5469 /* nothing to convert */
5470 *converted_limit_mb
= limit_mb
;
5472 return KERN_SUCCESS
;
5477 task_set_phys_footprint_limit_internal(
5481 boolean_t memlimit_is_active
,
5482 boolean_t memlimit_is_fatal
)
5484 ledger_amount_t old
;
5486 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &old
);
5489 * Check that limit >> 20 will not give an "unexpected" 32-bit
5490 * result. There are, however, implicit assumptions that -1 mb limit
5491 * equates to LEDGER_LIMIT_INFINITY.
5493 assert(((old
& 0xFFF0000000000000LL
) == 0) || (old
== LEDGER_LIMIT_INFINITY
));
5496 *old_limit_mb
= (int)(old
>> 20);
5499 if (new_limit_mb
== -1) {
5501 * Caller wishes to remove the limit.
5503 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
5504 max_task_footprint
? max_task_footprint
: LEDGER_LIMIT_INFINITY
,
5505 max_task_footprint
? max_task_footprint_warning_level
: 0);
5508 task_set_memlimit_is_active(task
, memlimit_is_active
);
5509 task_set_memlimit_is_fatal(task
, memlimit_is_fatal
);
5512 return KERN_SUCCESS
;
5515 #ifdef CONFIG_NOMONITORS
5516 return KERN_SUCCESS
;
5517 #endif /* CONFIG_NOMONITORS */
5521 if ((memlimit_is_active
== task_get_memlimit_is_active(task
)) &&
5522 (memlimit_is_fatal
== task_get_memlimit_is_fatal(task
)) &&
5523 (((ledger_amount_t
)new_limit_mb
<< 20) == old
)) {
5525 * memlimit state is not changing
5528 return KERN_SUCCESS
;
5531 task_set_memlimit_is_active(task
, memlimit_is_active
);
5532 task_set_memlimit_is_fatal(task
, memlimit_is_fatal
);
5534 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
5535 (ledger_amount_t
)new_limit_mb
<< 20, PHYS_FOOTPRINT_WARNING_LEVEL
);
5537 if (task
== current_task()) {
5538 ledger_check_new_balance(current_thread(), task
->ledger
,
5539 task_ledgers
.phys_footprint
);
5544 return KERN_SUCCESS
;
5548 task_get_phys_footprint_limit(
5552 ledger_amount_t limit
;
5554 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &limit
);
5556 * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
5557 * result. There are, however, implicit assumptions that -1 mb limit
5558 * equates to LEDGER_LIMIT_INFINITY.
5560 assert(((limit
& 0xFFF0000000000000LL
) == 0) || (limit
== LEDGER_LIMIT_INFINITY
));
5561 *limit_mb
= (int)(limit
>> 20);
5563 return KERN_SUCCESS
;
5565 #else /* CONFIG_MEMORYSTATUS */
5567 task_set_phys_footprint_limit(
5568 __unused task_t task
,
5569 __unused
int new_limit_mb
,
5570 __unused
int *old_limit_mb
)
5572 return KERN_FAILURE
;
5576 task_get_phys_footprint_limit(
5577 __unused task_t task
,
5578 __unused
int *limit_mb
)
5580 return KERN_FAILURE
;
5582 #endif /* CONFIG_MEMORYSTATUS */
5585 task_set_thread_limit(task_t task
, uint16_t thread_limit
)
5587 assert(task
!= kernel_task
);
5588 if (thread_limit
<= TASK_MAX_THREAD_LIMIT
) {
5590 task
->task_thread_limit
= thread_limit
;
5596 * We need to export some functions to other components that
5597 * are currently implemented in macros within the osfmk
5598 * component. Just export them as functions of the same name.
5601 is_kerneltask(task_t t
)
5603 if (t
== kernel_task
) {
5611 is_corpsetask(task_t t
)
5613 return task_is_a_corpse(t
);
5617 task_t
current_task(void);
5621 return current_task_fast();
5624 #undef task_reference
5625 void task_reference(task_t task
);
5630 if (task
!= TASK_NULL
) {
5631 task_reference_internal(task
);
5635 /* defined in bsd/kern/kern_prot.c */
5636 extern int get_audit_token_pid(audit_token_t
*audit_token
);
5639 task_pid(task_t task
)
5642 return get_audit_token_pid(&task
->audit_token
);
5649 * This routine finds a thread in a task by its unique id
5650 * Returns a referenced thread or THREAD_NULL if the thread was not found
5652 * TODO: This is super inefficient - it's an O(threads in task) list walk!
5653 * We should make a tid hash, or transition all tid clients to thread ports
5655 * Precondition: No locks held (will take task lock)
5658 task_findtid(task_t task
, uint64_t tid
)
5660 thread_t self
= current_thread();
5661 thread_t found_thread
= THREAD_NULL
;
5662 thread_t iter_thread
= THREAD_NULL
;
5664 /* Short-circuit the lookup if we're looking up ourselves */
5665 if (tid
== self
->thread_id
|| tid
== TID_NULL
) {
5666 assert(self
->task
== task
);
5668 thread_reference(self
);
5675 queue_iterate(&task
->threads
, iter_thread
, thread_t
, task_threads
) {
5676 if (iter_thread
->thread_id
== tid
) {
5677 found_thread
= iter_thread
;
5678 thread_reference(found_thread
);
5685 return found_thread
;
5689 pid_from_task(task_t task
)
5693 if (task
->bsd_info
) {
5694 pid
= proc_pid(task
->bsd_info
);
5696 pid
= task_pid(task
);
5703 * Control the CPU usage monitor for a task.
5706 task_cpu_usage_monitor_ctl(task_t task
, uint32_t *flags
)
5708 int error
= KERN_SUCCESS
;
5710 if (*flags
& CPUMON_MAKE_FATAL
) {
5711 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_CPUMON
;
5713 error
= KERN_INVALID_ARGUMENT
;
5720 * Control the wakeups monitor for a task.
5723 task_wakeups_monitor_ctl(task_t task
, uint32_t *flags
, int32_t *rate_hz
)
5725 ledger_t ledger
= task
->ledger
;
5728 if (*flags
& WAKEMON_GET_PARAMS
) {
5729 ledger_amount_t limit
;
5732 ledger_get_limit(ledger
, task_ledgers
.interrupt_wakeups
, &limit
);
5733 ledger_get_period(ledger
, task_ledgers
.interrupt_wakeups
, &period
);
5735 if (limit
!= LEDGER_LIMIT_INFINITY
) {
5737 * An active limit means the wakeups monitor is enabled.
5739 *rate_hz
= (int32_t)(limit
/ (int64_t)(period
/ NSEC_PER_SEC
));
5740 *flags
= WAKEMON_ENABLE
;
5741 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
5742 *flags
|= WAKEMON_MAKE_FATAL
;
5745 *flags
= WAKEMON_DISABLE
;
5750 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
5753 return KERN_SUCCESS
;
5756 if (*flags
& WAKEMON_ENABLE
) {
5757 if (*flags
& WAKEMON_SET_DEFAULTS
) {
5758 *rate_hz
= task_wakeups_monitor_rate
;
5761 #ifndef CONFIG_NOMONITORS
5762 if (*flags
& WAKEMON_MAKE_FATAL
) {
5763 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
5765 #endif /* CONFIG_NOMONITORS */
5767 if (*rate_hz
<= 0) {
5769 return KERN_INVALID_ARGUMENT
;
5772 #ifndef CONFIG_NOMONITORS
5773 ledger_set_limit(ledger
, task_ledgers
.interrupt_wakeups
, *rate_hz
* task_wakeups_monitor_interval
,
5774 task_wakeups_monitor_ustackshots_trigger_pct
);
5775 ledger_set_period(ledger
, task_ledgers
.interrupt_wakeups
, task_wakeups_monitor_interval
* NSEC_PER_SEC
);
5776 ledger_enable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
5777 #endif /* CONFIG_NOMONITORS */
5778 } else if (*flags
& WAKEMON_DISABLE
) {
5780 * Caller wishes to disable wakeups monitor on the task.
5782 * Disable telemetry if it was triggered by the wakeups monitor, and
5783 * remove the limit & callback on the wakeups ledger entry.
5785 #if CONFIG_TELEMETRY
5786 telemetry_task_ctl_locked(task
, TF_WAKEMON_WARNING
, 0);
5788 ledger_disable_refill(ledger
, task_ledgers
.interrupt_wakeups
);
5789 ledger_disable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
5793 return KERN_SUCCESS
;
5797 task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
5799 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
5800 #if CONFIG_TELEMETRY
5802 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
5803 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
5805 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 1);
5810 #if CONFIG_TELEMETRY
5812 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
5813 * exceeded the limit, turn telemetry off for the task.
5815 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 0);
5819 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS();
5823 void __attribute__((noinline
))
5824 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
5826 task_t task
= current_task();
5828 const char *procname
= "unknown";
5831 #ifdef EXC_RESOURCE_MONITORS
5832 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5833 #endif /* EXC_RESOURCE_MONITORS */
5834 struct ledger_entry_info lei
;
5837 pid
= proc_selfpid();
5838 if (task
->bsd_info
!= NULL
) {
5839 procname
= proc_name_address(current_task()->bsd_info
);
5843 ledger_get_entry_info(task
->ledger
, task_ledgers
.interrupt_wakeups
, &lei
);
5846 * Disable the exception notification so we don't overwhelm
5847 * the listener with an endless stream of redundant exceptions.
5848 * TODO: detect whether another thread is already reporting the violation.
5850 uint32_t flags
= WAKEMON_DISABLE
;
5851 task_wakeups_monitor_ctl(task
, &flags
, NULL
);
5853 fatal
= task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
5854 trace_resource_violation(RMON_CPUWAKES_VIOLATED
, &lei
);
5855 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught waking the CPU %llu times "
5856 "over ~%llu seconds, averaging %llu wakes / second and "
5857 "violating a %slimit of %llu wakes over %llu seconds.\n",
5859 lei
.lei_balance
, lei
.lei_last_refill
/ NSEC_PER_SEC
,
5860 lei
.lei_last_refill
== 0 ? 0 :
5861 (NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
),
5862 fatal
? "FATAL " : "",
5863 lei
.lei_limit
, lei
.lei_refill_period
/ NSEC_PER_SEC
);
5865 kr
= send_resource_violation(send_cpu_wakes_violation
, task
, &lei
,
5866 fatal
? kRNFatalLimitFlag
: 0);
5868 printf("send_resource_violation(CPU wakes, ...): error %#x\n", kr
);
5871 #ifdef EXC_RESOURCE_MONITORS
5872 if (disable_exc_resource
) {
5873 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5874 "supressed by a boot-arg\n", procname
, pid
);
5878 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5879 "supressed due to audio playback\n", procname
, pid
);
5882 if (lei
.lei_last_refill
== 0) {
5883 os_log(OS_LOG_DEFAULT
, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5884 "supressed due to lei.lei_last_refill = 0 \n", procname
, pid
);
5887 code
[0] = code
[1] = 0;
5888 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_WAKEUPS
);
5889 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_WAKEUPS_MONITOR
);
5890 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code
[0],
5891 NSEC_PER_SEC
* lei
.lei_limit
/ lei
.lei_refill_period
);
5892 EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code
[0],
5893 lei
.lei_last_refill
);
5894 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code
[1],
5895 NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
);
5896 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5897 #endif /* EXC_RESOURCE_MONITORS */
5900 task_terminate_internal(task
);
5905 global_update_logical_writes(int64_t io_delta
)
5907 int64_t old_count
, new_count
;
5908 boolean_t needs_telemetry
;
5911 new_count
= old_count
= global_logical_writes_count
;
5912 new_count
+= io_delta
;
5913 if (new_count
>= io_telemetry_limit
) {
5915 needs_telemetry
= TRUE
;
5917 needs_telemetry
= FALSE
;
5919 } while (!OSCompareAndSwap64(old_count
, new_count
, &global_logical_writes_count
));
5920 return needs_telemetry
;
5924 task_update_logical_writes(task_t task
, uint32_t io_size
, int flags
, void *vp
)
5926 int64_t io_delta
= 0;
5927 boolean_t needs_telemetry
= FALSE
;
5929 if ((!task
) || (!io_size
) || (!vp
)) {
5933 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_DATA_WRITE
)) | DBG_FUNC_NONE
,
5934 task_pid(task
), io_size
, flags
, (uintptr_t)VM_KERNEL_ADDRPERM(vp
), 0);
5935 DTRACE_IO4(logical_writes
, struct task
*, task
, uint32_t, io_size
, int, flags
, vnode
*, vp
);
5937 case TASK_WRITE_IMMEDIATE
:
5938 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_immediate_writes
));
5939 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5941 case TASK_WRITE_DEFERRED
:
5942 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_deferred_writes
));
5943 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5945 case TASK_WRITE_INVALIDATED
:
5946 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_invalidated_writes
));
5947 ledger_debit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5949 case TASK_WRITE_METADATA
:
5950 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_metadata_writes
));
5951 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5955 io_delta
= (flags
== TASK_WRITE_INVALIDATED
) ? ((int64_t)io_size
* -1ll) : ((int64_t)io_size
);
5956 if (io_telemetry_limit
!= 0) {
5957 /* If io_telemetry_limit is 0, disable global updates and I/O telemetry */
5958 needs_telemetry
= global_update_logical_writes(io_delta
);
5959 if (needs_telemetry
) {
5960 act_set_io_telemetry_ast(current_thread());
5966 * Control the I/O monitor for a task.
5969 task_io_monitor_ctl(task_t task
, uint32_t *flags
)
5971 ledger_t ledger
= task
->ledger
;
5974 if (*flags
& IOMON_ENABLE
) {
5975 /* Configure the physical I/O ledger */
5976 ledger_set_limit(ledger
, task_ledgers
.physical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5977 ledger_set_period(ledger
, task_ledgers
.physical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5979 /* Configure the logical I/O ledger */
5980 ledger_set_limit(ledger
, task_ledgers
.logical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5981 ledger_set_period(ledger
, task_ledgers
.logical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5982 } else if (*flags
& IOMON_DISABLE
) {
5984 * Caller wishes to disable I/O monitor on the task.
5986 ledger_disable_refill(ledger
, task_ledgers
.physical_writes
);
5987 ledger_disable_callback(ledger
, task_ledgers
.physical_writes
);
5988 ledger_disable_refill(ledger
, task_ledgers
.logical_writes
);
5989 ledger_disable_callback(ledger
, task_ledgers
.logical_writes
);
5993 return KERN_SUCCESS
;
5997 task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
)
6000 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO((int)param0
);
6004 void __attribute__((noinline
))
6005 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
)
6008 task_t task
= current_task();
6009 #ifdef EXC_RESOURCE_MONITORS
6010 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
6011 #endif /* EXC_RESOURCE_MONITORS */
6012 struct ledger_entry_info lei
;
6016 pid
= proc_selfpid();
6019 * Get the ledger entry info. We need to do this before disabling the exception
6020 * to get correct values for all fields.
6023 case FLAVOR_IO_PHYSICAL_WRITES
:
6024 ledger_get_entry_info(task
->ledger
, task_ledgers
.physical_writes
, &lei
);
6026 case FLAVOR_IO_LOGICAL_WRITES
:
6027 ledger_get_entry_info(task
->ledger
, task_ledgers
.logical_writes
, &lei
);
6033 * Disable the exception notification so we don't overwhelm
6034 * the listener with an endless stream of redundant exceptions.
6035 * TODO: detect whether another thread is already reporting the violation.
6037 uint32_t flags
= IOMON_DISABLE
;
6038 task_io_monitor_ctl(task
, &flags
);
6040 if (flavor
== FLAVOR_IO_LOGICAL_WRITES
) {
6041 trace_resource_violation(RMON_LOGWRITES_VIOLATED
, &lei
);
6043 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",
6044 pid
, flavor
, (lei
.lei_balance
/ (1024 * 1024)), (lei
.lei_limit
/ (1024 * 1024)), (lei
.lei_refill_period
/ NSEC_PER_SEC
));
6046 kr
= send_resource_violation(send_disk_writes_violation
, task
, &lei
, kRNFlagsNone
);
6048 printf("send_resource_violation(disk_writes, ...): error %#x\n", kr
);
6051 #ifdef EXC_RESOURCE_MONITORS
6052 code
[0] = code
[1] = 0;
6053 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_IO
);
6054 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], flavor
);
6055 EXC_RESOURCE_IO_ENCODE_INTERVAL(code
[0], (lei
.lei_refill_period
/ NSEC_PER_SEC
));
6056 EXC_RESOURCE_IO_ENCODE_LIMIT(code
[0], (lei
.lei_limit
/ (1024 * 1024)));
6057 EXC_RESOURCE_IO_ENCODE_OBSERVED(code
[1], (lei
.lei_balance
/ (1024 * 1024)));
6058 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
6059 #endif /* EXC_RESOURCE_MONITORS */
6062 /* Placeholders for the task set/get voucher interfaces */
6064 task_get_mach_voucher(
6066 mach_voucher_selector_t __unused which
,
6067 ipc_voucher_t
*voucher
)
6069 if (TASK_NULL
== task
) {
6070 return KERN_INVALID_TASK
;
6074 return KERN_SUCCESS
;
6078 task_set_mach_voucher(
6080 ipc_voucher_t __unused voucher
)
6082 if (TASK_NULL
== task
) {
6083 return KERN_INVALID_TASK
;
6086 return KERN_SUCCESS
;
6090 task_swap_mach_voucher(
6091 __unused task_t task
,
6092 __unused ipc_voucher_t new_voucher
,
6093 ipc_voucher_t
*in_out_old_voucher
)
6096 * Currently this function is only called from a MIG generated
6097 * routine which doesn't release the reference on the voucher
6098 * addressed by in_out_old_voucher. To avoid leaking this reference,
6099 * a call to release it has been added here.
6101 ipc_voucher_release(*in_out_old_voucher
);
6102 return KERN_NOT_SUPPORTED
;
6106 task_set_gpu_denied(task_t task
, boolean_t denied
)
6111 task
->t_flags
|= TF_GPU_DENIED
;
6113 task
->t_flags
&= ~TF_GPU_DENIED
;
6120 task_is_gpu_denied(task_t task
)
6122 /* We don't need the lock to read this flag */
6123 return (task
->t_flags
& TF_GPU_DENIED
) ? TRUE
: FALSE
;
6128 get_task_memory_region_count(task_t task
)
6131 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
6132 return (uint64_t)get_map_nentries(map
);
6136 kdebug_trace_dyld_internal(uint32_t base_code
,
6137 struct dyld_kernel_image_info
*info
)
6139 static_assert(sizeof(info
->uuid
) >= 16);
6141 #if defined(__LP64__)
6142 uint64_t *uuid
= (uint64_t *)&(info
->uuid
);
6144 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6145 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
), uuid
[0],
6146 uuid
[1], info
->load_addr
,
6147 (uint64_t)info
->fsid
.val
[0] | ((uint64_t)info
->fsid
.val
[1] << 32),
6149 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6150 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 1),
6151 (uint64_t)info
->fsobjid
.fid_objno
|
6152 ((uint64_t)info
->fsobjid
.fid_generation
<< 32),
6154 #else /* defined(__LP64__) */
6155 uint32_t *uuid
= (uint32_t *)&(info
->uuid
);
6157 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6158 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 2), uuid
[0],
6159 uuid
[1], uuid
[2], uuid
[3], 0);
6160 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6161 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 3),
6162 (uint32_t)info
->load_addr
, info
->fsid
.val
[0], info
->fsid
.val
[1],
6163 info
->fsobjid
.fid_objno
, 0);
6164 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6165 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 4),
6166 info
->fsobjid
.fid_generation
, 0, 0, 0, 0);
6167 #endif /* !defined(__LP64__) */
6170 static kern_return_t
6171 kdebug_trace_dyld(task_t task
, uint32_t base_code
,
6172 vm_map_copy_t infos_copy
, mach_msg_type_number_t infos_len
)
6175 dyld_kernel_image_info_array_t infos
;
6176 vm_map_offset_t map_data
;
6180 return KERN_INVALID_ADDRESS
;
6183 if (!kdebug_enable
||
6184 !kdebug_debugid_enabled(KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, 0))) {
6185 vm_map_copy_discard(infos_copy
);
6186 return KERN_SUCCESS
;
6189 if (task
== NULL
|| task
!= current_task()) {
6190 return KERN_INVALID_TASK
;
6193 kr
= vm_map_copyout(ipc_kernel_map
, &map_data
, (vm_map_copy_t
)infos_copy
);
6194 if (kr
!= KERN_SUCCESS
) {
6198 infos
= CAST_DOWN(dyld_kernel_image_info_array_t
, map_data
);
6200 for (mach_msg_type_number_t i
= 0; i
< infos_len
; i
++) {
6201 kdebug_trace_dyld_internal(base_code
, &(infos
[i
]));
6204 data
= CAST_DOWN(vm_offset_t
, map_data
);
6205 mach_vm_deallocate(ipc_kernel_map
, data
, infos_len
* sizeof(infos
[0]));
6206 return KERN_SUCCESS
;
6210 task_register_dyld_image_infos(task_t task
,
6211 dyld_kernel_image_info_array_t infos_copy
,
6212 mach_msg_type_number_t infos_len
)
6214 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_MAP_A
,
6215 (vm_map_copy_t
)infos_copy
, infos_len
);
6219 task_unregister_dyld_image_infos(task_t task
,
6220 dyld_kernel_image_info_array_t infos_copy
,
6221 mach_msg_type_number_t infos_len
)
6223 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_UNMAP_A
,
6224 (vm_map_copy_t
)infos_copy
, infos_len
);
6228 task_get_dyld_image_infos(__unused task_t task
,
6229 __unused dyld_kernel_image_info_array_t
* dyld_images
,
6230 __unused mach_msg_type_number_t
* dyld_imagesCnt
)
6232 return KERN_NOT_SUPPORTED
;
6236 task_register_dyld_shared_cache_image_info(task_t task
,
6237 dyld_kernel_image_info_t cache_img
,
6238 __unused boolean_t no_cache
,
6239 __unused boolean_t private_cache
)
6241 if (task
== NULL
|| task
!= current_task()) {
6242 return KERN_INVALID_TASK
;
6245 kdebug_trace_dyld_internal(DBG_DYLD_UUID_SHARED_CACHE_A
, &cache_img
);
6246 return KERN_SUCCESS
;
6250 task_register_dyld_set_dyld_state(__unused task_t task
,
6251 __unused
uint8_t dyld_state
)
6253 return KERN_NOT_SUPPORTED
;
6257 task_register_dyld_get_process_state(__unused task_t task
,
6258 __unused dyld_kernel_process_info_t
* dyld_process_state
)
6260 return KERN_NOT_SUPPORTED
;
6264 task_inspect(task_inspect_t task_insp
, task_inspect_flavor_t flavor
,
6265 task_inspect_info_t info_out
, mach_msg_type_number_t
*size_in_out
)
6268 task_t task
= (task_t
)task_insp
;
6269 kern_return_t kr
= KERN_SUCCESS
;
6270 mach_msg_type_number_t size
;
6272 if (task
== TASK_NULL
) {
6273 return KERN_INVALID_ARGUMENT
;
6276 size
= *size_in_out
;
6279 case TASK_INSPECT_BASIC_COUNTS
: {
6280 struct task_inspect_basic_counts
*bc
;
6281 uint64_t task_counts
[MT_CORE_NFIXED
] = { 0 };
6283 if (size
< TASK_INSPECT_BASIC_COUNTS_COUNT
) {
6284 kr
= KERN_INVALID_ARGUMENT
;
6288 mt_fixed_task_counts(task
, task_counts
);
6289 bc
= (struct task_inspect_basic_counts
*)info_out
;
6290 #ifdef MT_CORE_INSTRS
6291 bc
->instructions
= task_counts
[MT_CORE_INSTRS
];
6292 #else /* defined(MT_CORE_INSTRS) */
6293 bc
->instructions
= 0;
6294 #endif /* !defined(MT_CORE_INSTRS) */
6295 bc
->cycles
= task_counts
[MT_CORE_CYCLES
];
6296 size
= TASK_INSPECT_BASIC_COUNTS_COUNT
;
6300 kr
= KERN_INVALID_ARGUMENT
;
6304 if (kr
== KERN_SUCCESS
) {
6305 *size_in_out
= size
;
6308 #else /* MONOTONIC */
6309 #pragma unused(task_insp, flavor, info_out, size_in_out)
6310 return KERN_NOT_SUPPORTED
;
6311 #endif /* !MONOTONIC */
6314 #if CONFIG_SECLUDED_MEMORY
6315 int num_tasks_can_use_secluded_mem
= 0;
6318 task_set_can_use_secluded_mem(
6320 boolean_t can_use_secluded_mem
)
6322 if (!task
->task_could_use_secluded_mem
) {
6326 task_set_can_use_secluded_mem_locked(task
, can_use_secluded_mem
);
6331 task_set_can_use_secluded_mem_locked(
6333 boolean_t can_use_secluded_mem
)
6335 assert(task
->task_could_use_secluded_mem
);
6336 if (can_use_secluded_mem
&&
6337 secluded_for_apps
&& /* global boot-arg */
6338 !task
->task_can_use_secluded_mem
) {
6339 assert(num_tasks_can_use_secluded_mem
>= 0);
6341 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
6342 task
->task_can_use_secluded_mem
= TRUE
;
6343 } else if (!can_use_secluded_mem
&&
6344 task
->task_can_use_secluded_mem
) {
6345 assert(num_tasks_can_use_secluded_mem
> 0);
6347 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
6348 task
->task_can_use_secluded_mem
= FALSE
;
6353 task_set_could_use_secluded_mem(
6355 boolean_t could_use_secluded_mem
)
6357 task
->task_could_use_secluded_mem
= could_use_secluded_mem
;
6361 task_set_could_also_use_secluded_mem(
6363 boolean_t could_also_use_secluded_mem
)
6365 task
->task_could_also_use_secluded_mem
= could_also_use_secluded_mem
;
6369 task_can_use_secluded_mem(
6373 if (task
->task_can_use_secluded_mem
) {
6374 assert(task
->task_could_use_secluded_mem
);
6375 assert(num_tasks_can_use_secluded_mem
> 0);
6378 if (task
->task_could_also_use_secluded_mem
&&
6379 num_tasks_can_use_secluded_mem
> 0) {
6380 assert(num_tasks_can_use_secluded_mem
> 0);
6385 * If a single task is using more than some amount of
6386 * memory, allow it to dip into secluded and also begin
6387 * suppression of secluded memory until the tasks exits.
6389 if (is_alloc
&& secluded_shutoff_trigger
!= 0) {
6390 uint64_t phys_used
= get_task_phys_footprint(task
);
6391 if (phys_used
> secluded_shutoff_trigger
) {
6392 start_secluded_suppression(task
);
6401 task_could_use_secluded_mem(
6404 return task
->task_could_use_secluded_mem
;
6406 #endif /* CONFIG_SECLUDED_MEMORY */
6409 task_io_user_clients(task_t task
)
6411 return &task
->io_user_clients
;
6415 task_copy_fields_for_exec(task_t dst_task
, task_t src_task
)
6417 dst_task
->vtimers
= src_task
->vtimers
;
6420 #if DEVELOPMENT || DEBUG
6421 int vm_region_footprint
= 0;
6422 #endif /* DEVELOPMENT || DEBUG */
6425 task_self_region_footprint(void)
6427 #if DEVELOPMENT || DEBUG
6428 if (vm_region_footprint
) {
6429 /* system-wide override */
6432 #endif /* DEVELOPMENT || DEBUG */
6433 return current_task()->task_region_footprint
;
6437 task_self_region_footprint_set(
6442 curtask
= current_task();
6445 curtask
->task_region_footprint
= TRUE
;
6447 curtask
->task_region_footprint
= FALSE
;
6449 task_unlock(curtask
);
6453 task_set_darkwake_mode(task_t task
, boolean_t set_mode
)
6460 task
->t_flags
|= TF_DARKWAKE_MODE
;
6462 task
->t_flags
&= ~(TF_DARKWAKE_MODE
);
6469 task_get_darkwake_mode(task_t task
)
6472 return (task
->t_flags
& TF_DARKWAKE_MODE
) != 0;
6477 task_set_legacy_footprint(
6482 task
->task_legacy_footprint
= new_val
;
6485 #endif /* __arm64__ */