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_special_ports.h>
100 #include <ipc/ipc_importance.h>
101 #include <ipc/ipc_types.h>
102 #include <ipc/ipc_space.h>
103 #include <ipc/ipc_entry.h>
104 #include <ipc/ipc_hash.h>
106 #include <kern/kern_types.h>
107 #include <kern/mach_param.h>
108 #include <kern/misc_protos.h>
109 #include <kern/task.h>
110 #include <kern/thread.h>
111 #include <kern/coalition.h>
112 #include <kern/zalloc.h>
113 #include <kern/kalloc.h>
114 #include <kern/kern_cdata.h>
115 #include <kern/processor.h>
116 #include <kern/sched_prim.h> /* for thread_wakeup */
117 #include <kern/ipc_tt.h>
118 #include <kern/host.h>
119 #include <kern/clock.h>
120 #include <kern/timer.h>
121 #include <kern/assert.h>
122 #include <kern/sync_lock.h>
123 #include <kern/affinity.h>
124 #include <kern/exc_resource.h>
125 #include <kern/machine.h>
126 #include <kern/policy_internal.h>
128 #include <corpses/task_corpse.h>
130 #include <kern/telemetry.h>
134 #include <vm/vm_map.h>
135 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
136 #include <vm/vm_pageout.h>
137 #include <vm/vm_protos.h>
138 #include <vm/vm_purgeable_internal.h>
140 #include <sys/resource.h>
141 #include <sys/signalvar.h> /* for coredump */
144 * Exported interfaces
147 #include <mach/task_server.h>
148 #include <mach/mach_host_server.h>
149 #include <mach/host_security_server.h>
150 #include <mach/mach_port_server.h>
152 #include <vm/vm_shared_region.h>
154 #include <libkern/OSDebug.h>
155 #include <libkern/OSAtomic.h>
158 #include <atm/atm_internal.h>
161 #include <kern/sfi.h> /* picks up ledger.h */
164 #include <security/mac_mach_internal.h>
168 extern int kpc_force_all_ctrs(task_t
, int);
173 lck_attr_t task_lck_attr
;
174 lck_grp_t task_lck_grp
;
175 lck_grp_attr_t task_lck_grp_attr
;
177 extern int exc_via_corpse_forking
;
178 extern int unify_corpse_blob_alloc
;
179 extern int corpse_for_fatal_memkill
;
181 /* Flag set by core audio when audio is playing. Used to stifle EXC_RESOURCE generation when active. */
182 int audio_active
= 0;
184 zinfo_usage_store_t tasks_tkm_private
;
185 zinfo_usage_store_t tasks_tkm_shared
;
187 /* A container to accumulate statistics for expired tasks */
188 expired_task_statistics_t dead_task_statistics
;
189 lck_spin_t dead_task_statistics_lock
;
191 ledger_template_t task_ledger_template
= NULL
;
193 struct _task_ledger_indices task_ledgers
__attribute__((used
)) =
194 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
195 { 0 /* initialized at runtime */},
202 /* System sleep state */
203 boolean_t tasks_suspend_state
;
206 void init_task_ledgers(void);
207 void task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
208 void task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
209 void task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
);
210 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void);
211 void __attribute__((noinline
)) PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
);
212 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
);
214 kern_return_t
task_suspend_internal(task_t
);
215 kern_return_t
task_resume_internal(task_t
);
216 static kern_return_t
task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
);
217 int proc_list_uptrs(void *p
, uint64_t *udata_buffer
, int size
);
219 extern kern_return_t
iokit_task_terminate(task_t task
);
221 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
*);
222 extern void bsd_copythreadname(void *dst_uth
, void *src_uth
);
224 // Warn tasks when they hit 80% of their memory limit.
225 #define PHYS_FOOTPRINT_WARNING_LEVEL 80
227 #define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT 150 /* wakeups per second */
228 #define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL 300 /* in seconds. */
231 * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
233 * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
234 * stacktraces, aka micro-stackshots)
236 #define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER 70
238 int task_wakeups_monitor_interval
; /* In seconds. Time period over which wakeups rate is observed */
239 int task_wakeups_monitor_rate
; /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
241 int task_wakeups_monitor_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
243 int disable_exc_resource
; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
245 ledger_amount_t max_task_footprint
= 0; /* Per-task limit on physical memory consumption in bytes */
246 int max_task_footprint_warning_level
= 0; /* Per-task limit warning percentage */
247 int max_task_footprint_mb
= 0; /* Per-task limit on physical memory consumption in megabytes */
249 /* I/O Monitor Limits */
250 #define IOMON_DEFAULT_LIMIT (20480ull) /* MB of logical/physical I/O */
251 #define IOMON_DEFAULT_INTERVAL (86400ull) /* in seconds */
253 uint64_t task_iomon_limit_mb
; /* Per-task I/O monitor limit in MBs */
254 uint64_t task_iomon_interval_secs
; /* Per-task I/O monitor interval in secs */
256 #define IO_TELEMETRY_DEFAULT_LIMIT (10ll * 1024ll * 1024ll)
257 int64_t io_telemetry_limit
; /* Threshold to take a microstackshot (0 indicated I/O telemetry is turned off) */
258 int64_t global_logical_writes_count
= 0; /* Global count for logical writes */
259 static boolean_t
global_update_logical_writes(int64_t);
262 int pmap_ledgers_panic
= 1;
263 #endif /* MACH_ASSERT */
265 int task_max
= CONFIG_TASK_MAX
; /* Max number of tasks */
268 int hwm_user_cores
= 0; /* high watermark violations generate user core files */
272 extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
273 extern int proc_pid(struct proc
*p
);
274 extern int proc_selfpid(void);
275 extern char *proc_name_address(struct proc
*p
);
276 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
278 #if CONFIG_MEMORYSTATUS
279 extern void proc_memstat_terminated(struct proc
* p
, boolean_t set
);
280 extern boolean_t
memorystatus_turnoff_exception_and_get_fatalness(boolean_t warning
, const int max_footprint_mb
);
281 extern void memorystatus_on_ledger_footprint_exceeded(int warning
, boolean_t is_fatal
);
282 #endif /* CONFIG_MEMORYSTATUS */
284 #endif /* MACH_BSD */
288 static void task_hold_locked(task_t task
);
289 static void task_wait_locked(task_t task
, boolean_t until_not_runnable
);
290 static void task_release_locked(task_t task
);
292 static void task_synchronizer_destroy_all(task_t task
);
295 task_backing_store_privileged(
299 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
310 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
312 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
317 if (task_has_64BitAddr(task
))
319 task_set_64BitAddr(task
);
321 if ( !task_has_64BitAddr(task
))
323 task_clear_64BitAddr(task
);
325 /* FIXME: On x86, the thread save state flavor can diverge from the
326 * task's 64-bit feature flag due to the 32-bit/64-bit register save
327 * state dichotomy. Since we can be pre-empted in this interval,
328 * certain routines may observe the thread as being in an inconsistent
329 * state with respect to its task's 64-bitness.
332 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
333 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
334 thread_mtx_lock(thread
);
335 machine_thread_switch_addrmode(thread
);
336 thread_mtx_unlock(thread
);
338 if (thread
== current_thread()) {
341 spl_t spl
= splsched();
343 * This call tell that the current thread changed it's 32bitness.
344 * Other thread were no more on core when 32bitness was changed,
345 * but current_thread() is on core and the previous call to
346 * machine_thread_going_on_core() gave 32bitness which is now wrong.
348 * This is needed for bring-up, a different callback should be used
352 urgency
= thread_get_urgency(thread
, &arg1
, &arg2
);
353 machine_thread_going_on_core(thread
, urgency
, 0);
354 thread_unlock(thread
);
358 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
366 task_set_dyld_info(task_t task
, mach_vm_address_t addr
, mach_vm_size_t size
)
369 task
->all_image_info_addr
= addr
;
370 task
->all_image_info_size
= size
;
375 task_atm_reset(__unused task_t task
) {
378 if (task
->atm_context
!= NULL
) {
379 atm_task_descriptor_destroy(task
->atm_context
);
380 task
->atm_context
= NULL
;
387 task_bank_reset(__unused task_t task
) {
390 if (task
->bank_context
!= NULL
) {
391 bank_task_destroy(task
);
398 * NOTE: This should only be called when the P_LINTRANSIT
399 * flag is set (the proc_trans lock is held) on the
400 * proc associated with the task.
403 task_bank_init(__unused task_t task
) {
406 if (task
->bank_context
!= NULL
) {
407 panic("Task bank init called with non null bank context for task: %p and bank_context: %p", task
, task
->bank_context
);
409 bank_task_initialize(task
);
414 #if TASK_REFERENCE_LEAK_DEBUG
415 #include <kern/btlog.h>
417 static btlog_t
*task_ref_btlog
;
418 #define TASK_REF_OP_INCR 0x1
419 #define TASK_REF_OP_DECR 0x2
421 #define TASK_REF_NUM_RECORDS 100000
422 #define TASK_REF_BTDEPTH 7
425 task_reference_internal(task_t task
)
427 void * bt
[TASK_REF_BTDEPTH
];
430 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
432 (void)hw_atomic_add(&(task
)->ref_count
, 1);
433 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_INCR
,
438 task_deallocate_internal(task_t task
)
440 void * bt
[TASK_REF_BTDEPTH
];
443 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
445 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_DECR
,
447 return hw_atomic_sub(&(task
)->ref_count
, 1);
450 #endif /* TASK_REFERENCE_LEAK_DEBUG */
456 lck_grp_attr_setdefault(&task_lck_grp_attr
);
457 lck_grp_init(&task_lck_grp
, "task", &task_lck_grp_attr
);
458 lck_attr_setdefault(&task_lck_attr
);
459 lck_mtx_init(&tasks_threads_lock
, &task_lck_grp
, &task_lck_attr
);
460 lck_mtx_init(&tasks_corpse_lock
, &task_lck_grp
, &task_lck_attr
);
464 task_max
* sizeof(struct task
),
465 TASK_CHUNK
* sizeof(struct task
),
468 zone_change(task_zone
, Z_NOENCRYPT
, TRUE
);
472 * Configure per-task memory limit.
473 * The boot-arg is interpreted as Megabytes,
474 * and takes precedence over the device tree.
475 * Setting the boot-arg to 0 disables task limits.
477 if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb
,
478 sizeof (max_task_footprint_mb
))) {
480 * No limit was found in boot-args, so go look in the device tree.
482 if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb
,
483 sizeof(max_task_footprint_mb
))) {
485 * No limit was found in device tree.
487 max_task_footprint_mb
= 0;
491 if (max_task_footprint_mb
!= 0) {
492 #if CONFIG_MEMORYSTATUS
493 if (max_task_footprint_mb
< 50) {
494 printf("Warning: max_task_pmem %d below minimum.\n",
495 max_task_footprint_mb
);
496 max_task_footprint_mb
= 50;
498 printf("Limiting task physical memory footprint to %d MB\n",
499 max_task_footprint_mb
);
501 max_task_footprint
= (ledger_amount_t
)max_task_footprint_mb
* 1024 * 1024; // Convert MB to bytes
504 * Configure the per-task memory limit warning level.
505 * This is computed as a percentage.
507 max_task_footprint_warning_level
= 0;
509 if (max_mem
< 0x40000000) {
511 * On devices with < 1GB of memory:
512 * -- set warnings to 50MB below the per-task limit.
514 if (max_task_footprint_mb
> 50) {
515 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 50) * 100) / max_task_footprint_mb
;
519 * On devices with >= 1GB of memory:
520 * -- set warnings to 100MB below the per-task limit.
522 if (max_task_footprint_mb
> 100) {
523 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 100) * 100) / max_task_footprint_mb
;
528 * Never allow warning level to land below the default.
530 if (max_task_footprint_warning_level
< PHYS_FOOTPRINT_WARNING_LEVEL
) {
531 max_task_footprint_warning_level
= PHYS_FOOTPRINT_WARNING_LEVEL
;
534 printf("Limiting task physical memory warning to %d%%\n", max_task_footprint_warning_level
);
537 printf("Warning: max_task_pmem specified, but jetsam not configured; ignoring.\n");
538 #endif /* CONFIG_MEMORYSTATUS */
542 PE_parse_boot_argn("pmap_ledgers_panic", &pmap_ledgers_panic
,
543 sizeof (pmap_ledgers_panic
));
544 #endif /* MACH_ASSERT */
547 if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores
,
548 sizeof (hwm_user_cores
))) {
553 proc_init_cpumon_params();
555 if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate
, sizeof (task_wakeups_monitor_rate
))) {
556 task_wakeups_monitor_rate
= TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT
;
559 if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval
, sizeof (task_wakeups_monitor_interval
))) {
560 task_wakeups_monitor_interval
= TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL
;
563 if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct
,
564 sizeof (task_wakeups_monitor_ustackshots_trigger_pct
))) {
565 task_wakeups_monitor_ustackshots_trigger_pct
= TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER
;
568 if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource
,
569 sizeof (disable_exc_resource
))) {
570 disable_exc_resource
= 0;
573 if (!PE_parse_boot_argn("task_iomon_limit_mb", &task_iomon_limit_mb
, sizeof (task_iomon_limit_mb
))) {
574 task_iomon_limit_mb
= IOMON_DEFAULT_LIMIT
;
577 if (!PE_parse_boot_argn("task_iomon_interval_secs", &task_iomon_interval_secs
, sizeof (task_iomon_interval_secs
))) {
578 task_iomon_interval_secs
= IOMON_DEFAULT_INTERVAL
;
581 if (!PE_parse_boot_argn("io_telemetry_limit", &io_telemetry_limit
, sizeof (io_telemetry_limit
))) {
582 io_telemetry_limit
= IO_TELEMETRY_DEFAULT_LIMIT
;
586 * If we have coalitions, coalition_init() will call init_task_ledgers() as it
587 * sets up the ledgers for the default coalition. If we don't have coalitions,
588 * then we have to call it now.
590 #if CONFIG_COALITIONS
591 assert(task_ledger_template
);
592 #else /* CONFIG_COALITIONS */
594 #endif /* CONFIG_COALITIONS */
596 #if TASK_REFERENCE_LEAK_DEBUG
597 task_ref_btlog
= btlog_create(TASK_REF_NUM_RECORDS
, TASK_REF_BTDEPTH
, TRUE
/* caller_will_remove_entries_for_element? */);
598 assert(task_ref_btlog
);
602 * Create the kernel task as the first task.
605 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, TRUE
, TF_NONE
, &kernel_task
) != KERN_SUCCESS
)
607 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, FALSE
, TF_NONE
, &kernel_task
) != KERN_SUCCESS
)
609 panic("task_init\n");
611 vm_map_deallocate(kernel_task
->map
);
612 kernel_task
->map
= kernel_map
;
613 lck_spin_init(&dead_task_statistics_lock
, &task_lck_grp
, &task_lck_attr
);
617 * Create a task running in the kernel address space. It may
618 * have its own map of size mem_size and may have ipc privileges.
622 __unused task_t parent_task
,
623 __unused vm_offset_t map_base
,
624 __unused vm_size_t map_size
,
625 __unused task_t
*child_task
)
627 return (KERN_INVALID_ARGUMENT
);
633 __unused ledger_port_array_t ledger_ports
,
634 __unused mach_msg_type_number_t num_ledger_ports
,
635 __unused boolean_t inherit_memory
,
636 __unused task_t
*child_task
) /* OUT */
638 if (parent_task
== TASK_NULL
)
639 return(KERN_INVALID_ARGUMENT
);
642 * No longer supported: too many calls assume that a task has a valid
645 return(KERN_FAILURE
);
649 host_security_create_task_token(
650 host_security_t host_security
,
652 __unused security_token_t sec_token
,
653 __unused audit_token_t audit_token
,
654 __unused host_priv_t host_priv
,
655 __unused ledger_port_array_t ledger_ports
,
656 __unused mach_msg_type_number_t num_ledger_ports
,
657 __unused boolean_t inherit_memory
,
658 __unused task_t
*child_task
) /* OUT */
660 if (parent_task
== TASK_NULL
)
661 return(KERN_INVALID_ARGUMENT
);
663 if (host_security
== HOST_NULL
)
664 return(KERN_INVALID_SECURITY
);
667 * No longer supported.
669 return(KERN_FAILURE
);
677 * Physical footprint: This is the sum of:
678 * + (internal - alternate_accounting)
679 * + (internal_compressed - alternate_accounting_compressed)
681 * + purgeable_nonvolatile
682 * + purgeable_nonvolatile_compressed
686 * The task's anonymous memory, which on iOS is always resident.
688 * internal_compressed
689 * Amount of this task's internal memory which is held by the compressor.
690 * Such memory is no longer actually resident for the task [i.e., resident in its pmap],
691 * and could be either decompressed back into memory, or paged out to storage, depending
692 * on our implementation.
695 * IOKit mappings: The total size of all IOKit mappings in this task, regardless of
696 clean/dirty or internal/external state].
698 * alternate_accounting
699 * The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
700 * are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
704 init_task_ledgers(void)
708 assert(task_ledger_template
== NULL
);
709 assert(kernel_task
== TASK_NULL
);
712 PE_parse_boot_argn("pmap_ledgers_panic", &pmap_ledgers_panic
,
713 sizeof (pmap_ledgers_panic
));
714 #endif /* MACH_ASSERT */
716 if ((t
= ledger_template_create("Per-task ledger")) == NULL
)
717 panic("couldn't create task ledger template");
719 task_ledgers
.cpu_time
= ledger_entry_add(t
, "cpu_time", "sched", "ns");
720 task_ledgers
.tkm_private
= ledger_entry_add(t
, "tkm_private",
722 task_ledgers
.tkm_shared
= ledger_entry_add(t
, "tkm_shared", "physmem",
724 task_ledgers
.phys_mem
= ledger_entry_add(t
, "phys_mem", "physmem",
726 task_ledgers
.wired_mem
= ledger_entry_add(t
, "wired_mem", "physmem",
728 task_ledgers
.internal
= ledger_entry_add(t
, "internal", "physmem",
730 task_ledgers
.iokit_mapped
= ledger_entry_add(t
, "iokit_mapped", "mappings",
732 task_ledgers
.alternate_accounting
= ledger_entry_add(t
, "alternate_accounting", "physmem",
734 task_ledgers
.alternate_accounting_compressed
= ledger_entry_add(t
, "alternate_accounting_compressed", "physmem",
736 task_ledgers
.page_table
= ledger_entry_add(t
, "page_table", "physmem",
738 task_ledgers
.phys_footprint
= ledger_entry_add(t
, "phys_footprint", "physmem",
740 task_ledgers
.internal_compressed
= ledger_entry_add(t
, "internal_compressed", "physmem",
742 task_ledgers
.purgeable_volatile
= ledger_entry_add(t
, "purgeable_volatile", "physmem", "bytes");
743 task_ledgers
.purgeable_nonvolatile
= ledger_entry_add(t
, "purgeable_nonvolatile", "physmem", "bytes");
744 task_ledgers
.purgeable_volatile_compressed
= ledger_entry_add(t
, "purgeable_volatile_compress", "physmem", "bytes");
745 task_ledgers
.purgeable_nonvolatile_compressed
= ledger_entry_add(t
, "purgeable_nonvolatile_compress", "physmem", "bytes");
746 task_ledgers
.platform_idle_wakeups
= ledger_entry_add(t
, "platform_idle_wakeups", "power",
748 task_ledgers
.interrupt_wakeups
= ledger_entry_add(t
, "interrupt_wakeups", "power",
752 sfi_class_id_t class_id
, ledger_alias
;
753 for (class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
754 task_ledgers
.sfi_wait_times
[class_id
] = -1;
757 /* don't account for UNSPECIFIED */
758 for (class_id
= SFI_CLASS_UNSPECIFIED
+ 1; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
759 ledger_alias
= sfi_get_ledger_alias_for_class(class_id
);
760 if (ledger_alias
!= SFI_CLASS_UNSPECIFIED
) {
761 /* Check to see if alias has been registered yet */
762 if (task_ledgers
.sfi_wait_times
[ledger_alias
] != -1) {
763 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
];
765 /* Otherwise, initialize it first */
766 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
] = sfi_ledger_entry_add(t
, ledger_alias
);
769 task_ledgers
.sfi_wait_times
[class_id
] = sfi_ledger_entry_add(t
, class_id
);
772 if (task_ledgers
.sfi_wait_times
[class_id
] < 0) {
773 panic("couldn't create entries for task ledger template for SFI class 0x%x", class_id
);
777 assert(task_ledgers
.sfi_wait_times
[MAX_SFI_CLASS_ID
-1] != -1);
778 #endif /* CONFIG_SCHED_SFI */
781 task_ledgers
.cpu_time_billed_to_me
= ledger_entry_add(t
, "cpu_time_billed_to_me", "sched", "ns");
782 task_ledgers
.cpu_time_billed_to_others
= ledger_entry_add(t
, "cpu_time_billed_to_others", "sched", "ns");
784 task_ledgers
.physical_writes
= ledger_entry_add(t
, "physical_writes", "res", "bytes");
785 task_ledgers
.logical_writes
= ledger_entry_add(t
, "logical_writes", "res", "bytes");
787 if ((task_ledgers
.cpu_time
< 0) ||
788 (task_ledgers
.tkm_private
< 0) ||
789 (task_ledgers
.tkm_shared
< 0) ||
790 (task_ledgers
.phys_mem
< 0) ||
791 (task_ledgers
.wired_mem
< 0) ||
792 (task_ledgers
.internal
< 0) ||
793 (task_ledgers
.iokit_mapped
< 0) ||
794 (task_ledgers
.alternate_accounting
< 0) ||
795 (task_ledgers
.alternate_accounting_compressed
< 0) ||
796 (task_ledgers
.page_table
< 0) ||
797 (task_ledgers
.phys_footprint
< 0) ||
798 (task_ledgers
.internal_compressed
< 0) ||
799 (task_ledgers
.purgeable_volatile
< 0) ||
800 (task_ledgers
.purgeable_nonvolatile
< 0) ||
801 (task_ledgers
.purgeable_volatile_compressed
< 0) ||
802 (task_ledgers
.purgeable_nonvolatile_compressed
< 0) ||
803 (task_ledgers
.platform_idle_wakeups
< 0) ||
804 (task_ledgers
.interrupt_wakeups
< 0) ||
806 (task_ledgers
.cpu_time_billed_to_me
< 0) || (task_ledgers
.cpu_time_billed_to_others
< 0) ||
808 (task_ledgers
.physical_writes
< 0) ||
809 (task_ledgers
.logical_writes
< 0)
811 panic("couldn't create entries for task ledger template");
814 ledger_track_credit_only(t
, task_ledgers
.phys_footprint
);
815 ledger_track_credit_only(t
, task_ledgers
.internal
);
816 ledger_track_credit_only(t
, task_ledgers
.internal_compressed
);
817 ledger_track_credit_only(t
, task_ledgers
.iokit_mapped
);
818 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting
);
819 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting_compressed
);
820 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile
);
821 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile
);
822 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile_compressed
);
823 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
825 ledger_track_maximum(t
, task_ledgers
.phys_footprint
, 60);
827 if (pmap_ledgers_panic
) {
828 ledger_panic_on_negative(t
, task_ledgers
.phys_footprint
);
829 ledger_panic_on_negative(t
, task_ledgers
.page_table
);
830 ledger_panic_on_negative(t
, task_ledgers
.internal
);
831 ledger_panic_on_negative(t
, task_ledgers
.internal_compressed
);
832 ledger_panic_on_negative(t
, task_ledgers
.iokit_mapped
);
833 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting
);
834 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting_compressed
);
835 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile
);
836 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile
);
837 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile_compressed
);
838 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
840 #endif /* MACH_ASSERT */
842 #if CONFIG_MEMORYSTATUS
843 ledger_set_callback(t
, task_ledgers
.phys_footprint
, task_footprint_exceeded
, NULL
, NULL
);
844 #endif /* CONFIG_MEMORYSTATUS */
846 ledger_set_callback(t
, task_ledgers
.interrupt_wakeups
,
847 task_wakeups_rate_exceeded
, NULL
, NULL
);
848 ledger_set_callback(t
, task_ledgers
.physical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_PHYSICAL_WRITES
, NULL
);
849 ledger_set_callback(t
, task_ledgers
.logical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_LOGICAL_WRITES
, NULL
);
850 task_ledger_template
= t
;
854 task_create_internal(
856 coalition_t
*parent_coalitions __unused
,
857 boolean_t inherit_memory
,
860 task_t
*child_task
) /* OUT */
863 vm_shared_region_t shared_region
;
864 ledger_t ledger
= NULL
;
866 new_task
= (task_t
) zalloc(task_zone
);
868 if (new_task
== TASK_NULL
)
869 return(KERN_RESOURCE_SHORTAGE
);
871 /* one ref for just being alive; one for our caller */
872 new_task
->ref_count
= 2;
874 /* allocate with active entries */
875 assert(task_ledger_template
!= NULL
);
876 if ((ledger
= ledger_instantiate(task_ledger_template
,
877 LEDGER_CREATE_ACTIVE_ENTRIES
)) == NULL
) {
878 zfree(task_zone
, new_task
);
879 return(KERN_RESOURCE_SHORTAGE
);
882 new_task
->ledger
= ledger
;
884 #if defined(CONFIG_SCHED_MULTIQ)
885 new_task
->sched_group
= sched_group_create();
888 /* if inherit_memory is true, parent_task MUST not be NULL */
889 if (!(t_flags
& TF_CORPSE_FORK
) && inherit_memory
)
890 new_task
->map
= vm_map_fork(ledger
, parent_task
->map
, 0);
892 new_task
->map
= vm_map_create(pmap_create(ledger
, 0, is_64bit
),
893 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
894 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
896 /* Inherit memlock limit from parent */
898 vm_map_set_user_wire_limit(new_task
->map
, (vm_size_t
)parent_task
->map
->user_wire_limit
);
900 lck_mtx_init(&new_task
->lock
, &task_lck_grp
, &task_lck_attr
);
901 queue_init(&new_task
->threads
);
902 new_task
->suspend_count
= 0;
903 new_task
->thread_count
= 0;
904 new_task
->active_thread_count
= 0;
905 new_task
->user_stop_count
= 0;
906 new_task
->legacy_stop_count
= 0;
907 new_task
->active
= TRUE
;
908 new_task
->halting
= FALSE
;
909 new_task
->user_data
= NULL
;
910 new_task
->priv_flags
= 0;
911 new_task
->t_flags
= t_flags
;
912 new_task
->importance
= 0;
913 new_task
->corpse_info_kernel
= NULL
;
914 new_task
->exec_token
= 0;
917 new_task
->atm_context
= NULL
;
920 new_task
->bank_context
= NULL
;
924 new_task
->bsd_info
= NULL
;
925 new_task
->corpse_info
= NULL
;
926 #endif /* MACH_BSD */
929 new_task
->crash_label
= NULL
;
932 #if CONFIG_MEMORYSTATUS
933 if (max_task_footprint
!= 0) {
934 ledger_set_limit(ledger
, task_ledgers
.phys_footprint
, max_task_footprint
, PHYS_FOOTPRINT_WARNING_LEVEL
);
936 #endif /* CONFIG_MEMORYSTATUS */
938 if (task_wakeups_monitor_rate
!= 0) {
939 uint32_t flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
940 int32_t rate
; // Ignored because of WAKEMON_SET_DEFAULTS
941 task_wakeups_monitor_ctl(new_task
, &flags
, &rate
);
944 #if CONFIG_IO_ACCOUNTING
945 uint32_t flags
= IOMON_ENABLE
;
946 task_io_monitor_ctl(new_task
, &flags
);
947 #endif /* CONFIG_IO_ACCOUNTING */
949 #if defined(__i386__) || defined(__x86_64__)
950 new_task
->i386_ldt
= 0;
953 new_task
->task_debug
= NULL
;
955 #if DEVELOPMENT || DEBUG
956 new_task
->task_unnested
= FALSE
;
957 new_task
->task_disconnected_count
= 0;
959 queue_init(&new_task
->semaphore_list
);
960 new_task
->semaphores_owned
= 0;
962 ipc_task_init(new_task
, parent_task
);
964 new_task
->vtimers
= 0;
966 new_task
->shared_region
= NULL
;
968 new_task
->affinity_space
= NULL
;
970 new_task
->pidsuspended
= FALSE
;
971 new_task
->frozen
= FALSE
;
972 new_task
->changing_freeze_state
= FALSE
;
973 new_task
->rusage_cpu_flags
= 0;
974 new_task
->rusage_cpu_percentage
= 0;
975 new_task
->rusage_cpu_interval
= 0;
976 new_task
->rusage_cpu_deadline
= 0;
977 new_task
->rusage_cpu_callt
= NULL
;
979 new_task
->suspends_outstanding
= 0;
983 new_task
->hv_task_target
= NULL
;
984 #endif /* HYPERVISOR */
987 new_task
->mem_notify_reserved
= 0;
988 #if IMPORTANCE_INHERITANCE
989 new_task
->task_imp_base
= NULL
;
990 #endif /* IMPORTANCE_INHERITANCE */
992 #if defined(__x86_64__)
993 new_task
->uexc_range_start
= new_task
->uexc_range_size
= new_task
->uexc_handler
= 0;
996 new_task
->requested_policy
= default_task_requested_policy
;
997 new_task
->effective_policy
= default_task_effective_policy
;
999 if (parent_task
!= TASK_NULL
) {
1000 new_task
->sec_token
= parent_task
->sec_token
;
1001 new_task
->audit_token
= parent_task
->audit_token
;
1003 /* inherit the parent's shared region */
1004 shared_region
= vm_shared_region_get(parent_task
);
1005 vm_shared_region_set(new_task
, shared_region
);
1007 if(task_has_64BitAddr(parent_task
))
1008 task_set_64BitAddr(new_task
);
1009 new_task
->all_image_info_addr
= parent_task
->all_image_info_addr
;
1010 new_task
->all_image_info_size
= parent_task
->all_image_info_size
;
1012 #if defined(__i386__) || defined(__x86_64__)
1013 if (inherit_memory
&& parent_task
->i386_ldt
)
1014 new_task
->i386_ldt
= user_ldt_copy(parent_task
->i386_ldt
);
1016 if (inherit_memory
&& parent_task
->affinity_space
)
1017 task_affinity_create(parent_task
, new_task
);
1019 new_task
->pset_hint
= parent_task
->pset_hint
= task_choose_pset(parent_task
);
1021 #if IMPORTANCE_INHERITANCE
1022 ipc_importance_task_t new_task_imp
= IIT_NULL
;
1024 if (task_is_marked_importance_donor(parent_task
)) {
1025 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
1026 assert(IIT_NULL
!= new_task_imp
);
1027 ipc_importance_task_mark_donor(new_task_imp
, TRUE
);
1029 /* Embedded doesn't want this to inherit */
1030 if (task_is_marked_importance_receiver(parent_task
)) {
1031 if (IIT_NULL
== new_task_imp
)
1032 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
1033 assert(IIT_NULL
!= new_task_imp
);
1034 ipc_importance_task_mark_receiver(new_task_imp
, TRUE
);
1036 if (task_is_marked_importance_denap_receiver(parent_task
)) {
1037 if (IIT_NULL
== new_task_imp
)
1038 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
1039 assert(IIT_NULL
!= new_task_imp
);
1040 ipc_importance_task_mark_denap_receiver(new_task_imp
, TRUE
);
1043 if (IIT_NULL
!= new_task_imp
) {
1044 assert(new_task
->task_imp_base
== new_task_imp
);
1045 ipc_importance_task_release(new_task_imp
);
1047 #endif /* IMPORTANCE_INHERITANCE */
1049 new_task
->priority
= BASEPRI_DEFAULT
;
1050 new_task
->max_priority
= MAXPRI_USER
;
1052 task_policy_create(new_task
, parent_task
);
1054 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
1055 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
1058 task_set_64BitAddr(new_task
);
1060 new_task
->all_image_info_addr
= (mach_vm_address_t
)0;
1061 new_task
->all_image_info_size
= (mach_vm_size_t
)0;
1063 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
1065 if (kernel_task
== TASK_NULL
) {
1066 new_task
->priority
= BASEPRI_KERNEL
;
1067 new_task
->max_priority
= MAXPRI_KERNEL
;
1069 new_task
->priority
= BASEPRI_DEFAULT
;
1070 new_task
->max_priority
= MAXPRI_USER
;
1074 bzero(new_task
->coalition
, sizeof(new_task
->coalition
));
1075 for (int i
= 0; i
< COALITION_NUM_TYPES
; i
++)
1076 queue_chain_init(new_task
->task_coalition
[i
]);
1078 /* Allocate I/O Statistics */
1079 new_task
->task_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
1080 assert(new_task
->task_io_stats
!= NULL
);
1081 bzero(new_task
->task_io_stats
, sizeof(struct io_stat_info
));
1083 bzero(&(new_task
->cpu_time_qos_stats
), sizeof(struct _cpu_time_qos_stats
));
1085 bzero(&new_task
->extmod_statistics
, sizeof(new_task
->extmod_statistics
));
1087 /* Copy resource acc. info from Parent for Corpe Forked task. */
1088 if (parent_task
!= NULL
&& (t_flags
& TF_CORPSE_FORK
)) {
1089 new_task
->total_user_time
= parent_task
->total_user_time
;
1090 new_task
->total_system_time
= parent_task
->total_system_time
;
1091 ledger_rollup(new_task
->ledger
, parent_task
->ledger
);
1092 new_task
->faults
= parent_task
->faults
;
1093 new_task
->pageins
= parent_task
->pageins
;
1094 new_task
->cow_faults
= parent_task
->cow_faults
;
1095 new_task
->messages_sent
= parent_task
->messages_sent
;
1096 new_task
->messages_received
= parent_task
->messages_received
;
1097 new_task
->syscalls_mach
= parent_task
->syscalls_mach
;
1098 new_task
->syscalls_unix
= parent_task
->syscalls_unix
;
1099 new_task
->c_switch
= parent_task
->c_switch
;
1100 new_task
->p_switch
= parent_task
->p_switch
;
1101 new_task
->ps_switch
= parent_task
->ps_switch
;
1102 new_task
->extmod_statistics
= parent_task
->extmod_statistics
;
1103 new_task
->low_mem_notified_warn
= parent_task
->low_mem_notified_warn
;
1104 new_task
->low_mem_notified_critical
= parent_task
->low_mem_notified_critical
;
1105 new_task
->purged_memory_warn
= parent_task
->purged_memory_warn
;
1106 new_task
->purged_memory_critical
= parent_task
->purged_memory_critical
;
1107 new_task
->low_mem_privileged_listener
= parent_task
->low_mem_privileged_listener
;
1108 *new_task
->task_io_stats
= *parent_task
->task_io_stats
;
1109 new_task
->cpu_time_qos_stats
= parent_task
->cpu_time_qos_stats
;
1110 new_task
->task_timer_wakeups_bin_1
= parent_task
->task_timer_wakeups_bin_1
;
1111 new_task
->task_timer_wakeups_bin_2
= parent_task
->task_timer_wakeups_bin_2
;
1112 new_task
->task_gpu_ns
= parent_task
->task_gpu_ns
;
1113 new_task
->task_immediate_writes
= parent_task
->task_immediate_writes
;
1114 new_task
->task_deferred_writes
= parent_task
->task_deferred_writes
;
1115 new_task
->task_invalidated_writes
= parent_task
->task_invalidated_writes
;
1116 new_task
->task_metadata_writes
= parent_task
->task_metadata_writes
;
1117 new_task
->task_energy
= parent_task
->task_energy
;
1119 /* Initialize to zero for standard fork/spawn case */
1120 new_task
->total_user_time
= 0;
1121 new_task
->total_system_time
= 0;
1122 new_task
->faults
= 0;
1123 new_task
->pageins
= 0;
1124 new_task
->cow_faults
= 0;
1125 new_task
->messages_sent
= 0;
1126 new_task
->messages_received
= 0;
1127 new_task
->syscalls_mach
= 0;
1128 new_task
->syscalls_unix
= 0;
1129 new_task
->c_switch
= 0;
1130 new_task
->p_switch
= 0;
1131 new_task
->ps_switch
= 0;
1132 new_task
->low_mem_notified_warn
= 0;
1133 new_task
->low_mem_notified_critical
= 0;
1134 new_task
->purged_memory_warn
= 0;
1135 new_task
->purged_memory_critical
= 0;
1136 new_task
->low_mem_privileged_listener
= 0;
1137 new_task
->task_timer_wakeups_bin_1
= 0;
1138 new_task
->task_timer_wakeups_bin_2
= 0;
1139 new_task
->task_gpu_ns
= 0;
1140 new_task
->task_immediate_writes
= 0;
1141 new_task
->task_deferred_writes
= 0;
1142 new_task
->task_invalidated_writes
= 0;
1143 new_task
->task_metadata_writes
= 0;
1144 new_task
->task_energy
= 0;
1148 #if CONFIG_COALITIONS
1149 if (!(t_flags
& TF_CORPSE_FORK
)) {
1150 /* TODO: there is no graceful failure path here... */
1151 if (parent_coalitions
&& parent_coalitions
[COALITION_TYPE_RESOURCE
]) {
1152 coalitions_adopt_task(parent_coalitions
, new_task
);
1153 } else if (parent_task
&& parent_task
->coalition
[COALITION_TYPE_RESOURCE
]) {
1155 * all tasks at least have a resource coalition, so
1156 * if the parent has one then inherit all coalitions
1157 * the parent is a part of
1159 coalitions_adopt_task(parent_task
->coalition
, new_task
);
1161 /* TODO: assert that new_task will be PID 1 (launchd) */
1162 coalitions_adopt_init_task(new_task
);
1165 coalitions_adopt_corpse_task(new_task
);
1168 if (new_task
->coalition
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
1169 panic("created task is not a member of a resource coalition");
1171 #endif /* CONFIG_COALITIONS */
1173 new_task
->dispatchqueue_offset
= 0;
1174 if (parent_task
!= NULL
) {
1175 new_task
->dispatchqueue_offset
= parent_task
->dispatchqueue_offset
;
1178 if (vm_backing_store_low
&& parent_task
!= NULL
)
1179 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
1181 new_task
->task_volatile_objects
= 0;
1182 new_task
->task_nonvolatile_objects
= 0;
1183 new_task
->task_purgeable_disowning
= FALSE
;
1184 new_task
->task_purgeable_disowned
= FALSE
;
1186 #if CONFIG_SECLUDED_MEMORY
1187 new_task
->task_can_use_secluded_mem
= FALSE
;
1188 new_task
->task_could_use_secluded_mem
= FALSE
;
1189 new_task
->task_could_also_use_secluded_mem
= FALSE
;
1190 #endif /* CONFIG_SECLUDED_MEMORY */
1192 queue_init(&new_task
->io_user_clients
);
1194 ipc_task_enable(new_task
);
1196 lck_mtx_lock(&tasks_threads_lock
);
1197 queue_enter(&tasks
, new_task
, task_t
, tasks
);
1199 if (tasks_suspend_state
) {
1200 task_suspend_internal(new_task
);
1202 lck_mtx_unlock(&tasks_threads_lock
);
1204 *child_task
= new_task
;
1205 return(KERN_SUCCESS
);
1208 int task_dropped_imp_count
= 0;
1213 * Drop a reference on a task.
1219 ledger_amount_t credit
, debit
, interrupt_wakeups
, platform_idle_wakeups
;
1222 if (task
== TASK_NULL
)
1225 refs
= task_deallocate_internal(task
);
1227 #if IMPORTANCE_INHERITANCE
1233 * If last ref potentially comes from the task's importance,
1234 * disconnect it. But more task refs may be added before
1235 * that completes, so wait for the reference to go to zero
1236 * naturually (it may happen on a recursive task_deallocate()
1237 * from the ipc_importance_disconnect_task() call).
1239 if (IIT_NULL
!= task
->task_imp_base
)
1240 ipc_importance_disconnect_task(task
);
1246 #endif /* IMPORTANCE_INHERITANCE */
1248 lck_mtx_lock(&tasks_threads_lock
);
1249 queue_remove(&terminated_tasks
, task
, task_t
, tasks
);
1250 terminated_tasks_count
--;
1251 lck_mtx_unlock(&tasks_threads_lock
);
1254 * remove the reference on atm descriptor
1256 task_atm_reset(task
);
1259 * remove the reference on bank context
1261 task_bank_reset(task
);
1263 if (task
->task_io_stats
)
1264 kfree(task
->task_io_stats
, sizeof(struct io_stat_info
));
1267 * Give the machine dependent code a chance
1268 * to perform cleanup before ripping apart
1271 machine_task_terminate(task
);
1273 ipc_task_terminate(task
);
1275 /* let iokit know */
1276 iokit_task_terminate(task
);
1278 if (task
->affinity_space
)
1279 task_affinity_deallocate(task
);
1282 if (task
->ledger
!= NULL
&&
1283 task
->map
!= NULL
&&
1284 task
->map
->pmap
!= NULL
&&
1285 task
->map
->pmap
->ledger
!= NULL
) {
1286 assert(task
->ledger
== task
->map
->pmap
->ledger
);
1288 #endif /* MACH_ASSERT */
1290 vm_purgeable_disown(task
);
1291 assert(task
->task_purgeable_disowned
);
1292 if (task
->task_volatile_objects
!= 0 ||
1293 task
->task_nonvolatile_objects
!= 0) {
1294 panic("task_deallocate(%p): "
1295 "volatile_objects=%d nonvolatile_objects=%d\n",
1297 task
->task_volatile_objects
,
1298 task
->task_nonvolatile_objects
);
1301 vm_map_deallocate(task
->map
);
1302 is_release(task
->itk_space
);
1304 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
1305 &interrupt_wakeups
, &debit
);
1306 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
1307 &platform_idle_wakeups
, &debit
);
1309 #if defined(CONFIG_SCHED_MULTIQ)
1310 sched_group_destroy(task
->sched_group
);
1313 /* Accumulate statistics for dead tasks */
1314 lck_spin_lock(&dead_task_statistics_lock
);
1315 dead_task_statistics
.total_user_time
+= task
->total_user_time
;
1316 dead_task_statistics
.total_system_time
+= task
->total_system_time
;
1318 dead_task_statistics
.task_interrupt_wakeups
+= interrupt_wakeups
;
1319 dead_task_statistics
.task_platform_idle_wakeups
+= platform_idle_wakeups
;
1321 dead_task_statistics
.task_timer_wakeups_bin_1
+= task
->task_timer_wakeups_bin_1
;
1322 dead_task_statistics
.task_timer_wakeups_bin_2
+= task
->task_timer_wakeups_bin_2
;
1324 lck_spin_unlock(&dead_task_statistics_lock
);
1325 lck_mtx_destroy(&task
->lock
, &task_lck_grp
);
1327 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_private
, &credit
,
1329 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_private
.alloc
);
1330 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_private
.free
);
1332 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_shared
, &credit
,
1334 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_shared
.alloc
);
1335 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_shared
.free
);
1337 ledger_dereference(task
->ledger
);
1339 #if TASK_REFERENCE_LEAK_DEBUG
1340 btlog_remove_entries_for_element(task_ref_btlog
, task
);
1343 #if CONFIG_COALITIONS
1344 task_release_coalitions(task
);
1345 #endif /* CONFIG_COALITIONS */
1347 bzero(task
->coalition
, sizeof(task
->coalition
));
1350 /* clean up collected information since last reference to task is gone */
1351 if (task
->corpse_info
) {
1352 task_crashinfo_destroy(task
->corpse_info
, RELEASE_CORPSE_REF
);
1353 task
->corpse_info
= NULL
;
1356 if (task
->corpse_info_kernel
) {
1357 kfree(task
->corpse_info_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1361 if (task
->crash_label
) {
1362 mac_exc_action_label_task_destroy(task
);
1366 zfree(task_zone
, task
);
1370 * task_name_deallocate:
1372 * Drop a reference on a task name.
1375 task_name_deallocate(
1376 task_name_t task_name
)
1378 return(task_deallocate((task_t
)task_name
));
1382 * task_suspension_token_deallocate:
1384 * Drop a reference on a task suspension token.
1387 task_suspension_token_deallocate(
1388 task_suspension_token_t token
)
1390 return(task_deallocate((task_t
)token
));
1395 * task_collect_crash_info:
1397 * collect crash info from bsd and mach based data
1400 task_collect_crash_info(task_t task
, struct proc
*proc
, int is_corpse_fork
)
1402 kern_return_t kr
= KERN_SUCCESS
;
1404 kcdata_descriptor_t crash_data
= NULL
;
1405 kcdata_descriptor_t crash_data_release
= NULL
;
1406 mach_msg_type_number_t size
= CORPSEINFO_ALLOCATION_SIZE
;
1407 mach_vm_offset_t crash_data_ptr
= 0;
1408 void *crash_data_kernel
= NULL
;
1409 void *crash_data_kernel_release
= NULL
;
1410 int corpse_blob_kernel_alloc
= (is_corpse_fork
|| unify_corpse_blob_alloc
);
1412 if (!corpses_enabled()) {
1413 return KERN_NOT_SUPPORTED
;
1418 assert(is_corpse_fork
|| task
->bsd_info
!= NULL
);
1419 if (task
->corpse_info
== NULL
&& (is_corpse_fork
|| task
->bsd_info
!= NULL
)) {
1421 /* Update the corpse label, used by the exception delivery mac hook */
1422 mac_exc_action_label_task_update(task
, proc
);
1426 if (!corpse_blob_kernel_alloc
) {
1427 /* map crash data memory in task's vm map */
1428 kr
= mach_vm_allocate(task
->map
, &crash_data_ptr
, size
, (VM_MAKE_TAG(VM_MEMORY_CORPSEINFO
) | VM_FLAGS_ANYWHERE
));
1430 crash_data_kernel
= (void *) kalloc(CORPSEINFO_ALLOCATION_SIZE
);
1431 if (crash_data_kernel
== 0)
1432 kr
= KERN_RESOURCE_SHORTAGE
;
1433 bzero(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1434 crash_data_ptr
= (mach_vm_offset_t
) crash_data_kernel
;
1436 if (kr
!= KERN_SUCCESS
)
1439 /* Do not get a corpse ref for corpse fork */
1440 crash_data
= task_crashinfo_alloc_init((mach_vm_address_t
)crash_data_ptr
, size
, is_corpse_fork
? !GET_CORPSE_REF
: GET_CORPSE_REF
, corpse_blob_kernel_alloc
? KCFLAG_USE_MEMCOPY
: KCFLAG_USE_COPYOUT
);
1443 crash_data_release
= task
->corpse_info
;
1444 crash_data_kernel_release
= task
->corpse_info_kernel
;
1445 task
->corpse_info
= crash_data
;
1446 task
->corpse_info_kernel
= crash_data_kernel
;
1451 /* if failed to create corpse info, free the mapping */
1452 if (!corpse_blob_kernel_alloc
) {
1453 if (KERN_SUCCESS
!= mach_vm_deallocate(task
->map
, crash_data_ptr
, size
)) {
1454 printf("mach_vm_deallocate failed to clear corpse_data for pid %d.\n", task_pid(task
));
1457 kfree(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1462 if (crash_data_release
!= NULL
) {
1463 task_crashinfo_destroy(crash_data_release
, is_corpse_fork
? !RELEASE_CORPSE_REF
: RELEASE_CORPSE_REF
);
1465 if (crash_data_kernel_release
!= NULL
) {
1466 kfree(crash_data_kernel_release
, CORPSEINFO_ALLOCATION_SIZE
);
1477 * task_deliver_crash_notification:
1479 * Makes outcall to registered host port for a corpse.
1482 task_deliver_crash_notification(task_t task
, thread_t thread
, mach_exception_data_type_t subcode
)
1484 kcdata_descriptor_t crash_info
= task
->corpse_info
;
1485 thread_t th_iter
= NULL
;
1486 kern_return_t kr
= KERN_SUCCESS
;
1487 wait_interrupt_t wsave
;
1488 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1489 ipc_port_t task_port
, old_notify
;
1491 if (crash_info
== NULL
)
1492 return KERN_FAILURE
;
1495 if (task_is_a_corpse_fork(task
)) {
1496 /* Populate code with EXC_RESOURCE for corpse fork */
1497 code
[0] = EXC_RESOURCE
;
1499 } else if (unify_corpse_blob_alloc
) {
1500 /* Populate code with EXC_CRASH for corpses */
1501 code
[0] = EXC_CRASH
;
1503 /* Update the code[1] if the boot-arg corpse_for_fatal_memkill is set */
1504 if (corpse_for_fatal_memkill
) {
1508 /* Populate code with address and length for EXC_CRASH */
1509 code
[0] = crash_info
->kcd_addr_begin
;
1510 code
[1] = crash_info
->kcd_length
;
1512 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1514 if (th_iter
->corpse_dup
== FALSE
) {
1515 ipc_thread_reset(th_iter
);
1520 /* Arm the no-sender notification for taskport */
1521 task_reference(task
);
1522 task_port
= convert_task_to_port(task
);
1524 assert(ip_active(task_port
));
1525 ipc_port_nsrequest(task_port
, task_port
->ip_mscount
, ipc_port_make_sonce_locked(task_port
), &old_notify
);
1527 assert(IP_NULL
== old_notify
);
1529 wsave
= thread_interrupt_level(THREAD_UNINT
);
1530 kr
= exception_triage_thread(EXC_CORPSE_NOTIFY
, code
, EXCEPTION_CODE_MAX
, thread
);
1531 if (kr
!= KERN_SUCCESS
) {
1532 printf("Failed to send exception EXC_CORPSE_NOTIFY. error code: %d for pid %d\n", kr
, task_pid(task
));
1535 (void)thread_interrupt_level(wsave
);
1538 * Drop the send right on task port, will fire the
1539 * no-sender notification if exception deliver failed.
1541 ipc_port_release_send(task_port
);
1548 * Terminate the specified task. See comments on thread_terminate
1549 * (kern/thread.c) about problems with terminating the "current task."
1556 if (task
== TASK_NULL
)
1557 return (KERN_INVALID_ARGUMENT
);
1560 return (KERN_FAILURE
);
1562 return (task_terminate_internal(task
));
1566 extern int proc_pid(struct proc
*);
1567 extern void proc_name_kdp(task_t t
, char *buf
, int size
);
1568 #endif /* MACH_ASSERT */
1570 #define VM_MAP_PARTIAL_REAP 0x54 /* 0x150 */
1572 __unused
task_partial_reap(task_t task
, __unused
int pid
)
1574 unsigned int reclaimed_resident
= 0;
1575 unsigned int reclaimed_compressed
= 0;
1576 uint64_t task_page_count
;
1578 task_page_count
= (get_task_phys_footprint(task
) / PAGE_SIZE_64
);
1580 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_START
),
1581 pid
, task_page_count
, 0, 0, 0);
1583 vm_map_partial_reap(task
->map
, &reclaimed_resident
, &reclaimed_compressed
);
1585 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_END
),
1586 pid
, reclaimed_resident
, reclaimed_compressed
, 0, 0);
1590 task_mark_corpse(task_t task
)
1592 kern_return_t kr
= KERN_SUCCESS
;
1593 thread_t self_thread
;
1595 wait_interrupt_t wsave
;
1597 assert(task
!= kernel_task
);
1598 assert(task
== current_task());
1599 assert(!task_is_a_corpse(task
));
1601 kr
= task_collect_crash_info(task
, (struct proc
*)task
->bsd_info
, FALSE
);
1602 if (kr
!= KERN_SUCCESS
) {
1606 self_thread
= current_thread();
1608 wsave
= thread_interrupt_level(THREAD_UNINT
);
1611 task_set_corpse_pending_report(task
);
1612 task_set_corpse(task
);
1614 kr
= task_start_halt_locked(task
, TRUE
);
1615 assert(kr
== KERN_SUCCESS
);
1617 ipc_task_reset(task
);
1618 /* Remove the naked send right for task port, needed to arm no sender notification */
1619 task_set_special_port(task
, TASK_KERNEL_PORT
, IPC_PORT_NULL
);
1620 ipc_task_enable(task
);
1623 /* terminate the ipc space */
1624 ipc_space_terminate(task
->itk_space
);
1626 /* Add it to global corpse task list */
1627 task_add_to_corpse_task_list(task
);
1629 task_start_halt(task
);
1630 thread_terminate_internal(self_thread
);
1632 (void) thread_interrupt_level(wsave
);
1633 assert(task
->halting
== TRUE
);
1640 * Clears the corpse pending bit on task.
1641 * Removes inspection bit on the threads.
1644 task_clear_corpse(task_t task
)
1646 thread_t th_iter
= NULL
;
1649 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1651 thread_mtx_lock(th_iter
);
1652 th_iter
->inspection
= FALSE
;
1653 thread_mtx_unlock(th_iter
);
1656 thread_terminate_crashed_threads();
1657 /* remove the pending corpse report flag */
1658 task_clear_corpse_pending_report(task
);
1666 * Called whenever the Mach port system detects no-senders on
1667 * the task port of a corpse.
1668 * Each notification that comes in should terminate the task (corpse).
1671 task_port_notify(mach_msg_header_t
*msg
)
1673 mach_no_senders_notification_t
*notification
= (void *)msg
;
1674 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
1677 assert(ip_active(port
));
1678 assert(IKOT_TASK
== ip_kotype(port
));
1679 task
= (task_t
) port
->ip_kobject
;
1681 assert(task_is_a_corpse(task
));
1683 /* Remove the task from global corpse task list */
1684 task_remove_from_corpse_task_list(task
);
1686 task_clear_corpse(task
);
1687 task_terminate_internal(task
);
1691 * task_wait_till_threads_terminate_locked
1693 * Wait till all the threads in the task are terminated.
1694 * Might release the task lock and re-acquire it.
1697 task_wait_till_threads_terminate_locked(task_t task
)
1699 /* wait for all the threads in the task to terminate */
1700 while (task
->active_thread_count
!= 0) {
1701 assert_wait((event_t
)&task
->active_thread_count
, THREAD_UNINT
);
1703 thread_block(THREAD_CONTINUE_NULL
);
1710 * task_duplicate_map_and_threads
1712 * Copy vmmap of source task.
1713 * Copy active threads from source task to destination task.
1714 * Source task would be suspended during the copy.
1717 task_duplicate_map_and_threads(
1721 thread_t
*thread_ret
,
1723 uint64_t **udata_buffer
,
1727 kern_return_t kr
= KERN_SUCCESS
;
1729 thread_t thread
, self
, thread_return
= THREAD_NULL
;
1730 thread_t new_thread
= THREAD_NULL
;
1731 thread_t
*thread_array
;
1732 uint32_t active_thread_count
= 0, array_count
= 0, i
;
1734 uint64_t *buffer
= NULL
;
1736 int est_knotes
= 0, num_knotes
= 0;
1738 self
= current_thread();
1741 * Suspend the task to copy thread state, use the internal
1742 * variant so that no user-space process can resume
1743 * the task from under us
1745 kr
= task_suspend_internal(task
);
1746 if (kr
!= KERN_SUCCESS
) {
1750 if (task
->map
->disable_vmentry_reuse
== TRUE
) {
1752 * Quite likely GuardMalloc (or some debugging tool)
1753 * is being used on this task. And it has gone through
1754 * its limit. Making a corpse will likely encounter
1755 * a lot of VM entries that will need COW.
1759 task_resume_internal(task
);
1760 return KERN_FAILURE
;
1763 /* Setup new task's vmmap, switch from parent task's map to it COW map */
1764 oldmap
= new_task
->map
;
1765 new_task
->map
= vm_map_fork(new_task
->ledger
,
1767 (VM_MAP_FORK_SHARE_IF_INHERIT_NONE
|
1768 VM_MAP_FORK_PRESERVE_PURGEABLE
));
1769 vm_map_deallocate(oldmap
);
1772 vm_map_set_64bit(get_task_map(new_task
));
1774 vm_map_set_32bit(get_task_map(new_task
));
1777 /* Get all the udata pointers from kqueue */
1778 est_knotes
= proc_list_uptrs(p
, NULL
, 0);
1779 if (est_knotes
> 0) {
1780 buf_size
= (est_knotes
+ 32) * sizeof(uint64_t);
1781 buffer
= (uint64_t *) kalloc(buf_size
);
1782 num_knotes
= proc_list_uptrs(p
, buffer
, buf_size
);
1783 if (num_knotes
> est_knotes
+ 32) {
1784 num_knotes
= est_knotes
+ 32;
1788 active_thread_count
= task
->active_thread_count
;
1789 if (active_thread_count
== 0) {
1790 if (buffer
!= NULL
) {
1791 kfree(buffer
, buf_size
);
1793 task_resume_internal(task
);
1794 return KERN_FAILURE
;
1797 thread_array
= (thread_t
*) kalloc(sizeof(thread_t
) * active_thread_count
);
1799 /* Iterate all the threads and drop the task lock before calling thread_create_with_continuation */
1801 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1802 /* Skip inactive threads */
1803 active
= thread
->active
;
1808 if (array_count
>= active_thread_count
) {
1812 thread_array
[array_count
++] = thread
;
1813 thread_reference(thread
);
1817 for (i
= 0; i
< array_count
; i
++) {
1819 kr
= thread_create_with_continuation(new_task
, &new_thread
, (thread_continue_t
)thread_corpse_continue
);
1820 if (kr
!= KERN_SUCCESS
) {
1824 /* Equivalent of current thread in corpse */
1825 if (thread_array
[i
] == self
) {
1826 thread_return
= new_thread
;
1828 /* drop the extra ref returned by thread_create_with_continuation */
1829 thread_deallocate(new_thread
);
1832 kr
= thread_dup2(thread_array
[i
], new_thread
);
1833 if (kr
!= KERN_SUCCESS
) {
1834 thread_mtx_lock(new_thread
);
1835 new_thread
->corpse_dup
= TRUE
;
1836 thread_mtx_unlock(new_thread
);
1840 /* Copy thread name */
1841 bsd_copythreadname(new_thread
->uthread
, thread_array
[i
]->uthread
);
1842 thread_copy_resource_info(new_thread
, thread_array
[i
]);
1845 task_resume_internal(task
);
1847 for (i
= 0; i
< array_count
; i
++) {
1848 thread_deallocate(thread_array
[i
]);
1850 kfree(thread_array
, sizeof(thread_t
) * active_thread_count
);
1852 if (kr
== KERN_SUCCESS
) {
1853 *thread_ret
= thread_return
;
1854 *udata_buffer
= buffer
;
1856 *num_udata
= num_knotes
;
1858 if (thread_return
!= THREAD_NULL
) {
1859 thread_deallocate(thread_return
);
1861 if (buffer
!= NULL
) {
1862 kfree(buffer
, buf_size
);
1869 #if CONFIG_SECLUDED_MEMORY
1870 extern void task_set_can_use_secluded_mem_locked(
1872 boolean_t can_use_secluded_mem
);
1873 #endif /* CONFIG_SECLUDED_MEMORY */
1876 task_terminate_internal(
1879 thread_t thread
, self
;
1881 boolean_t interrupt_save
;
1884 assert(task
!= kernel_task
);
1886 self
= current_thread();
1887 self_task
= self
->task
;
1890 * Get the task locked and make sure that we are not racing
1891 * with someone else trying to terminate us.
1893 if (task
== self_task
)
1896 if (task
< self_task
) {
1898 task_lock(self_task
);
1901 task_lock(self_task
);
1905 #if CONFIG_SECLUDED_MEMORY
1906 if (task
->task_can_use_secluded_mem
) {
1907 task_set_can_use_secluded_mem_locked(task
, FALSE
);
1909 task
->task_could_use_secluded_mem
= FALSE
;
1910 task
->task_could_also_use_secluded_mem
= FALSE
;
1911 #endif /* CONFIG_SECLUDED_MEMORY */
1913 if (!task
->active
) {
1915 * Task is already being terminated.
1916 * Just return an error. If we are dying, this will
1917 * just get us to our AST special handler and that
1918 * will get us to finalize the termination of ourselves.
1921 if (self_task
!= task
)
1922 task_unlock(self_task
);
1924 return (KERN_FAILURE
);
1927 if (task_corpse_pending_report(task
)) {
1929 * Task is marked for reporting as corpse.
1930 * Just return an error. This will
1931 * just get us to our AST special handler and that
1932 * will get us to finish the path to death
1935 if (self_task
!= task
)
1936 task_unlock(self_task
);
1938 return (KERN_FAILURE
);
1941 if (self_task
!= task
)
1942 task_unlock(self_task
);
1945 * Make sure the current thread does not get aborted out of
1946 * the waits inside these operations.
1948 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
1951 * Indicate that we want all the threads to stop executing
1952 * at user space by holding the task (we would have held
1953 * each thread independently in thread_terminate_internal -
1954 * but this way we may be more likely to already find it
1955 * held there). Mark the task inactive, and prevent
1956 * further task operations via the task port.
1958 task_hold_locked(task
);
1959 task
->active
= FALSE
;
1960 ipc_task_disable(task
);
1962 #if CONFIG_TELEMETRY
1964 * Notify telemetry that this task is going away.
1966 telemetry_task_ctl_locked(task
, TF_TELEMETRY
, 0);
1970 * Terminate each thread in the task.
1972 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1973 thread_terminate_internal(thread
);
1977 if (task
->bsd_info
!= NULL
) {
1978 pid
= proc_pid(task
->bsd_info
);
1980 #endif /* MACH_BSD */
1984 proc_set_task_policy(task
, TASK_POLICY_ATTRIBUTE
,
1985 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
1987 /* Early object reap phase */
1989 // PR-17045188: Revisit implementation
1990 // task_partial_reap(task, pid);
1994 * Destroy all synchronizers owned by the task.
1996 task_synchronizer_destroy_all(task
);
1999 * Destroy the IPC space, leaving just a reference for it.
2001 ipc_space_terminate(task
->itk_space
);
2004 /* if some ledgers go negative on tear-down again... */
2005 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2006 task_ledgers
.phys_footprint
);
2007 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2008 task_ledgers
.internal
);
2009 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2010 task_ledgers
.internal_compressed
);
2011 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2012 task_ledgers
.iokit_mapped
);
2013 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2014 task_ledgers
.alternate_accounting
);
2015 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2016 task_ledgers
.alternate_accounting_compressed
);
2020 * If the current thread is a member of the task
2021 * being terminated, then the last reference to
2022 * the task will not be dropped until the thread
2023 * is finally reaped. To avoid incurring the
2024 * expense of removing the address space regions
2025 * at reap time, we do it explictly here.
2028 vm_map_lock(task
->map
);
2029 vm_map_disable_hole_optimization(task
->map
);
2030 vm_map_unlock(task
->map
);
2032 vm_map_remove(task
->map
,
2033 task
->map
->min_offset
,
2034 task
->map
->max_offset
,
2035 /* no unnesting on final cleanup: */
2036 VM_MAP_REMOVE_NO_UNNESTING
);
2038 /* release our shared region */
2039 vm_shared_region_set(task
, NULL
);
2044 * Identify the pmap's process, in case the pmap ledgers drift
2045 * and we have to report it.
2048 if (task
->bsd_info
) {
2049 pid
= proc_pid(task
->bsd_info
);
2050 proc_name_kdp(task
, procname
, sizeof (procname
));
2053 strlcpy(procname
, "<unknown>", sizeof (procname
));
2055 pmap_set_process(task
->map
->pmap
, pid
, procname
);
2056 #endif /* MACH_ASSERT */
2058 lck_mtx_lock(&tasks_threads_lock
);
2059 queue_remove(&tasks
, task
, task_t
, tasks
);
2060 queue_enter(&terminated_tasks
, task
, task_t
, tasks
);
2062 terminated_tasks_count
++;
2063 lck_mtx_unlock(&tasks_threads_lock
);
2066 * We no longer need to guard against being aborted, so restore
2067 * the previous interruptible state.
2069 thread_interrupt_level(interrupt_save
);
2072 /* force the task to release all ctrs */
2073 if (task
->t_chud
& TASK_KPC_FORCED_ALL_CTRS
)
2074 kpc_force_all_ctrs(task
, 0);
2077 #if CONFIG_COALITIONS
2079 * Leave our coalitions. (drop activation but not reference)
2081 coalitions_remove_task(task
);
2085 * Get rid of the task active reference on itself.
2087 task_deallocate(task
);
2089 return (KERN_SUCCESS
);
2093 tasks_system_suspend(boolean_t suspend
)
2097 lck_mtx_lock(&tasks_threads_lock
);
2098 assert(tasks_suspend_state
!= suspend
);
2099 tasks_suspend_state
= suspend
;
2100 queue_iterate(&tasks
, task
, task_t
, tasks
) {
2101 if (task
== kernel_task
) {
2104 suspend
? task_suspend_internal(task
) : task_resume_internal(task
);
2106 lck_mtx_unlock(&tasks_threads_lock
);
2112 * Shut the current task down (except for the current thread) in
2113 * preparation for dramatic changes to the task (probably exec).
2114 * We hold the task and mark all other threads in the task for
2118 task_start_halt(task_t task
)
2120 kern_return_t kr
= KERN_SUCCESS
;
2122 kr
= task_start_halt_locked(task
, FALSE
);
2127 static kern_return_t
2128 task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
)
2130 thread_t thread
, self
;
2131 uint64_t dispatchqueue_offset
;
2133 assert(task
!= kernel_task
);
2135 self
= current_thread();
2137 if (task
!= self
->task
&& !task_is_a_corpse_fork(task
))
2138 return (KERN_INVALID_ARGUMENT
);
2140 if (task
->halting
|| !task
->active
|| !self
->active
) {
2142 * Task or current thread is already being terminated.
2143 * Hurry up and return out of the current kernel context
2144 * so that we run our AST special handler to terminate
2147 return (KERN_FAILURE
);
2150 task
->halting
= TRUE
;
2153 * Mark all the threads to keep them from starting any more
2154 * user-level execution. The thread_terminate_internal code
2155 * would do this on a thread by thread basis anyway, but this
2156 * gives us a better chance of not having to wait there.
2158 task_hold_locked(task
);
2159 dispatchqueue_offset
= get_dispatchqueue_offset_from_proc(task
->bsd_info
);
2162 * Terminate all the other threads in the task.
2164 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
)
2166 if (should_mark_corpse
) {
2167 thread_mtx_lock(thread
);
2168 thread
->inspection
= TRUE
;
2169 thread_mtx_unlock(thread
);
2172 thread_terminate_internal(thread
);
2174 task
->dispatchqueue_offset
= dispatchqueue_offset
;
2176 task_release_locked(task
);
2178 return KERN_SUCCESS
;
2183 * task_complete_halt:
2185 * Complete task halt by waiting for threads to terminate, then clean
2186 * up task resources (VM, port namespace, etc...) and then let the
2187 * current thread go in the (practically empty) task context.
2190 task_complete_halt(task_t task
)
2193 assert(task
->halting
);
2194 assert(task
== current_task());
2197 * Wait for the other threads to get shut down.
2198 * When the last other thread is reaped, we'll be
2201 if (task
->thread_count
> 1) {
2202 assert_wait((event_t
)&task
->halting
, THREAD_UNINT
);
2204 thread_block(THREAD_CONTINUE_NULL
);
2210 * Give the machine dependent code a chance
2211 * to perform cleanup of task-level resources
2212 * associated with the current thread before
2213 * ripping apart the task.
2215 machine_task_terminate(task
);
2218 * Destroy all synchronizers owned by the task.
2220 task_synchronizer_destroy_all(task
);
2223 * Destroy the contents of the IPC space, leaving just
2224 * a reference for it.
2226 ipc_space_clean(task
->itk_space
);
2229 * Clean out the address space, as we are going to be
2230 * getting a new one.
2232 vm_map_remove(task
->map
, task
->map
->min_offset
,
2233 task
->map
->max_offset
,
2234 /* no unnesting on final cleanup: */
2235 VM_MAP_REMOVE_NO_UNNESTING
);
2238 * Kick out any IOKitUser handles to the task. At best they're stale,
2239 * at worst someone is racing a SUID exec.
2241 iokit_task_terminate(task
);
2243 task
->halting
= FALSE
;
2249 * Suspend execution of the specified task.
2250 * This is a recursive-style suspension of the task, a count of
2251 * suspends is maintained.
2253 * CONDITIONS: the task is locked and active.
2261 assert(task
->active
);
2263 if (task
->suspend_count
++ > 0)
2267 * Iterate through all the threads and hold them.
2269 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2270 thread_mtx_lock(thread
);
2271 thread_hold(thread
);
2272 thread_mtx_unlock(thread
);
2279 * Same as the internal routine above, except that is must lock
2280 * and verify that the task is active. This differs from task_suspend
2281 * in that it places a kernel hold on the task rather than just a
2282 * user-level hold. This keeps users from over resuming and setting
2283 * it running out from under the kernel.
2285 * CONDITIONS: the caller holds a reference on the task
2291 if (task
== TASK_NULL
)
2292 return (KERN_INVALID_ARGUMENT
);
2296 if (!task
->active
) {
2299 return (KERN_FAILURE
);
2302 task_hold_locked(task
);
2305 return (KERN_SUCCESS
);
2311 boolean_t until_not_runnable
)
2313 if (task
== TASK_NULL
)
2314 return (KERN_INVALID_ARGUMENT
);
2318 if (!task
->active
) {
2321 return (KERN_FAILURE
);
2324 task_wait_locked(task
, until_not_runnable
);
2327 return (KERN_SUCCESS
);
2333 * Wait for all threads in task to stop.
2336 * Called with task locked, active, and held.
2341 boolean_t until_not_runnable
)
2343 thread_t thread
, self
;
2345 assert(task
->active
);
2346 assert(task
->suspend_count
> 0);
2348 self
= current_thread();
2351 * Iterate through all the threads and wait for them to
2352 * stop. Do not wait for the current thread if it is within
2355 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2357 thread_wait(thread
, until_not_runnable
);
2362 * task_release_locked:
2364 * Release a kernel hold on a task.
2366 * CONDITIONS: the task is locked and active
2369 task_release_locked(
2374 assert(task
->active
);
2375 assert(task
->suspend_count
> 0);
2377 if (--task
->suspend_count
> 0)
2380 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2381 thread_mtx_lock(thread
);
2382 thread_release(thread
);
2383 thread_mtx_unlock(thread
);
2390 * Same as the internal routine above, except that it must lock
2391 * and verify that the task is active.
2393 * CONDITIONS: The caller holds a reference to the task
2399 if (task
== TASK_NULL
)
2400 return (KERN_INVALID_ARGUMENT
);
2404 if (!task
->active
) {
2407 return (KERN_FAILURE
);
2410 task_release_locked(task
);
2413 return (KERN_SUCCESS
);
2419 thread_act_array_t
*threads_out
,
2420 mach_msg_type_number_t
*count
)
2422 mach_msg_type_number_t actual
;
2423 thread_t
*thread_list
;
2425 vm_size_t size
, size_needed
;
2429 if (task
== TASK_NULL
)
2430 return (KERN_INVALID_ARGUMENT
);
2432 size
= 0; addr
= NULL
;
2436 if (!task
->active
) {
2442 return (KERN_FAILURE
);
2445 actual
= task
->thread_count
;
2447 /* do we have the memory we need? */
2448 size_needed
= actual
* sizeof (mach_port_t
);
2449 if (size_needed
<= size
)
2452 /* unlock the task and allocate more memory */
2458 assert(size_needed
> 0);
2461 addr
= kalloc(size
);
2463 return (KERN_RESOURCE_SHORTAGE
);
2466 /* OK, have memory and the task is locked & active */
2467 thread_list
= (thread_t
*)addr
;
2471 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
2472 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
2473 thread_reference_internal(thread
);
2474 thread_list
[j
++] = thread
;
2477 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
2480 size_needed
= actual
* sizeof (mach_port_t
);
2482 /* can unlock task now that we've got the thread refs */
2486 /* no threads, so return null pointer and deallocate memory */
2488 *threads_out
= NULL
;
2495 /* if we allocated too much, must copy */
2497 if (size_needed
< size
) {
2500 newaddr
= kalloc(size_needed
);
2502 for (i
= 0; i
< actual
; ++i
)
2503 thread_deallocate(thread_list
[i
]);
2505 return (KERN_RESOURCE_SHORTAGE
);
2508 bcopy(addr
, newaddr
, size_needed
);
2510 thread_list
= (thread_t
*)newaddr
;
2513 *threads_out
= thread_list
;
2516 /* do the conversion that Mig should handle */
2518 for (i
= 0; i
< actual
; ++i
)
2519 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
2522 return (KERN_SUCCESS
);
2525 #define TASK_HOLD_NORMAL 0
2526 #define TASK_HOLD_PIDSUSPEND 1
2527 #define TASK_HOLD_LEGACY 2
2528 #define TASK_HOLD_LEGACY_ALL 3
2530 static kern_return_t
2535 if (!task
->active
&& !task_is_a_corpse(task
)) {
2536 return (KERN_FAILURE
);
2539 /* Return success for corpse task */
2540 if (task_is_a_corpse(task
)) {
2541 return KERN_SUCCESS
;
2544 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2545 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_SUSPEND
) | DBG_FUNC_NONE
,
2546 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2547 task
->user_stop_count
, task
->user_stop_count
+ 1, 0);
2550 current_task()->suspends_outstanding
++;
2553 if (mode
== TASK_HOLD_LEGACY
)
2554 task
->legacy_stop_count
++;
2556 if (task
->user_stop_count
++ > 0) {
2558 * If the stop count was positive, the task is
2559 * already stopped and we can exit.
2561 return (KERN_SUCCESS
);
2565 * Put a kernel-level hold on the threads in the task (all
2566 * user-level task suspensions added together represent a
2567 * single kernel-level hold). We then wait for the threads
2568 * to stop executing user code.
2570 task_hold_locked(task
);
2571 task_wait_locked(task
, FALSE
);
2573 return (KERN_SUCCESS
);
2576 static kern_return_t
2581 boolean_t release
= FALSE
;
2583 if (!task
->active
&& !task_is_a_corpse(task
)) {
2584 return (KERN_FAILURE
);
2587 /* Return success for corpse task */
2588 if (task_is_a_corpse(task
)) {
2589 return KERN_SUCCESS
;
2592 if (mode
== TASK_HOLD_PIDSUSPEND
) {
2593 if (task
->pidsuspended
== FALSE
) {
2594 return (KERN_FAILURE
);
2596 task
->pidsuspended
= FALSE
;
2599 if (task
->user_stop_count
> (task
->pidsuspended
? 1 : 0)) {
2601 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2602 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_RESUME
) | DBG_FUNC_NONE
,
2603 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2604 task
->user_stop_count
, mode
, task
->legacy_stop_count
);
2608 * This is obviously not robust; if we suspend one task and then resume a different one,
2609 * we'll fly under the radar. This is only meant to catch the common case of a crashed
2610 * or buggy suspender.
2612 current_task()->suspends_outstanding
--;
2615 if (mode
== TASK_HOLD_LEGACY_ALL
) {
2616 if (task
->legacy_stop_count
>= task
->user_stop_count
) {
2617 task
->user_stop_count
= 0;
2620 task
->user_stop_count
-= task
->legacy_stop_count
;
2622 task
->legacy_stop_count
= 0;
2624 if (mode
== TASK_HOLD_LEGACY
&& task
->legacy_stop_count
> 0)
2625 task
->legacy_stop_count
--;
2626 if (--task
->user_stop_count
== 0)
2631 return (KERN_FAILURE
);
2635 * Release the task if necessary.
2638 task_release_locked(task
);
2640 return (KERN_SUCCESS
);
2647 * Implement an (old-fashioned) user-level suspension on a task.
2649 * Because the user isn't expecting to have to manage a suspension
2650 * token, we'll track it for him in the kernel in the form of a naked
2651 * send right to the task's resume port. All such send rights
2652 * account for a single suspension against the task (unlike task_suspend2()
2653 * where each caller gets a unique suspension count represented by a
2654 * unique send-once right).
2657 * The caller holds a reference to the task
2664 mach_port_t port
, send
, old_notify
;
2665 mach_port_name_t name
;
2667 if (task
== TASK_NULL
|| task
== kernel_task
)
2668 return (KERN_INVALID_ARGUMENT
);
2673 * Claim a send right on the task resume port, and request a no-senders
2674 * notification on that port (if none outstanding).
2676 if (task
->itk_resume
== IP_NULL
) {
2677 task
->itk_resume
= ipc_port_alloc_kernel();
2678 if (!IP_VALID(task
->itk_resume
))
2679 panic("failed to create resume port");
2680 ipc_kobject_set(task
->itk_resume
, (ipc_kobject_t
)task
, IKOT_TASK_RESUME
);
2683 port
= task
->itk_resume
;
2685 assert(ip_active(port
));
2687 send
= ipc_port_make_send_locked(port
);
2688 assert(IP_VALID(send
));
2690 if (port
->ip_nsrequest
== IP_NULL
) {
2691 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
2692 assert(old_notify
== IP_NULL
);
2699 * place a legacy hold on the task.
2701 kr
= place_task_hold(task
, TASK_HOLD_LEGACY
);
2702 if (kr
!= KERN_SUCCESS
) {
2704 ipc_port_release_send(send
);
2711 * Copyout the send right into the calling task's IPC space. It won't know it is there,
2712 * but we'll look it up when calling a traditional resume. Any IPC operations that
2713 * deallocate the send right will auto-release the suspension.
2715 if ((kr
= ipc_kmsg_copyout_object(current_task()->itk_space
, (ipc_object_t
)send
,
2716 MACH_MSG_TYPE_MOVE_SEND
, &name
)) != KERN_SUCCESS
) {
2717 printf("warning: %s(%d) failed to copyout suspension token for pid %d with error: %d\n",
2718 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
2719 task_pid(task
), kr
);
2728 * Release a user hold on a task.
2731 * The caller holds a reference to the task
2738 mach_port_name_t resume_port_name
;
2739 ipc_entry_t resume_port_entry
;
2740 ipc_space_t space
= current_task()->itk_space
;
2742 if (task
== TASK_NULL
|| task
== kernel_task
)
2743 return (KERN_INVALID_ARGUMENT
);
2745 /* release a legacy task hold */
2747 kr
= release_task_hold(task
, TASK_HOLD_LEGACY
);
2750 is_write_lock(space
);
2751 if (is_active(space
) && IP_VALID(task
->itk_resume
) &&
2752 ipc_hash_lookup(space
, (ipc_object_t
)task
->itk_resume
, &resume_port_name
, &resume_port_entry
) == TRUE
) {
2754 * We found a suspension token in the caller's IPC space. Release a send right to indicate that
2755 * we are holding one less legacy hold on the task from this caller. If the release failed,
2756 * go ahead and drop all the rights, as someone either already released our holds or the task
2759 if (kr
== KERN_SUCCESS
)
2760 ipc_right_dealloc(space
, resume_port_name
, resume_port_entry
);
2762 ipc_right_destroy(space
, resume_port_name
, resume_port_entry
, FALSE
, 0);
2763 /* space unlocked */
2765 is_write_unlock(space
);
2766 if (kr
== KERN_SUCCESS
)
2767 printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
2768 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
2776 * Suspend the target task.
2777 * Making/holding a token/reference/port is the callers responsibility.
2780 task_suspend_internal(task_t task
)
2784 if (task
== TASK_NULL
|| task
== kernel_task
)
2785 return (KERN_INVALID_ARGUMENT
);
2788 kr
= place_task_hold(task
, TASK_HOLD_NORMAL
);
2794 * Suspend the target task, and return a suspension token. The token
2795 * represents a reference on the suspended task.
2800 task_suspension_token_t
*suspend_token
)
2804 kr
= task_suspend_internal(task
);
2805 if (kr
!= KERN_SUCCESS
) {
2806 *suspend_token
= TASK_NULL
;
2811 * Take a reference on the target task and return that to the caller
2812 * as a "suspension token," which can be converted into an SO right to
2813 * the now-suspended task's resume port.
2815 task_reference_internal(task
);
2816 *suspend_token
= task
;
2818 return (KERN_SUCCESS
);
2823 * (reference/token/port management is caller's responsibility).
2826 task_resume_internal(
2827 task_suspension_token_t task
)
2831 if (task
== TASK_NULL
|| task
== kernel_task
)
2832 return (KERN_INVALID_ARGUMENT
);
2835 kr
= release_task_hold(task
, TASK_HOLD_NORMAL
);
2841 * Resume the task using a suspension token. Consumes the token's ref.
2845 task_suspension_token_t task
)
2849 kr
= task_resume_internal(task
);
2850 task_suspension_token_deallocate(task
);
2856 task_suspension_notify(mach_msg_header_t
*request_header
)
2858 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
2859 task_t task
= convert_port_to_task_suspension_token(port
);
2860 mach_msg_type_number_t not_count
;
2862 if (task
== TASK_NULL
|| task
== kernel_task
)
2863 return TRUE
; /* nothing to do */
2865 switch (request_header
->msgh_id
) {
2867 case MACH_NOTIFY_SEND_ONCE
:
2868 /* release the hold held by this specific send-once right */
2870 release_task_hold(task
, TASK_HOLD_NORMAL
);
2874 case MACH_NOTIFY_NO_SENDERS
:
2875 not_count
= ((mach_no_senders_notification_t
*)request_header
)->not_count
;
2879 if (port
->ip_mscount
== not_count
) {
2881 /* release all the [remaining] outstanding legacy holds */
2882 assert(port
->ip_nsrequest
== IP_NULL
);
2884 release_task_hold(task
, TASK_HOLD_LEGACY_ALL
);
2887 } else if (port
->ip_nsrequest
== IP_NULL
) {
2888 ipc_port_t old_notify
;
2891 /* new send rights, re-arm notification at current make-send count */
2892 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
2893 assert(old_notify
== IP_NULL
);
2905 task_suspension_token_deallocate(task
); /* drop token reference */
2910 task_pidsuspend_locked(task_t task
)
2914 if (task
->pidsuspended
) {
2919 task
->pidsuspended
= TRUE
;
2921 kr
= place_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
2922 if (kr
!= KERN_SUCCESS
) {
2923 task
->pidsuspended
= FALSE
;
2933 * Suspends a task by placing a hold on its threads.
2936 * The caller holds a reference to the task
2944 if (task
== TASK_NULL
|| task
== kernel_task
)
2945 return (KERN_INVALID_ARGUMENT
);
2949 kr
= task_pidsuspend_locked(task
);
2958 * Resumes a previously suspended task.
2961 * The caller holds a reference to the task
2969 if (task
== TASK_NULL
|| task
== kernel_task
)
2970 return (KERN_INVALID_ARGUMENT
);
2976 while (task
->changing_freeze_state
) {
2978 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
2980 thread_block(THREAD_CONTINUE_NULL
);
2984 task
->changing_freeze_state
= TRUE
;
2987 kr
= release_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
2995 if (kr
== KERN_SUCCESS
)
2996 task
->frozen
= FALSE
;
2997 task
->changing_freeze_state
= FALSE
;
2998 thread_wakeup(&task
->changing_freeze_state
);
3007 #if DEVELOPMENT || DEBUG
3009 extern void IOSleep(int);
3012 task_disconnect_page_mappings(task_t task
)
3016 if (task
== TASK_NULL
|| task
== kernel_task
)
3017 return (KERN_INVALID_ARGUMENT
);
3020 * this function is used to strip all of the mappings from
3021 * the pmap for the specified task to force the task to
3022 * re-fault all of the pages it is actively using... this
3023 * allows us to approximate the true working set of the
3024 * specified task. We only engage if at least 1 of the
3025 * threads in the task is runnable, but we want to continuously
3026 * sweep (at least for a while - I've arbitrarily set the limit at
3027 * 100 sweeps to be re-looked at as we gain experience) to get a better
3028 * view into what areas within a page are being visited (as opposed to only
3029 * seeing the first fault of a page after the task becomes
3030 * runnable)... in the future I may
3031 * try to block until awakened by a thread in this task
3032 * being made runnable, but for now we'll periodically poll from the
3033 * user level debug tool driving the sysctl
3035 for (n
= 0; n
< 100; n
++) {
3038 boolean_t do_unnest
;
3046 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3048 if (thread
->state
& TH_RUN
) {
3054 task
->task_disconnected_count
++;
3056 if (task
->task_unnested
== FALSE
) {
3057 if (runnable
== TRUE
) {
3058 task
->task_unnested
= TRUE
;
3064 if (runnable
== FALSE
)
3067 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_START
,
3068 task
, do_unnest
, task
->task_disconnected_count
, 0, 0);
3070 page_count
= vm_map_disconnect_page_mappings(task
->map
, do_unnest
);
3072 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_END
,
3073 task
, page_count
, 0, 0, 0);
3078 return (KERN_SUCCESS
);
3092 * The caller holds a reference to the task
3094 extern void vm_wake_compactor_swapper();
3095 extern queue_head_t c_swapout_list_head
;
3100 uint32_t *purgeable_count
,
3101 uint32_t *wired_count
,
3102 uint32_t *clean_count
,
3103 uint32_t *dirty_count
,
3104 uint32_t dirty_budget
,
3106 boolean_t walk_only
)
3108 kern_return_t kr
= KERN_SUCCESS
;
3110 if (task
== TASK_NULL
|| task
== kernel_task
)
3111 return (KERN_INVALID_ARGUMENT
);
3115 while (task
->changing_freeze_state
) {
3117 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3119 thread_block(THREAD_CONTINUE_NULL
);
3125 return (KERN_FAILURE
);
3127 task
->changing_freeze_state
= TRUE
;
3132 panic("task_freeze - walk_only == TRUE");
3134 kr
= vm_map_freeze(task
->map
, purgeable_count
, wired_count
, clean_count
, dirty_count
, dirty_budget
, shared
);
3139 if (walk_only
== FALSE
&& kr
== KERN_SUCCESS
)
3140 task
->frozen
= TRUE
;
3141 task
->changing_freeze_state
= FALSE
;
3142 thread_wakeup(&task
->changing_freeze_state
);
3146 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
) {
3147 vm_wake_compactor_swapper();
3149 * We do an explicit wakeup of the swapout thread here
3150 * because the compact_and_swap routines don't have
3151 * knowledge about these kind of "per-task packed c_segs"
3152 * and so will not be evaluating whether we need to do
3155 thread_wakeup((event_t
)&c_swapout_list_head
);
3164 * Thaw a currently frozen task.
3167 * The caller holds a reference to the task
3173 if (task
== TASK_NULL
|| task
== kernel_task
)
3174 return (KERN_INVALID_ARGUMENT
);
3178 while (task
->changing_freeze_state
) {
3180 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3182 thread_block(THREAD_CONTINUE_NULL
);
3186 if (!task
->frozen
) {
3188 return (KERN_FAILURE
);
3190 task
->frozen
= FALSE
;
3194 return (KERN_SUCCESS
);
3197 #endif /* CONFIG_FREEZE */
3200 host_security_set_task_token(
3201 host_security_t host_security
,
3203 security_token_t sec_token
,
3204 audit_token_t audit_token
,
3205 host_priv_t host_priv
)
3207 ipc_port_t host_port
;
3210 if (task
== TASK_NULL
)
3211 return(KERN_INVALID_ARGUMENT
);
3213 if (host_security
== HOST_NULL
)
3214 return(KERN_INVALID_SECURITY
);
3217 task
->sec_token
= sec_token
;
3218 task
->audit_token
= audit_token
;
3222 if (host_priv
!= HOST_PRIV_NULL
) {
3223 kr
= host_get_host_priv_port(host_priv
, &host_port
);
3225 kr
= host_get_host_port(host_priv_self(), &host_port
);
3227 assert(kr
== KERN_SUCCESS
);
3228 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
3233 task_send_trace_memory(
3235 __unused
uint32_t pid
,
3236 __unused
uint64_t uniqueid
)
3238 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
3239 if (target_task
== TASK_NULL
)
3240 return (KERN_INVALID_ARGUMENT
);
3243 kr
= atm_send_proc_inspect_notification(target_task
,
3251 * This routine was added, pretty much exclusively, for registering the
3252 * RPC glue vector for in-kernel short circuited tasks. Rather than
3253 * removing it completely, I have only disabled that feature (which was
3254 * the only feature at the time). It just appears that we are going to
3255 * want to add some user data to tasks in the future (i.e. bsd info,
3256 * task names, etc...), so I left it in the formal task interface.
3261 task_flavor_t flavor
,
3262 __unused task_info_t task_info_in
, /* pointer to IN array */
3263 __unused mach_msg_type_number_t task_info_count
)
3265 if (task
== TASK_NULL
)
3266 return(KERN_INVALID_ARGUMENT
);
3271 case TASK_TRACE_MEMORY_INFO
:
3273 if (task_info_count
!= TASK_TRACE_MEMORY_INFO_COUNT
)
3274 return (KERN_INVALID_ARGUMENT
);
3276 assert(task_info_in
!= NULL
);
3277 task_trace_memory_info_t mem_info
;
3278 mem_info
= (task_trace_memory_info_t
) task_info_in
;
3279 kern_return_t kr
= atm_register_trace_memory(task
,
3280 mem_info
->user_memory_address
,
3281 mem_info
->buffer_size
);
3287 return (KERN_INVALID_ARGUMENT
);
3289 return (KERN_SUCCESS
);
3292 int radar_20146450
= 1;
3296 task_flavor_t flavor
,
3297 task_info_t task_info_out
,
3298 mach_msg_type_number_t
*task_info_count
)
3300 kern_return_t error
= KERN_SUCCESS
;
3301 mach_msg_type_number_t original_task_info_count
;
3303 if (task
== TASK_NULL
)
3304 return (KERN_INVALID_ARGUMENT
);
3306 original_task_info_count
= *task_info_count
;
3309 if ((task
!= current_task()) && (!task
->active
)) {
3311 return (KERN_INVALID_ARGUMENT
);
3316 case TASK_BASIC_INFO_32
:
3317 case TASK_BASIC2_INFO_32
:
3319 task_basic_info_32_t basic_info
;
3324 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
) {
3325 error
= KERN_INVALID_ARGUMENT
;
3329 basic_info
= (task_basic_info_32_t
)task_info_out
;
3331 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3332 basic_info
->virtual_size
= (typeof(basic_info
->virtual_size
))map
->size
;
3333 if (flavor
== TASK_BASIC2_INFO_32
) {
3335 * The "BASIC2" flavor gets the maximum resident
3336 * size instead of the current resident size...
3338 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
3340 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
3342 basic_info
->resident_size
*= PAGE_SIZE
;
3344 basic_info
->policy
= ((task
!= kernel_task
)?
3345 POLICY_TIMESHARE
: POLICY_RR
);
3346 basic_info
->suspend_count
= task
->user_stop_count
;
3348 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3349 basic_info
->user_time
.seconds
=
3350 (typeof(basic_info
->user_time
.seconds
))secs
;
3351 basic_info
->user_time
.microseconds
= usecs
;
3353 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3354 basic_info
->system_time
.seconds
=
3355 (typeof(basic_info
->system_time
.seconds
))secs
;
3356 basic_info
->system_time
.microseconds
= usecs
;
3358 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
3362 case TASK_BASIC_INFO_64
:
3364 task_basic_info_64_t basic_info
;
3369 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
) {
3370 error
= KERN_INVALID_ARGUMENT
;
3374 basic_info
= (task_basic_info_64_t
)task_info_out
;
3376 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3377 basic_info
->virtual_size
= map
->size
;
3378 basic_info
->resident_size
=
3379 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
3382 basic_info
->policy
= ((task
!= kernel_task
)?
3383 POLICY_TIMESHARE
: POLICY_RR
);
3384 basic_info
->suspend_count
= task
->user_stop_count
;
3386 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3387 basic_info
->user_time
.seconds
=
3388 (typeof(basic_info
->user_time
.seconds
))secs
;
3389 basic_info
->user_time
.microseconds
= usecs
;
3391 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3392 basic_info
->system_time
.seconds
=
3393 (typeof(basic_info
->system_time
.seconds
))secs
;
3394 basic_info
->system_time
.microseconds
= usecs
;
3396 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
3400 case MACH_TASK_BASIC_INFO
:
3402 mach_task_basic_info_t basic_info
;
3407 if (*task_info_count
< MACH_TASK_BASIC_INFO_COUNT
) {
3408 error
= KERN_INVALID_ARGUMENT
;
3412 basic_info
= (mach_task_basic_info_t
)task_info_out
;
3414 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
3416 basic_info
->virtual_size
= map
->size
;
3418 basic_info
->resident_size
=
3419 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
));
3420 basic_info
->resident_size
*= PAGE_SIZE_64
;
3422 basic_info
->resident_size_max
=
3423 (mach_vm_size_t
)(pmap_resident_max(map
->pmap
));
3424 basic_info
->resident_size_max
*= PAGE_SIZE_64
;
3426 basic_info
->policy
= ((task
!= kernel_task
) ?
3427 POLICY_TIMESHARE
: POLICY_RR
);
3429 basic_info
->suspend_count
= task
->user_stop_count
;
3431 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3432 basic_info
->user_time
.seconds
=
3433 (typeof(basic_info
->user_time
.seconds
))secs
;
3434 basic_info
->user_time
.microseconds
= usecs
;
3436 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3437 basic_info
->system_time
.seconds
=
3438 (typeof(basic_info
->system_time
.seconds
))secs
;
3439 basic_info
->system_time
.microseconds
= usecs
;
3441 *task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
3445 case TASK_THREAD_TIMES_INFO
:
3447 task_thread_times_info_t times_info
;
3450 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
3451 error
= KERN_INVALID_ARGUMENT
;
3455 times_info
= (task_thread_times_info_t
) task_info_out
;
3456 times_info
->user_time
.seconds
= 0;
3457 times_info
->user_time
.microseconds
= 0;
3458 times_info
->system_time
.seconds
= 0;
3459 times_info
->system_time
.microseconds
= 0;
3462 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3463 time_value_t user_time
, system_time
;
3465 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3468 thread_read_times(thread
, &user_time
, &system_time
);
3470 time_value_add(×_info
->user_time
, &user_time
);
3471 time_value_add(×_info
->system_time
, &system_time
);
3474 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
3478 case TASK_ABSOLUTETIME_INFO
:
3480 task_absolutetime_info_t info
;
3483 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
) {
3484 error
= KERN_INVALID_ARGUMENT
;
3488 info
= (task_absolutetime_info_t
)task_info_out
;
3489 info
->threads_user
= info
->threads_system
= 0;
3492 info
->total_user
= task
->total_user_time
;
3493 info
->total_system
= task
->total_system_time
;
3495 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3499 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3503 thread_lock(thread
);
3505 tval
= timer_grab(&thread
->user_timer
);
3506 info
->threads_user
+= tval
;
3507 info
->total_user
+= tval
;
3509 tval
= timer_grab(&thread
->system_timer
);
3510 if (thread
->precise_user_kernel_time
) {
3511 info
->threads_system
+= tval
;
3512 info
->total_system
+= tval
;
3514 /* system_timer may represent either sys or user */
3515 info
->threads_user
+= tval
;
3516 info
->total_user
+= tval
;
3519 thread_unlock(thread
);
3524 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
3528 case TASK_DYLD_INFO
:
3530 task_dyld_info_t info
;
3533 * We added the format field to TASK_DYLD_INFO output. For
3534 * temporary backward compatibility, accept the fact that
3535 * clients may ask for the old version - distinquished by the
3536 * size of the expected result structure.
3538 #define TASK_LEGACY_DYLD_INFO_COUNT \
3539 offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
3541 if (*task_info_count
< TASK_LEGACY_DYLD_INFO_COUNT
) {
3542 error
= KERN_INVALID_ARGUMENT
;
3546 info
= (task_dyld_info_t
)task_info_out
;
3547 info
->all_image_info_addr
= task
->all_image_info_addr
;
3548 info
->all_image_info_size
= task
->all_image_info_size
;
3550 /* only set format on output for those expecting it */
3551 if (*task_info_count
>= TASK_DYLD_INFO_COUNT
) {
3552 info
->all_image_info_format
= task_has_64BitAddr(task
) ?
3553 TASK_DYLD_ALL_IMAGE_INFO_64
:
3554 TASK_DYLD_ALL_IMAGE_INFO_32
;
3555 *task_info_count
= TASK_DYLD_INFO_COUNT
;
3557 *task_info_count
= TASK_LEGACY_DYLD_INFO_COUNT
;
3562 case TASK_EXTMOD_INFO
:
3564 task_extmod_info_t info
;
3567 if (*task_info_count
< TASK_EXTMOD_INFO_COUNT
) {
3568 error
= KERN_INVALID_ARGUMENT
;
3572 info
= (task_extmod_info_t
)task_info_out
;
3574 p
= get_bsdtask_info(task
);
3576 proc_getexecutableuuid(p
, info
->task_uuid
, sizeof(info
->task_uuid
));
3578 bzero(info
->task_uuid
, sizeof(info
->task_uuid
));
3580 info
->extmod_statistics
= task
->extmod_statistics
;
3581 *task_info_count
= TASK_EXTMOD_INFO_COUNT
;
3586 case TASK_KERNELMEMORY_INFO
:
3588 task_kernelmemory_info_t tkm_info
;
3589 ledger_amount_t credit
, debit
;
3591 if (*task_info_count
< TASK_KERNELMEMORY_INFO_COUNT
) {
3592 error
= KERN_INVALID_ARGUMENT
;
3596 tkm_info
= (task_kernelmemory_info_t
) task_info_out
;
3597 tkm_info
->total_palloc
= 0;
3598 tkm_info
->total_pfree
= 0;
3599 tkm_info
->total_salloc
= 0;
3600 tkm_info
->total_sfree
= 0;
3602 if (task
== kernel_task
) {
3604 * All shared allocs/frees from other tasks count against
3605 * the kernel private memory usage. If we are looking up
3606 * info for the kernel task, gather from everywhere.
3610 /* start by accounting for all the terminated tasks against the kernel */
3611 tkm_info
->total_palloc
= tasks_tkm_private
.alloc
+ tasks_tkm_shared
.alloc
;
3612 tkm_info
->total_pfree
= tasks_tkm_private
.free
+ tasks_tkm_shared
.free
;
3614 /* count all other task/thread shared alloc/free against the kernel */
3615 lck_mtx_lock(&tasks_threads_lock
);
3617 /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
3618 queue_iterate(&tasks
, task
, task_t
, tasks
) {
3619 if (task
== kernel_task
) {
3620 if (ledger_get_entries(task
->ledger
,
3621 task_ledgers
.tkm_private
, &credit
,
3622 &debit
) == KERN_SUCCESS
) {
3623 tkm_info
->total_palloc
+= credit
;
3624 tkm_info
->total_pfree
+= debit
;
3627 if (!ledger_get_entries(task
->ledger
,
3628 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3629 tkm_info
->total_palloc
+= credit
;
3630 tkm_info
->total_pfree
+= debit
;
3633 lck_mtx_unlock(&tasks_threads_lock
);
3635 if (!ledger_get_entries(task
->ledger
,
3636 task_ledgers
.tkm_private
, &credit
, &debit
)) {
3637 tkm_info
->total_palloc
= credit
;
3638 tkm_info
->total_pfree
= debit
;
3640 if (!ledger_get_entries(task
->ledger
,
3641 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3642 tkm_info
->total_salloc
= credit
;
3643 tkm_info
->total_sfree
= debit
;
3648 *task_info_count
= TASK_KERNELMEMORY_INFO_COUNT
;
3649 return KERN_SUCCESS
;
3653 case TASK_SCHED_FIFO_INFO
:
3656 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
) {
3657 error
= KERN_INVALID_ARGUMENT
;
3661 error
= KERN_INVALID_POLICY
;
3666 case TASK_SCHED_RR_INFO
:
3668 policy_rr_base_t rr_base
;
3669 uint32_t quantum_time
;
3670 uint64_t quantum_ns
;
3672 if (*task_info_count
< POLICY_RR_BASE_COUNT
) {
3673 error
= KERN_INVALID_ARGUMENT
;
3677 rr_base
= (policy_rr_base_t
) task_info_out
;
3679 if (task
!= kernel_task
) {
3680 error
= KERN_INVALID_POLICY
;
3684 rr_base
->base_priority
= task
->priority
;
3686 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
3687 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
3689 rr_base
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
3691 *task_info_count
= POLICY_RR_BASE_COUNT
;
3696 case TASK_SCHED_TIMESHARE_INFO
:
3698 policy_timeshare_base_t ts_base
;
3700 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
) {
3701 error
= KERN_INVALID_ARGUMENT
;
3705 ts_base
= (policy_timeshare_base_t
) task_info_out
;
3707 if (task
== kernel_task
) {
3708 error
= KERN_INVALID_POLICY
;
3712 ts_base
->base_priority
= task
->priority
;
3714 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
3718 case TASK_SECURITY_TOKEN
:
3720 security_token_t
*sec_token_p
;
3722 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
3723 error
= KERN_INVALID_ARGUMENT
;
3727 sec_token_p
= (security_token_t
*) task_info_out
;
3729 *sec_token_p
= task
->sec_token
;
3731 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
3735 case TASK_AUDIT_TOKEN
:
3737 audit_token_t
*audit_token_p
;
3739 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
3740 error
= KERN_INVALID_ARGUMENT
;
3744 audit_token_p
= (audit_token_t
*) task_info_out
;
3746 *audit_token_p
= task
->audit_token
;
3748 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
3752 case TASK_SCHED_INFO
:
3753 error
= KERN_INVALID_ARGUMENT
;
3756 case TASK_EVENTS_INFO
:
3758 task_events_info_t events_info
;
3761 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
3762 error
= KERN_INVALID_ARGUMENT
;
3766 events_info
= (task_events_info_t
) task_info_out
;
3769 events_info
->faults
= task
->faults
;
3770 events_info
->pageins
= task
->pageins
;
3771 events_info
->cow_faults
= task
->cow_faults
;
3772 events_info
->messages_sent
= task
->messages_sent
;
3773 events_info
->messages_received
= task
->messages_received
;
3774 events_info
->syscalls_mach
= task
->syscalls_mach
;
3775 events_info
->syscalls_unix
= task
->syscalls_unix
;
3777 events_info
->csw
= task
->c_switch
;
3779 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3780 events_info
->csw
+= thread
->c_switch
;
3781 events_info
->syscalls_mach
+= thread
->syscalls_mach
;
3782 events_info
->syscalls_unix
+= thread
->syscalls_unix
;
3786 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
3789 case TASK_AFFINITY_TAG_INFO
:
3791 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
) {
3792 error
= KERN_INVALID_ARGUMENT
;
3796 error
= task_affinity_info(task
, task_info_out
, task_info_count
);
3799 case TASK_POWER_INFO
:
3801 if (*task_info_count
< TASK_POWER_INFO_COUNT
) {
3802 error
= KERN_INVALID_ARGUMENT
;
3806 task_power_info_locked(task
, (task_power_info_t
)task_info_out
, NULL
, NULL
);
3810 case TASK_POWER_INFO_V2
:
3812 if (*task_info_count
< TASK_POWER_INFO_V2_COUNT
) {
3813 error
= KERN_INVALID_ARGUMENT
;
3816 task_power_info_v2_t tpiv2
= (task_power_info_v2_t
) task_info_out
;
3818 uint64_t *task_energy
= NULL
;
3819 task_power_info_locked(task
, &tpiv2
->cpu_energy
, &tpiv2
->gpu_energy
, task_energy
);
3824 case TASK_VM_INFO_PURGEABLE
:
3826 task_vm_info_t vm_info
;
3829 if (*task_info_count
< TASK_VM_INFO_REV0_COUNT
) {
3830 error
= KERN_INVALID_ARGUMENT
;
3834 vm_info
= (task_vm_info_t
)task_info_out
;
3836 if (task
== kernel_task
) {
3841 vm_map_lock_read(map
);
3844 vm_info
->virtual_size
= (typeof(vm_info
->virtual_size
))map
->size
;
3845 vm_info
->region_count
= map
->hdr
.nentries
;
3846 vm_info
->page_size
= vm_map_page_size(map
);
3848 vm_info
->resident_size
= pmap_resident_count(map
->pmap
);
3849 vm_info
->resident_size
*= PAGE_SIZE
;
3850 vm_info
->resident_size_peak
= pmap_resident_max(map
->pmap
);
3851 vm_info
->resident_size_peak
*= PAGE_SIZE
;
3853 #define _VM_INFO(_name) \
3854 vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
3857 _VM_INFO(device_peak
);
3859 _VM_INFO(external_peak
);
3861 _VM_INFO(internal_peak
);
3863 _VM_INFO(reusable_peak
);
3864 _VM_INFO(compressed
);
3865 _VM_INFO(compressed_peak
);
3866 _VM_INFO(compressed_lifetime
);
3868 vm_info
->purgeable_volatile_pmap
= 0;
3869 vm_info
->purgeable_volatile_resident
= 0;
3870 vm_info
->purgeable_volatile_virtual
= 0;
3871 if (task
== kernel_task
) {
3873 * We do not maintain the detailed stats for the
3874 * kernel_pmap, so just count everything as
3877 vm_info
->internal
= vm_info
->resident_size
;
3879 * ... but since the memory held by the VM compressor
3880 * in the kernel address space ought to be attributed
3881 * to user-space tasks, we subtract it from "internal"
3882 * to give memory reporting tools a more accurate idea
3883 * of what the kernel itself is actually using, instead
3884 * of making it look like the kernel is leaking memory
3885 * when the system is under memory pressure.
3887 vm_info
->internal
-= (VM_PAGE_COMPRESSOR_COUNT
*
3890 mach_vm_size_t volatile_virtual_size
;
3891 mach_vm_size_t volatile_resident_size
;
3892 mach_vm_size_t volatile_compressed_size
;
3893 mach_vm_size_t volatile_pmap_size
;
3894 mach_vm_size_t volatile_compressed_pmap_size
;
3897 if (flavor
== TASK_VM_INFO_PURGEABLE
) {
3898 kr
= vm_map_query_volatile(
3900 &volatile_virtual_size
,
3901 &volatile_resident_size
,
3902 &volatile_compressed_size
,
3903 &volatile_pmap_size
,
3904 &volatile_compressed_pmap_size
);
3905 if (kr
== KERN_SUCCESS
) {
3906 vm_info
->purgeable_volatile_pmap
=
3908 if (radar_20146450
) {
3909 vm_info
->compressed
-=
3910 volatile_compressed_pmap_size
;
3912 vm_info
->purgeable_volatile_resident
=
3913 volatile_resident_size
;
3914 vm_info
->purgeable_volatile_virtual
=
3915 volatile_virtual_size
;
3919 *task_info_count
= TASK_VM_INFO_REV0_COUNT
;
3921 if (original_task_info_count
>= TASK_VM_INFO_REV1_COUNT
) {
3922 vm_info
->phys_footprint
=
3923 (mach_vm_size_t
) get_task_phys_footprint(task
);
3924 *task_info_count
= TASK_VM_INFO_REV1_COUNT
;
3926 if (original_task_info_count
>= TASK_VM_INFO_REV2_COUNT
) {
3927 vm_info
->min_address
= map
->min_offset
;
3928 vm_info
->max_address
= map
->max_offset
;
3929 *task_info_count
= TASK_VM_INFO_REV2_COUNT
;
3932 if (task
!= kernel_task
) {
3933 vm_map_unlock_read(map
);
3939 case TASK_WAIT_STATE_INFO
:
3942 * Deprecated flavor. Currently allowing some results until all users
3943 * stop calling it. The results may not be accurate.
3945 task_wait_state_info_t wait_state_info
;
3946 uint64_t total_sfi_ledger_val
= 0;
3948 if (*task_info_count
< TASK_WAIT_STATE_INFO_COUNT
) {
3949 error
= KERN_INVALID_ARGUMENT
;
3953 wait_state_info
= (task_wait_state_info_t
) task_info_out
;
3955 wait_state_info
->total_wait_state_time
= 0;
3956 bzero(wait_state_info
->_reserved
, sizeof(wait_state_info
->_reserved
));
3958 #if CONFIG_SCHED_SFI
3959 int i
, prev_lentry
= -1;
3960 int64_t val_credit
, val_debit
;
3962 for (i
= 0; i
< MAX_SFI_CLASS_ID
; i
++){
3965 * checking with prev_lentry != entry ensures adjacent classes
3966 * which share the same ledger do not add wait times twice.
3967 * Note: Use ledger() call to get data for each individual sfi class.
3969 if (prev_lentry
!= task_ledgers
.sfi_wait_times
[i
] &&
3970 KERN_SUCCESS
== ledger_get_entries(task
->ledger
,
3971 task_ledgers
.sfi_wait_times
[i
], &val_credit
, &val_debit
)) {
3972 total_sfi_ledger_val
+= val_credit
;
3974 prev_lentry
= task_ledgers
.sfi_wait_times
[i
];
3977 #endif /* CONFIG_SCHED_SFI */
3978 wait_state_info
->total_wait_sfi_state_time
= total_sfi_ledger_val
;
3979 *task_info_count
= TASK_WAIT_STATE_INFO_COUNT
;
3983 case TASK_VM_INFO_PURGEABLE_ACCOUNT
:
3985 #if DEVELOPMENT || DEBUG
3986 pvm_account_info_t acnt_info
;
3988 if (*task_info_count
< PVM_ACCOUNT_INFO_COUNT
) {
3989 error
= KERN_INVALID_ARGUMENT
;
3993 if (task_info_out
== NULL
) {
3994 error
= KERN_INVALID_ARGUMENT
;
3998 acnt_info
= (pvm_account_info_t
) task_info_out
;
4000 error
= vm_purgeable_account(task
, acnt_info
);
4002 *task_info_count
= PVM_ACCOUNT_INFO_COUNT
;
4005 #else /* DEVELOPMENT || DEBUG */
4006 error
= KERN_NOT_SUPPORTED
;
4008 #endif /* DEVELOPMENT || DEBUG */
4010 case TASK_FLAGS_INFO
:
4012 task_flags_info_t flags_info
;
4014 if (*task_info_count
< TASK_FLAGS_INFO_COUNT
) {
4015 error
= KERN_INVALID_ARGUMENT
;
4019 flags_info
= (task_flags_info_t
)task_info_out
;
4021 /* only publish the 64-bit flag of the task */
4022 flags_info
->flags
= task
->t_flags
& TF_64B_ADDR
;
4024 *task_info_count
= TASK_FLAGS_INFO_COUNT
;
4028 case TASK_DEBUG_INFO_INTERNAL
:
4030 #if DEVELOPMENT || DEBUG
4031 task_debug_info_internal_t dbg_info
;
4032 if (*task_info_count
< TASK_DEBUG_INFO_INTERNAL_COUNT
) {
4033 error
= KERN_NOT_SUPPORTED
;
4037 if (task_info_out
== NULL
) {
4038 error
= KERN_INVALID_ARGUMENT
;
4041 dbg_info
= (task_debug_info_internal_t
) task_info_out
;
4042 dbg_info
->ipc_space_size
= 0;
4043 if (task
->itk_space
){
4044 dbg_info
->ipc_space_size
= task
->itk_space
->is_table_size
;
4047 error
= KERN_SUCCESS
;
4048 *task_info_count
= TASK_DEBUG_INFO_INTERNAL_COUNT
;
4050 #else /* DEVELOPMENT || DEBUG */
4051 error
= KERN_NOT_SUPPORTED
;
4053 #endif /* DEVELOPMENT || DEBUG */
4056 error
= KERN_INVALID_ARGUMENT
;
4066 * Returns power stats for the task.
4067 * Note: Called with task locked.
4070 task_power_info_locked(
4072 task_power_info_t info
,
4073 gpu_energy_data_t ginfo
,
4074 uint64_t *task_energy
)
4077 ledger_amount_t tmp
;
4079 task_lock_assert_owned(task
);
4081 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
4082 (ledger_amount_t
*)&info
->task_interrupt_wakeups
, &tmp
);
4083 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
4084 (ledger_amount_t
*)&info
->task_platform_idle_wakeups
, &tmp
);
4086 info
->task_timer_wakeups_bin_1
= task
->task_timer_wakeups_bin_1
;
4087 info
->task_timer_wakeups_bin_2
= task
->task_timer_wakeups_bin_2
;
4089 info
->total_user
= task
->total_user_time
;
4090 info
->total_system
= task
->total_system_time
;
4093 *task_energy
= task
->task_energy
;
4097 ginfo
->task_gpu_utilisation
= task
->task_gpu_ns
;
4100 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4104 if (thread
->options
& TH_OPT_IDLE_THREAD
)
4108 thread_lock(thread
);
4110 info
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
4111 info
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
4114 *task_energy
+= ml_energy_stat(thread
);
4117 tval
= timer_grab(&thread
->user_timer
);
4118 info
->total_user
+= tval
;
4120 tval
= timer_grab(&thread
->system_timer
);
4121 if (thread
->precise_user_kernel_time
) {
4122 info
->total_system
+= tval
;
4124 /* system_timer may represent either sys or user */
4125 info
->total_user
+= tval
;
4129 ginfo
->task_gpu_utilisation
+= ml_gpu_stat(thread
);
4131 thread_unlock(thread
);
4137 * task_gpu_utilisation
4139 * Returns the total gpu time used by the all the threads of the task
4140 * (both dead and alive)
4143 task_gpu_utilisation(
4146 uint64_t gpu_time
= 0;
4150 gpu_time
+= task
->task_gpu_ns
;
4152 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4155 thread_lock(thread
);
4156 gpu_time
+= ml_gpu_stat(thread
);
4157 thread_unlock(thread
);
4168 * Returns the total energy used by the all the threads of the task
4169 * (both dead and alive)
4175 uint64_t energy
= 0;
4179 energy
+= task
->task_energy
;
4181 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4184 thread_lock(thread
);
4185 energy
+= ml_energy_stat(thread
);
4186 thread_unlock(thread
);
4197 task_purgable_info_t
*stats
)
4199 if (task
== TASK_NULL
|| stats
== NULL
)
4200 return KERN_INVALID_ARGUMENT
;
4201 /* Take task reference */
4202 task_reference(task
);
4203 vm_purgeable_stats((vm_purgeable_info_t
)stats
, task
);
4204 /* Drop task reference */
4205 task_deallocate(task
);
4206 return KERN_SUCCESS
;
4219 task
->vtimers
|= which
;
4223 case TASK_VTIMER_USER
:
4224 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4226 thread_lock(thread
);
4227 if (thread
->precise_user_kernel_time
)
4228 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
4230 thread
->vtimer_user_save
= timer_grab(&thread
->system_timer
);
4231 thread_unlock(thread
);
4236 case TASK_VTIMER_PROF
:
4237 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4239 thread_lock(thread
);
4240 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
4241 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
4242 thread_unlock(thread
);
4247 case TASK_VTIMER_RLIM
:
4248 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4250 thread_lock(thread
);
4251 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
4252 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
4253 thread_unlock(thread
);
4267 assert(task
== current_task());
4271 task
->vtimers
&= ~which
;
4281 uint32_t *microsecs
)
4283 thread_t thread
= current_thread();
4285 clock_sec_t secs
= 0;
4288 assert(task
== current_task());
4290 spl_t s
= splsched();
4291 thread_lock(thread
);
4293 if ((task
->vtimers
& which
) != (uint32_t)which
) {
4294 thread_unlock(thread
);
4301 case TASK_VTIMER_USER
:
4302 if (thread
->precise_user_kernel_time
) {
4303 tdelt
= (uint32_t)timer_delta(&thread
->user_timer
,
4304 &thread
->vtimer_user_save
);
4306 tdelt
= (uint32_t)timer_delta(&thread
->system_timer
,
4307 &thread
->vtimer_user_save
);
4309 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4312 case TASK_VTIMER_PROF
:
4313 tsum
= timer_grab(&thread
->user_timer
);
4314 tsum
+= timer_grab(&thread
->system_timer
);
4315 tdelt
= (uint32_t)(tsum
- thread
->vtimer_prof_save
);
4316 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4317 /* if the time delta is smaller than a usec, ignore */
4318 if (*microsecs
!= 0)
4319 thread
->vtimer_prof_save
= tsum
;
4322 case TASK_VTIMER_RLIM
:
4323 tsum
= timer_grab(&thread
->user_timer
);
4324 tsum
+= timer_grab(&thread
->system_timer
);
4325 tdelt
= (uint32_t)(tsum
- thread
->vtimer_rlim_save
);
4326 thread
->vtimer_rlim_save
= tsum
;
4327 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4331 thread_unlock(thread
);
4338 * Change the assigned processor set for the task
4342 __unused task_t task
,
4343 __unused processor_set_t new_pset
,
4344 __unused boolean_t assign_threads
)
4346 return(KERN_FAILURE
);
4350 * task_assign_default:
4352 * Version of task_assign to assign to default processor set.
4355 task_assign_default(
4357 boolean_t assign_threads
)
4359 return (task_assign(task
, &pset0
, assign_threads
));
4363 * task_get_assignment
4365 * Return name of processor set that task is assigned to.
4368 task_get_assignment(
4370 processor_set_t
*pset
)
4372 if (!task
|| !task
->active
)
4373 return KERN_FAILURE
;
4377 return KERN_SUCCESS
;
4381 get_task_dispatchqueue_offset(
4384 return task
->dispatchqueue_offset
;
4390 * Set scheduling policy and parameters, both base and limit, for
4391 * the given task. Policy must be a policy which is enabled for the
4392 * processor set. Change contained threads if requested.
4396 __unused task_t task
,
4397 __unused policy_t policy_id
,
4398 __unused policy_base_t base
,
4399 __unused mach_msg_type_number_t count
,
4400 __unused boolean_t set_limit
,
4401 __unused boolean_t change
)
4403 return(KERN_FAILURE
);
4409 * Set scheduling policy and parameters, both base and limit, for
4410 * the given task. Policy can be any policy implemented by the
4411 * processor set, whether enabled or not. Change contained threads
4416 __unused task_t task
,
4417 __unused processor_set_t pset
,
4418 __unused policy_t policy_id
,
4419 __unused policy_base_t base
,
4420 __unused mach_msg_type_number_t base_count
,
4421 __unused policy_limit_t limit
,
4422 __unused mach_msg_type_number_t limit_count
,
4423 __unused boolean_t change
)
4425 return(KERN_FAILURE
);
4430 __unused task_t task
,
4431 __unused vm_offset_t pc
,
4432 __unused vm_offset_t endpc
)
4434 return KERN_FAILURE
;
4438 task_synchronizer_destroy_all(task_t task
)
4441 * Destroy owned semaphores
4443 semaphore_destroy_all(task
);
4447 * Install default (machine-dependent) initial thread state
4448 * on the task. Subsequent thread creation will have this initial
4449 * state set on the thread by machine_thread_inherit_taskwide().
4450 * Flavors and structures are exactly the same as those to thread_set_state()
4456 thread_state_t state
,
4457 mach_msg_type_number_t state_count
)
4461 if (task
== TASK_NULL
) {
4462 return (KERN_INVALID_ARGUMENT
);
4467 if (!task
->active
) {
4469 return (KERN_FAILURE
);
4472 ret
= machine_task_set_state(task
, flavor
, state
, state_count
);
4479 * Examine the default (machine-dependent) initial thread state
4480 * on the task, as set by task_set_state(). Flavors and structures
4481 * are exactly the same as those passed to thread_get_state().
4487 thread_state_t state
,
4488 mach_msg_type_number_t
*state_count
)
4492 if (task
== TASK_NULL
) {
4493 return (KERN_INVALID_ARGUMENT
);
4498 if (!task
->active
) {
4500 return (KERN_FAILURE
);
4503 ret
= machine_task_get_state(task
, flavor
, state
, state_count
);
4509 #if CONFIG_MEMORYSTATUS
4510 #define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
4512 void __attribute__((noinline
))
4513 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
)
4515 task_t task
= current_task();
4517 const char *procname
= "unknown";
4518 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
4521 pid
= proc_selfpid();
4525 * Cannot have ReportCrash analyzing
4526 * a suspended initproc.
4531 if (task
->bsd_info
!= NULL
)
4532 procname
= proc_name_address(current_task()->bsd_info
);
4535 if (hwm_user_cores
) {
4537 uint64_t starttime
, end
;
4538 clock_sec_t secs
= 0;
4539 uint32_t microsecs
= 0;
4541 starttime
= mach_absolute_time();
4543 * Trigger a coredump of this process. Don't proceed unless we know we won't
4544 * be filling up the disk; and ignore the core size resource limit for this
4547 if ((error
= coredump(current_task()->bsd_info
, HWM_USERCORE_MINSPACE
, COREDUMP_IGNORE_ULIMIT
)) != 0) {
4548 printf("couldn't take coredump of %s[%d]: %d\n", procname
, pid
, error
);
4551 * coredump() leaves the task suspended.
4553 task_resume_internal(current_task());
4555 end
= mach_absolute_time();
4556 absolutetime_to_microtime(end
- starttime
, &secs
, µsecs
);
4557 printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
4558 proc_name_address(current_task()->bsd_info
), pid
, (int)secs
, microsecs
);
4560 #endif /* CONFIG_COREDUMP */
4562 if (disable_exc_resource
) {
4563 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
4564 "supressed by a boot-arg.\n", procname
, pid
, max_footprint_mb
);
4569 * A task that has triggered an EXC_RESOURCE, should not be
4570 * jetsammed when the device is under memory pressure. Here
4571 * we set the P_MEMSTAT_TERMINATED flag so that the process
4572 * will be skipped if the memorystatus_thread wakes up.
4574 proc_memstat_terminated(current_task()->bsd_info
, TRUE
);
4576 printf("process %s[%d] crossed memory high watermark (%d MB); sending "
4577 "EXC_RESOURCE.\n", procname
, pid
, max_footprint_mb
);
4579 code
[0] = code
[1] = 0;
4580 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_MEMORY
);
4581 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_HIGH_WATERMARK
);
4582 EXC_RESOURCE_HWM_ENCODE_LIMIT(code
[0], max_footprint_mb
);
4584 /* Do not generate a corpse fork if the violation is a fatal one */
4585 if (is_fatal
|| exc_via_corpse_forking
== 0) {
4586 /* Do not send a EXC_RESOURCE is corpse_for_fatal_memkill is set */
4587 if (corpse_for_fatal_memkill
== 0) {
4589 * Use the _internal_ variant so that no user-space
4590 * process can resume our task from under us.
4592 task_suspend_internal(task
);
4593 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
4594 task_resume_internal(task
);
4597 task_enqueue_exception_with_corpse(task
, code
, EXCEPTION_CODE_MAX
);
4601 * After the EXC_RESOURCE has been handled, we must clear the
4602 * P_MEMSTAT_TERMINATED flag so that the process can again be
4603 * considered for jetsam if the memorystatus_thread wakes up.
4605 proc_memstat_terminated(current_task()->bsd_info
, FALSE
); /* clear the flag */
4609 * Callback invoked when a task exceeds its physical footprint limit.
4612 task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
4614 ledger_amount_t max_footprint
, max_footprint_mb
;
4617 boolean_t trigger_exception
;
4619 if (warning
== LEDGER_WARNING_DIPPED_BELOW
) {
4621 * Task memory limits only provide a warning on the way up.
4626 task
= current_task();
4628 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &max_footprint
);
4629 max_footprint_mb
= max_footprint
>> 20;
4632 * Capture the trigger exception flag before turning off the exception.
4634 trigger_exception
= task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
? TRUE
: FALSE
;
4636 is_fatal
= memorystatus_turnoff_exception_and_get_fatalness((warning
== LEDGER_WARNING_ROSE_ABOVE
) ? TRUE
: FALSE
, (int)max_footprint_mb
);
4639 * If this an actual violation (not a warning),
4640 * generate a non-fatal high watermark EXC_RESOURCE.
4642 if ((warning
== 0) && trigger_exception
) {
4643 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND((int)max_footprint_mb
, is_fatal
);
4646 memorystatus_on_ledger_footprint_exceeded((warning
== LEDGER_WARNING_ROSE_ABOVE
) ? TRUE
: FALSE
,
4650 extern int proc_check_footprint_priv(void);
4653 task_set_phys_footprint_limit(
4658 kern_return_t error
;
4660 if ((error
= proc_check_footprint_priv())) {
4661 return (KERN_NO_ACCESS
);
4664 return task_set_phys_footprint_limit_internal(task
, new_limit_mb
, old_limit_mb
, FALSE
);
4668 task_convert_phys_footprint_limit(
4670 int *converted_limit_mb
)
4672 if (limit_mb
== -1) {
4676 if (max_task_footprint
!= 0) {
4677 *converted_limit_mb
= (int)(max_task_footprint
/ 1024 / 1024); /* bytes to MB */
4679 *converted_limit_mb
= (int)(LEDGER_LIMIT_INFINITY
>> 20);
4682 /* nothing to convert */
4683 *converted_limit_mb
= limit_mb
;
4685 return (KERN_SUCCESS
);
4690 task_set_phys_footprint_limit_internal(
4694 boolean_t trigger_exception
)
4696 ledger_amount_t old
;
4698 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &old
);
4702 * Check that limit >> 20 will not give an "unexpected" 32-bit
4703 * result. There are, however, implicit assumptions that -1 mb limit
4704 * equates to LEDGER_LIMIT_INFINITY.
4706 assert(((old
& 0xFFF0000000000000LL
) == 0) || (old
== LEDGER_LIMIT_INFINITY
));
4707 *old_limit_mb
= (int)(old
>> 20);
4710 if (new_limit_mb
== -1) {
4712 * Caller wishes to remove the limit.
4714 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
4715 max_task_footprint
? max_task_footprint
: LEDGER_LIMIT_INFINITY
,
4716 max_task_footprint
? max_task_footprint_warning_level
: 0);
4717 return (KERN_SUCCESS
);
4720 #ifdef CONFIG_NOMONITORS
4721 return (KERN_SUCCESS
);
4722 #endif /* CONFIG_NOMONITORS */
4726 if (trigger_exception
) {
4727 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
4729 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
4732 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
4733 (ledger_amount_t
)new_limit_mb
<< 20, PHYS_FOOTPRINT_WARNING_LEVEL
);
4735 if (task
== current_task()) {
4736 ledger_check_new_balance(task
->ledger
, task_ledgers
.phys_footprint
);
4741 return (KERN_SUCCESS
);
4745 task_get_phys_footprint_limit(
4749 ledger_amount_t limit
;
4751 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &limit
);
4753 * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
4754 * result. There are, however, implicit assumptions that -1 mb limit
4755 * equates to LEDGER_LIMIT_INFINITY.
4757 assert(((limit
& 0xFFF0000000000000LL
) == 0) || (limit
== LEDGER_LIMIT_INFINITY
));
4758 *limit_mb
= (int)(limit
>> 20);
4760 return (KERN_SUCCESS
);
4762 #else /* CONFIG_MEMORYSTATUS */
4764 task_set_phys_footprint_limit(
4765 __unused task_t task
,
4766 __unused
int new_limit_mb
,
4767 __unused
int *old_limit_mb
)
4769 return (KERN_FAILURE
);
4773 task_get_phys_footprint_limit(
4774 __unused task_t task
,
4775 __unused
int *limit_mb
)
4777 return (KERN_FAILURE
);
4779 #endif /* CONFIG_MEMORYSTATUS */
4782 * We need to export some functions to other components that
4783 * are currently implemented in macros within the osfmk
4784 * component. Just export them as functions of the same name.
4786 boolean_t
is_kerneltask(task_t t
)
4788 if (t
== kernel_task
)
4794 boolean_t
is_corpsetask(task_t t
)
4796 return (task_is_a_corpse(t
));
4800 task_t
current_task(void);
4801 task_t
current_task(void)
4803 return (current_task_fast());
4806 #undef task_reference
4807 void task_reference(task_t task
);
4812 if (task
!= TASK_NULL
)
4813 task_reference_internal(task
);
4816 /* defined in bsd/kern/kern_prot.c */
4817 extern int get_audit_token_pid(audit_token_t
*audit_token
);
4819 int task_pid(task_t task
)
4822 return get_audit_token_pid(&task
->audit_token
);
4828 * This routine finds a thread in a task by its unique id
4829 * Returns a referenced thread or THREAD_NULL if the thread was not found
4831 * TODO: This is super inefficient - it's an O(threads in task) list walk!
4832 * We should make a tid hash, or transition all tid clients to thread ports
4834 * Precondition: No locks held (will take task lock)
4837 task_findtid(task_t task
, uint64_t tid
)
4839 thread_t self
= current_thread();
4840 thread_t found_thread
= THREAD_NULL
;
4841 thread_t iter_thread
= THREAD_NULL
;
4843 /* Short-circuit the lookup if we're looking up ourselves */
4844 if (tid
== self
->thread_id
|| tid
== TID_NULL
) {
4845 assert(self
->task
== task
);
4847 thread_reference(self
);
4854 queue_iterate(&task
->threads
, iter_thread
, thread_t
, task_threads
) {
4855 if (iter_thread
->thread_id
== tid
) {
4856 found_thread
= iter_thread
;
4857 thread_reference(found_thread
);
4864 return (found_thread
);
4869 * Control the CPU usage monitor for a task.
4872 task_cpu_usage_monitor_ctl(task_t task
, uint32_t *flags
)
4874 int error
= KERN_SUCCESS
;
4876 if (*flags
& CPUMON_MAKE_FATAL
) {
4877 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_CPUMON
;
4879 error
= KERN_INVALID_ARGUMENT
;
4886 * Control the wakeups monitor for a task.
4889 task_wakeups_monitor_ctl(task_t task
, uint32_t *flags
, int32_t *rate_hz
)
4891 ledger_t ledger
= task
->ledger
;
4894 if (*flags
& WAKEMON_GET_PARAMS
) {
4895 ledger_amount_t limit
;
4898 ledger_get_limit(ledger
, task_ledgers
.interrupt_wakeups
, &limit
);
4899 ledger_get_period(ledger
, task_ledgers
.interrupt_wakeups
, &period
);
4901 if (limit
!= LEDGER_LIMIT_INFINITY
) {
4903 * An active limit means the wakeups monitor is enabled.
4905 *rate_hz
= (int32_t)(limit
/ (int64_t)(period
/ NSEC_PER_SEC
));
4906 *flags
= WAKEMON_ENABLE
;
4907 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
4908 *flags
|= WAKEMON_MAKE_FATAL
;
4911 *flags
= WAKEMON_DISABLE
;
4916 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
4919 return KERN_SUCCESS
;
4922 if (*flags
& WAKEMON_ENABLE
) {
4923 if (*flags
& WAKEMON_SET_DEFAULTS
) {
4924 *rate_hz
= task_wakeups_monitor_rate
;
4927 #ifndef CONFIG_NOMONITORS
4928 if (*flags
& WAKEMON_MAKE_FATAL
) {
4929 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
4931 #endif /* CONFIG_NOMONITORS */
4933 if (*rate_hz
<= 0) {
4935 return KERN_INVALID_ARGUMENT
;
4938 #ifndef CONFIG_NOMONITORS
4939 ledger_set_limit(ledger
, task_ledgers
.interrupt_wakeups
, *rate_hz
* task_wakeups_monitor_interval
,
4940 task_wakeups_monitor_ustackshots_trigger_pct
);
4941 ledger_set_period(ledger
, task_ledgers
.interrupt_wakeups
, task_wakeups_monitor_interval
* NSEC_PER_SEC
);
4942 ledger_enable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
4943 #endif /* CONFIG_NOMONITORS */
4944 } else if (*flags
& WAKEMON_DISABLE
) {
4946 * Caller wishes to disable wakeups monitor on the task.
4948 * Disable telemetry if it was triggered by the wakeups monitor, and
4949 * remove the limit & callback on the wakeups ledger entry.
4951 #if CONFIG_TELEMETRY
4952 telemetry_task_ctl_locked(task
, TF_WAKEMON_WARNING
, 0);
4954 ledger_disable_refill(ledger
, task_ledgers
.interrupt_wakeups
);
4955 ledger_disable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
4959 return KERN_SUCCESS
;
4963 task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
4965 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
4966 #if CONFIG_TELEMETRY
4968 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
4969 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
4971 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 1);
4976 #if CONFIG_TELEMETRY
4978 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
4979 * exceeded the limit, turn telemetry off for the task.
4981 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 0);
4985 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS();
4989 void __attribute__((noinline
))
4990 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
4992 task_t task
= current_task();
4994 const char *procname
= "unknown";
4997 #ifdef EXC_RESOURCE_MONITORS
4998 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
4999 #endif /* EXC_RESOURCE_MONITORS */
5000 struct ledger_entry_info lei
;
5003 pid
= proc_selfpid();
5004 if (task
->bsd_info
!= NULL
)
5005 procname
= proc_name_address(current_task()->bsd_info
);
5008 ledger_get_entry_info(task
->ledger
, task_ledgers
.interrupt_wakeups
, &lei
);
5011 * Disable the exception notification so we don't overwhelm
5012 * the listener with an endless stream of redundant exceptions.
5013 * TODO: detect whether another thread is already reporting the violation.
5015 uint32_t flags
= WAKEMON_DISABLE
;
5016 task_wakeups_monitor_ctl(task
, &flags
, NULL
);
5018 fatal
= task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
5019 trace_resource_violation(RMON_CPUWAKES_VIOLATED
, &lei
);
5020 printf("process %s[%d] caught waking the CPU %llu times "
5021 "over ~%llu seconds, averaging %llu wakes / second and "
5022 "violating a %slimit of %llu wakes over %llu seconds.\n",
5024 lei
.lei_balance
, lei
.lei_last_refill
/ NSEC_PER_SEC
,
5025 lei
.lei_last_refill
== 0 ? 0 :
5026 (NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
),
5027 fatal
? "FATAL " : "",
5028 lei
.lei_limit
, lei
.lei_refill_period
/ NSEC_PER_SEC
);
5030 kr
= send_resource_violation(send_cpu_wakes_violation
, task
, &lei
,
5031 fatal
? kRNFatalLimitFlag
: 0);
5033 printf("send_resource_violation(CPU wakes, ...): error %#x\n", kr
);
5036 #ifdef EXC_RESOURCE_MONITORS
5037 if (disable_exc_resource
) {
5038 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5039 "supressed by a boot-arg\n", procname
, pid
);
5043 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5044 "supressed due to audio playback\n", procname
, pid
);
5047 if (lei
.lei_last_refill
== 0) {
5048 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5049 "supressed due to lei.lei_last_refill = 0 \n", procname
, pid
);
5052 code
[0] = code
[1] = 0;
5053 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_WAKEUPS
);
5054 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_WAKEUPS_MONITOR
);
5055 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code
[0],
5056 NSEC_PER_SEC
* lei
.lei_limit
/ lei
.lei_refill_period
);
5057 EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code
[0],
5058 lei
.lei_last_refill
);
5059 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code
[1],
5060 NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
);
5061 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5062 #endif /* EXC_RESOURCE_MONITORS */
5065 task_terminate_internal(task
);
5070 global_update_logical_writes(int64_t io_delta
)
5072 int64_t old_count
, new_count
;
5073 boolean_t needs_telemetry
;
5076 new_count
= old_count
= global_logical_writes_count
;
5077 new_count
+= io_delta
;
5078 if (new_count
>= io_telemetry_limit
) {
5080 needs_telemetry
= TRUE
;
5082 needs_telemetry
= FALSE
;
5084 } while(!OSCompareAndSwap64(old_count
, new_count
, &global_logical_writes_count
));
5085 return needs_telemetry
;
5088 void task_update_logical_writes(task_t task
, uint32_t io_size
, int flags
, void *vp
)
5090 int64_t io_delta
= 0;
5091 boolean_t needs_telemetry
= FALSE
;
5093 if ((!task
) || (!io_size
) || (!vp
))
5096 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_DATA_WRITE
)) | DBG_FUNC_NONE
,
5097 task_pid(task
), io_size
, flags
, (uintptr_t)VM_KERNEL_ADDRPERM(vp
), 0);
5098 DTRACE_IO4(logical_writes
, struct task
*, task
, uint32_t, io_size
, int, flags
, vnode
*, vp
);
5100 case TASK_WRITE_IMMEDIATE
:
5101 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_immediate_writes
));
5102 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5104 case TASK_WRITE_DEFERRED
:
5105 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_deferred_writes
));
5106 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5108 case TASK_WRITE_INVALIDATED
:
5109 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_invalidated_writes
));
5110 ledger_debit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5112 case TASK_WRITE_METADATA
:
5113 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_metadata_writes
));
5114 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5118 io_delta
= (flags
== TASK_WRITE_INVALIDATED
) ? ((int64_t)io_size
* -1ll) : ((int64_t)io_size
);
5119 if (io_telemetry_limit
!= 0) {
5120 /* If io_telemetry_limit is 0, disable global updates and I/O telemetry */
5121 needs_telemetry
= global_update_logical_writes(io_delta
);
5122 if (needs_telemetry
) {
5123 act_set_io_telemetry_ast(current_thread());
5129 * Control the I/O monitor for a task.
5132 task_io_monitor_ctl(task_t task
, uint32_t *flags
)
5134 ledger_t ledger
= task
->ledger
;
5137 if (*flags
& IOMON_ENABLE
) {
5138 /* Configure the physical I/O ledger */
5139 ledger_set_limit(ledger
, task_ledgers
.physical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5140 ledger_set_period(ledger
, task_ledgers
.physical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5142 /* Configure the logical I/O ledger */
5143 ledger_set_limit(ledger
, task_ledgers
.logical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5144 ledger_set_period(ledger
, task_ledgers
.logical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5146 } else if (*flags
& IOMON_DISABLE
) {
5148 * Caller wishes to disable I/O monitor on the task.
5150 ledger_disable_refill(ledger
, task_ledgers
.physical_writes
);
5151 ledger_disable_callback(ledger
, task_ledgers
.physical_writes
);
5152 ledger_disable_refill(ledger
, task_ledgers
.logical_writes
);
5153 ledger_disable_callback(ledger
, task_ledgers
.logical_writes
);
5157 return KERN_SUCCESS
;
5161 task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
)
5164 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO((int)param0
);
5168 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
)
5171 task_t task
= current_task();
5172 #ifdef EXC_RESOURCE_MONITORS
5173 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5174 #endif /* EXC_RESOURCE_MONITORS */
5175 struct ledger_entry_info lei
;
5179 pid
= proc_selfpid();
5182 * Get the ledger entry info. We need to do this before disabling the exception
5183 * to get correct values for all fields.
5186 case FLAVOR_IO_PHYSICAL_WRITES
:
5187 ledger_get_entry_info(task
->ledger
, task_ledgers
.physical_writes
, &lei
);
5189 case FLAVOR_IO_LOGICAL_WRITES
:
5190 ledger_get_entry_info(task
->ledger
, task_ledgers
.logical_writes
, &lei
);
5196 * Disable the exception notification so we don't overwhelm
5197 * the listener with an endless stream of redundant exceptions.
5198 * TODO: detect whether another thread is already reporting the violation.
5200 uint32_t flags
= IOMON_DISABLE
;
5201 task_io_monitor_ctl(task
, &flags
);
5203 if (flavor
== FLAVOR_IO_LOGICAL_WRITES
) {
5204 trace_resource_violation(RMON_LOGWRITES_VIOLATED
, &lei
);
5206 printf("process [%d] caught causing excessive I/O (flavor: %d). Task I/O: %lld MB. [Limit : %lld MB per %lld secs]\n",
5207 pid
, flavor
, (lei
.lei_balance
/ (1024 * 1024)), (lei
.lei_limit
/ (1024 * 1024)), (lei
.lei_refill_period
/ NSEC_PER_SEC
));
5209 kr
= send_resource_violation(send_disk_writes_violation
, task
, &lei
, kRNFlagsNone
);
5211 printf("send_resource_violation(disk_writes, ...): error %#x\n", kr
);
5214 #ifdef EXC_RESOURCE_MONITORS
5215 code
[0] = code
[1] = 0;
5216 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_IO
);
5217 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], flavor
);
5218 EXC_RESOURCE_IO_ENCODE_INTERVAL(code
[0], (lei
.lei_refill_period
/ NSEC_PER_SEC
));
5219 EXC_RESOURCE_IO_ENCODE_LIMIT(code
[0], (lei
.lei_limit
/ (1024 * 1024)));
5220 EXC_RESOURCE_IO_ENCODE_OBSERVED(code
[1], (lei
.lei_balance
/ (1024 * 1024)));
5221 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5222 #endif /* EXC_RESOURCE_MONITORS */
5225 /* Placeholders for the task set/get voucher interfaces */
5227 task_get_mach_voucher(
5229 mach_voucher_selector_t __unused which
,
5230 ipc_voucher_t
*voucher
)
5232 if (TASK_NULL
== task
)
5233 return KERN_INVALID_TASK
;
5236 return KERN_SUCCESS
;
5240 task_set_mach_voucher(
5242 ipc_voucher_t __unused voucher
)
5244 if (TASK_NULL
== task
)
5245 return KERN_INVALID_TASK
;
5247 return KERN_SUCCESS
;
5251 task_swap_mach_voucher(
5253 ipc_voucher_t new_voucher
,
5254 ipc_voucher_t
*in_out_old_voucher
)
5256 if (TASK_NULL
== task
)
5257 return KERN_INVALID_TASK
;
5259 *in_out_old_voucher
= new_voucher
;
5260 return KERN_SUCCESS
;
5263 void task_set_gpu_denied(task_t task
, boolean_t denied
)
5268 task
->t_flags
|= TF_GPU_DENIED
;
5270 task
->t_flags
&= ~TF_GPU_DENIED
;
5276 boolean_t
task_is_gpu_denied(task_t task
)
5278 /* We don't need the lock to read this flag */
5279 return (task
->t_flags
& TF_GPU_DENIED
) ? TRUE
: FALSE
;
5283 uint64_t get_task_memory_region_count(task_t task
)
5286 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
5287 return((uint64_t)get_map_nentries(map
));
5291 kdebug_trace_dyld_internal(uint32_t base_code
,
5292 struct dyld_kernel_image_info
*info
)
5294 static_assert(sizeof(info
->uuid
) >= 16);
5296 #if defined(__LP64__)
5297 uint64_t *uuid
= (uint64_t *)&(info
->uuid
);
5299 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5300 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
), uuid
[0],
5301 uuid
[1], info
->load_addr
,
5302 (uint64_t)info
->fsid
.val
[0] | ((uint64_t)info
->fsid
.val
[1] << 32),
5304 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5305 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 1),
5306 (uint64_t)info
->fsobjid
.fid_objno
|
5307 ((uint64_t)info
->fsobjid
.fid_generation
<< 32),
5309 #else /* defined(__LP64__) */
5310 uint32_t *uuid
= (uint32_t *)&(info
->uuid
);
5312 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5313 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 2), uuid
[0],
5314 uuid
[1], uuid
[2], uuid
[3], 0);
5315 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5316 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 3),
5317 (uint32_t)info
->load_addr
, info
->fsid
.val
[0], info
->fsid
.val
[1],
5318 info
->fsobjid
.fid_objno
, 0);
5319 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5320 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 4),
5321 info
->fsobjid
.fid_generation
, 0, 0, 0, 0);
5322 #endif /* !defined(__LP64__) */
5325 static kern_return_t
5326 kdebug_trace_dyld(task_t task
, uint32_t base_code
,
5327 vm_map_copy_t infos_copy
, mach_msg_type_number_t infos_len
)
5330 dyld_kernel_image_info_array_t infos
;
5331 vm_map_offset_t map_data
;
5334 assert(infos_copy
!= NULL
);
5336 if (task
== NULL
|| task
!= current_task()) {
5337 return KERN_INVALID_TASK
;
5340 kr
= vm_map_copyout(ipc_kernel_map
, &map_data
, (vm_map_copy_t
)infos_copy
);
5341 if (kr
!= KERN_SUCCESS
) {
5345 infos
= CAST_DOWN(dyld_kernel_image_info_array_t
, map_data
);
5347 for (mach_msg_type_number_t i
= 0; i
< infos_len
; i
++) {
5348 kdebug_trace_dyld_internal(base_code
, &(infos
[i
]));
5351 data
= CAST_DOWN(vm_offset_t
, map_data
);
5352 mach_vm_deallocate(ipc_kernel_map
, data
, infos_len
* sizeof(infos
[0]));
5353 return KERN_SUCCESS
;
5357 task_register_dyld_image_infos(task_t task
,
5358 dyld_kernel_image_info_array_t infos_copy
,
5359 mach_msg_type_number_t infos_len
)
5361 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_MAP_A
,
5362 (vm_map_copy_t
)infos_copy
, infos_len
);
5366 task_unregister_dyld_image_infos(task_t task
,
5367 dyld_kernel_image_info_array_t infos_copy
,
5368 mach_msg_type_number_t infos_len
)
5370 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_UNMAP_A
,
5371 (vm_map_copy_t
)infos_copy
, infos_len
);
5375 task_get_dyld_image_infos(__unused task_t task
,
5376 __unused dyld_kernel_image_info_array_t
* dyld_images
,
5377 __unused mach_msg_type_number_t
* dyld_imagesCnt
)
5379 return KERN_NOT_SUPPORTED
;
5383 task_register_dyld_shared_cache_image_info(task_t task
,
5384 dyld_kernel_image_info_t cache_img
,
5385 __unused boolean_t no_cache
,
5386 __unused boolean_t private_cache
)
5388 if (task
== NULL
|| task
!= current_task()) {
5389 return KERN_INVALID_TASK
;
5392 kdebug_trace_dyld_internal(DBG_DYLD_UUID_SHARED_CACHE_A
, &cache_img
);
5393 return KERN_SUCCESS
;
5397 task_register_dyld_set_dyld_state(__unused task_t task
,
5398 __unused
uint8_t dyld_state
)
5400 return KERN_NOT_SUPPORTED
;
5404 task_register_dyld_get_process_state(__unused task_t task
,
5405 __unused dyld_kernel_process_info_t
* dyld_process_state
)
5407 return KERN_NOT_SUPPORTED
;
5410 #if CONFIG_SECLUDED_MEMORY
5411 int num_tasks_can_use_secluded_mem
= 0;
5414 task_set_can_use_secluded_mem(
5416 boolean_t can_use_secluded_mem
)
5418 if (!task
->task_could_use_secluded_mem
) {
5422 task_set_can_use_secluded_mem_locked(task
, can_use_secluded_mem
);
5427 task_set_can_use_secluded_mem_locked(
5429 boolean_t can_use_secluded_mem
)
5431 assert(task
->task_could_use_secluded_mem
);
5432 if (can_use_secluded_mem
&&
5433 secluded_for_apps
&& /* global boot-arg */
5434 !task
->task_can_use_secluded_mem
) {
5435 assert(num_tasks_can_use_secluded_mem
>= 0);
5437 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
5438 task
->task_can_use_secluded_mem
= TRUE
;
5439 } else if (!can_use_secluded_mem
&&
5440 task
->task_can_use_secluded_mem
) {
5441 assert(num_tasks_can_use_secluded_mem
> 0);
5443 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
5444 task
->task_can_use_secluded_mem
= FALSE
;
5449 task_set_could_use_secluded_mem(
5451 boolean_t could_use_secluded_mem
)
5453 task
->task_could_use_secluded_mem
= could_use_secluded_mem
;
5457 task_set_could_also_use_secluded_mem(
5459 boolean_t could_also_use_secluded_mem
)
5461 task
->task_could_also_use_secluded_mem
= could_also_use_secluded_mem
;
5465 task_can_use_secluded_mem(
5468 if (task
->task_can_use_secluded_mem
) {
5469 assert(task
->task_could_use_secluded_mem
);
5470 assert(num_tasks_can_use_secluded_mem
> 0);
5473 if (task
->task_could_also_use_secluded_mem
&&
5474 num_tasks_can_use_secluded_mem
> 0) {
5475 assert(num_tasks_can_use_secluded_mem
> 0);
5482 task_could_use_secluded_mem(
5485 return task
->task_could_use_secluded_mem
;
5487 #endif /* CONFIG_SECLUDED_MEMORY */
5490 task_io_user_clients(task_t task
)
5492 return (&task
->io_user_clients
);