2 * Copyright (c) 2000-2010, 2015 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>
99 #include <ipc/ipc_importance.h>
100 #include <ipc/ipc_types.h>
101 #include <ipc/ipc_space.h>
102 #include <ipc/ipc_entry.h>
103 #include <ipc/ipc_hash.h>
105 #include <kern/kern_types.h>
106 #include <kern/mach_param.h>
107 #include <kern/misc_protos.h>
108 #include <kern/task.h>
109 #include <kern/thread.h>
110 #include <kern/coalition.h>
111 #include <kern/zalloc.h>
112 #include <kern/kalloc.h>
113 #include <kern/kern_cdata.h>
114 #include <kern/processor.h>
115 #include <kern/sched_prim.h> /* for thread_wakeup */
116 #include <kern/ipc_tt.h>
117 #include <kern/host.h>
118 #include <kern/clock.h>
119 #include <kern/timer.h>
120 #include <kern/assert.h>
121 #include <kern/sync_lock.h>
122 #include <kern/affinity.h>
123 #include <kern/exc_resource.h>
124 #include <kern/machine.h>
125 #include <corpses/task_corpse.h>
127 #include <kern/telemetry.h>
131 #include <vm/vm_map.h>
132 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
133 #include <vm/vm_pageout.h>
134 #include <vm/vm_protos.h>
135 #include <vm/vm_purgeable_internal.h>
137 #include <sys/resource.h>
138 #include <sys/signalvar.h> /* for coredump */
141 * Exported interfaces
144 #include <mach/task_server.h>
145 #include <mach/mach_host_server.h>
146 #include <mach/host_security_server.h>
147 #include <mach/mach_port_server.h>
149 #include <vm/vm_shared_region.h>
151 #include <libkern/OSDebug.h>
152 #include <libkern/OSAtomic.h>
155 #include <atm/atm_internal.h>
158 #include <kern/sfi.h>
161 extern int kpc_force_all_ctrs(task_t
, int);
164 uint32_t qos_override_mode
;
168 lck_attr_t task_lck_attr
;
169 lck_grp_t task_lck_grp
;
170 lck_grp_attr_t task_lck_grp_attr
;
172 /* Flag set by core audio when audio is playing. Used to stifle EXC_RESOURCE generation when active. */
173 int audio_active
= 0;
175 zinfo_usage_store_t tasks_tkm_private
;
176 zinfo_usage_store_t tasks_tkm_shared
;
178 /* A container to accumulate statistics for expired tasks */
179 expired_task_statistics_t dead_task_statistics
;
180 lck_spin_t dead_task_statistics_lock
;
182 ledger_template_t task_ledger_template
= NULL
;
184 struct _task_ledger_indices task_ledgers
__attribute__((used
)) =
185 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
186 { 0 /* initialized at runtime */},
192 /* System sleep state */
193 boolean_t tasks_suspend_state
;
196 void init_task_ledgers(void);
197 void task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
198 void task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
199 void __attribute__((noinline
)) THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS__SENDING_EXC_RESOURCE(void);
200 void __attribute__((noinline
)) PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
);
202 kern_return_t
task_suspend_internal(task_t
);
203 kern_return_t
task_resume_internal(task_t
);
204 static kern_return_t
task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
);
207 void proc_init_cpumon_params(void);
208 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
*);
210 // Warn tasks when they hit 80% of their memory limit.
211 #define PHYS_FOOTPRINT_WARNING_LEVEL 80
213 #define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT 150 /* wakeups per second */
214 #define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL 300 /* in seconds. */
217 * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
219 * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
220 * stacktraces, aka micro-stackshots)
222 #define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER 70
224 int task_wakeups_monitor_interval
; /* In seconds. Time period over which wakeups rate is observed */
225 int task_wakeups_monitor_rate
; /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
227 int task_wakeups_monitor_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
229 int disable_exc_resource
; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
231 ledger_amount_t max_task_footprint
= 0; /* Per-task limit on physical memory consumption in bytes */
232 int max_task_footprint_mb
= 0; /* Per-task limit on physical memory consumption in megabytes */
235 int pmap_ledgers_panic
= 1;
236 #endif /* MACH_ASSERT */
238 int task_max
= CONFIG_TASK_MAX
; /* Max number of tasks */
240 int hwm_user_cores
= 0; /* high watermark violations generate user core files */
243 extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
244 extern int proc_pid(struct proc
*p
);
245 extern int proc_selfpid(void);
246 extern char *proc_name_address(struct proc
*p
);
247 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
249 extern void proc_memstat_terminated(struct proc
* p
, boolean_t set
);
250 extern void memorystatus_on_ledger_footprint_exceeded(int warning
, const int max_footprint_mb
);
254 extern int pmap_ledgers_panic
;
255 #endif /* MACH_ASSERT */
259 void task_hold_locked(
261 void task_wait_locked(
263 boolean_t until_not_runnable
);
264 void task_release_locked(
268 void task_synchronizer_destroy_all(
271 int check_for_tasksuspend(
275 task_backing_store_privileged(
279 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
290 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
292 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
297 if (task_has_64BitAddr(task
))
299 task_set_64BitAddr(task
);
301 if ( !task_has_64BitAddr(task
))
303 task_clear_64BitAddr(task
);
305 /* FIXME: On x86, the thread save state flavor can diverge from the
306 * task's 64-bit feature flag due to the 32-bit/64-bit register save
307 * state dichotomy. Since we can be pre-empted in this interval,
308 * certain routines may observe the thread as being in an inconsistent
309 * state with respect to its task's 64-bitness.
312 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
313 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
314 thread_mtx_lock(thread
);
315 machine_thread_switch_addrmode(thread
);
316 thread_mtx_unlock(thread
);
318 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
326 task_set_dyld_info(task_t task
, mach_vm_address_t addr
, mach_vm_size_t size
)
329 task
->all_image_info_addr
= addr
;
330 task
->all_image_info_size
= size
;
335 task_atm_reset(__unused task_t task
) {
338 if (task
->atm_context
!= NULL
) {
339 atm_task_descriptor_destroy(task
->atm_context
);
340 task
->atm_context
= NULL
;
347 task_bank_reset(__unused task_t task
) {
350 if (task
->bank_context
!= NULL
) {
351 bank_task_destroy(task
);
358 * NOTE: This should only be called when the P_LINTRANSIT
359 * flag is set (the proc_trans lock is held) on the
360 * proc associated with the task.
363 task_bank_init(__unused task_t task
) {
366 if (task
->bank_context
!= NULL
) {
367 panic("Task bank init called with non null bank context for task: %p and bank_context: %p", task
, task
->bank_context
);
369 bank_task_initialize(task
);
374 #if TASK_REFERENCE_LEAK_DEBUG
375 #include <kern/btlog.h>
377 decl_simple_lock_data(static,task_ref_lock
);
378 static btlog_t
*task_ref_btlog
;
379 #define TASK_REF_OP_INCR 0x1
380 #define TASK_REF_OP_DECR 0x2
382 #define TASK_REF_BTDEPTH 7
385 task_ref_lock_lock(void *context
)
387 simple_lock((simple_lock_t
)context
);
390 task_ref_lock_unlock(void *context
)
392 simple_unlock((simple_lock_t
)context
);
396 task_reference_internal(task_t task
)
398 void * bt
[TASK_REF_BTDEPTH
];
401 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
403 (void)hw_atomic_add(&(task
)->ref_count
, 1);
404 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_INCR
,
409 task_deallocate_internal(task_t task
)
411 void * bt
[TASK_REF_BTDEPTH
];
414 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
416 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_DECR
,
418 return hw_atomic_sub(&(task
)->ref_count
, 1);
421 #endif /* TASK_REFERENCE_LEAK_DEBUG */
427 lck_grp_attr_setdefault(&task_lck_grp_attr
);
428 lck_grp_init(&task_lck_grp
, "task", &task_lck_grp_attr
);
429 lck_attr_setdefault(&task_lck_attr
);
430 lck_mtx_init(&tasks_threads_lock
, &task_lck_grp
, &task_lck_attr
);
434 task_max
* sizeof(struct task
),
435 TASK_CHUNK
* sizeof(struct task
),
438 zone_change(task_zone
, Z_NOENCRYPT
, TRUE
);
441 * Configure per-task memory limit.
442 * The boot-arg is interpreted as Megabytes,
443 * and takes precedence over the device tree.
444 * Setting the boot-arg to 0 disables task limits.
446 if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb
,
447 sizeof (max_task_footprint_mb
))) {
449 * No limit was found in boot-args, so go look in the device tree.
451 if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb
,
452 sizeof(max_task_footprint_mb
))) {
454 * No limit was found in device tree.
456 max_task_footprint_mb
= 0;
460 if (max_task_footprint_mb
!= 0) {
462 if (max_task_footprint_mb
< 50) {
463 printf("Warning: max_task_pmem %d below minimum.\n",
464 max_task_footprint_mb
);
465 max_task_footprint_mb
= 50;
467 printf("Limiting task physical memory footprint to %d MB\n",
468 max_task_footprint_mb
);
470 max_task_footprint
= (ledger_amount_t
)max_task_footprint_mb
* 1024 * 1024; // Convert MB to bytes
472 printf("Warning: max_task_footprint specified, but jetsam not configured; ignoring.\n");
477 PE_parse_boot_argn("pmap_ledgers_panic", &pmap_ledgers_panic
,
478 sizeof (pmap_ledgers_panic
));
479 #endif /* MACH_ASSERT */
481 if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores
,
482 sizeof (hwm_user_cores
))) {
486 if (PE_parse_boot_argn("qos_override_mode", &qos_override_mode
, sizeof(qos_override_mode
))) {
487 printf("QOS override mode: 0x%08x\n", qos_override_mode
);
489 qos_override_mode
= QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE_BUT_SINGLE_MUTEX_OVERRIDE
;
492 proc_init_cpumon_params();
494 if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate
, sizeof (task_wakeups_monitor_rate
))) {
495 task_wakeups_monitor_rate
= TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT
;
498 if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval
, sizeof (task_wakeups_monitor_interval
))) {
499 task_wakeups_monitor_interval
= TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL
;
502 if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct
,
503 sizeof (task_wakeups_monitor_ustackshots_trigger_pct
))) {
504 task_wakeups_monitor_ustackshots_trigger_pct
= TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER
;
507 if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource
,
508 sizeof (disable_exc_resource
))) {
509 disable_exc_resource
= 0;
513 * If we have coalitions, coalition_init() will call init_task_ledgers() as it
514 * sets up the ledgers for the default coalition. If we don't have coalitions,
515 * then we have to call it now.
517 #if CONFIG_COALITIONS
518 assert(task_ledger_template
);
519 #else /* CONFIG_COALITIONS */
521 #endif /* CONFIG_COALITIONS */
523 #if TASK_REFERENCE_LEAK_DEBUG
524 simple_lock_init(&task_ref_lock
, 0);
525 task_ref_btlog
= btlog_create(100000,
528 task_ref_lock_unlock
,
530 assert(task_ref_btlog
);
534 * Create the kernel task as the first task.
537 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, TRUE
, &kernel_task
) != KERN_SUCCESS
)
539 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
541 panic("task_init\n");
543 vm_map_deallocate(kernel_task
->map
);
544 kernel_task
->map
= kernel_map
;
545 lck_spin_init(&dead_task_statistics_lock
, &task_lck_grp
, &task_lck_attr
);
550 * Create a task running in the kernel address space. It may
551 * have its own map of size mem_size and may have ipc privileges.
555 __unused task_t parent_task
,
556 __unused vm_offset_t map_base
,
557 __unused vm_size_t map_size
,
558 __unused task_t
*child_task
)
560 return (KERN_INVALID_ARGUMENT
);
566 __unused ledger_port_array_t ledger_ports
,
567 __unused mach_msg_type_number_t num_ledger_ports
,
568 __unused boolean_t inherit_memory
,
569 __unused task_t
*child_task
) /* OUT */
571 if (parent_task
== TASK_NULL
)
572 return(KERN_INVALID_ARGUMENT
);
575 * No longer supported: too many calls assume that a task has a valid
578 return(KERN_FAILURE
);
582 host_security_create_task_token(
583 host_security_t host_security
,
585 __unused security_token_t sec_token
,
586 __unused audit_token_t audit_token
,
587 __unused host_priv_t host_priv
,
588 __unused ledger_port_array_t ledger_ports
,
589 __unused mach_msg_type_number_t num_ledger_ports
,
590 __unused boolean_t inherit_memory
,
591 __unused task_t
*child_task
) /* OUT */
593 if (parent_task
== TASK_NULL
)
594 return(KERN_INVALID_ARGUMENT
);
596 if (host_security
== HOST_NULL
)
597 return(KERN_INVALID_SECURITY
);
600 * No longer supported.
602 return(KERN_FAILURE
);
610 * Physical footprint: This is the sum of:
611 * + (internal - alternate_accounting)
612 * + (internal_compressed - alternate_accounting_compressed)
614 * + purgeable_nonvolatile
615 * + purgeable_nonvolatile_compressed
618 * The task's anonymous memory, which on iOS is always resident.
620 * internal_compressed
621 * Amount of this task's internal memory which is held by the compressor.
622 * Such memory is no longer actually resident for the task [i.e., resident in its pmap],
623 * and could be either decompressed back into memory, or paged out to storage, depending
624 * on our implementation.
627 * IOKit mappings: The total size of all IOKit mappings in this task, regardless of
628 clean/dirty or internal/external state].
630 * alternate_accounting
631 * The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
632 * are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
636 init_task_ledgers(void)
640 assert(task_ledger_template
== NULL
);
641 assert(kernel_task
== TASK_NULL
);
643 if ((t
= ledger_template_create("Per-task ledger")) == NULL
)
644 panic("couldn't create task ledger template");
646 task_ledgers
.cpu_time
= ledger_entry_add(t
, "cpu_time", "sched", "ns");
647 task_ledgers
.tkm_private
= ledger_entry_add(t
, "tkm_private",
649 task_ledgers
.tkm_shared
= ledger_entry_add(t
, "tkm_shared", "physmem",
651 task_ledgers
.phys_mem
= ledger_entry_add(t
, "phys_mem", "physmem",
653 task_ledgers
.wired_mem
= ledger_entry_add(t
, "wired_mem", "physmem",
655 task_ledgers
.internal
= ledger_entry_add(t
, "internal", "physmem",
657 task_ledgers
.iokit_mapped
= ledger_entry_add(t
, "iokit_mapped", "mappings",
659 task_ledgers
.alternate_accounting
= ledger_entry_add(t
, "alternate_accounting", "physmem",
661 task_ledgers
.alternate_accounting_compressed
= ledger_entry_add(t
, "alternate_accounting_compressed", "physmem",
663 task_ledgers
.phys_footprint
= ledger_entry_add(t
, "phys_footprint", "physmem",
665 task_ledgers
.internal_compressed
= ledger_entry_add(t
, "internal_compressed", "physmem",
667 task_ledgers
.purgeable_volatile
= ledger_entry_add(t
, "purgeable_volatile", "physmem", "bytes");
668 task_ledgers
.purgeable_nonvolatile
= ledger_entry_add(t
, "purgeable_nonvolatile", "physmem", "bytes");
669 task_ledgers
.purgeable_volatile_compressed
= ledger_entry_add(t
, "purgeable_volatile_compress", "physmem", "bytes");
670 task_ledgers
.purgeable_nonvolatile_compressed
= ledger_entry_add(t
, "purgeable_nonvolatile_compress", "physmem", "bytes");
671 task_ledgers
.platform_idle_wakeups
= ledger_entry_add(t
, "platform_idle_wakeups", "power",
673 task_ledgers
.interrupt_wakeups
= ledger_entry_add(t
, "interrupt_wakeups", "power",
677 sfi_class_id_t class_id
, ledger_alias
;
678 for (class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
679 task_ledgers
.sfi_wait_times
[class_id
] = -1;
682 /* don't account for UNSPECIFIED */
683 for (class_id
= SFI_CLASS_UNSPECIFIED
+ 1; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
684 ledger_alias
= sfi_get_ledger_alias_for_class(class_id
);
685 if (ledger_alias
!= SFI_CLASS_UNSPECIFIED
) {
686 /* Check to see if alias has been registered yet */
687 if (task_ledgers
.sfi_wait_times
[ledger_alias
] != -1) {
688 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
];
690 /* Otherwise, initialize it first */
691 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
] = sfi_ledger_entry_add(t
, ledger_alias
);
694 task_ledgers
.sfi_wait_times
[class_id
] = sfi_ledger_entry_add(t
, class_id
);
697 if (task_ledgers
.sfi_wait_times
[class_id
] < 0) {
698 panic("couldn't create entries for task ledger template for SFI class 0x%x", class_id
);
702 assert(task_ledgers
.sfi_wait_times
[MAX_SFI_CLASS_ID
-1] != -1);
703 #endif /* CONFIG_SCHED_SFI */
706 task_ledgers
.cpu_time_billed_to_me
= ledger_entry_add(t
, "cpu_time_billed_to_me", "sched", "ns");
707 task_ledgers
.cpu_time_billed_to_others
= ledger_entry_add(t
, "cpu_time_billed_to_others", "sched", "ns");
709 if ((task_ledgers
.cpu_time
< 0) ||
710 (task_ledgers
.tkm_private
< 0) ||
711 (task_ledgers
.tkm_shared
< 0) ||
712 (task_ledgers
.phys_mem
< 0) ||
713 (task_ledgers
.wired_mem
< 0) ||
714 (task_ledgers
.internal
< 0) ||
715 (task_ledgers
.iokit_mapped
< 0) ||
716 (task_ledgers
.alternate_accounting
< 0) ||
717 (task_ledgers
.alternate_accounting_compressed
< 0) ||
718 (task_ledgers
.phys_footprint
< 0) ||
719 (task_ledgers
.internal_compressed
< 0) ||
720 (task_ledgers
.purgeable_volatile
< 0) ||
721 (task_ledgers
.purgeable_nonvolatile
< 0) ||
722 (task_ledgers
.purgeable_volatile_compressed
< 0) ||
723 (task_ledgers
.purgeable_nonvolatile_compressed
< 0) ||
724 (task_ledgers
.platform_idle_wakeups
< 0) ||
725 (task_ledgers
.interrupt_wakeups
< 0)
727 || (task_ledgers
.cpu_time_billed_to_me
< 0) || (task_ledgers
.cpu_time_billed_to_others
< 0)
730 panic("couldn't create entries for task ledger template");
733 ledger_track_maximum(t
, task_ledgers
.phys_footprint
, 60);
735 if (pmap_ledgers_panic
) {
736 ledger_panic_on_negative(t
, task_ledgers
.phys_footprint
);
737 ledger_panic_on_negative(t
, task_ledgers
.internal
);
738 ledger_panic_on_negative(t
, task_ledgers
.internal_compressed
);
739 ledger_panic_on_negative(t
, task_ledgers
.iokit_mapped
);
740 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting
);
741 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting_compressed
);
742 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile
);
743 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile
);
744 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile_compressed
);
745 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
747 #endif /* MACH_ASSERT */
750 ledger_set_callback(t
, task_ledgers
.phys_footprint
, task_footprint_exceeded
, NULL
, NULL
);
753 ledger_set_callback(t
, task_ledgers
.interrupt_wakeups
,
754 task_wakeups_rate_exceeded
, NULL
, NULL
);
756 task_ledger_template
= t
;
760 task_create_internal(
762 coalition_t
*parent_coalitions __unused
,
763 boolean_t inherit_memory
,
765 task_t
*child_task
) /* OUT */
768 vm_shared_region_t shared_region
;
769 ledger_t ledger
= NULL
;
771 new_task
= (task_t
) zalloc(task_zone
);
773 if (new_task
== TASK_NULL
)
774 return(KERN_RESOURCE_SHORTAGE
);
776 /* one ref for just being alive; one for our caller */
777 new_task
->ref_count
= 2;
779 /* allocate with active entries */
780 assert(task_ledger_template
!= NULL
);
781 if ((ledger
= ledger_instantiate(task_ledger_template
,
782 LEDGER_CREATE_ACTIVE_ENTRIES
)) == NULL
) {
783 zfree(task_zone
, new_task
);
784 return(KERN_RESOURCE_SHORTAGE
);
787 new_task
->ledger
= ledger
;
789 #if defined(CONFIG_SCHED_MULTIQ)
790 new_task
->sched_group
= sched_group_create();
793 /* if inherit_memory is true, parent_task MUST not be NULL */
795 new_task
->map
= vm_map_fork(ledger
, parent_task
->map
);
797 new_task
->map
= vm_map_create(pmap_create(ledger
, 0, is_64bit
),
798 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
799 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
801 /* Inherit memlock limit from parent */
803 vm_map_set_user_wire_limit(new_task
->map
, (vm_size_t
)parent_task
->map
->user_wire_limit
);
805 lck_mtx_init(&new_task
->lock
, &task_lck_grp
, &task_lck_attr
);
806 queue_init(&new_task
->threads
);
807 new_task
->suspend_count
= 0;
808 new_task
->thread_count
= 0;
809 new_task
->active_thread_count
= 0;
810 new_task
->user_stop_count
= 0;
811 new_task
->legacy_stop_count
= 0;
812 new_task
->active
= TRUE
;
813 new_task
->halting
= FALSE
;
814 new_task
->user_data
= NULL
;
815 new_task
->faults
= 0;
816 new_task
->cow_faults
= 0;
817 new_task
->pageins
= 0;
818 new_task
->messages_sent
= 0;
819 new_task
->messages_received
= 0;
820 new_task
->syscalls_mach
= 0;
821 new_task
->priv_flags
= 0;
822 new_task
->syscalls_unix
=0;
823 new_task
->c_switch
= new_task
->p_switch
= new_task
->ps_switch
= 0;
824 new_task
->t_flags
= 0;
825 new_task
->importance
= 0;
828 new_task
->atm_context
= NULL
;
831 new_task
->bank_context
= NULL
;
834 zinfo_task_init(new_task
);
837 new_task
->bsd_info
= NULL
;
838 new_task
->corpse_info
= NULL
;
839 #endif /* MACH_BSD */
842 if (max_task_footprint
!= 0) {
843 ledger_set_limit(ledger
, task_ledgers
.phys_footprint
, max_task_footprint
, PHYS_FOOTPRINT_WARNING_LEVEL
);
847 if (task_wakeups_monitor_rate
!= 0) {
848 uint32_t flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
849 int32_t rate
; // Ignored because of WAKEMON_SET_DEFAULTS
850 task_wakeups_monitor_ctl(new_task
, &flags
, &rate
);
853 #if defined(__i386__) || defined(__x86_64__)
854 new_task
->i386_ldt
= 0;
857 new_task
->task_debug
= NULL
;
859 queue_init(&new_task
->semaphore_list
);
860 new_task
->semaphores_owned
= 0;
862 ipc_task_init(new_task
, parent_task
);
864 new_task
->total_user_time
= 0;
865 new_task
->total_system_time
= 0;
867 new_task
->vtimers
= 0;
869 new_task
->shared_region
= NULL
;
871 new_task
->affinity_space
= NULL
;
873 new_task
->pidsuspended
= FALSE
;
874 new_task
->frozen
= FALSE
;
875 new_task
->changing_freeze_state
= FALSE
;
876 new_task
->rusage_cpu_flags
= 0;
877 new_task
->rusage_cpu_percentage
= 0;
878 new_task
->rusage_cpu_interval
= 0;
879 new_task
->rusage_cpu_deadline
= 0;
880 new_task
->rusage_cpu_callt
= NULL
;
882 new_task
->suspends_outstanding
= 0;
886 new_task
->hv_task_target
= NULL
;
887 #endif /* HYPERVISOR */
890 new_task
->low_mem_notified_warn
= 0;
891 new_task
->low_mem_notified_critical
= 0;
892 new_task
->low_mem_privileged_listener
= 0;
893 new_task
->purged_memory_warn
= 0;
894 new_task
->purged_memory_critical
= 0;
895 new_task
->mem_notify_reserved
= 0;
896 #if IMPORTANCE_INHERITANCE
897 new_task
->task_imp_base
= NULL
;
898 #endif /* IMPORTANCE_INHERITANCE */
900 #if defined(__x86_64__)
901 new_task
->uexc_range_start
= new_task
->uexc_range_size
= new_task
->uexc_handler
= 0;
904 new_task
->requested_policy
= default_task_requested_policy
;
905 new_task
->effective_policy
= default_task_effective_policy
;
906 new_task
->pended_policy
= default_task_pended_policy
;
908 if (parent_task
!= TASK_NULL
) {
909 new_task
->sec_token
= parent_task
->sec_token
;
910 new_task
->audit_token
= parent_task
->audit_token
;
912 /* inherit the parent's shared region */
913 shared_region
= vm_shared_region_get(parent_task
);
914 vm_shared_region_set(new_task
, shared_region
);
916 if(task_has_64BitAddr(parent_task
))
917 task_set_64BitAddr(new_task
);
918 new_task
->all_image_info_addr
= parent_task
->all_image_info_addr
;
919 new_task
->all_image_info_size
= parent_task
->all_image_info_size
;
921 #if defined(__i386__) || defined(__x86_64__)
922 if (inherit_memory
&& parent_task
->i386_ldt
)
923 new_task
->i386_ldt
= user_ldt_copy(parent_task
->i386_ldt
);
925 if (inherit_memory
&& parent_task
->affinity_space
)
926 task_affinity_create(parent_task
, new_task
);
928 new_task
->pset_hint
= parent_task
->pset_hint
= task_choose_pset(parent_task
);
930 #if IMPORTANCE_INHERITANCE
931 ipc_importance_task_t new_task_imp
= IIT_NULL
;
933 if (task_is_marked_importance_donor(parent_task
)) {
934 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
935 assert(IIT_NULL
!= new_task_imp
);
936 ipc_importance_task_mark_donor(new_task_imp
, TRUE
);
938 /* Embedded doesn't want this to inherit */
939 if (task_is_marked_importance_receiver(parent_task
)) {
940 if (IIT_NULL
== new_task_imp
)
941 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
942 assert(IIT_NULL
!= new_task_imp
);
943 ipc_importance_task_mark_receiver(new_task_imp
, TRUE
);
945 if (task_is_marked_importance_denap_receiver(parent_task
)) {
946 if (IIT_NULL
== new_task_imp
)
947 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
948 assert(IIT_NULL
!= new_task_imp
);
949 ipc_importance_task_mark_denap_receiver(new_task_imp
, TRUE
);
952 if (IIT_NULL
!= new_task_imp
) {
953 assert(new_task
->task_imp_base
== new_task_imp
);
954 ipc_importance_task_release(new_task_imp
);
956 #endif /* IMPORTANCE_INHERITANCE */
958 new_task
->priority
= BASEPRI_DEFAULT
;
959 new_task
->max_priority
= MAXPRI_USER
;
961 new_task
->requested_policy
.t_apptype
= parent_task
->requested_policy
.t_apptype
;
963 new_task
->requested_policy
.int_darwinbg
= parent_task
->requested_policy
.int_darwinbg
;
964 new_task
->requested_policy
.ext_darwinbg
= parent_task
->requested_policy
.ext_darwinbg
;
965 new_task
->requested_policy
.int_iotier
= parent_task
->requested_policy
.int_iotier
;
966 new_task
->requested_policy
.ext_iotier
= parent_task
->requested_policy
.ext_iotier
;
967 new_task
->requested_policy
.int_iopassive
= parent_task
->requested_policy
.int_iopassive
;
968 new_task
->requested_policy
.ext_iopassive
= parent_task
->requested_policy
.ext_iopassive
;
969 new_task
->requested_policy
.bg_iotier
= parent_task
->requested_policy
.bg_iotier
;
970 new_task
->requested_policy
.terminated
= parent_task
->requested_policy
.terminated
;
971 new_task
->requested_policy
.t_qos_clamp
= parent_task
->requested_policy
.t_qos_clamp
;
973 task_policy_create(new_task
, parent_task
->requested_policy
.t_boosted
);
975 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
976 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
979 task_set_64BitAddr(new_task
);
981 new_task
->all_image_info_addr
= (mach_vm_address_t
)0;
982 new_task
->all_image_info_size
= (mach_vm_size_t
)0;
984 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
986 if (kernel_task
== TASK_NULL
) {
987 new_task
->priority
= BASEPRI_KERNEL
;
988 new_task
->max_priority
= MAXPRI_KERNEL
;
990 new_task
->priority
= BASEPRI_DEFAULT
;
991 new_task
->max_priority
= MAXPRI_USER
;
995 bzero(new_task
->coalition
, sizeof(new_task
->coalition
));
996 for (int i
= 0; i
< COALITION_NUM_TYPES
; i
++)
997 queue_chain_init(new_task
->task_coalition
[i
]);
999 /* Allocate I/O Statistics */
1000 new_task
->task_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
1001 assert(new_task
->task_io_stats
!= NULL
);
1002 bzero(new_task
->task_io_stats
, sizeof(struct io_stat_info
));
1003 new_task
->task_immediate_writes
= 0;
1004 new_task
->task_deferred_writes
= 0;
1005 new_task
->task_invalidated_writes
= 0;
1006 new_task
->task_metadata_writes
= 0;
1008 bzero(&(new_task
->cpu_time_qos_stats
), sizeof(struct _cpu_time_qos_stats
));
1010 bzero(&new_task
->extmod_statistics
, sizeof(new_task
->extmod_statistics
));
1011 new_task
->task_timer_wakeups_bin_1
= new_task
->task_timer_wakeups_bin_2
= 0;
1012 new_task
->task_gpu_ns
= 0;
1014 #if CONFIG_COALITIONS
1016 /* TODO: there is no graceful failure path here... */
1017 if (parent_coalitions
&& parent_coalitions
[COALITION_TYPE_RESOURCE
]) {
1018 coalitions_adopt_task(parent_coalitions
, new_task
);
1019 } else if (parent_task
&& parent_task
->coalition
[COALITION_TYPE_RESOURCE
]) {
1021 * all tasks at least have a resource coalition, so
1022 * if the parent has one then inherit all coalitions
1023 * the parent is a part of
1025 coalitions_adopt_task(parent_task
->coalition
, new_task
);
1027 /* TODO: assert that new_task will be PID 1 (launchd) */
1028 coalitions_adopt_init_task(new_task
);
1031 if (new_task
->coalition
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
1032 panic("created task is not a member of a resource coalition");
1034 #endif /* CONFIG_COALITIONS */
1036 new_task
->dispatchqueue_offset
= 0;
1037 if (parent_task
!= NULL
) {
1038 new_task
->dispatchqueue_offset
= parent_task
->dispatchqueue_offset
;
1041 if (vm_backing_store_low
&& parent_task
!= NULL
)
1042 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
1044 new_task
->task_volatile_objects
= 0;
1045 new_task
->task_nonvolatile_objects
= 0;
1046 new_task
->task_purgeable_disowning
= FALSE
;
1047 new_task
->task_purgeable_disowned
= FALSE
;
1049 ipc_task_enable(new_task
);
1051 lck_mtx_lock(&tasks_threads_lock
);
1052 queue_enter(&tasks
, new_task
, task_t
, tasks
);
1054 if (tasks_suspend_state
) {
1055 task_suspend_internal(new_task
);
1057 lck_mtx_unlock(&tasks_threads_lock
);
1059 *child_task
= new_task
;
1060 return(KERN_SUCCESS
);
1063 int task_dropped_imp_count
= 0;
1068 * Drop a reference on a task.
1074 ledger_amount_t credit
, debit
, interrupt_wakeups
, platform_idle_wakeups
;
1077 if (task
== TASK_NULL
)
1080 refs
= task_deallocate_internal(task
);
1082 #if IMPORTANCE_INHERITANCE
1088 * If last ref potentially comes from the task's importance,
1089 * disconnect it. But more task refs may be added before
1090 * that completes, so wait for the reference to go to zero
1091 * naturually (it may happen on a recursive task_deallocate()
1092 * from the ipc_importance_disconnect_task() call).
1094 if (IIT_NULL
!= task
->task_imp_base
)
1095 ipc_importance_disconnect_task(task
);
1101 #endif /* IMPORTANCE_INHERITANCE */
1103 lck_mtx_lock(&tasks_threads_lock
);
1104 queue_remove(&terminated_tasks
, task
, task_t
, tasks
);
1105 terminated_tasks_count
--;
1106 lck_mtx_unlock(&tasks_threads_lock
);
1109 * remove the reference on atm descriptor
1111 task_atm_reset(task
);
1114 * remove the reference on bank context
1116 task_bank_reset(task
);
1118 if (task
->task_io_stats
)
1119 kfree(task
->task_io_stats
, sizeof(struct io_stat_info
));
1122 * Give the machine dependent code a chance
1123 * to perform cleanup before ripping apart
1126 machine_task_terminate(task
);
1128 ipc_task_terminate(task
);
1130 if (task
->affinity_space
)
1131 task_affinity_deallocate(task
);
1134 if (task
->ledger
!= NULL
&&
1135 task
->map
!= NULL
&&
1136 task
->map
->pmap
!= NULL
&&
1137 task
->map
->pmap
->ledger
!= NULL
) {
1138 assert(task
->ledger
== task
->map
->pmap
->ledger
);
1140 #endif /* MACH_ASSERT */
1142 vm_purgeable_disown(task
);
1143 assert(task
->task_purgeable_disowned
);
1144 if (task
->task_volatile_objects
!= 0 ||
1145 task
->task_nonvolatile_objects
!= 0) {
1146 panic("task_deallocate(%p): "
1147 "volatile_objects=%d nonvolatile_objects=%d\n",
1149 task
->task_volatile_objects
,
1150 task
->task_nonvolatile_objects
);
1153 vm_map_deallocate(task
->map
);
1154 is_release(task
->itk_space
);
1156 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
1157 &interrupt_wakeups
, &debit
);
1158 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
1159 &platform_idle_wakeups
, &debit
);
1161 #if defined(CONFIG_SCHED_MULTIQ)
1162 sched_group_destroy(task
->sched_group
);
1165 /* Accumulate statistics for dead tasks */
1166 lck_spin_lock(&dead_task_statistics_lock
);
1167 dead_task_statistics
.total_user_time
+= task
->total_user_time
;
1168 dead_task_statistics
.total_system_time
+= task
->total_system_time
;
1170 dead_task_statistics
.task_interrupt_wakeups
+= interrupt_wakeups
;
1171 dead_task_statistics
.task_platform_idle_wakeups
+= platform_idle_wakeups
;
1173 dead_task_statistics
.task_timer_wakeups_bin_1
+= task
->task_timer_wakeups_bin_1
;
1174 dead_task_statistics
.task_timer_wakeups_bin_2
+= task
->task_timer_wakeups_bin_2
;
1176 lck_spin_unlock(&dead_task_statistics_lock
);
1177 lck_mtx_destroy(&task
->lock
, &task_lck_grp
);
1179 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_private
, &credit
,
1181 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_private
.alloc
);
1182 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_private
.free
);
1184 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_shared
, &credit
,
1186 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_shared
.alloc
);
1187 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_shared
.free
);
1189 ledger_dereference(task
->ledger
);
1190 zinfo_task_free(task
);
1192 #if TASK_REFERENCE_LEAK_DEBUG
1193 btlog_remove_entries_for_element(task_ref_btlog
, task
);
1196 #if CONFIG_COALITIONS
1197 if (!task
->coalition
[COALITION_TYPE_RESOURCE
])
1198 panic("deallocating task was not a member of a resource coalition");
1199 task_release_coalitions(task
);
1200 #endif /* CONFIG_COALITIONS */
1202 bzero(task
->coalition
, sizeof(task
->coalition
));
1205 /* clean up collected information since last reference to task is gone */
1206 if (task
->corpse_info
) {
1207 task_crashinfo_destroy(task
->corpse_info
);
1208 task
->corpse_info
= NULL
;
1212 zfree(task_zone
, task
);
1216 * task_name_deallocate:
1218 * Drop a reference on a task name.
1221 task_name_deallocate(
1222 task_name_t task_name
)
1224 return(task_deallocate((task_t
)task_name
));
1228 * task_suspension_token_deallocate:
1230 * Drop a reference on a task suspension token.
1233 task_suspension_token_deallocate(
1234 task_suspension_token_t token
)
1236 return(task_deallocate((task_t
)token
));
1241 * task_collect_crash_info:
1243 * collect crash info from bsd and mach based data
1246 task_collect_crash_info(task_t task
)
1248 kern_return_t kr
= KERN_SUCCESS
;
1250 kcdata_descriptor_t crash_data
= NULL
;
1251 kcdata_descriptor_t crash_data_release
= NULL
;
1252 mach_msg_type_number_t size
= CORPSEINFO_ALLOCATION_SIZE
;
1253 mach_vm_offset_t crash_data_user_ptr
= 0;
1255 if (!corpses_enabled()) {
1256 return KERN_NOT_SUPPORTED
;
1260 assert(task
->bsd_info
!= NULL
);
1261 if (task
->corpse_info
== NULL
&& task
->bsd_info
!= NULL
) {
1263 /* map crash data memory in task's vm map */
1264 kr
= mach_vm_allocate(task
->map
, &crash_data_user_ptr
, size
, (VM_MAKE_TAG(VM_MEMORY_CORPSEINFO
) | VM_FLAGS_ANYWHERE
));
1266 if (kr
!= KERN_SUCCESS
)
1269 crash_data
= task_crashinfo_alloc_init((mach_vm_address_t
)crash_data_user_ptr
, size
);
1272 crash_data_release
= task
->corpse_info
;
1273 task
->corpse_info
= crash_data
;
1277 /* if failed to create corpse info, free the mapping */
1278 if (KERN_SUCCESS
!= mach_vm_deallocate(task
->map
, crash_data_user_ptr
, size
)) {
1279 printf("mach_vm_deallocate failed to clear corpse_data for pid %d.\n", task_pid(task
));
1284 if (crash_data_release
!= NULL
) {
1285 task_crashinfo_destroy(crash_data_release
);
1296 * task_deliver_crash_notification:
1298 * Makes outcall to registered host port for a corpse.
1301 task_deliver_crash_notification(task_t task
)
1303 kcdata_descriptor_t crash_info
= task
->corpse_info
;
1304 thread_t th_iter
= NULL
;
1305 kern_return_t kr
= KERN_SUCCESS
;
1306 wait_interrupt_t wsave
;
1307 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1309 if (crash_info
== NULL
)
1310 return KERN_FAILURE
;
1312 code
[0] = crash_info
->kcd_addr_begin
;
1313 code
[1] = crash_info
->kcd_length
;
1316 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1318 ipc_thread_reset(th_iter
);
1322 wsave
= thread_interrupt_level(THREAD_UNINT
);
1323 kr
= exception_triage(EXC_CORPSE_NOTIFY
, code
, EXCEPTION_CODE_MAX
);
1324 if (kr
!= KERN_SUCCESS
) {
1325 printf("Failed to send exception EXC_CORPSE_NOTIFY. error code: %d for pid %d\n", kr
, task_pid(task
));
1329 * crash reporting is done. Now release threads
1330 * for reaping by thread_terminate_daemon
1333 assert(task
->active_thread_count
== 0);
1334 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1336 thread_mtx_lock(th_iter
);
1337 assert(th_iter
->inspection
== TRUE
);
1338 th_iter
->inspection
= FALSE
;
1339 /* now that the corpse has been autopsied, dispose of the thread name */
1340 uthread_cleanup_name(th_iter
->uthread
);
1341 thread_mtx_unlock(th_iter
);
1344 thread_terminate_crashed_threads();
1345 /* remove the pending corpse report flag */
1346 task_clear_corpse_pending_report(task
);
1350 (void)thread_interrupt_level(wsave
);
1351 task_terminate_internal(task
);
1359 * Terminate the specified task. See comments on thread_terminate
1360 * (kern/thread.c) about problems with terminating the "current task."
1367 if (task
== TASK_NULL
)
1368 return (KERN_INVALID_ARGUMENT
);
1371 return (KERN_FAILURE
);
1373 return (task_terminate_internal(task
));
1377 extern int proc_pid(struct proc
*);
1378 extern void proc_name_kdp(task_t t
, char *buf
, int size
);
1379 #endif /* MACH_ASSERT */
1381 #define VM_MAP_PARTIAL_REAP 0x54 /* 0x150 */
1383 __unused
task_partial_reap(task_t task
, __unused
int pid
)
1385 unsigned int reclaimed_resident
= 0;
1386 unsigned int reclaimed_compressed
= 0;
1387 uint64_t task_page_count
;
1389 task_page_count
= (get_task_phys_footprint(task
) / PAGE_SIZE_64
);
1391 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_START
),
1392 pid
, task_page_count
, 0, 0, 0);
1394 vm_map_partial_reap(task
->map
, &reclaimed_resident
, &reclaimed_compressed
);
1396 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_END
),
1397 pid
, reclaimed_resident
, reclaimed_compressed
, 0, 0);
1401 task_mark_corpse(task_t task
)
1403 kern_return_t kr
= KERN_SUCCESS
;
1404 thread_t self_thread
;
1406 wait_interrupt_t wsave
;
1408 assert(task
!= kernel_task
);
1409 assert(task
== current_task());
1410 assert(!task_is_a_corpse(task
));
1412 kr
= task_collect_crash_info(task
);
1413 if (kr
!= KERN_SUCCESS
) {
1417 self_thread
= current_thread();
1419 wsave
= thread_interrupt_level(THREAD_UNINT
);
1422 task_set_corpse_pending_report(task
);
1423 task_set_corpse(task
);
1425 kr
= task_start_halt_locked(task
, TRUE
);
1426 assert(kr
== KERN_SUCCESS
);
1427 ipc_task_reset(task
);
1428 ipc_task_enable(task
);
1431 /* terminate the ipc space */
1432 ipc_space_terminate(task
->itk_space
);
1434 task_start_halt(task
);
1435 thread_terminate_internal(self_thread
);
1436 (void) thread_interrupt_level(wsave
);
1437 assert(task
->halting
== TRUE
);
1442 task_terminate_internal(
1445 thread_t thread
, self
;
1447 boolean_t interrupt_save
;
1450 assert(task
!= kernel_task
);
1452 self
= current_thread();
1453 self_task
= self
->task
;
1456 * Get the task locked and make sure that we are not racing
1457 * with someone else trying to terminate us.
1459 if (task
== self_task
)
1462 if (task
< self_task
) {
1464 task_lock(self_task
);
1467 task_lock(self_task
);
1471 if (!task
->active
) {
1473 * Task is already being terminated.
1474 * Just return an error. If we are dying, this will
1475 * just get us to our AST special handler and that
1476 * will get us to finalize the termination of ourselves.
1479 if (self_task
!= task
)
1480 task_unlock(self_task
);
1482 return (KERN_FAILURE
);
1485 if (task_corpse_pending_report(task
)) {
1487 * Task is marked for reporting as corpse.
1488 * Just return an error. This will
1489 * just get us to our AST special handler and that
1490 * will get us to finish the path to death
1493 if (self_task
!= task
)
1494 task_unlock(self_task
);
1496 return (KERN_FAILURE
);
1499 if (self_task
!= task
)
1500 task_unlock(self_task
);
1503 * Make sure the current thread does not get aborted out of
1504 * the waits inside these operations.
1506 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
1509 * Indicate that we want all the threads to stop executing
1510 * at user space by holding the task (we would have held
1511 * each thread independently in thread_terminate_internal -
1512 * but this way we may be more likely to already find it
1513 * held there). Mark the task inactive, and prevent
1514 * further task operations via the task port.
1516 task_hold_locked(task
);
1517 task
->active
= FALSE
;
1518 ipc_task_disable(task
);
1520 #if CONFIG_TELEMETRY
1522 * Notify telemetry that this task is going away.
1524 telemetry_task_ctl_locked(task
, TF_TELEMETRY
, 0);
1528 * Terminate each thread in the task.
1530 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1531 thread_terminate_internal(thread
);
1535 if (task
->bsd_info
!= NULL
) {
1536 pid
= proc_pid(task
->bsd_info
);
1538 #endif /* MACH_BSD */
1542 proc_set_task_policy(task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
,
1543 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
1545 /* Early object reap phase */
1547 // PR-17045188: Revisit implementation
1548 // task_partial_reap(task, pid);
1552 * Destroy all synchronizers owned by the task.
1554 task_synchronizer_destroy_all(task
);
1557 * Destroy the IPC space, leaving just a reference for it.
1559 ipc_space_terminate(task
->itk_space
);
1562 /* if some ledgers go negative on tear-down again... */
1563 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1564 task_ledgers
.phys_footprint
);
1565 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1566 task_ledgers
.internal
);
1567 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1568 task_ledgers
.internal_compressed
);
1569 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1570 task_ledgers
.iokit_mapped
);
1571 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1572 task_ledgers
.alternate_accounting
);
1573 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1574 task_ledgers
.alternate_accounting_compressed
);
1578 * If the current thread is a member of the task
1579 * being terminated, then the last reference to
1580 * the task will not be dropped until the thread
1581 * is finally reaped. To avoid incurring the
1582 * expense of removing the address space regions
1583 * at reap time, we do it explictly here.
1586 vm_map_lock(task
->map
);
1587 vm_map_disable_hole_optimization(task
->map
);
1588 vm_map_unlock(task
->map
);
1590 vm_map_remove(task
->map
,
1591 task
->map
->min_offset
,
1592 task
->map
->max_offset
,
1593 /* no unnesting on final cleanup: */
1594 VM_MAP_REMOVE_NO_UNNESTING
);
1596 /* release our shared region */
1597 vm_shared_region_set(task
, NULL
);
1602 * Identify the pmap's process, in case the pmap ledgers drift
1603 * and we have to report it.
1606 if (task
->bsd_info
) {
1607 pid
= proc_pid(task
->bsd_info
);
1608 proc_name_kdp(task
, procname
, sizeof (procname
));
1611 strlcpy(procname
, "<unknown>", sizeof (procname
));
1613 pmap_set_process(task
->map
->pmap
, pid
, procname
);
1614 #endif /* MACH_ASSERT */
1616 lck_mtx_lock(&tasks_threads_lock
);
1617 queue_remove(&tasks
, task
, task_t
, tasks
);
1618 queue_enter(&terminated_tasks
, task
, task_t
, tasks
);
1620 terminated_tasks_count
++;
1621 lck_mtx_unlock(&tasks_threads_lock
);
1624 * We no longer need to guard against being aborted, so restore
1625 * the previous interruptible state.
1627 thread_interrupt_level(interrupt_save
);
1630 /* force the task to release all ctrs */
1631 if (task
->t_chud
& TASK_KPC_FORCED_ALL_CTRS
)
1632 kpc_force_all_ctrs(task
, 0);
1635 #if CONFIG_COALITIONS
1637 * Leave our coalitions. (drop activation but not reference)
1639 coalitions_remove_task(task
);
1643 * Get rid of the task active reference on itself.
1645 task_deallocate(task
);
1647 return (KERN_SUCCESS
);
1651 tasks_system_suspend(boolean_t suspend
)
1655 lck_mtx_lock(&tasks_threads_lock
);
1656 assert(tasks_suspend_state
!= suspend
);
1657 tasks_suspend_state
= suspend
;
1658 queue_iterate(&tasks
, task
, task_t
, tasks
) {
1659 if (task
== kernel_task
) {
1662 suspend
? task_suspend_internal(task
) : task_resume_internal(task
);
1664 lck_mtx_unlock(&tasks_threads_lock
);
1670 * Shut the current task down (except for the current thread) in
1671 * preparation for dramatic changes to the task (probably exec).
1672 * We hold the task and mark all other threads in the task for
1676 task_start_halt(task_t task
)
1678 kern_return_t kr
= KERN_SUCCESS
;
1680 kr
= task_start_halt_locked(task
, FALSE
);
1685 static kern_return_t
1686 task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
)
1688 thread_t thread
, self
;
1689 uint64_t dispatchqueue_offset
;
1691 assert(task
!= kernel_task
);
1693 self
= current_thread();
1695 if (task
!= self
->task
)
1696 return (KERN_INVALID_ARGUMENT
);
1698 if (task
->halting
|| !task
->active
|| !self
->active
) {
1700 * Task or current thread is already being terminated.
1701 * Hurry up and return out of the current kernel context
1702 * so that we run our AST special handler to terminate
1705 return (KERN_FAILURE
);
1708 task
->halting
= TRUE
;
1711 * Mark all the threads to keep them from starting any more
1712 * user-level execution. The thread_terminate_internal code
1713 * would do this on a thread by thread basis anyway, but this
1714 * gives us a better chance of not having to wait there.
1716 task_hold_locked(task
);
1717 dispatchqueue_offset
= get_dispatchqueue_offset_from_proc(task
->bsd_info
);
1720 * Terminate all the other threads in the task.
1722 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
)
1724 if (should_mark_corpse
) {
1725 thread_mtx_lock(thread
);
1726 thread
->inspection
= TRUE
;
1727 thread_mtx_unlock(thread
);
1730 thread_terminate_internal(thread
);
1732 task
->dispatchqueue_offset
= dispatchqueue_offset
;
1734 task_release_locked(task
);
1736 return KERN_SUCCESS
;
1741 * task_complete_halt:
1743 * Complete task halt by waiting for threads to terminate, then clean
1744 * up task resources (VM, port namespace, etc...) and then let the
1745 * current thread go in the (practically empty) task context.
1748 task_complete_halt(task_t task
)
1751 assert(task
->halting
);
1752 assert(task
== current_task());
1755 * Wait for the other threads to get shut down.
1756 * When the last other thread is reaped, we'll be
1759 if (task
->thread_count
> 1) {
1760 assert_wait((event_t
)&task
->halting
, THREAD_UNINT
);
1762 thread_block(THREAD_CONTINUE_NULL
);
1768 * Give the machine dependent code a chance
1769 * to perform cleanup of task-level resources
1770 * associated with the current thread before
1771 * ripping apart the task.
1773 machine_task_terminate(task
);
1776 * Destroy all synchronizers owned by the task.
1778 task_synchronizer_destroy_all(task
);
1781 * Destroy the contents of the IPC space, leaving just
1782 * a reference for it.
1784 ipc_space_clean(task
->itk_space
);
1787 * Clean out the address space, as we are going to be
1788 * getting a new one.
1790 vm_map_remove(task
->map
, task
->map
->min_offset
,
1791 task
->map
->max_offset
,
1792 /* no unnesting on final cleanup: */
1793 VM_MAP_REMOVE_NO_UNNESTING
);
1795 task
->halting
= FALSE
;
1801 * Suspend execution of the specified task.
1802 * This is a recursive-style suspension of the task, a count of
1803 * suspends is maintained.
1805 * CONDITIONS: the task is locked and active.
1809 register task_t task
)
1811 register thread_t thread
;
1813 assert(task
->active
);
1815 if (task
->suspend_count
++ > 0)
1819 * Iterate through all the threads and hold them.
1821 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1822 thread_mtx_lock(thread
);
1823 thread_hold(thread
);
1824 thread_mtx_unlock(thread
);
1831 * Same as the internal routine above, except that is must lock
1832 * and verify that the task is active. This differs from task_suspend
1833 * in that it places a kernel hold on the task rather than just a
1834 * user-level hold. This keeps users from over resuming and setting
1835 * it running out from under the kernel.
1837 * CONDITIONS: the caller holds a reference on the task
1841 register task_t task
)
1843 if (task
== TASK_NULL
)
1844 return (KERN_INVALID_ARGUMENT
);
1848 if (!task
->active
) {
1851 return (KERN_FAILURE
);
1854 task_hold_locked(task
);
1857 return (KERN_SUCCESS
);
1863 boolean_t until_not_runnable
)
1865 if (task
== TASK_NULL
)
1866 return (KERN_INVALID_ARGUMENT
);
1870 if (!task
->active
) {
1873 return (KERN_FAILURE
);
1876 task_wait_locked(task
, until_not_runnable
);
1879 return (KERN_SUCCESS
);
1885 * Wait for all threads in task to stop.
1888 * Called with task locked, active, and held.
1892 register task_t task
,
1893 boolean_t until_not_runnable
)
1895 register thread_t thread
, self
;
1897 assert(task
->active
);
1898 assert(task
->suspend_count
> 0);
1900 self
= current_thread();
1903 * Iterate through all the threads and wait for them to
1904 * stop. Do not wait for the current thread if it is within
1907 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1909 thread_wait(thread
, until_not_runnable
);
1914 * task_release_locked:
1916 * Release a kernel hold on a task.
1918 * CONDITIONS: the task is locked and active
1921 task_release_locked(
1922 register task_t task
)
1924 register thread_t thread
;
1926 assert(task
->active
);
1927 assert(task
->suspend_count
> 0);
1929 if (--task
->suspend_count
> 0)
1932 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1933 thread_mtx_lock(thread
);
1934 thread_release(thread
);
1935 thread_mtx_unlock(thread
);
1942 * Same as the internal routine above, except that it must lock
1943 * and verify that the task is active.
1945 * CONDITIONS: The caller holds a reference to the task
1951 if (task
== TASK_NULL
)
1952 return (KERN_INVALID_ARGUMENT
);
1956 if (!task
->active
) {
1959 return (KERN_FAILURE
);
1962 task_release_locked(task
);
1965 return (KERN_SUCCESS
);
1971 thread_act_array_t
*threads_out
,
1972 mach_msg_type_number_t
*count
)
1974 mach_msg_type_number_t actual
;
1975 thread_t
*thread_list
;
1977 vm_size_t size
, size_needed
;
1981 if (task
== TASK_NULL
)
1982 return (KERN_INVALID_ARGUMENT
);
1984 size
= 0; addr
= NULL
;
1988 if (!task
->active
) {
1994 return (KERN_FAILURE
);
1997 actual
= task
->thread_count
;
1999 /* do we have the memory we need? */
2000 size_needed
= actual
* sizeof (mach_port_t
);
2001 if (size_needed
<= size
)
2004 /* unlock the task and allocate more memory */
2010 assert(size_needed
> 0);
2013 addr
= kalloc(size
);
2015 return (KERN_RESOURCE_SHORTAGE
);
2018 /* OK, have memory and the task is locked & active */
2019 thread_list
= (thread_t
*)addr
;
2023 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
2024 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
2025 thread_reference_internal(thread
);
2026 thread_list
[j
++] = thread
;
2029 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
2032 size_needed
= actual
* sizeof (mach_port_t
);
2034 /* can unlock task now that we've got the thread refs */
2038 /* no threads, so return null pointer and deallocate memory */
2040 *threads_out
= NULL
;
2047 /* if we allocated too much, must copy */
2049 if (size_needed
< size
) {
2052 newaddr
= kalloc(size_needed
);
2054 for (i
= 0; i
< actual
; ++i
)
2055 thread_deallocate(thread_list
[i
]);
2057 return (KERN_RESOURCE_SHORTAGE
);
2060 bcopy(addr
, newaddr
, size_needed
);
2062 thread_list
= (thread_t
*)newaddr
;
2065 *threads_out
= thread_list
;
2068 /* do the conversion that Mig should handle */
2070 for (i
= 0; i
< actual
; ++i
)
2071 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
2074 return (KERN_SUCCESS
);
2077 #define TASK_HOLD_NORMAL 0
2078 #define TASK_HOLD_PIDSUSPEND 1
2079 #define TASK_HOLD_LEGACY 2
2080 #define TASK_HOLD_LEGACY_ALL 3
2082 static kern_return_t
2084 register task_t task
,
2087 if (!task
->active
) {
2088 return (KERN_FAILURE
);
2091 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2092 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_SUSPEND
) | DBG_FUNC_NONE
,
2093 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2094 task
->user_stop_count
, task
->user_stop_count
+ 1, 0);
2097 current_task()->suspends_outstanding
++;
2100 if (mode
== TASK_HOLD_LEGACY
)
2101 task
->legacy_stop_count
++;
2103 if (task
->user_stop_count
++ > 0) {
2105 * If the stop count was positive, the task is
2106 * already stopped and we can exit.
2108 return (KERN_SUCCESS
);
2112 * Put a kernel-level hold on the threads in the task (all
2113 * user-level task suspensions added together represent a
2114 * single kernel-level hold). We then wait for the threads
2115 * to stop executing user code.
2117 task_hold_locked(task
);
2118 task_wait_locked(task
, FALSE
);
2120 return (KERN_SUCCESS
);
2123 static kern_return_t
2125 register task_t task
,
2128 register boolean_t release
= FALSE
;
2130 if (!task
->active
) {
2131 return (KERN_FAILURE
);
2134 if (mode
== TASK_HOLD_PIDSUSPEND
) {
2135 if (task
->pidsuspended
== FALSE
) {
2136 return (KERN_FAILURE
);
2138 task
->pidsuspended
= FALSE
;
2141 if (task
->user_stop_count
> (task
->pidsuspended
? 1 : 0)) {
2143 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2144 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_RESUME
) | DBG_FUNC_NONE
,
2145 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2146 task
->user_stop_count
, mode
, task
->legacy_stop_count
);
2150 * This is obviously not robust; if we suspend one task and then resume a different one,
2151 * we'll fly under the radar. This is only meant to catch the common case of a crashed
2152 * or buggy suspender.
2154 current_task()->suspends_outstanding
--;
2157 if (mode
== TASK_HOLD_LEGACY_ALL
) {
2158 if (task
->legacy_stop_count
>= task
->user_stop_count
) {
2159 task
->user_stop_count
= 0;
2162 task
->user_stop_count
-= task
->legacy_stop_count
;
2164 task
->legacy_stop_count
= 0;
2166 if (mode
== TASK_HOLD_LEGACY
&& task
->legacy_stop_count
> 0)
2167 task
->legacy_stop_count
--;
2168 if (--task
->user_stop_count
== 0)
2173 return (KERN_FAILURE
);
2177 * Release the task if necessary.
2180 task_release_locked(task
);
2182 return (KERN_SUCCESS
);
2189 * Implement an (old-fashioned) user-level suspension on a task.
2191 * Because the user isn't expecting to have to manage a suspension
2192 * token, we'll track it for him in the kernel in the form of a naked
2193 * send right to the task's resume port. All such send rights
2194 * account for a single suspension against the task (unlike task_suspend2()
2195 * where each caller gets a unique suspension count represented by a
2196 * unique send-once right).
2199 * The caller holds a reference to the task
2203 register task_t task
)
2206 mach_port_t port
, send
, old_notify
;
2207 mach_port_name_t name
;
2209 if (task
== TASK_NULL
|| task
== kernel_task
)
2210 return (KERN_INVALID_ARGUMENT
);
2215 * Claim a send right on the task resume port, and request a no-senders
2216 * notification on that port (if none outstanding).
2218 if (task
->itk_resume
== IP_NULL
) {
2219 task
->itk_resume
= ipc_port_alloc_kernel();
2220 if (!IP_VALID(task
->itk_resume
))
2221 panic("failed to create resume port");
2222 ipc_kobject_set(task
->itk_resume
, (ipc_kobject_t
)task
, IKOT_TASK_RESUME
);
2225 port
= task
->itk_resume
;
2227 assert(ip_active(port
));
2229 send
= ipc_port_make_send_locked(port
);
2230 assert(IP_VALID(send
));
2232 if (port
->ip_nsrequest
== IP_NULL
) {
2233 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
2234 assert(old_notify
== IP_NULL
);
2241 * place a legacy hold on the task.
2243 kr
= place_task_hold(task
, TASK_HOLD_LEGACY
);
2244 if (kr
!= KERN_SUCCESS
) {
2246 ipc_port_release_send(send
);
2253 * Copyout the send right into the calling task's IPC space. It won't know it is there,
2254 * but we'll look it up when calling a traditional resume. Any IPC operations that
2255 * deallocate the send right will auto-release the suspension.
2257 if ((kr
= ipc_kmsg_copyout_object(current_task()->itk_space
, (ipc_object_t
)send
,
2258 MACH_MSG_TYPE_MOVE_SEND
, &name
)) != KERN_SUCCESS
) {
2259 printf("warning: %s(%d) failed to copyout suspension token for pid %d with error: %d\n",
2260 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
2261 task_pid(task
), kr
);
2270 * Release a user hold on a task.
2273 * The caller holds a reference to the task
2277 register task_t task
)
2280 mach_port_name_t resume_port_name
;
2281 ipc_entry_t resume_port_entry
;
2282 ipc_space_t space
= current_task()->itk_space
;
2284 if (task
== TASK_NULL
|| task
== kernel_task
)
2285 return (KERN_INVALID_ARGUMENT
);
2287 /* release a legacy task hold */
2289 kr
= release_task_hold(task
, TASK_HOLD_LEGACY
);
2292 is_write_lock(space
);
2293 if (is_active(space
) && IP_VALID(task
->itk_resume
) &&
2294 ipc_hash_lookup(space
, (ipc_object_t
)task
->itk_resume
, &resume_port_name
, &resume_port_entry
) == TRUE
) {
2296 * We found a suspension token in the caller's IPC space. Release a send right to indicate that
2297 * we are holding one less legacy hold on the task from this caller. If the release failed,
2298 * go ahead and drop all the rights, as someone either already released our holds or the task
2301 if (kr
== KERN_SUCCESS
)
2302 ipc_right_dealloc(space
, resume_port_name
, resume_port_entry
);
2304 ipc_right_destroy(space
, resume_port_name
, resume_port_entry
, FALSE
, 0);
2305 /* space unlocked */
2307 is_write_unlock(space
);
2308 if (kr
== KERN_SUCCESS
)
2309 printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
2310 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
2318 * Suspend the target task.
2319 * Making/holding a token/reference/port is the callers responsibility.
2322 task_suspend_internal(task_t task
)
2326 if (task
== TASK_NULL
|| task
== kernel_task
)
2327 return (KERN_INVALID_ARGUMENT
);
2330 kr
= place_task_hold(task
, TASK_HOLD_NORMAL
);
2336 * Suspend the target task, and return a suspension token. The token
2337 * represents a reference on the suspended task.
2341 register task_t task
,
2342 task_suspension_token_t
*suspend_token
)
2346 kr
= task_suspend_internal(task
);
2347 if (kr
!= KERN_SUCCESS
) {
2348 *suspend_token
= TASK_NULL
;
2353 * Take a reference on the target task and return that to the caller
2354 * as a "suspension token," which can be converted into an SO right to
2355 * the now-suspended task's resume port.
2357 task_reference_internal(task
);
2358 *suspend_token
= task
;
2360 return (KERN_SUCCESS
);
2365 * (reference/token/port management is caller's responsibility).
2368 task_resume_internal(
2369 register task_suspension_token_t task
)
2373 if (task
== TASK_NULL
|| task
== kernel_task
)
2374 return (KERN_INVALID_ARGUMENT
);
2377 kr
= release_task_hold(task
, TASK_HOLD_NORMAL
);
2383 * Resume the task using a suspension token. Consumes the token's ref.
2387 register task_suspension_token_t task
)
2391 kr
= task_resume_internal(task
);
2392 task_suspension_token_deallocate(task
);
2398 task_suspension_notify(mach_msg_header_t
*request_header
)
2400 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
2401 task_t task
= convert_port_to_task_suspension_token(port
);
2402 mach_msg_type_number_t not_count
;
2404 if (task
== TASK_NULL
|| task
== kernel_task
)
2405 return TRUE
; /* nothing to do */
2407 switch (request_header
->msgh_id
) {
2409 case MACH_NOTIFY_SEND_ONCE
:
2410 /* release the hold held by this specific send-once right */
2412 release_task_hold(task
, TASK_HOLD_NORMAL
);
2416 case MACH_NOTIFY_NO_SENDERS
:
2417 not_count
= ((mach_no_senders_notification_t
*)request_header
)->not_count
;
2421 if (port
->ip_mscount
== not_count
) {
2423 /* release all the [remaining] outstanding legacy holds */
2424 assert(port
->ip_nsrequest
== IP_NULL
);
2426 release_task_hold(task
, TASK_HOLD_LEGACY_ALL
);
2429 } else if (port
->ip_nsrequest
== IP_NULL
) {
2430 ipc_port_t old_notify
;
2433 /* new send rights, re-arm notification at current make-send count */
2434 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
2435 assert(old_notify
== IP_NULL
);
2447 task_suspension_token_deallocate(task
); /* drop token reference */
2452 task_pidsuspend_locked(task_t task
)
2456 if (task
->pidsuspended
) {
2461 task
->pidsuspended
= TRUE
;
2463 kr
= place_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
2464 if (kr
!= KERN_SUCCESS
) {
2465 task
->pidsuspended
= FALSE
;
2475 * Suspends a task by placing a hold on its threads.
2478 * The caller holds a reference to the task
2482 register task_t task
)
2486 if (task
== TASK_NULL
|| task
== kernel_task
)
2487 return (KERN_INVALID_ARGUMENT
);
2491 kr
= task_pidsuspend_locked(task
);
2498 /* If enabled, we bring all the frozen pages back in prior to resumption; otherwise, they're faulted back in on demand */
2499 #define THAW_ON_RESUME 1
2503 * Resumes a previously suspended task.
2506 * The caller holds a reference to the task
2510 register task_t task
)
2514 if (task
== TASK_NULL
|| task
== kernel_task
)
2515 return (KERN_INVALID_ARGUMENT
);
2519 #if (CONFIG_FREEZE && THAW_ON_RESUME)
2521 while (task
->changing_freeze_state
) {
2523 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
2525 thread_block(THREAD_CONTINUE_NULL
);
2529 task
->changing_freeze_state
= TRUE
;
2532 kr
= release_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
2536 #if (CONFIG_FREEZE && THAW_ON_RESUME)
2537 if ((kr
== KERN_SUCCESS
) && (task
->frozen
== TRUE
)) {
2539 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2544 kr
= vm_map_thaw(task
->map
);
2549 if (kr
== KERN_SUCCESS
)
2550 task
->frozen
= FALSE
;
2551 task
->changing_freeze_state
= FALSE
;
2552 thread_wakeup(&task
->changing_freeze_state
);
2568 * The caller holds a reference to the task
2570 extern void vm_wake_compactor_swapper();
2571 extern queue_head_t c_swapout_list_head
;
2575 register task_t task
,
2576 uint32_t *purgeable_count
,
2577 uint32_t *wired_count
,
2578 uint32_t *clean_count
,
2579 uint32_t *dirty_count
,
2580 uint32_t dirty_budget
,
2582 boolean_t walk_only
)
2586 if (task
== TASK_NULL
|| task
== kernel_task
)
2587 return (KERN_INVALID_ARGUMENT
);
2591 while (task
->changing_freeze_state
) {
2593 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
2595 thread_block(THREAD_CONTINUE_NULL
);
2601 return (KERN_FAILURE
);
2603 task
->changing_freeze_state
= TRUE
;
2608 kr
= vm_map_freeze_walk(task
->map
, purgeable_count
, wired_count
, clean_count
, dirty_count
, dirty_budget
, shared
);
2610 kr
= vm_map_freeze(task
->map
, purgeable_count
, wired_count
, clean_count
, dirty_count
, dirty_budget
, shared
);
2615 if (walk_only
== FALSE
&& kr
== KERN_SUCCESS
)
2616 task
->frozen
= TRUE
;
2617 task
->changing_freeze_state
= FALSE
;
2618 thread_wakeup(&task
->changing_freeze_state
);
2622 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2623 vm_wake_compactor_swapper();
2625 * We do an explicit wakeup of the swapout thread here
2626 * because the compact_and_swap routines don't have
2627 * knowledge about these kind of "per-task packed c_segs"
2628 * and so will not be evaluating whether we need to do
2631 thread_wakeup((event_t
)&c_swapout_list_head
);
2640 * Thaw a currently frozen task.
2643 * The caller holds a reference to the task
2647 register task_t task
)
2651 if (task
== TASK_NULL
|| task
== kernel_task
)
2652 return (KERN_INVALID_ARGUMENT
);
2656 while (task
->changing_freeze_state
) {
2658 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
2660 thread_block(THREAD_CONTINUE_NULL
);
2664 if (!task
->frozen
) {
2666 return (KERN_FAILURE
);
2668 task
->changing_freeze_state
= TRUE
;
2670 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
) {
2673 kr
= vm_map_thaw(task
->map
);
2677 if (kr
== KERN_SUCCESS
)
2678 task
->frozen
= FALSE
;
2680 task
->frozen
= FALSE
;
2684 task
->changing_freeze_state
= FALSE
;
2685 thread_wakeup(&task
->changing_freeze_state
);
2689 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2690 vm_wake_compactor_swapper();
2696 #endif /* CONFIG_FREEZE */
2699 host_security_set_task_token(
2700 host_security_t host_security
,
2702 security_token_t sec_token
,
2703 audit_token_t audit_token
,
2704 host_priv_t host_priv
)
2706 ipc_port_t host_port
;
2709 if (task
== TASK_NULL
)
2710 return(KERN_INVALID_ARGUMENT
);
2712 if (host_security
== HOST_NULL
)
2713 return(KERN_INVALID_SECURITY
);
2716 task
->sec_token
= sec_token
;
2717 task
->audit_token
= audit_token
;
2721 if (host_priv
!= HOST_PRIV_NULL
) {
2722 kr
= host_get_host_priv_port(host_priv
, &host_port
);
2724 kr
= host_get_host_port(host_priv_self(), &host_port
);
2726 assert(kr
== KERN_SUCCESS
);
2727 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
2732 task_send_trace_memory(
2734 __unused
uint32_t pid
,
2735 __unused
uint64_t uniqueid
)
2737 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
2738 if (target_task
== TASK_NULL
)
2739 return (KERN_INVALID_ARGUMENT
);
2742 kr
= atm_send_proc_inspect_notification(target_task
,
2750 * This routine was added, pretty much exclusively, for registering the
2751 * RPC glue vector for in-kernel short circuited tasks. Rather than
2752 * removing it completely, I have only disabled that feature (which was
2753 * the only feature at the time). It just appears that we are going to
2754 * want to add some user data to tasks in the future (i.e. bsd info,
2755 * task names, etc...), so I left it in the formal task interface.
2760 task_flavor_t flavor
,
2761 __unused task_info_t task_info_in
, /* pointer to IN array */
2762 __unused mach_msg_type_number_t task_info_count
)
2764 if (task
== TASK_NULL
)
2765 return(KERN_INVALID_ARGUMENT
);
2770 case TASK_TRACE_MEMORY_INFO
:
2772 if (task_info_count
!= TASK_TRACE_MEMORY_INFO_COUNT
)
2773 return (KERN_INVALID_ARGUMENT
);
2775 assert(task_info_in
!= NULL
);
2776 task_trace_memory_info_t mem_info
;
2777 mem_info
= (task_trace_memory_info_t
) task_info_in
;
2778 kern_return_t kr
= atm_register_trace_memory(task
,
2779 mem_info
->user_memory_address
,
2780 mem_info
->buffer_size
);
2787 return (KERN_INVALID_ARGUMENT
);
2789 return (KERN_SUCCESS
);
2792 int radar_20146450
= 1;
2796 task_flavor_t flavor
,
2797 task_info_t task_info_out
,
2798 mach_msg_type_number_t
*task_info_count
)
2800 kern_return_t error
= KERN_SUCCESS
;
2802 if (task
== TASK_NULL
)
2803 return (KERN_INVALID_ARGUMENT
);
2807 if ((task
!= current_task()) && (!task
->active
)) {
2809 return (KERN_INVALID_ARGUMENT
);
2814 case TASK_BASIC_INFO_32
:
2815 case TASK_BASIC2_INFO_32
:
2817 task_basic_info_32_t basic_info
;
2822 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
) {
2823 error
= KERN_INVALID_ARGUMENT
;
2827 basic_info
= (task_basic_info_32_t
)task_info_out
;
2829 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
2830 basic_info
->virtual_size
= (typeof(basic_info
->virtual_size
))map
->size
;
2831 if (flavor
== TASK_BASIC2_INFO_32
) {
2833 * The "BASIC2" flavor gets the maximum resident
2834 * size instead of the current resident size...
2836 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
2838 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
2840 basic_info
->resident_size
*= PAGE_SIZE
;
2842 basic_info
->policy
= ((task
!= kernel_task
)?
2843 POLICY_TIMESHARE
: POLICY_RR
);
2844 basic_info
->suspend_count
= task
->user_stop_count
;
2846 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
2847 basic_info
->user_time
.seconds
=
2848 (typeof(basic_info
->user_time
.seconds
))secs
;
2849 basic_info
->user_time
.microseconds
= usecs
;
2851 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
2852 basic_info
->system_time
.seconds
=
2853 (typeof(basic_info
->system_time
.seconds
))secs
;
2854 basic_info
->system_time
.microseconds
= usecs
;
2856 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
2860 case TASK_BASIC_INFO_64
:
2862 task_basic_info_64_t basic_info
;
2867 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
) {
2868 error
= KERN_INVALID_ARGUMENT
;
2872 basic_info
= (task_basic_info_64_t
)task_info_out
;
2874 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
2875 basic_info
->virtual_size
= map
->size
;
2876 basic_info
->resident_size
=
2877 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
2880 basic_info
->policy
= ((task
!= kernel_task
)?
2881 POLICY_TIMESHARE
: POLICY_RR
);
2882 basic_info
->suspend_count
= task
->user_stop_count
;
2884 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
2885 basic_info
->user_time
.seconds
=
2886 (typeof(basic_info
->user_time
.seconds
))secs
;
2887 basic_info
->user_time
.microseconds
= usecs
;
2889 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
2890 basic_info
->system_time
.seconds
=
2891 (typeof(basic_info
->system_time
.seconds
))secs
;
2892 basic_info
->system_time
.microseconds
= usecs
;
2894 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
2898 case MACH_TASK_BASIC_INFO
:
2900 mach_task_basic_info_t basic_info
;
2905 if (*task_info_count
< MACH_TASK_BASIC_INFO_COUNT
) {
2906 error
= KERN_INVALID_ARGUMENT
;
2910 basic_info
= (mach_task_basic_info_t
)task_info_out
;
2912 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
2914 basic_info
->virtual_size
= map
->size
;
2916 basic_info
->resident_size
=
2917 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
));
2918 basic_info
->resident_size
*= PAGE_SIZE_64
;
2920 basic_info
->resident_size_max
=
2921 (mach_vm_size_t
)(pmap_resident_max(map
->pmap
));
2922 basic_info
->resident_size_max
*= PAGE_SIZE_64
;
2924 basic_info
->policy
= ((task
!= kernel_task
) ?
2925 POLICY_TIMESHARE
: POLICY_RR
);
2927 basic_info
->suspend_count
= task
->user_stop_count
;
2929 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
2930 basic_info
->user_time
.seconds
=
2931 (typeof(basic_info
->user_time
.seconds
))secs
;
2932 basic_info
->user_time
.microseconds
= usecs
;
2934 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
2935 basic_info
->system_time
.seconds
=
2936 (typeof(basic_info
->system_time
.seconds
))secs
;
2937 basic_info
->system_time
.microseconds
= usecs
;
2939 *task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
2943 case TASK_THREAD_TIMES_INFO
:
2945 register task_thread_times_info_t times_info
;
2946 register thread_t thread
;
2948 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
2949 error
= KERN_INVALID_ARGUMENT
;
2953 times_info
= (task_thread_times_info_t
) task_info_out
;
2954 times_info
->user_time
.seconds
= 0;
2955 times_info
->user_time
.microseconds
= 0;
2956 times_info
->system_time
.seconds
= 0;
2957 times_info
->system_time
.microseconds
= 0;
2960 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2961 time_value_t user_time
, system_time
;
2963 if (thread
->options
& TH_OPT_IDLE_THREAD
)
2966 thread_read_times(thread
, &user_time
, &system_time
);
2968 time_value_add(×_info
->user_time
, &user_time
);
2969 time_value_add(×_info
->system_time
, &system_time
);
2972 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
2976 case TASK_ABSOLUTETIME_INFO
:
2978 task_absolutetime_info_t info
;
2979 register thread_t thread
;
2981 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
) {
2982 error
= KERN_INVALID_ARGUMENT
;
2986 info
= (task_absolutetime_info_t
)task_info_out
;
2987 info
->threads_user
= info
->threads_system
= 0;
2990 info
->total_user
= task
->total_user_time
;
2991 info
->total_system
= task
->total_system_time
;
2993 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2997 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3001 thread_lock(thread
);
3003 tval
= timer_grab(&thread
->user_timer
);
3004 info
->threads_user
+= tval
;
3005 info
->total_user
+= tval
;
3007 tval
= timer_grab(&thread
->system_timer
);
3008 if (thread
->precise_user_kernel_time
) {
3009 info
->threads_system
+= tval
;
3010 info
->total_system
+= tval
;
3012 /* system_timer may represent either sys or user */
3013 info
->threads_user
+= tval
;
3014 info
->total_user
+= tval
;
3017 thread_unlock(thread
);
3022 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
3026 case TASK_DYLD_INFO
:
3028 task_dyld_info_t info
;
3031 * We added the format field to TASK_DYLD_INFO output. For
3032 * temporary backward compatibility, accept the fact that
3033 * clients may ask for the old version - distinquished by the
3034 * size of the expected result structure.
3036 #define TASK_LEGACY_DYLD_INFO_COUNT \
3037 offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
3039 if (*task_info_count
< TASK_LEGACY_DYLD_INFO_COUNT
) {
3040 error
= KERN_INVALID_ARGUMENT
;
3044 info
= (task_dyld_info_t
)task_info_out
;
3045 info
->all_image_info_addr
= task
->all_image_info_addr
;
3046 info
->all_image_info_size
= task
->all_image_info_size
;
3048 /* only set format on output for those expecting it */
3049 if (*task_info_count
>= TASK_DYLD_INFO_COUNT
) {
3050 info
->all_image_info_format
= task_has_64BitAddr(task
) ?
3051 TASK_DYLD_ALL_IMAGE_INFO_64
:
3052 TASK_DYLD_ALL_IMAGE_INFO_32
;
3053 *task_info_count
= TASK_DYLD_INFO_COUNT
;
3055 *task_info_count
= TASK_LEGACY_DYLD_INFO_COUNT
;
3060 case TASK_EXTMOD_INFO
:
3062 task_extmod_info_t info
;
3065 if (*task_info_count
< TASK_EXTMOD_INFO_COUNT
) {
3066 error
= KERN_INVALID_ARGUMENT
;
3070 info
= (task_extmod_info_t
)task_info_out
;
3072 p
= get_bsdtask_info(task
);
3074 proc_getexecutableuuid(p
, info
->task_uuid
, sizeof(info
->task_uuid
));
3076 bzero(info
->task_uuid
, sizeof(info
->task_uuid
));
3078 info
->extmod_statistics
= task
->extmod_statistics
;
3079 *task_info_count
= TASK_EXTMOD_INFO_COUNT
;
3084 case TASK_KERNELMEMORY_INFO
:
3086 task_kernelmemory_info_t tkm_info
;
3087 ledger_amount_t credit
, debit
;
3089 if (*task_info_count
< TASK_KERNELMEMORY_INFO_COUNT
) {
3090 error
= KERN_INVALID_ARGUMENT
;
3094 tkm_info
= (task_kernelmemory_info_t
) task_info_out
;
3095 tkm_info
->total_palloc
= 0;
3096 tkm_info
->total_pfree
= 0;
3097 tkm_info
->total_salloc
= 0;
3098 tkm_info
->total_sfree
= 0;
3100 if (task
== kernel_task
) {
3102 * All shared allocs/frees from other tasks count against
3103 * the kernel private memory usage. If we are looking up
3104 * info for the kernel task, gather from everywhere.
3108 /* start by accounting for all the terminated tasks against the kernel */
3109 tkm_info
->total_palloc
= tasks_tkm_private
.alloc
+ tasks_tkm_shared
.alloc
;
3110 tkm_info
->total_pfree
= tasks_tkm_private
.free
+ tasks_tkm_shared
.free
;
3112 /* count all other task/thread shared alloc/free against the kernel */
3113 lck_mtx_lock(&tasks_threads_lock
);
3115 /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
3116 queue_iterate(&tasks
, task
, task_t
, tasks
) {
3117 if (task
== kernel_task
) {
3118 if (ledger_get_entries(task
->ledger
,
3119 task_ledgers
.tkm_private
, &credit
,
3120 &debit
) == KERN_SUCCESS
) {
3121 tkm_info
->total_palloc
+= credit
;
3122 tkm_info
->total_pfree
+= debit
;
3125 if (!ledger_get_entries(task
->ledger
,
3126 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3127 tkm_info
->total_palloc
+= credit
;
3128 tkm_info
->total_pfree
+= debit
;
3131 lck_mtx_unlock(&tasks_threads_lock
);
3133 if (!ledger_get_entries(task
->ledger
,
3134 task_ledgers
.tkm_private
, &credit
, &debit
)) {
3135 tkm_info
->total_palloc
= credit
;
3136 tkm_info
->total_pfree
= debit
;
3138 if (!ledger_get_entries(task
->ledger
,
3139 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3140 tkm_info
->total_salloc
= credit
;
3141 tkm_info
->total_sfree
= debit
;
3146 *task_info_count
= TASK_KERNELMEMORY_INFO_COUNT
;
3147 return KERN_SUCCESS
;
3151 case TASK_SCHED_FIFO_INFO
:
3154 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
) {
3155 error
= KERN_INVALID_ARGUMENT
;
3159 error
= KERN_INVALID_POLICY
;
3164 case TASK_SCHED_RR_INFO
:
3166 register policy_rr_base_t rr_base
;
3167 uint32_t quantum_time
;
3168 uint64_t quantum_ns
;
3170 if (*task_info_count
< POLICY_RR_BASE_COUNT
) {
3171 error
= KERN_INVALID_ARGUMENT
;
3175 rr_base
= (policy_rr_base_t
) task_info_out
;
3177 if (task
!= kernel_task
) {
3178 error
= KERN_INVALID_POLICY
;
3182 rr_base
->base_priority
= task
->priority
;
3184 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
3185 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
3187 rr_base
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
3189 *task_info_count
= POLICY_RR_BASE_COUNT
;
3194 case TASK_SCHED_TIMESHARE_INFO
:
3196 register policy_timeshare_base_t ts_base
;
3198 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
) {
3199 error
= KERN_INVALID_ARGUMENT
;
3203 ts_base
= (policy_timeshare_base_t
) task_info_out
;
3205 if (task
== kernel_task
) {
3206 error
= KERN_INVALID_POLICY
;
3210 ts_base
->base_priority
= task
->priority
;
3212 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
3216 case TASK_SECURITY_TOKEN
:
3218 register security_token_t
*sec_token_p
;
3220 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
3221 error
= KERN_INVALID_ARGUMENT
;
3225 sec_token_p
= (security_token_t
*) task_info_out
;
3227 *sec_token_p
= task
->sec_token
;
3229 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
3233 case TASK_AUDIT_TOKEN
:
3235 register audit_token_t
*audit_token_p
;
3237 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
3238 error
= KERN_INVALID_ARGUMENT
;
3242 audit_token_p
= (audit_token_t
*) task_info_out
;
3244 *audit_token_p
= task
->audit_token
;
3246 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
3250 case TASK_SCHED_INFO
:
3251 error
= KERN_INVALID_ARGUMENT
;
3254 case TASK_EVENTS_INFO
:
3256 register task_events_info_t events_info
;
3257 register thread_t thread
;
3259 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
3260 error
= KERN_INVALID_ARGUMENT
;
3264 events_info
= (task_events_info_t
) task_info_out
;
3267 events_info
->faults
= task
->faults
;
3268 events_info
->pageins
= task
->pageins
;
3269 events_info
->cow_faults
= task
->cow_faults
;
3270 events_info
->messages_sent
= task
->messages_sent
;
3271 events_info
->messages_received
= task
->messages_received
;
3272 events_info
->syscalls_mach
= task
->syscalls_mach
;
3273 events_info
->syscalls_unix
= task
->syscalls_unix
;
3275 events_info
->csw
= task
->c_switch
;
3277 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3278 events_info
->csw
+= thread
->c_switch
;
3279 events_info
->syscalls_mach
+= thread
->syscalls_mach
;
3280 events_info
->syscalls_unix
+= thread
->syscalls_unix
;
3284 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
3287 case TASK_AFFINITY_TAG_INFO
:
3289 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
) {
3290 error
= KERN_INVALID_ARGUMENT
;
3294 error
= task_affinity_info(task
, task_info_out
, task_info_count
);
3297 case TASK_POWER_INFO
:
3299 if (*task_info_count
< TASK_POWER_INFO_COUNT
) {
3300 error
= KERN_INVALID_ARGUMENT
;
3304 task_power_info_locked(task
, (task_power_info_t
)task_info_out
, NULL
);
3308 case TASK_POWER_INFO_V2
:
3310 if (*task_info_count
< TASK_POWER_INFO_V2_COUNT
) {
3311 error
= KERN_INVALID_ARGUMENT
;
3314 task_power_info_v2_t tpiv2
= (task_power_info_v2_t
) task_info_out
;
3315 task_power_info_locked(task
, &tpiv2
->cpu_energy
, &tpiv2
->gpu_energy
);
3320 case TASK_VM_INFO_PURGEABLE
:
3322 task_vm_info_t vm_info
;
3325 if (*task_info_count
< TASK_VM_INFO_REV0_COUNT
) {
3326 error
= KERN_INVALID_ARGUMENT
;
3330 vm_info
= (task_vm_info_t
)task_info_out
;
3332 if (task
== kernel_task
) {
3337 vm_map_lock_read(map
);
3340 vm_info
->virtual_size
= (typeof(vm_info
->virtual_size
))map
->size
;
3341 vm_info
->region_count
= map
->hdr
.nentries
;
3342 vm_info
->page_size
= vm_map_page_size(map
);
3344 vm_info
->resident_size
= pmap_resident_count(map
->pmap
);
3345 vm_info
->resident_size
*= PAGE_SIZE
;
3346 vm_info
->resident_size_peak
= pmap_resident_max(map
->pmap
);
3347 vm_info
->resident_size_peak
*= PAGE_SIZE
;
3349 #define _VM_INFO(_name) \
3350 vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
3353 _VM_INFO(device_peak
);
3355 _VM_INFO(external_peak
);
3357 _VM_INFO(internal_peak
);
3359 _VM_INFO(reusable_peak
);
3360 _VM_INFO(compressed
);
3361 _VM_INFO(compressed_peak
);
3362 _VM_INFO(compressed_lifetime
);
3364 vm_info
->purgeable_volatile_pmap
= 0;
3365 vm_info
->purgeable_volatile_resident
= 0;
3366 vm_info
->purgeable_volatile_virtual
= 0;
3367 if (task
== kernel_task
) {
3369 * We do not maintain the detailed stats for the
3370 * kernel_pmap, so just count everything as
3373 vm_info
->internal
= vm_info
->resident_size
;
3375 * ... but since the memory held by the VM compressor
3376 * in the kernel address space ought to be attributed
3377 * to user-space tasks, we subtract it from "internal"
3378 * to give memory reporting tools a more accurate idea
3379 * of what the kernel itself is actually using, instead
3380 * of making it look like the kernel is leaking memory
3381 * when the system is under memory pressure.
3383 vm_info
->internal
-= (VM_PAGE_COMPRESSOR_COUNT
*
3386 mach_vm_size_t volatile_virtual_size
;
3387 mach_vm_size_t volatile_resident_size
;
3388 mach_vm_size_t volatile_compressed_size
;
3389 mach_vm_size_t volatile_pmap_size
;
3390 mach_vm_size_t volatile_compressed_pmap_size
;
3393 if (flavor
== TASK_VM_INFO_PURGEABLE
) {
3394 kr
= vm_map_query_volatile(
3396 &volatile_virtual_size
,
3397 &volatile_resident_size
,
3398 &volatile_compressed_size
,
3399 &volatile_pmap_size
,
3400 &volatile_compressed_pmap_size
);
3401 if (kr
== KERN_SUCCESS
) {
3402 vm_info
->purgeable_volatile_pmap
=
3404 if (radar_20146450
) {
3405 vm_info
->compressed
-=
3406 volatile_compressed_pmap_size
;
3408 vm_info
->purgeable_volatile_resident
=
3409 volatile_resident_size
;
3410 vm_info
->purgeable_volatile_virtual
=
3411 volatile_virtual_size
;
3414 vm_map_unlock_read(map
);
3417 if (*task_info_count
>= TASK_VM_INFO_COUNT
) {
3418 vm_info
->phys_footprint
= 0;
3419 *task_info_count
= TASK_VM_INFO_COUNT
;
3421 *task_info_count
= TASK_VM_INFO_REV0_COUNT
;
3427 case TASK_WAIT_STATE_INFO
:
3430 * Deprecated flavor. Currently allowing some results until all users
3431 * stop calling it. The results may not be accurate.
3433 task_wait_state_info_t wait_state_info
;
3434 uint64_t total_sfi_ledger_val
= 0;
3436 if (*task_info_count
< TASK_WAIT_STATE_INFO_COUNT
) {
3437 error
= KERN_INVALID_ARGUMENT
;
3441 wait_state_info
= (task_wait_state_info_t
) task_info_out
;
3443 wait_state_info
->total_wait_state_time
= 0;
3444 bzero(wait_state_info
->_reserved
, sizeof(wait_state_info
->_reserved
));
3446 #if CONFIG_SCHED_SFI
3447 int i
, prev_lentry
= -1;
3448 int64_t val_credit
, val_debit
;
3450 for (i
= 0; i
< MAX_SFI_CLASS_ID
; i
++){
3453 * checking with prev_lentry != entry ensures adjacent classes
3454 * which share the same ledger do not add wait times twice.
3455 * Note: Use ledger() call to get data for each individual sfi class.
3457 if (prev_lentry
!= task_ledgers
.sfi_wait_times
[i
] &&
3458 KERN_SUCCESS
== ledger_get_entries(task
->ledger
,
3459 task_ledgers
.sfi_wait_times
[i
], &val_credit
, &val_debit
)) {
3460 total_sfi_ledger_val
+= val_credit
;
3462 prev_lentry
= task_ledgers
.sfi_wait_times
[i
];
3465 #endif /* CONFIG_SCHED_SFI */
3466 wait_state_info
->total_wait_sfi_state_time
= total_sfi_ledger_val
;
3467 *task_info_count
= TASK_WAIT_STATE_INFO_COUNT
;
3471 case TASK_VM_INFO_PURGEABLE_ACCOUNT
:
3473 #if DEVELOPMENT || DEBUG
3474 pvm_account_info_t acnt_info
;
3476 if (*task_info_count
< PVM_ACCOUNT_INFO_COUNT
) {
3477 error
= KERN_INVALID_ARGUMENT
;
3481 if (task_info_out
== NULL
) {
3482 error
= KERN_INVALID_ARGUMENT
;
3486 acnt_info
= (pvm_account_info_t
) task_info_out
;
3488 error
= vm_purgeable_account(task
, acnt_info
);
3490 *task_info_count
= PVM_ACCOUNT_INFO_COUNT
;
3493 #else /* DEVELOPMENT || DEBUG */
3494 error
= KERN_NOT_SUPPORTED
;
3496 #endif /* DEVELOPMENT || DEBUG */
3498 case TASK_FLAGS_INFO
:
3500 task_flags_info_t flags_info
;
3502 if (*task_info_count
< TASK_FLAGS_INFO_COUNT
) {
3503 error
= KERN_INVALID_ARGUMENT
;
3507 flags_info
= (task_flags_info_t
)task_info_out
;
3509 /* only publish the 64-bit flag of the task */
3510 flags_info
->flags
= task
->t_flags
& TF_64B_ADDR
;
3512 *task_info_count
= TASK_FLAGS_INFO_COUNT
;
3516 case TASK_DEBUG_INFO_INTERNAL
:
3518 #if DEVELOPMENT || DEBUG
3519 task_debug_info_internal_t dbg_info
;
3520 if (*task_info_count
< TASK_DEBUG_INFO_INTERNAL_COUNT
) {
3521 error
= KERN_NOT_SUPPORTED
;
3525 if (task_info_out
== NULL
) {
3526 error
= KERN_INVALID_ARGUMENT
;
3529 dbg_info
= (task_debug_info_internal_t
) task_info_out
;
3530 dbg_info
->ipc_space_size
= 0;
3531 if (task
->itk_space
){
3532 dbg_info
->ipc_space_size
= task
->itk_space
->is_table_size
;
3535 error
= KERN_SUCCESS
;
3536 *task_info_count
= TASK_DEBUG_INFO_INTERNAL_COUNT
;
3538 #else /* DEVELOPMENT || DEBUG */
3539 error
= KERN_NOT_SUPPORTED
;
3541 #endif /* DEVELOPMENT || DEBUG */
3544 error
= KERN_INVALID_ARGUMENT
;
3554 * Returns power stats for the task.
3555 * Note: Called with task locked.
3558 task_power_info_locked(
3560 task_power_info_t info
,
3561 gpu_energy_data_t ginfo
)
3564 ledger_amount_t tmp
;
3566 task_lock_assert_owned(task
);
3568 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
3569 (ledger_amount_t
*)&info
->task_interrupt_wakeups
, &tmp
);
3570 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
3571 (ledger_amount_t
*)&info
->task_platform_idle_wakeups
, &tmp
);
3573 info
->task_timer_wakeups_bin_1
= task
->task_timer_wakeups_bin_1
;
3574 info
->task_timer_wakeups_bin_2
= task
->task_timer_wakeups_bin_2
;
3576 info
->total_user
= task
->total_user_time
;
3577 info
->total_system
= task
->total_system_time
;
3580 ginfo
->task_gpu_utilisation
= task
->task_gpu_ns
;
3583 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3587 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3591 thread_lock(thread
);
3593 info
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
3594 info
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
3596 tval
= timer_grab(&thread
->user_timer
);
3597 info
->total_user
+= tval
;
3599 tval
= timer_grab(&thread
->system_timer
);
3600 if (thread
->precise_user_kernel_time
) {
3601 info
->total_system
+= tval
;
3603 /* system_timer may represent either sys or user */
3604 info
->total_user
+= tval
;
3608 ginfo
->task_gpu_utilisation
+= ml_gpu_stat(thread
);
3610 thread_unlock(thread
);
3616 * task_gpu_utilisation
3618 * Returns the total gpu time used by the all the threads of the task
3619 * (both dead and alive)
3622 task_gpu_utilisation(
3625 uint64_t gpu_time
= 0;
3629 gpu_time
+= task
->task_gpu_ns
;
3631 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3634 thread_lock(thread
);
3635 gpu_time
+= ml_gpu_stat(thread
);
3636 thread_unlock(thread
);
3647 task_purgable_info_t
*stats
)
3649 if (task
== TASK_NULL
|| stats
== NULL
)
3650 return KERN_INVALID_ARGUMENT
;
3651 /* Take task reference */
3652 task_reference(task
);
3653 vm_purgeable_stats((vm_purgeable_info_t
)stats
, task
);
3654 /* Drop task reference */
3655 task_deallocate(task
);
3656 return KERN_SUCCESS
;
3667 /* assert(task == current_task()); */ /* bogus assert 4803227 4807483 */
3671 task
->vtimers
|= which
;
3675 case TASK_VTIMER_USER
:
3676 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3678 thread_lock(thread
);
3679 if (thread
->precise_user_kernel_time
)
3680 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
3682 thread
->vtimer_user_save
= timer_grab(&thread
->system_timer
);
3683 thread_unlock(thread
);
3688 case TASK_VTIMER_PROF
:
3689 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3691 thread_lock(thread
);
3692 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
3693 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
3694 thread_unlock(thread
);
3699 case TASK_VTIMER_RLIM
:
3700 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3702 thread_lock(thread
);
3703 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
3704 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
3705 thread_unlock(thread
);
3719 assert(task
== current_task());
3723 task
->vtimers
&= ~which
;
3733 uint32_t *microsecs
)
3735 thread_t thread
= current_thread();
3740 assert(task
== current_task());
3742 assert(task
->vtimers
& which
);
3748 case TASK_VTIMER_USER
:
3749 if (thread
->precise_user_kernel_time
) {
3750 tdelt
= (uint32_t)timer_delta(&thread
->user_timer
,
3751 &thread
->vtimer_user_save
);
3753 tdelt
= (uint32_t)timer_delta(&thread
->system_timer
,
3754 &thread
->vtimer_user_save
);
3756 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
3759 case TASK_VTIMER_PROF
:
3760 tsum
= timer_grab(&thread
->user_timer
);
3761 tsum
+= timer_grab(&thread
->system_timer
);
3762 tdelt
= (uint32_t)(tsum
- thread
->vtimer_prof_save
);
3763 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
3764 /* if the time delta is smaller than a usec, ignore */
3765 if (*microsecs
!= 0)
3766 thread
->vtimer_prof_save
= tsum
;
3769 case TASK_VTIMER_RLIM
:
3770 tsum
= timer_grab(&thread
->user_timer
);
3771 tsum
+= timer_grab(&thread
->system_timer
);
3772 tdelt
= (uint32_t)(tsum
- thread
->vtimer_rlim_save
);
3773 thread
->vtimer_rlim_save
= tsum
;
3774 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
3783 * Change the assigned processor set for the task
3787 __unused task_t task
,
3788 __unused processor_set_t new_pset
,
3789 __unused boolean_t assign_threads
)
3791 return(KERN_FAILURE
);
3795 * task_assign_default:
3797 * Version of task_assign to assign to default processor set.
3800 task_assign_default(
3802 boolean_t assign_threads
)
3804 return (task_assign(task
, &pset0
, assign_threads
));
3808 * task_get_assignment
3810 * Return name of processor set that task is assigned to.
3813 task_get_assignment(
3815 processor_set_t
*pset
)
3818 return(KERN_FAILURE
);
3822 return (KERN_SUCCESS
);
3826 get_task_dispatchqueue_offset(
3829 return task
->dispatchqueue_offset
;
3835 * Set scheduling policy and parameters, both base and limit, for
3836 * the given task. Policy must be a policy which is enabled for the
3837 * processor set. Change contained threads if requested.
3841 __unused task_t task
,
3842 __unused policy_t policy_id
,
3843 __unused policy_base_t base
,
3844 __unused mach_msg_type_number_t count
,
3845 __unused boolean_t set_limit
,
3846 __unused boolean_t change
)
3848 return(KERN_FAILURE
);
3854 * Set scheduling policy and parameters, both base and limit, for
3855 * the given task. Policy can be any policy implemented by the
3856 * processor set, whether enabled or not. Change contained threads
3861 __unused task_t task
,
3862 __unused processor_set_t pset
,
3863 __unused policy_t policy_id
,
3864 __unused policy_base_t base
,
3865 __unused mach_msg_type_number_t base_count
,
3866 __unused policy_limit_t limit
,
3867 __unused mach_msg_type_number_t limit_count
,
3868 __unused boolean_t change
)
3870 return(KERN_FAILURE
);
3875 __unused task_t task
,
3876 __unused vm_offset_t pc
,
3877 __unused vm_offset_t endpc
)
3879 return KERN_FAILURE
;
3883 task_synchronizer_destroy_all(task_t task
)
3886 * Destroy owned semaphores
3888 semaphore_destroy_all(task
);
3892 * Install default (machine-dependent) initial thread state
3893 * on the task. Subsequent thread creation will have this initial
3894 * state set on the thread by machine_thread_inherit_taskwide().
3895 * Flavors and structures are exactly the same as those to thread_set_state()
3901 thread_state_t state
,
3902 mach_msg_type_number_t state_count
)
3906 if (task
== TASK_NULL
) {
3907 return (KERN_INVALID_ARGUMENT
);
3912 if (!task
->active
) {
3914 return (KERN_FAILURE
);
3917 ret
= machine_task_set_state(task
, flavor
, state
, state_count
);
3924 * Examine the default (machine-dependent) initial thread state
3925 * on the task, as set by task_set_state(). Flavors and structures
3926 * are exactly the same as those passed to thread_get_state().
3932 thread_state_t state
,
3933 mach_msg_type_number_t
*state_count
)
3937 if (task
== TASK_NULL
) {
3938 return (KERN_INVALID_ARGUMENT
);
3943 if (!task
->active
) {
3945 return (KERN_FAILURE
);
3948 ret
= machine_task_get_state(task
, flavor
, state
, state_count
);
3955 #define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
3957 void __attribute__((noinline
))
3958 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
)
3960 task_t task
= current_task();
3962 const char *procname
= "unknown";
3963 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
3966 pid
= proc_selfpid();
3970 * Cannot have ReportCrash analyzing
3971 * a suspended initproc.
3976 if (task
->bsd_info
!= NULL
)
3977 procname
= proc_name_address(current_task()->bsd_info
);
3980 if (hwm_user_cores
) {
3982 uint64_t starttime
, end
;
3983 clock_sec_t secs
= 0;
3984 uint32_t microsecs
= 0;
3986 starttime
= mach_absolute_time();
3988 * Trigger a coredump of this process. Don't proceed unless we know we won't
3989 * be filling up the disk; and ignore the core size resource limit for this
3992 if ((error
= coredump(current_task()->bsd_info
, HWM_USERCORE_MINSPACE
, COREDUMP_IGNORE_ULIMIT
)) != 0) {
3993 printf("couldn't take coredump of %s[%d]: %d\n", procname
, pid
, error
);
3996 * coredump() leaves the task suspended.
3998 task_resume_internal(current_task());
4000 end
= mach_absolute_time();
4001 absolutetime_to_microtime(end
- starttime
, &secs
, µsecs
);
4002 printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
4003 proc_name_address(current_task()->bsd_info
), pid
, (int)secs
, microsecs
);
4006 if (disable_exc_resource
) {
4007 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
4008 "supressed by a boot-arg.\n", procname
, pid
, max_footprint_mb
);
4013 * A task that has triggered an EXC_RESOURCE, should not be
4014 * jetsammed when the device is under memory pressure. Here
4015 * we set the P_MEMSTAT_TERMINATED flag so that the process
4016 * will be skipped if the memorystatus_thread wakes up.
4018 proc_memstat_terminated(current_task()->bsd_info
, TRUE
);
4020 printf("process %s[%d] crossed memory high watermark (%d MB); sending "
4021 "EXC_RESOURCE.\n", procname
, pid
, max_footprint_mb
);
4023 code
[0] = code
[1] = 0;
4024 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_MEMORY
);
4025 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_HIGH_WATERMARK
);
4026 EXC_RESOURCE_HWM_ENCODE_LIMIT(code
[0], max_footprint_mb
);
4029 * Use the _internal_ variant so that no user-space
4030 * process can resume our task from under us.
4032 task_suspend_internal(task
);
4033 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
4034 task_resume_internal(task
);
4037 * After the EXC_RESOURCE has been handled, we must clear the
4038 * P_MEMSTAT_TERMINATED flag so that the process can again be
4039 * considered for jetsam if the memorystatus_thread wakes up.
4041 proc_memstat_terminated(current_task()->bsd_info
, FALSE
); /* clear the flag */
4045 * Callback invoked when a task exceeds its physical footprint limit.
4048 task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
4050 ledger_amount_t max_footprint
, max_footprint_mb
;
4051 ledger_amount_t footprint_after_purge
;
4054 if (warning
== LEDGER_WARNING_DIPPED_BELOW
) {
4056 * Task memory limits only provide a warning on the way up.
4061 task
= current_task();
4063 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &max_footprint
);
4064 max_footprint_mb
= max_footprint
>> 20;
4067 * Try and purge all "volatile" memory in that task first.
4069 (void) task_purge_volatile_memory(task
);
4070 /* are we still over the limit ? */
4071 ledger_get_balance(task
->ledger
,
4072 task_ledgers
.phys_footprint
,
4073 &footprint_after_purge
);
4075 footprint_after_purge
<= max_footprint
) ||
4077 footprint_after_purge
<= ((max_footprint
*
4078 PHYS_FOOTPRINT_WARNING_LEVEL
) / 100))) {
4079 /* all better now */
4080 ledger_reset_callback_state(task
->ledger
,
4081 task_ledgers
.phys_footprint
);
4084 /* still over the limit after purging... */
4087 * If this an actual violation (not a warning),
4088 * generate a non-fatal high watermark EXC_RESOURCE.
4090 if ((warning
== 0) && (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
)) {
4091 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND((int)max_footprint_mb
);
4094 memorystatus_on_ledger_footprint_exceeded((warning
== LEDGER_WARNING_ROSE_ABOVE
) ? TRUE
: FALSE
,
4095 (int)max_footprint_mb
);
4098 extern int proc_check_footprint_priv(void);
4101 task_set_phys_footprint_limit(
4106 kern_return_t error
;
4108 if ((error
= proc_check_footprint_priv())) {
4109 return (KERN_NO_ACCESS
);
4112 return task_set_phys_footprint_limit_internal(task
, new_limit_mb
, old_limit_mb
, FALSE
);
4116 task_convert_phys_footprint_limit(
4118 int *converted_limit_mb
)
4120 if (limit_mb
== -1) {
4124 if (max_task_footprint
!= 0) {
4125 *converted_limit_mb
= (int)(max_task_footprint
/ 1024 / 1024); /* bytes to MB */
4127 *converted_limit_mb
= (int)(LEDGER_LIMIT_INFINITY
>> 20);
4130 /* nothing to convert */
4131 *converted_limit_mb
= limit_mb
;
4133 return (KERN_SUCCESS
);
4138 task_set_phys_footprint_limit_internal(
4142 boolean_t trigger_exception
)
4144 ledger_amount_t old
;
4146 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &old
);
4150 * Check that limit >> 20 will not give an "unexpected" 32-bit
4151 * result. There are, however, implicit assumptions that -1 mb limit
4152 * equates to LEDGER_LIMIT_INFINITY.
4154 assert(((old
& 0xFFF0000000000000LL
) == 0) || (old
== LEDGER_LIMIT_INFINITY
));
4155 *old_limit_mb
= (int)(old
>> 20);
4158 if (new_limit_mb
== -1) {
4160 * Caller wishes to remove the limit.
4162 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
4163 max_task_footprint
? max_task_footprint
: LEDGER_LIMIT_INFINITY
,
4164 max_task_footprint
? PHYS_FOOTPRINT_WARNING_LEVEL
: 0);
4165 return (KERN_SUCCESS
);
4168 #ifdef CONFIG_NOMONITORS
4169 return (KERN_SUCCESS
);
4170 #endif /* CONFIG_NOMONITORS */
4174 if (trigger_exception
) {
4175 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
4177 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
4180 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
4181 (ledger_amount_t
)new_limit_mb
<< 20, PHYS_FOOTPRINT_WARNING_LEVEL
);
4183 if (task
== current_task()) {
4184 ledger_check_new_balance(task
->ledger
, task_ledgers
.phys_footprint
);
4189 return (KERN_SUCCESS
);
4193 task_get_phys_footprint_limit(
4197 ledger_amount_t limit
;
4199 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &limit
);
4201 * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
4202 * result. There are, however, implicit assumptions that -1 mb limit
4203 * equates to LEDGER_LIMIT_INFINITY.
4205 assert(((limit
& 0xFFF0000000000000LL
) == 0) || (limit
== LEDGER_LIMIT_INFINITY
));
4206 *limit_mb
= (int)(limit
>> 20);
4208 return (KERN_SUCCESS
);
4210 #else /* CONFIG_JETSAM */
4212 task_set_phys_footprint_limit(
4213 __unused task_t task
,
4214 __unused
int new_limit_mb
,
4215 __unused
int *old_limit_mb
)
4217 return (KERN_FAILURE
);
4221 task_get_phys_footprint_limit(
4222 __unused task_t task
,
4223 __unused
int *limit_mb
)
4225 return (KERN_FAILURE
);
4227 #endif /* CONFIG_JETSAM */
4230 * We need to export some functions to other components that
4231 * are currently implemented in macros within the osfmk
4232 * component. Just export them as functions of the same name.
4234 boolean_t
is_kerneltask(task_t t
)
4236 if (t
== kernel_task
)
4243 check_for_tasksuspend(task_t task
)
4246 if (task
== TASK_NULL
)
4249 return (task
->suspend_count
> 0);
4253 task_t
current_task(void);
4254 task_t
current_task(void)
4256 return (current_task_fast());
4259 #undef task_reference
4260 void task_reference(task_t task
);
4265 if (task
!= TASK_NULL
)
4266 task_reference_internal(task
);
4269 /* defined in bsd/kern/kern_prot.c */
4270 extern int get_audit_token_pid(audit_token_t
*audit_token
);
4272 int task_pid(task_t task
)
4275 return get_audit_token_pid(&task
->audit_token
);
4281 * This routine is called always with task lock held.
4282 * And it returns a thread handle without reference as the caller
4283 * operates on it under the task lock held.
4286 task_findtid(task_t task
, uint64_t tid
)
4288 thread_t thread
= THREAD_NULL
;
4290 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4291 if (thread
->thread_id
== tid
)
4294 return(THREAD_NULL
);
4298 * Control the CPU usage monitor for a task.
4301 task_cpu_usage_monitor_ctl(task_t task
, uint32_t *flags
)
4303 int error
= KERN_SUCCESS
;
4305 if (*flags
& CPUMON_MAKE_FATAL
) {
4306 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_CPUMON
;
4308 error
= KERN_INVALID_ARGUMENT
;
4315 * Control the wakeups monitor for a task.
4318 task_wakeups_monitor_ctl(task_t task
, uint32_t *flags
, int32_t *rate_hz
)
4320 ledger_t ledger
= task
->ledger
;
4323 if (*flags
& WAKEMON_GET_PARAMS
) {
4324 ledger_amount_t limit
;
4327 ledger_get_limit(ledger
, task_ledgers
.interrupt_wakeups
, &limit
);
4328 ledger_get_period(ledger
, task_ledgers
.interrupt_wakeups
, &period
);
4330 if (limit
!= LEDGER_LIMIT_INFINITY
) {
4332 * An active limit means the wakeups monitor is enabled.
4334 *rate_hz
= (int32_t)(limit
/ (int64_t)(period
/ NSEC_PER_SEC
));
4335 *flags
= WAKEMON_ENABLE
;
4336 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
4337 *flags
|= WAKEMON_MAKE_FATAL
;
4340 *flags
= WAKEMON_DISABLE
;
4345 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
4348 return KERN_SUCCESS
;
4351 if (*flags
& WAKEMON_ENABLE
) {
4352 if (*flags
& WAKEMON_SET_DEFAULTS
) {
4353 *rate_hz
= task_wakeups_monitor_rate
;
4356 #ifndef CONFIG_NOMONITORS
4357 if (*flags
& WAKEMON_MAKE_FATAL
) {
4358 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
4360 #endif /* CONFIG_NOMONITORS */
4364 return KERN_INVALID_ARGUMENT
;
4367 #ifndef CONFIG_NOMONITORS
4368 ledger_set_limit(ledger
, task_ledgers
.interrupt_wakeups
, *rate_hz
* task_wakeups_monitor_interval
,
4369 task_wakeups_monitor_ustackshots_trigger_pct
);
4370 ledger_set_period(ledger
, task_ledgers
.interrupt_wakeups
, task_wakeups_monitor_interval
* NSEC_PER_SEC
);
4371 ledger_enable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
4372 #endif /* CONFIG_NOMONITORS */
4373 } else if (*flags
& WAKEMON_DISABLE
) {
4375 * Caller wishes to disable wakeups monitor on the task.
4377 * Disable telemetry if it was triggered by the wakeups monitor, and
4378 * remove the limit & callback on the wakeups ledger entry.
4380 #if CONFIG_TELEMETRY
4381 telemetry_task_ctl_locked(task
, TF_WAKEMON_WARNING
, 0);
4383 ledger_disable_refill(ledger
, task_ledgers
.interrupt_wakeups
);
4384 ledger_disable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
4388 return KERN_SUCCESS
;
4392 task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
4394 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
4395 #if CONFIG_TELEMETRY
4397 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
4398 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
4400 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 1);
4405 #if CONFIG_TELEMETRY
4407 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
4408 * exceeded the limit, turn telemetry off for the task.
4410 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 0);
4414 THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS__SENDING_EXC_RESOURCE();
4418 void __attribute__((noinline
))
4419 THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS__SENDING_EXC_RESOURCE(void)
4421 task_t task
= current_task();
4423 const char *procname
= "unknown";
4424 uint64_t observed_wakeups_rate
;
4425 uint64_t permitted_wakeups_rate
;
4426 uint64_t observation_interval
;
4427 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
4428 struct ledger_entry_info lei
;
4431 pid
= proc_selfpid();
4432 if (task
->bsd_info
!= NULL
)
4433 procname
= proc_name_address(current_task()->bsd_info
);
4436 ledger_get_entry_info(task
->ledger
, task_ledgers
.interrupt_wakeups
, &lei
);
4439 * Disable the exception notification so we don't overwhelm
4440 * the listener with an endless stream of redundant exceptions.
4442 uint32_t flags
= WAKEMON_DISABLE
;
4443 task_wakeups_monitor_ctl(task
, &flags
, NULL
);
4445 observed_wakeups_rate
= (lei
.lei_balance
* (int64_t)NSEC_PER_SEC
) / lei
.lei_last_refill
;
4446 permitted_wakeups_rate
= lei
.lei_limit
/ task_wakeups_monitor_interval
;
4447 observation_interval
= lei
.lei_refill_period
/ NSEC_PER_SEC
;
4449 if (disable_exc_resource
) {
4450 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
4451 "supressed by a boot-arg\n", procname
, pid
);
4455 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
4456 "supressed due to audio playback\n", procname
, pid
);
4459 printf("process %s[%d] caught causing excessive wakeups. Observed wakeups rate "
4460 "(per sec): %lld; Maximum permitted wakeups rate (per sec): %lld; Observation "
4461 "period: %lld seconds; Task lifetime number of wakeups: %lld\n",
4462 procname
, pid
, observed_wakeups_rate
, permitted_wakeups_rate
,
4463 observation_interval
, lei
.lei_credit
);
4465 code
[0] = code
[1] = 0;
4466 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_WAKEUPS
);
4467 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_WAKEUPS_MONITOR
);
4468 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code
[0], task_wakeups_monitor_rate
);
4469 EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code
[0], observation_interval
);
4470 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code
[1], lei
.lei_balance
* (int64_t)NSEC_PER_SEC
/ lei
.lei_last_refill
);
4471 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
4473 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
4474 task_terminate_internal(task
);
4479 task_purge_volatile_memory(
4483 int num_object_purged
;
4485 if (task
== TASK_NULL
)
4486 return KERN_INVALID_TASK
;
4490 if (!task
->active
) {
4492 return KERN_INVALID_TASK
;
4495 if (map
== VM_MAP_NULL
) {
4497 return KERN_INVALID_TASK
;
4499 vm_map_reference(task
->map
);
4503 num_object_purged
= vm_map_purge(map
);
4504 vm_map_deallocate(map
);
4506 return KERN_SUCCESS
;
4509 /* Placeholders for the task set/get voucher interfaces */
4511 task_get_mach_voucher(
4513 mach_voucher_selector_t __unused which
,
4514 ipc_voucher_t
*voucher
)
4516 if (TASK_NULL
== task
)
4517 return KERN_INVALID_TASK
;
4520 return KERN_SUCCESS
;
4524 task_set_mach_voucher(
4526 ipc_voucher_t __unused voucher
)
4528 if (TASK_NULL
== task
)
4529 return KERN_INVALID_TASK
;
4531 return KERN_SUCCESS
;
4535 task_swap_mach_voucher(
4537 ipc_voucher_t new_voucher
,
4538 ipc_voucher_t
*in_out_old_voucher
)
4540 if (TASK_NULL
== task
)
4541 return KERN_INVALID_TASK
;
4543 *in_out_old_voucher
= new_voucher
;
4544 return KERN_SUCCESS
;
4547 void task_set_gpu_denied(task_t task
, boolean_t denied
)
4552 task
->t_flags
|= TF_GPU_DENIED
;
4554 task
->t_flags
&= ~TF_GPU_DENIED
;
4560 boolean_t
task_is_gpu_denied(task_t task
)
4562 /* We don't need the lock to read this flag */
4563 return (task
->t_flags
& TF_GPU_DENIED
) ? TRUE
: FALSE
;
4566 void task_update_logical_writes(task_t task
, uint32_t io_size
, int flags
)
4568 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_DATA_WRITE
)) | DBG_FUNC_NONE
, task_pid(task
), io_size
, flags
, 0, 0);
4570 case TASK_WRITE_IMMEDIATE
:
4571 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_immediate_writes
));
4573 case TASK_WRITE_DEFERRED
:
4574 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_deferred_writes
));
4576 case TASK_WRITE_INVALIDATED
:
4577 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_invalidated_writes
));
4579 case TASK_WRITE_METADATA
:
4580 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_metadata_writes
));