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 void init_task_ledgers(void);
193 void task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
194 void task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
195 void __attribute__((noinline
)) THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS__SENDING_EXC_RESOURCE(void);
196 void __attribute__((noinline
)) PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
);
198 kern_return_t
task_suspend_internal(task_t
);
199 kern_return_t
task_resume_internal(task_t
);
200 static kern_return_t
task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
);
203 void proc_init_cpumon_params(void);
204 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
*);
206 // Warn tasks when they hit 80% of their memory limit.
207 #define PHYS_FOOTPRINT_WARNING_LEVEL 80
209 #define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT 150 /* wakeups per second */
210 #define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL 300 /* in seconds. */
213 * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
215 * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
216 * stacktraces, aka micro-stackshots)
218 #define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER 70
220 int task_wakeups_monitor_interval
; /* In seconds. Time period over which wakeups rate is observed */
221 int task_wakeups_monitor_rate
; /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
223 int task_wakeups_monitor_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
225 int disable_exc_resource
; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
227 ledger_amount_t max_task_footprint
= 0; /* Per-task limit on physical memory consumption in bytes */
228 int max_task_footprint_mb
= 0; /* Per-task limit on physical memory consumption in megabytes */
231 int pmap_ledgers_panic
= 1;
232 #endif /* MACH_ASSERT */
234 int task_max
= CONFIG_TASK_MAX
; /* Max number of tasks */
236 int hwm_user_cores
= 0; /* high watermark violations generate user core files */
239 extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
240 extern int proc_pid(struct proc
*p
);
241 extern int proc_selfpid(void);
242 extern char *proc_name_address(struct proc
*p
);
243 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
245 extern void proc_memstat_terminated(struct proc
* p
, boolean_t set
);
246 extern void memorystatus_on_ledger_footprint_exceeded(int warning
, const int max_footprint_mb
);
250 extern int pmap_ledgers_panic
;
251 #endif /* MACH_ASSERT */
255 void task_hold_locked(
257 void task_wait_locked(
259 boolean_t until_not_runnable
);
260 void task_release_locked(
264 void task_synchronizer_destroy_all(
267 int check_for_tasksuspend(
271 task_backing_store_privileged(
275 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
286 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
288 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
293 if (task_has_64BitAddr(task
))
295 task_set_64BitAddr(task
);
297 if ( !task_has_64BitAddr(task
))
299 task_clear_64BitAddr(task
);
301 /* FIXME: On x86, the thread save state flavor can diverge from the
302 * task's 64-bit feature flag due to the 32-bit/64-bit register save
303 * state dichotomy. Since we can be pre-empted in this interval,
304 * certain routines may observe the thread as being in an inconsistent
305 * state with respect to its task's 64-bitness.
308 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
309 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
310 thread_mtx_lock(thread
);
311 machine_thread_switch_addrmode(thread
);
312 thread_mtx_unlock(thread
);
314 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
322 task_set_dyld_info(task_t task
, mach_vm_address_t addr
, mach_vm_size_t size
)
325 task
->all_image_info_addr
= addr
;
326 task
->all_image_info_size
= size
;
331 task_atm_reset(__unused task_t task
) {
334 if (task
->atm_context
!= NULL
) {
335 atm_task_descriptor_destroy(task
->atm_context
);
336 task
->atm_context
= NULL
;
342 #if TASK_REFERENCE_LEAK_DEBUG
343 #include <kern/btlog.h>
345 decl_simple_lock_data(static,task_ref_lock
);
346 static btlog_t
*task_ref_btlog
;
347 #define TASK_REF_OP_INCR 0x1
348 #define TASK_REF_OP_DECR 0x2
350 #define TASK_REF_BTDEPTH 7
353 task_ref_lock_lock(void *context
)
355 simple_lock((simple_lock_t
)context
);
358 task_ref_lock_unlock(void *context
)
360 simple_unlock((simple_lock_t
)context
);
364 task_reference_internal(task_t task
)
366 void * bt
[TASK_REF_BTDEPTH
];
369 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
371 (void)hw_atomic_add(&(task
)->ref_count
, 1);
372 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_INCR
,
377 task_deallocate_internal(task_t task
)
379 void * bt
[TASK_REF_BTDEPTH
];
382 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
384 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_DECR
,
386 return hw_atomic_sub(&(task
)->ref_count
, 1);
389 #endif /* TASK_REFERENCE_LEAK_DEBUG */
395 lck_grp_attr_setdefault(&task_lck_grp_attr
);
396 lck_grp_init(&task_lck_grp
, "task", &task_lck_grp_attr
);
397 lck_attr_setdefault(&task_lck_attr
);
398 lck_mtx_init(&tasks_threads_lock
, &task_lck_grp
, &task_lck_attr
);
402 task_max
* sizeof(struct task
),
403 TASK_CHUNK
* sizeof(struct task
),
406 zone_change(task_zone
, Z_NOENCRYPT
, TRUE
);
409 * Configure per-task memory limit.
410 * The boot-arg is interpreted as Megabytes,
411 * and takes precedence over the device tree.
412 * Setting the boot-arg to 0 disables task limits.
414 if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb
,
415 sizeof (max_task_footprint_mb
))) {
417 * No limit was found in boot-args, so go look in the device tree.
419 if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb
,
420 sizeof(max_task_footprint_mb
))) {
422 * No limit was found in device tree.
424 max_task_footprint_mb
= 0;
428 if (max_task_footprint_mb
!= 0) {
430 if (max_task_footprint_mb
< 50) {
431 printf("Warning: max_task_pmem %d below minimum.\n",
432 max_task_footprint_mb
);
433 max_task_footprint_mb
= 50;
435 printf("Limiting task physical memory footprint to %d MB\n",
436 max_task_footprint_mb
);
438 max_task_footprint
= (ledger_amount_t
)max_task_footprint_mb
* 1024 * 1024; // Convert MB to bytes
440 printf("Warning: max_task_footprint specified, but jetsam not configured; ignoring.\n");
445 PE_parse_boot_argn("pmap_ledgers_panic", &pmap_ledgers_panic
,
446 sizeof (pmap_ledgers_panic
));
447 #endif /* MACH_ASSERT */
449 if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores
,
450 sizeof (hwm_user_cores
))) {
454 if (PE_parse_boot_argn("qos_override_mode", &qos_override_mode
, sizeof(qos_override_mode
))) {
455 printf("QOS override mode: 0x%08x\n", qos_override_mode
);
457 qos_override_mode
= QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE_BUT_SINGLE_MUTEX_OVERRIDE
;
460 proc_init_cpumon_params();
462 if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate
, sizeof (task_wakeups_monitor_rate
))) {
463 task_wakeups_monitor_rate
= TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT
;
466 if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval
, sizeof (task_wakeups_monitor_interval
))) {
467 task_wakeups_monitor_interval
= TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL
;
470 if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct
,
471 sizeof (task_wakeups_monitor_ustackshots_trigger_pct
))) {
472 task_wakeups_monitor_ustackshots_trigger_pct
= TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER
;
475 if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource
,
476 sizeof (disable_exc_resource
))) {
477 disable_exc_resource
= 0;
481 * If we have coalitions, coalition_init() will call init_task_ledgers() as it
482 * sets up the ledgers for the default coalition. If we don't have coalitions,
483 * then we have to call it now.
485 #if CONFIG_COALITIONS
486 assert(task_ledger_template
);
487 #else /* CONFIG_COALITIONS */
489 #endif /* CONFIG_COALITIONS */
491 #if TASK_REFERENCE_LEAK_DEBUG
492 simple_lock_init(&task_ref_lock
, 0);
493 task_ref_btlog
= btlog_create(100000,
496 task_ref_lock_unlock
,
498 assert(task_ref_btlog
);
502 * Create the kernel task as the first task.
505 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, TRUE
, &kernel_task
) != KERN_SUCCESS
)
507 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
509 panic("task_init\n");
511 vm_map_deallocate(kernel_task
->map
);
512 kernel_task
->map
= kernel_map
;
513 lck_spin_init(&dead_task_statistics_lock
, &task_lck_grp
, &task_lck_attr
);
518 * Create a task running in the kernel address space. It may
519 * have its own map of size mem_size and may have ipc privileges.
523 __unused task_t parent_task
,
524 __unused vm_offset_t map_base
,
525 __unused vm_size_t map_size
,
526 __unused task_t
*child_task
)
528 return (KERN_INVALID_ARGUMENT
);
534 __unused ledger_port_array_t ledger_ports
,
535 __unused mach_msg_type_number_t num_ledger_ports
,
536 __unused boolean_t inherit_memory
,
537 __unused task_t
*child_task
) /* OUT */
539 if (parent_task
== TASK_NULL
)
540 return(KERN_INVALID_ARGUMENT
);
543 * No longer supported: too many calls assume that a task has a valid
546 return(KERN_FAILURE
);
550 host_security_create_task_token(
551 host_security_t host_security
,
553 __unused security_token_t sec_token
,
554 __unused audit_token_t audit_token
,
555 __unused host_priv_t host_priv
,
556 __unused ledger_port_array_t ledger_ports
,
557 __unused mach_msg_type_number_t num_ledger_ports
,
558 __unused boolean_t inherit_memory
,
559 __unused task_t
*child_task
) /* OUT */
561 if (parent_task
== TASK_NULL
)
562 return(KERN_INVALID_ARGUMENT
);
564 if (host_security
== HOST_NULL
)
565 return(KERN_INVALID_SECURITY
);
568 * No longer supported.
570 return(KERN_FAILURE
);
578 * Physical footprint: This is the sum of:
579 * + (internal - alternate_accounting)
580 * + (internal_compressed - alternate_accounting_compressed)
582 * + purgeable_nonvolatile
583 * + purgeable_nonvolatile_compressed
586 * The task's anonymous memory, which on iOS is always resident.
588 * internal_compressed
589 * Amount of this task's internal memory which is held by the compressor.
590 * Such memory is no longer actually resident for the task [i.e., resident in its pmap],
591 * and could be either decompressed back into memory, or paged out to storage, depending
592 * on our implementation.
595 * IOKit mappings: The total size of all IOKit mappings in this task, regardless of
596 clean/dirty or internal/external state].
598 * alternate_accounting
599 * The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
600 * are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
604 init_task_ledgers(void)
608 assert(task_ledger_template
== NULL
);
609 assert(kernel_task
== TASK_NULL
);
611 if ((t
= ledger_template_create("Per-task ledger")) == NULL
)
612 panic("couldn't create task ledger template");
614 task_ledgers
.cpu_time
= ledger_entry_add(t
, "cpu_time", "sched", "ns");
615 task_ledgers
.tkm_private
= ledger_entry_add(t
, "tkm_private",
617 task_ledgers
.tkm_shared
= ledger_entry_add(t
, "tkm_shared", "physmem",
619 task_ledgers
.phys_mem
= ledger_entry_add(t
, "phys_mem", "physmem",
621 task_ledgers
.wired_mem
= ledger_entry_add(t
, "wired_mem", "physmem",
623 task_ledgers
.internal
= ledger_entry_add(t
, "internal", "physmem",
625 task_ledgers
.iokit_mapped
= ledger_entry_add(t
, "iokit_mapped", "mappings",
627 task_ledgers
.alternate_accounting
= ledger_entry_add(t
, "alternate_accounting", "physmem",
629 task_ledgers
.alternate_accounting_compressed
= ledger_entry_add(t
, "alternate_accounting_compressed", "physmem",
631 task_ledgers
.phys_footprint
= ledger_entry_add(t
, "phys_footprint", "physmem",
633 task_ledgers
.internal_compressed
= ledger_entry_add(t
, "internal_compressed", "physmem",
635 task_ledgers
.purgeable_volatile
= ledger_entry_add(t
, "purgeable_volatile", "physmem", "bytes");
636 task_ledgers
.purgeable_nonvolatile
= ledger_entry_add(t
, "purgeable_nonvolatile", "physmem", "bytes");
637 task_ledgers
.purgeable_volatile_compressed
= ledger_entry_add(t
, "purgeable_volatile_compress", "physmem", "bytes");
638 task_ledgers
.purgeable_nonvolatile_compressed
= ledger_entry_add(t
, "purgeable_nonvolatile_compress", "physmem", "bytes");
639 task_ledgers
.platform_idle_wakeups
= ledger_entry_add(t
, "platform_idle_wakeups", "power",
641 task_ledgers
.interrupt_wakeups
= ledger_entry_add(t
, "interrupt_wakeups", "power",
645 sfi_class_id_t class_id
, ledger_alias
;
646 for (class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
647 task_ledgers
.sfi_wait_times
[class_id
] = -1;
650 /* don't account for UNSPECIFIED */
651 for (class_id
= SFI_CLASS_UNSPECIFIED
+ 1; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
652 ledger_alias
= sfi_get_ledger_alias_for_class(class_id
);
653 if (ledger_alias
!= SFI_CLASS_UNSPECIFIED
) {
654 /* Check to see if alias has been registered yet */
655 if (task_ledgers
.sfi_wait_times
[ledger_alias
] != -1) {
656 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
];
658 /* Otherwise, initialize it first */
659 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
] = sfi_ledger_entry_add(t
, ledger_alias
);
662 task_ledgers
.sfi_wait_times
[class_id
] = sfi_ledger_entry_add(t
, class_id
);
665 if (task_ledgers
.sfi_wait_times
[class_id
] < 0) {
666 panic("couldn't create entries for task ledger template for SFI class 0x%x", class_id
);
670 assert(task_ledgers
.sfi_wait_times
[MAX_SFI_CLASS_ID
-1] != -1);
671 #endif /* CONFIG_SCHED_SFI */
674 task_ledgers
.cpu_time_billed_to_me
= ledger_entry_add(t
, "cpu_time_billed_to_me", "sched", "ns");
675 task_ledgers
.cpu_time_billed_to_others
= ledger_entry_add(t
, "cpu_time_billed_to_others", "sched", "ns");
677 if ((task_ledgers
.cpu_time
< 0) ||
678 (task_ledgers
.tkm_private
< 0) ||
679 (task_ledgers
.tkm_shared
< 0) ||
680 (task_ledgers
.phys_mem
< 0) ||
681 (task_ledgers
.wired_mem
< 0) ||
682 (task_ledgers
.internal
< 0) ||
683 (task_ledgers
.iokit_mapped
< 0) ||
684 (task_ledgers
.alternate_accounting
< 0) ||
685 (task_ledgers
.alternate_accounting_compressed
< 0) ||
686 (task_ledgers
.phys_footprint
< 0) ||
687 (task_ledgers
.internal_compressed
< 0) ||
688 (task_ledgers
.purgeable_volatile
< 0) ||
689 (task_ledgers
.purgeable_nonvolatile
< 0) ||
690 (task_ledgers
.purgeable_volatile_compressed
< 0) ||
691 (task_ledgers
.purgeable_nonvolatile_compressed
< 0) ||
692 (task_ledgers
.platform_idle_wakeups
< 0) ||
693 (task_ledgers
.interrupt_wakeups
< 0)
695 || (task_ledgers
.cpu_time_billed_to_me
< 0) || (task_ledgers
.cpu_time_billed_to_others
< 0)
698 panic("couldn't create entries for task ledger template");
701 ledger_track_maximum(t
, task_ledgers
.phys_footprint
, 60);
703 if (pmap_ledgers_panic
) {
704 ledger_panic_on_negative(t
, task_ledgers
.phys_footprint
);
705 ledger_panic_on_negative(t
, task_ledgers
.internal
);
706 ledger_panic_on_negative(t
, task_ledgers
.internal_compressed
);
707 ledger_panic_on_negative(t
, task_ledgers
.iokit_mapped
);
708 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting
);
709 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting_compressed
);
710 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile
);
711 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile
);
712 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile_compressed
);
713 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
715 #endif /* MACH_ASSERT */
718 ledger_set_callback(t
, task_ledgers
.phys_footprint
, task_footprint_exceeded
, NULL
, NULL
);
721 ledger_set_callback(t
, task_ledgers
.interrupt_wakeups
,
722 task_wakeups_rate_exceeded
, NULL
, NULL
);
724 task_ledger_template
= t
;
728 task_create_internal(
730 coalition_t
*parent_coalitions __unused
,
731 boolean_t inherit_memory
,
733 task_t
*child_task
) /* OUT */
736 vm_shared_region_t shared_region
;
737 ledger_t ledger
= NULL
;
739 new_task
= (task_t
) zalloc(task_zone
);
741 if (new_task
== TASK_NULL
)
742 return(KERN_RESOURCE_SHORTAGE
);
744 /* one ref for just being alive; one for our caller */
745 new_task
->ref_count
= 2;
747 /* allocate with active entries */
748 assert(task_ledger_template
!= NULL
);
749 if ((ledger
= ledger_instantiate(task_ledger_template
,
750 LEDGER_CREATE_ACTIVE_ENTRIES
)) == NULL
) {
751 zfree(task_zone
, new_task
);
752 return(KERN_RESOURCE_SHORTAGE
);
755 new_task
->ledger
= ledger
;
757 #if defined(CONFIG_SCHED_MULTIQ)
758 new_task
->sched_group
= sched_group_create();
761 /* if inherit_memory is true, parent_task MUST not be NULL */
763 new_task
->map
= vm_map_fork(ledger
, parent_task
->map
);
765 new_task
->map
= vm_map_create(pmap_create(ledger
, 0, is_64bit
),
766 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
767 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
769 /* Inherit memlock limit from parent */
771 vm_map_set_user_wire_limit(new_task
->map
, (vm_size_t
)parent_task
->map
->user_wire_limit
);
773 lck_mtx_init(&new_task
->lock
, &task_lck_grp
, &task_lck_attr
);
774 queue_init(&new_task
->threads
);
775 new_task
->suspend_count
= 0;
776 new_task
->thread_count
= 0;
777 new_task
->active_thread_count
= 0;
778 new_task
->user_stop_count
= 0;
779 new_task
->legacy_stop_count
= 0;
780 new_task
->active
= TRUE
;
781 new_task
->halting
= FALSE
;
782 new_task
->user_data
= NULL
;
783 new_task
->faults
= 0;
784 new_task
->cow_faults
= 0;
785 new_task
->pageins
= 0;
786 new_task
->messages_sent
= 0;
787 new_task
->messages_received
= 0;
788 new_task
->syscalls_mach
= 0;
789 new_task
->priv_flags
= 0;
790 new_task
->syscalls_unix
=0;
791 new_task
->c_switch
= new_task
->p_switch
= new_task
->ps_switch
= 0;
792 new_task
->t_flags
= 0;
793 new_task
->importance
= 0;
796 new_task
->atm_context
= NULL
;
799 new_task
->bank_context
= NULL
;
802 zinfo_task_init(new_task
);
805 new_task
->bsd_info
= NULL
;
806 new_task
->corpse_info
= NULL
;
807 #endif /* MACH_BSD */
810 if (max_task_footprint
!= 0) {
811 ledger_set_limit(ledger
, task_ledgers
.phys_footprint
, max_task_footprint
, PHYS_FOOTPRINT_WARNING_LEVEL
);
815 if (task_wakeups_monitor_rate
!= 0) {
816 uint32_t flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
817 int32_t rate
; // Ignored because of WAKEMON_SET_DEFAULTS
818 task_wakeups_monitor_ctl(new_task
, &flags
, &rate
);
821 #if defined(__i386__) || defined(__x86_64__)
822 new_task
->i386_ldt
= 0;
825 new_task
->task_debug
= NULL
;
827 queue_init(&new_task
->semaphore_list
);
828 new_task
->semaphores_owned
= 0;
830 ipc_task_init(new_task
, parent_task
);
832 new_task
->total_user_time
= 0;
833 new_task
->total_system_time
= 0;
835 new_task
->vtimers
= 0;
837 new_task
->shared_region
= NULL
;
839 new_task
->affinity_space
= NULL
;
841 new_task
->pidsuspended
= FALSE
;
842 new_task
->frozen
= FALSE
;
843 new_task
->changing_freeze_state
= FALSE
;
844 new_task
->rusage_cpu_flags
= 0;
845 new_task
->rusage_cpu_percentage
= 0;
846 new_task
->rusage_cpu_interval
= 0;
847 new_task
->rusage_cpu_deadline
= 0;
848 new_task
->rusage_cpu_callt
= NULL
;
850 new_task
->suspends_outstanding
= 0;
854 new_task
->hv_task_target
= NULL
;
855 #endif /* HYPERVISOR */
858 new_task
->low_mem_notified_warn
= 0;
859 new_task
->low_mem_notified_critical
= 0;
860 new_task
->low_mem_privileged_listener
= 0;
861 new_task
->purged_memory_warn
= 0;
862 new_task
->purged_memory_critical
= 0;
863 new_task
->mem_notify_reserved
= 0;
864 #if IMPORTANCE_INHERITANCE
865 new_task
->task_imp_base
= NULL
;
866 #endif /* IMPORTANCE_INHERITANCE */
868 #if defined(__x86_64__)
869 new_task
->uexc_range_start
= new_task
->uexc_range_size
= new_task
->uexc_handler
= 0;
872 new_task
->requested_policy
= default_task_requested_policy
;
873 new_task
->effective_policy
= default_task_effective_policy
;
874 new_task
->pended_policy
= default_task_pended_policy
;
876 if (parent_task
!= TASK_NULL
) {
877 new_task
->sec_token
= parent_task
->sec_token
;
878 new_task
->audit_token
= parent_task
->audit_token
;
880 /* inherit the parent's shared region */
881 shared_region
= vm_shared_region_get(parent_task
);
882 vm_shared_region_set(new_task
, shared_region
);
884 if(task_has_64BitAddr(parent_task
))
885 task_set_64BitAddr(new_task
);
886 new_task
->all_image_info_addr
= parent_task
->all_image_info_addr
;
887 new_task
->all_image_info_size
= parent_task
->all_image_info_size
;
889 #if defined(__i386__) || defined(__x86_64__)
890 if (inherit_memory
&& parent_task
->i386_ldt
)
891 new_task
->i386_ldt
= user_ldt_copy(parent_task
->i386_ldt
);
893 if (inherit_memory
&& parent_task
->affinity_space
)
894 task_affinity_create(parent_task
, new_task
);
896 new_task
->pset_hint
= parent_task
->pset_hint
= task_choose_pset(parent_task
);
898 #if IMPORTANCE_INHERITANCE
899 ipc_importance_task_t new_task_imp
= IIT_NULL
;
901 if (task_is_marked_importance_donor(parent_task
)) {
902 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
903 assert(IIT_NULL
!= new_task_imp
);
904 ipc_importance_task_mark_donor(new_task_imp
, TRUE
);
906 /* Embedded doesn't want this to inherit */
907 if (task_is_marked_importance_receiver(parent_task
)) {
908 if (IIT_NULL
== new_task_imp
)
909 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
910 assert(IIT_NULL
!= new_task_imp
);
911 ipc_importance_task_mark_receiver(new_task_imp
, TRUE
);
913 if (task_is_marked_importance_denap_receiver(parent_task
)) {
914 if (IIT_NULL
== new_task_imp
)
915 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
916 assert(IIT_NULL
!= new_task_imp
);
917 ipc_importance_task_mark_denap_receiver(new_task_imp
, TRUE
);
920 if (IIT_NULL
!= new_task_imp
) {
921 assert(new_task
->task_imp_base
== new_task_imp
);
922 ipc_importance_task_release(new_task_imp
);
924 #endif /* IMPORTANCE_INHERITANCE */
926 new_task
->priority
= BASEPRI_DEFAULT
;
927 new_task
->max_priority
= MAXPRI_USER
;
929 new_task
->requested_policy
.t_apptype
= parent_task
->requested_policy
.t_apptype
;
931 new_task
->requested_policy
.int_darwinbg
= parent_task
->requested_policy
.int_darwinbg
;
932 new_task
->requested_policy
.ext_darwinbg
= parent_task
->requested_policy
.ext_darwinbg
;
933 new_task
->requested_policy
.int_iotier
= parent_task
->requested_policy
.int_iotier
;
934 new_task
->requested_policy
.ext_iotier
= parent_task
->requested_policy
.ext_iotier
;
935 new_task
->requested_policy
.int_iopassive
= parent_task
->requested_policy
.int_iopassive
;
936 new_task
->requested_policy
.ext_iopassive
= parent_task
->requested_policy
.ext_iopassive
;
937 new_task
->requested_policy
.bg_iotier
= parent_task
->requested_policy
.bg_iotier
;
938 new_task
->requested_policy
.terminated
= parent_task
->requested_policy
.terminated
;
939 new_task
->requested_policy
.t_qos_clamp
= parent_task
->requested_policy
.t_qos_clamp
;
941 task_policy_create(new_task
, parent_task
->requested_policy
.t_boosted
);
943 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
944 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
947 task_set_64BitAddr(new_task
);
949 new_task
->all_image_info_addr
= (mach_vm_address_t
)0;
950 new_task
->all_image_info_size
= (mach_vm_size_t
)0;
952 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
954 if (kernel_task
== TASK_NULL
) {
955 new_task
->priority
= BASEPRI_KERNEL
;
956 new_task
->max_priority
= MAXPRI_KERNEL
;
958 new_task
->priority
= BASEPRI_DEFAULT
;
959 new_task
->max_priority
= MAXPRI_USER
;
963 bzero(new_task
->coalition
, sizeof(new_task
->coalition
));
964 for (int i
= 0; i
< COALITION_NUM_TYPES
; i
++)
965 queue_chain_init(new_task
->task_coalition
[i
]);
967 /* Allocate I/O Statistics */
968 new_task
->task_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
969 assert(new_task
->task_io_stats
!= NULL
);
970 bzero(new_task
->task_io_stats
, sizeof(struct io_stat_info
));
972 bzero(&(new_task
->cpu_time_qos_stats
), sizeof(struct _cpu_time_qos_stats
));
974 bzero(&new_task
->extmod_statistics
, sizeof(new_task
->extmod_statistics
));
975 new_task
->task_timer_wakeups_bin_1
= new_task
->task_timer_wakeups_bin_2
= 0;
976 new_task
->task_gpu_ns
= 0;
978 #if CONFIG_COALITIONS
980 /* TODO: there is no graceful failure path here... */
981 if (parent_coalitions
&& parent_coalitions
[COALITION_TYPE_RESOURCE
]) {
982 coalitions_adopt_task(parent_coalitions
, new_task
);
983 } else if (parent_task
&& parent_task
->coalition
[COALITION_TYPE_RESOURCE
]) {
985 * all tasks at least have a resource coalition, so
986 * if the parent has one then inherit all coalitions
987 * the parent is a part of
989 coalitions_adopt_task(parent_task
->coalition
, new_task
);
991 /* TODO: assert that new_task will be PID 1 (launchd) */
992 coalitions_adopt_init_task(new_task
);
995 if (new_task
->coalition
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
996 panic("created task is not a member of a resource coalition");
998 #endif /* CONFIG_COALITIONS */
1000 new_task
->dispatchqueue_offset
= 0;
1001 if (parent_task
!= NULL
) {
1002 new_task
->dispatchqueue_offset
= parent_task
->dispatchqueue_offset
;
1005 if (vm_backing_store_low
&& parent_task
!= NULL
)
1006 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
1008 new_task
->task_volatile_objects
= 0;
1009 new_task
->task_nonvolatile_objects
= 0;
1010 new_task
->task_purgeable_disowning
= FALSE
;
1011 new_task
->task_purgeable_disowned
= FALSE
;
1013 ipc_task_enable(new_task
);
1015 lck_mtx_lock(&tasks_threads_lock
);
1016 queue_enter(&tasks
, new_task
, task_t
, tasks
);
1018 lck_mtx_unlock(&tasks_threads_lock
);
1020 *child_task
= new_task
;
1021 return(KERN_SUCCESS
);
1024 int task_dropped_imp_count
= 0;
1029 * Drop a reference on a task.
1035 ledger_amount_t credit
, debit
, interrupt_wakeups
, platform_idle_wakeups
;
1038 if (task
== TASK_NULL
)
1041 refs
= task_deallocate_internal(task
);
1043 #if IMPORTANCE_INHERITANCE
1049 * If last ref potentially comes from the task's importance,
1050 * disconnect it. But more task refs may be added before
1051 * that completes, so wait for the reference to go to zero
1052 * naturually (it may happen on a recursive task_deallocate()
1053 * from the ipc_importance_disconnect_task() call).
1055 if (IIT_NULL
!= task
->task_imp_base
)
1056 ipc_importance_disconnect_task(task
);
1062 #endif /* IMPORTANCE_INHERITANCE */
1064 lck_mtx_lock(&tasks_threads_lock
);
1065 queue_remove(&terminated_tasks
, task
, task_t
, tasks
);
1066 terminated_tasks_count
--;
1067 lck_mtx_unlock(&tasks_threads_lock
);
1070 * remove the reference on atm descriptor
1072 task_atm_reset(task
);
1076 * remove the reference on bank context
1078 if (task
->bank_context
!= NULL
) {
1079 bank_task_destroy(task
->bank_context
);
1080 task
->bank_context
= NULL
;
1084 if (task
->task_io_stats
)
1085 kfree(task
->task_io_stats
, sizeof(struct io_stat_info
));
1088 * Give the machine dependent code a chance
1089 * to perform cleanup before ripping apart
1092 machine_task_terminate(task
);
1094 ipc_task_terminate(task
);
1096 if (task
->affinity_space
)
1097 task_affinity_deallocate(task
);
1100 if (task
->ledger
!= NULL
&&
1101 task
->map
!= NULL
&&
1102 task
->map
->pmap
!= NULL
&&
1103 task
->map
->pmap
->ledger
!= NULL
) {
1104 assert(task
->ledger
== task
->map
->pmap
->ledger
);
1106 #endif /* MACH_ASSERT */
1108 vm_purgeable_disown(task
);
1109 assert(task
->task_purgeable_disowned
);
1110 if (task
->task_volatile_objects
!= 0 ||
1111 task
->task_nonvolatile_objects
!= 0) {
1112 panic("task_deallocate(%p): "
1113 "volatile_objects=%d nonvolatile_objects=%d\n",
1115 task
->task_volatile_objects
,
1116 task
->task_nonvolatile_objects
);
1119 vm_map_deallocate(task
->map
);
1120 is_release(task
->itk_space
);
1122 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
1123 &interrupt_wakeups
, &debit
);
1124 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
1125 &platform_idle_wakeups
, &debit
);
1127 #if defined(CONFIG_SCHED_MULTIQ)
1128 sched_group_destroy(task
->sched_group
);
1131 /* Accumulate statistics for dead tasks */
1132 lck_spin_lock(&dead_task_statistics_lock
);
1133 dead_task_statistics
.total_user_time
+= task
->total_user_time
;
1134 dead_task_statistics
.total_system_time
+= task
->total_system_time
;
1136 dead_task_statistics
.task_interrupt_wakeups
+= interrupt_wakeups
;
1137 dead_task_statistics
.task_platform_idle_wakeups
+= platform_idle_wakeups
;
1139 dead_task_statistics
.task_timer_wakeups_bin_1
+= task
->task_timer_wakeups_bin_1
;
1140 dead_task_statistics
.task_timer_wakeups_bin_2
+= task
->task_timer_wakeups_bin_2
;
1142 lck_spin_unlock(&dead_task_statistics_lock
);
1143 lck_mtx_destroy(&task
->lock
, &task_lck_grp
);
1145 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_private
, &credit
,
1147 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_private
.alloc
);
1148 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_private
.free
);
1150 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_shared
, &credit
,
1152 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_shared
.alloc
);
1153 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_shared
.free
);
1155 ledger_dereference(task
->ledger
);
1156 zinfo_task_free(task
);
1158 #if TASK_REFERENCE_LEAK_DEBUG
1159 btlog_remove_entries_for_element(task_ref_btlog
, task
);
1162 #if CONFIG_COALITIONS
1163 if (!task
->coalition
[COALITION_TYPE_RESOURCE
])
1164 panic("deallocating task was not a member of a resource coalition");
1165 task_release_coalitions(task
);
1166 #endif /* CONFIG_COALITIONS */
1168 bzero(task
->coalition
, sizeof(task
->coalition
));
1171 /* clean up collected information since last reference to task is gone */
1172 if (task
->corpse_info
) {
1173 task_crashinfo_destroy(task
->corpse_info
);
1174 task
->corpse_info
= NULL
;
1178 zfree(task_zone
, task
);
1182 * task_name_deallocate:
1184 * Drop a reference on a task name.
1187 task_name_deallocate(
1188 task_name_t task_name
)
1190 return(task_deallocate((task_t
)task_name
));
1194 * task_suspension_token_deallocate:
1196 * Drop a reference on a task suspension token.
1199 task_suspension_token_deallocate(
1200 task_suspension_token_t token
)
1202 return(task_deallocate((task_t
)token
));
1207 * task_collect_crash_info:
1209 * collect crash info from bsd and mach based data
1212 task_collect_crash_info(task_t task
)
1214 kern_return_t kr
= KERN_SUCCESS
;
1216 kcdata_descriptor_t crash_data
= NULL
;
1217 kcdata_descriptor_t crash_data_release
= NULL
;
1218 mach_msg_type_number_t size
= CORPSEINFO_ALLOCATION_SIZE
;
1219 mach_vm_offset_t crash_data_user_ptr
= 0;
1221 if (!corpses_enabled()) {
1222 return KERN_NOT_SUPPORTED
;
1226 assert(task
->bsd_info
!= NULL
);
1227 if (task
->corpse_info
== NULL
&& task
->bsd_info
!= NULL
) {
1229 /* map crash data memory in task's vm map */
1230 kr
= mach_vm_allocate(task
->map
, &crash_data_user_ptr
, size
, (VM_MAKE_TAG(VM_MEMORY_CORPSEINFO
) | VM_FLAGS_ANYWHERE
));
1232 if (kr
!= KERN_SUCCESS
)
1235 crash_data
= task_crashinfo_alloc_init((mach_vm_address_t
)crash_data_user_ptr
, size
);
1238 crash_data_release
= task
->corpse_info
;
1239 task
->corpse_info
= crash_data
;
1243 /* if failed to create corpse info, free the mapping */
1244 if (KERN_SUCCESS
!= mach_vm_deallocate(task
->map
, crash_data_user_ptr
, size
)) {
1245 printf("mach_vm_deallocate failed to clear corpse_data for pid %d.\n", task_pid(task
));
1250 if (crash_data_release
!= NULL
) {
1251 task_crashinfo_destroy(crash_data_release
);
1262 * task_deliver_crash_notification:
1264 * Makes outcall to registered host port for a corpse.
1267 task_deliver_crash_notification(task_t task
)
1269 kcdata_descriptor_t crash_info
= task
->corpse_info
;
1270 thread_t th_iter
= NULL
;
1271 kern_return_t kr
= KERN_SUCCESS
;
1272 wait_interrupt_t wsave
;
1273 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1275 if (crash_info
== NULL
)
1276 return KERN_FAILURE
;
1278 code
[0] = crash_info
->kcd_addr_begin
;
1279 code
[1] = crash_info
->kcd_length
;
1282 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1284 ipc_thread_reset(th_iter
);
1288 wsave
= thread_interrupt_level(THREAD_UNINT
);
1289 kr
= exception_triage(EXC_CORPSE_NOTIFY
, code
, EXCEPTION_CODE_MAX
);
1290 if (kr
!= KERN_SUCCESS
) {
1291 printf("Failed to send exception EXC_CORPSE_NOTIFY. error code: %d for pid %d\n", kr
, task_pid(task
));
1295 * crash reporting is done. Now release threads
1296 * for reaping by thread_terminate_daemon
1299 assert(task
->active_thread_count
== 0);
1300 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1302 thread_mtx_lock(th_iter
);
1303 assert(th_iter
->inspection
== TRUE
);
1304 th_iter
->inspection
= FALSE
;
1305 /* now that the corpse has been autopsied, dispose of the thread name */
1306 uthread_cleanup_name(th_iter
->uthread
);
1307 thread_mtx_unlock(th_iter
);
1310 thread_terminate_crashed_threads();
1311 /* remove the pending corpse report flag */
1312 task_clear_corpse_pending_report(task
);
1316 (void)thread_interrupt_level(wsave
);
1317 task_terminate_internal(task
);
1325 * Terminate the specified task. See comments on thread_terminate
1326 * (kern/thread.c) about problems with terminating the "current task."
1333 if (task
== TASK_NULL
)
1334 return (KERN_INVALID_ARGUMENT
);
1337 return (KERN_FAILURE
);
1339 return (task_terminate_internal(task
));
1343 extern int proc_pid(struct proc
*);
1344 extern void proc_name_kdp(task_t t
, char *buf
, int size
);
1345 #endif /* MACH_ASSERT */
1347 #define VM_MAP_PARTIAL_REAP 0x54 /* 0x150 */
1349 __unused
task_partial_reap(task_t task
, __unused
int pid
)
1351 unsigned int reclaimed_resident
= 0;
1352 unsigned int reclaimed_compressed
= 0;
1353 uint64_t task_page_count
;
1355 task_page_count
= (get_task_phys_footprint(task
) / PAGE_SIZE_64
);
1357 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_START
),
1358 pid
, task_page_count
, 0, 0, 0);
1360 vm_map_partial_reap(task
->map
, &reclaimed_resident
, &reclaimed_compressed
);
1362 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_END
),
1363 pid
, reclaimed_resident
, reclaimed_compressed
, 0, 0);
1367 task_mark_corpse(task_t task
)
1369 kern_return_t kr
= KERN_SUCCESS
;
1370 thread_t self_thread
;
1372 wait_interrupt_t wsave
;
1374 assert(task
!= kernel_task
);
1375 assert(task
== current_task());
1376 assert(!task_is_a_corpse(task
));
1378 kr
= task_collect_crash_info(task
);
1379 if (kr
!= KERN_SUCCESS
) {
1383 self_thread
= current_thread();
1385 wsave
= thread_interrupt_level(THREAD_UNINT
);
1388 task_set_corpse_pending_report(task
);
1389 task_set_corpse(task
);
1391 kr
= task_start_halt_locked(task
, TRUE
);
1392 assert(kr
== KERN_SUCCESS
);
1393 ipc_task_reset(task
);
1394 ipc_task_enable(task
);
1397 /* terminate the ipc space */
1398 ipc_space_terminate(task
->itk_space
);
1400 task_start_halt(task
);
1401 thread_terminate_internal(self_thread
);
1402 (void) thread_interrupt_level(wsave
);
1403 assert(task
->halting
== TRUE
);
1408 task_terminate_internal(
1411 thread_t thread
, self
;
1413 boolean_t interrupt_save
;
1416 assert(task
!= kernel_task
);
1418 self
= current_thread();
1419 self_task
= self
->task
;
1422 * Get the task locked and make sure that we are not racing
1423 * with someone else trying to terminate us.
1425 if (task
== self_task
)
1428 if (task
< self_task
) {
1430 task_lock(self_task
);
1433 task_lock(self_task
);
1437 if (!task
->active
) {
1439 * Task is already being terminated.
1440 * Just return an error. If we are dying, this will
1441 * just get us to our AST special handler and that
1442 * will get us to finalize the termination of ourselves.
1445 if (self_task
!= task
)
1446 task_unlock(self_task
);
1448 return (KERN_FAILURE
);
1451 if (task_corpse_pending_report(task
)) {
1453 * Task is marked for reporting as corpse.
1454 * Just return an error. This will
1455 * just get us to our AST special handler and that
1456 * will get us to finish the path to death
1459 if (self_task
!= task
)
1460 task_unlock(self_task
);
1462 return (KERN_FAILURE
);
1465 if (self_task
!= task
)
1466 task_unlock(self_task
);
1469 * Make sure the current thread does not get aborted out of
1470 * the waits inside these operations.
1472 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
1475 * Indicate that we want all the threads to stop executing
1476 * at user space by holding the task (we would have held
1477 * each thread independently in thread_terminate_internal -
1478 * but this way we may be more likely to already find it
1479 * held there). Mark the task inactive, and prevent
1480 * further task operations via the task port.
1482 task_hold_locked(task
);
1483 task
->active
= FALSE
;
1484 ipc_task_disable(task
);
1486 #if CONFIG_TELEMETRY
1488 * Notify telemetry that this task is going away.
1490 telemetry_task_ctl_locked(task
, TF_TELEMETRY
, 0);
1494 * Terminate each thread in the task.
1496 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1497 thread_terminate_internal(thread
);
1501 if (task
->bsd_info
!= NULL
) {
1502 pid
= proc_pid(task
->bsd_info
);
1504 #endif /* MACH_BSD */
1508 proc_set_task_policy(task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
,
1509 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
1511 /* Early object reap phase */
1513 // PR-17045188: Revisit implementation
1514 // task_partial_reap(task, pid);
1518 * Destroy all synchronizers owned by the task.
1520 task_synchronizer_destroy_all(task
);
1523 * Destroy the IPC space, leaving just a reference for it.
1525 ipc_space_terminate(task
->itk_space
);
1528 /* if some ledgers go negative on tear-down again... */
1529 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1530 task_ledgers
.phys_footprint
);
1531 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1532 task_ledgers
.internal
);
1533 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1534 task_ledgers
.internal_compressed
);
1535 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1536 task_ledgers
.iokit_mapped
);
1537 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1538 task_ledgers
.alternate_accounting
);
1539 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
1540 task_ledgers
.alternate_accounting_compressed
);
1544 * If the current thread is a member of the task
1545 * being terminated, then the last reference to
1546 * the task will not be dropped until the thread
1547 * is finally reaped. To avoid incurring the
1548 * expense of removing the address space regions
1549 * at reap time, we do it explictly here.
1552 vm_map_lock(task
->map
);
1553 vm_map_disable_hole_optimization(task
->map
);
1554 vm_map_unlock(task
->map
);
1556 vm_map_remove(task
->map
,
1557 task
->map
->min_offset
,
1558 task
->map
->max_offset
,
1559 /* no unnesting on final cleanup: */
1560 VM_MAP_REMOVE_NO_UNNESTING
);
1562 /* release our shared region */
1563 vm_shared_region_set(task
, NULL
);
1568 * Identify the pmap's process, in case the pmap ledgers drift
1569 * and we have to report it.
1572 if (task
->bsd_info
) {
1573 pid
= proc_pid(task
->bsd_info
);
1574 proc_name_kdp(task
, procname
, sizeof (procname
));
1577 strlcpy(procname
, "<unknown>", sizeof (procname
));
1579 pmap_set_process(task
->map
->pmap
, pid
, procname
);
1580 #endif /* MACH_ASSERT */
1582 lck_mtx_lock(&tasks_threads_lock
);
1583 queue_remove(&tasks
, task
, task_t
, tasks
);
1584 queue_enter(&terminated_tasks
, task
, task_t
, tasks
);
1586 terminated_tasks_count
++;
1587 lck_mtx_unlock(&tasks_threads_lock
);
1590 * We no longer need to guard against being aborted, so restore
1591 * the previous interruptible state.
1593 thread_interrupt_level(interrupt_save
);
1596 /* force the task to release all ctrs */
1597 if (task
->t_chud
& TASK_KPC_FORCED_ALL_CTRS
)
1598 kpc_force_all_ctrs(task
, 0);
1601 #if CONFIG_COALITIONS
1603 * Leave our coalitions. (drop activation but not reference)
1605 coalitions_remove_task(task
);
1609 * Get rid of the task active reference on itself.
1611 task_deallocate(task
);
1613 return (KERN_SUCCESS
);
1619 * Shut the current task down (except for the current thread) in
1620 * preparation for dramatic changes to the task (probably exec).
1621 * We hold the task and mark all other threads in the task for
1625 task_start_halt(task_t task
)
1627 kern_return_t kr
= KERN_SUCCESS
;
1629 kr
= task_start_halt_locked(task
, FALSE
);
1634 static kern_return_t
1635 task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
)
1637 thread_t thread
, self
;
1638 uint64_t dispatchqueue_offset
;
1640 assert(task
!= kernel_task
);
1642 self
= current_thread();
1644 if (task
!= self
->task
)
1645 return (KERN_INVALID_ARGUMENT
);
1647 if (task
->halting
|| !task
->active
|| !self
->active
) {
1649 * Task or current thread is already being terminated.
1650 * Hurry up and return out of the current kernel context
1651 * so that we run our AST special handler to terminate
1654 return (KERN_FAILURE
);
1657 task
->halting
= TRUE
;
1660 * Mark all the threads to keep them from starting any more
1661 * user-level execution. The thread_terminate_internal code
1662 * would do this on a thread by thread basis anyway, but this
1663 * gives us a better chance of not having to wait there.
1665 task_hold_locked(task
);
1666 dispatchqueue_offset
= get_dispatchqueue_offset_from_proc(task
->bsd_info
);
1669 * Terminate all the other threads in the task.
1671 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
)
1673 if (should_mark_corpse
) {
1674 thread_mtx_lock(thread
);
1675 thread
->inspection
= TRUE
;
1676 thread_mtx_unlock(thread
);
1679 thread_terminate_internal(thread
);
1681 task
->dispatchqueue_offset
= dispatchqueue_offset
;
1683 task_release_locked(task
);
1685 return KERN_SUCCESS
;
1690 * task_complete_halt:
1692 * Complete task halt by waiting for threads to terminate, then clean
1693 * up task resources (VM, port namespace, etc...) and then let the
1694 * current thread go in the (practically empty) task context.
1697 task_complete_halt(task_t task
)
1700 assert(task
->halting
);
1701 assert(task
== current_task());
1704 * Wait for the other threads to get shut down.
1705 * When the last other thread is reaped, we'll be
1708 if (task
->thread_count
> 1) {
1709 assert_wait((event_t
)&task
->halting
, THREAD_UNINT
);
1711 thread_block(THREAD_CONTINUE_NULL
);
1717 * Give the machine dependent code a chance
1718 * to perform cleanup of task-level resources
1719 * associated with the current thread before
1720 * ripping apart the task.
1722 machine_task_terminate(task
);
1725 * Destroy all synchronizers owned by the task.
1727 task_synchronizer_destroy_all(task
);
1730 * Destroy the contents of the IPC space, leaving just
1731 * a reference for it.
1733 ipc_space_clean(task
->itk_space
);
1736 * Clean out the address space, as we are going to be
1737 * getting a new one.
1739 vm_map_remove(task
->map
, task
->map
->min_offset
,
1740 task
->map
->max_offset
,
1741 /* no unnesting on final cleanup: */
1742 VM_MAP_REMOVE_NO_UNNESTING
);
1744 task
->halting
= FALSE
;
1750 * Suspend execution of the specified task.
1751 * This is a recursive-style suspension of the task, a count of
1752 * suspends is maintained.
1754 * CONDITIONS: the task is locked and active.
1758 register task_t task
)
1760 register thread_t thread
;
1762 assert(task
->active
);
1764 if (task
->suspend_count
++ > 0)
1768 * Iterate through all the threads and hold them.
1770 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1771 thread_mtx_lock(thread
);
1772 thread_hold(thread
);
1773 thread_mtx_unlock(thread
);
1780 * Same as the internal routine above, except that is must lock
1781 * and verify that the task is active. This differs from task_suspend
1782 * in that it places a kernel hold on the task rather than just a
1783 * user-level hold. This keeps users from over resuming and setting
1784 * it running out from under the kernel.
1786 * CONDITIONS: the caller holds a reference on the task
1790 register task_t task
)
1792 if (task
== TASK_NULL
)
1793 return (KERN_INVALID_ARGUMENT
);
1797 if (!task
->active
) {
1800 return (KERN_FAILURE
);
1803 task_hold_locked(task
);
1806 return (KERN_SUCCESS
);
1812 boolean_t until_not_runnable
)
1814 if (task
== TASK_NULL
)
1815 return (KERN_INVALID_ARGUMENT
);
1819 if (!task
->active
) {
1822 return (KERN_FAILURE
);
1825 task_wait_locked(task
, until_not_runnable
);
1828 return (KERN_SUCCESS
);
1834 * Wait for all threads in task to stop.
1837 * Called with task locked, active, and held.
1841 register task_t task
,
1842 boolean_t until_not_runnable
)
1844 register thread_t thread
, self
;
1846 assert(task
->active
);
1847 assert(task
->suspend_count
> 0);
1849 self
= current_thread();
1852 * Iterate through all the threads and wait for them to
1853 * stop. Do not wait for the current thread if it is within
1856 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1858 thread_wait(thread
, until_not_runnable
);
1863 * task_release_locked:
1865 * Release a kernel hold on a task.
1867 * CONDITIONS: the task is locked and active
1870 task_release_locked(
1871 register task_t task
)
1873 register thread_t thread
;
1875 assert(task
->active
);
1876 assert(task
->suspend_count
> 0);
1878 if (--task
->suspend_count
> 0)
1881 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1882 thread_mtx_lock(thread
);
1883 thread_release(thread
);
1884 thread_mtx_unlock(thread
);
1891 * Same as the internal routine above, except that it must lock
1892 * and verify that the task is active.
1894 * CONDITIONS: The caller holds a reference to the task
1900 if (task
== TASK_NULL
)
1901 return (KERN_INVALID_ARGUMENT
);
1905 if (!task
->active
) {
1908 return (KERN_FAILURE
);
1911 task_release_locked(task
);
1914 return (KERN_SUCCESS
);
1920 thread_act_array_t
*threads_out
,
1921 mach_msg_type_number_t
*count
)
1923 mach_msg_type_number_t actual
;
1924 thread_t
*thread_list
;
1926 vm_size_t size
, size_needed
;
1930 if (task
== TASK_NULL
)
1931 return (KERN_INVALID_ARGUMENT
);
1933 size
= 0; addr
= NULL
;
1937 if (!task
->active
) {
1943 return (KERN_FAILURE
);
1946 actual
= task
->thread_count
;
1948 /* do we have the memory we need? */
1949 size_needed
= actual
* sizeof (mach_port_t
);
1950 if (size_needed
<= size
)
1953 /* unlock the task and allocate more memory */
1959 assert(size_needed
> 0);
1962 addr
= kalloc(size
);
1964 return (KERN_RESOURCE_SHORTAGE
);
1967 /* OK, have memory and the task is locked & active */
1968 thread_list
= (thread_t
*)addr
;
1972 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
1973 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
1974 thread_reference_internal(thread
);
1975 thread_list
[j
++] = thread
;
1978 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
1981 size_needed
= actual
* sizeof (mach_port_t
);
1983 /* can unlock task now that we've got the thread refs */
1987 /* no threads, so return null pointer and deallocate memory */
1989 *threads_out
= NULL
;
1996 /* if we allocated too much, must copy */
1998 if (size_needed
< size
) {
2001 newaddr
= kalloc(size_needed
);
2003 for (i
= 0; i
< actual
; ++i
)
2004 thread_deallocate(thread_list
[i
]);
2006 return (KERN_RESOURCE_SHORTAGE
);
2009 bcopy(addr
, newaddr
, size_needed
);
2011 thread_list
= (thread_t
*)newaddr
;
2014 *threads_out
= thread_list
;
2017 /* do the conversion that Mig should handle */
2019 for (i
= 0; i
< actual
; ++i
)
2020 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
2023 return (KERN_SUCCESS
);
2026 #define TASK_HOLD_NORMAL 0
2027 #define TASK_HOLD_PIDSUSPEND 1
2028 #define TASK_HOLD_LEGACY 2
2029 #define TASK_HOLD_LEGACY_ALL 3
2031 static kern_return_t
2033 register task_t task
,
2036 if (!task
->active
) {
2037 return (KERN_FAILURE
);
2040 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2041 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_SUSPEND
) | DBG_FUNC_NONE
,
2042 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2043 task
->user_stop_count
, task
->user_stop_count
+ 1, 0);
2046 current_task()->suspends_outstanding
++;
2049 if (mode
== TASK_HOLD_LEGACY
)
2050 task
->legacy_stop_count
++;
2052 if (task
->user_stop_count
++ > 0) {
2054 * If the stop count was positive, the task is
2055 * already stopped and we can exit.
2057 return (KERN_SUCCESS
);
2061 * Put a kernel-level hold on the threads in the task (all
2062 * user-level task suspensions added together represent a
2063 * single kernel-level hold). We then wait for the threads
2064 * to stop executing user code.
2066 task_hold_locked(task
);
2067 task_wait_locked(task
, FALSE
);
2069 return (KERN_SUCCESS
);
2072 static kern_return_t
2074 register task_t task
,
2077 register boolean_t release
= FALSE
;
2079 if (!task
->active
) {
2080 return (KERN_FAILURE
);
2083 if (mode
== TASK_HOLD_PIDSUSPEND
) {
2084 if (task
->pidsuspended
== FALSE
) {
2085 return (KERN_FAILURE
);
2087 task
->pidsuspended
= FALSE
;
2090 if (task
->user_stop_count
> (task
->pidsuspended
? 1 : 0)) {
2092 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2093 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_RESUME
) | DBG_FUNC_NONE
,
2094 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2095 task
->user_stop_count
, mode
, task
->legacy_stop_count
);
2099 * This is obviously not robust; if we suspend one task and then resume a different one,
2100 * we'll fly under the radar. This is only meant to catch the common case of a crashed
2101 * or buggy suspender.
2103 current_task()->suspends_outstanding
--;
2106 if (mode
== TASK_HOLD_LEGACY_ALL
) {
2107 if (task
->legacy_stop_count
>= task
->user_stop_count
) {
2108 task
->user_stop_count
= 0;
2111 task
->user_stop_count
-= task
->legacy_stop_count
;
2113 task
->legacy_stop_count
= 0;
2115 if (mode
== TASK_HOLD_LEGACY
&& task
->legacy_stop_count
> 0)
2116 task
->legacy_stop_count
--;
2117 if (--task
->user_stop_count
== 0)
2122 return (KERN_FAILURE
);
2126 * Release the task if necessary.
2129 task_release_locked(task
);
2131 return (KERN_SUCCESS
);
2138 * Implement an (old-fashioned) user-level suspension on a task.
2140 * Because the user isn't expecting to have to manage a suspension
2141 * token, we'll track it for him in the kernel in the form of a naked
2142 * send right to the task's resume port. All such send rights
2143 * account for a single suspension against the task (unlike task_suspend2()
2144 * where each caller gets a unique suspension count represented by a
2145 * unique send-once right).
2148 * The caller holds a reference to the task
2152 register task_t task
)
2155 mach_port_t port
, send
, old_notify
;
2156 mach_port_name_t name
;
2158 if (task
== TASK_NULL
|| task
== kernel_task
)
2159 return (KERN_INVALID_ARGUMENT
);
2164 * Claim a send right on the task resume port, and request a no-senders
2165 * notification on that port (if none outstanding).
2167 if (task
->itk_resume
== IP_NULL
) {
2168 task
->itk_resume
= ipc_port_alloc_kernel();
2169 if (!IP_VALID(task
->itk_resume
))
2170 panic("failed to create resume port");
2171 ipc_kobject_set(task
->itk_resume
, (ipc_kobject_t
)task
, IKOT_TASK_RESUME
);
2174 port
= task
->itk_resume
;
2176 assert(ip_active(port
));
2178 send
= ipc_port_make_send_locked(port
);
2179 assert(IP_VALID(send
));
2181 if (port
->ip_nsrequest
== IP_NULL
) {
2182 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
2183 assert(old_notify
== IP_NULL
);
2190 * place a legacy hold on the task.
2192 kr
= place_task_hold(task
, TASK_HOLD_LEGACY
);
2193 if (kr
!= KERN_SUCCESS
) {
2195 ipc_port_release_send(send
);
2202 * Copyout the send right into the calling task's IPC space. It won't know it is there,
2203 * but we'll look it up when calling a traditional resume. Any IPC operations that
2204 * deallocate the send right will auto-release the suspension.
2206 if ((kr
= ipc_kmsg_copyout_object(current_task()->itk_space
, (ipc_object_t
)send
,
2207 MACH_MSG_TYPE_MOVE_SEND
, &name
)) != KERN_SUCCESS
) {
2208 printf("warning: %s(%d) failed to copyout suspension token for pid %d with error: %d\n",
2209 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
2210 task_pid(task
), kr
);
2219 * Release a user hold on a task.
2222 * The caller holds a reference to the task
2226 register task_t task
)
2229 mach_port_name_t resume_port_name
;
2230 ipc_entry_t resume_port_entry
;
2231 ipc_space_t space
= current_task()->itk_space
;
2233 if (task
== TASK_NULL
|| task
== kernel_task
)
2234 return (KERN_INVALID_ARGUMENT
);
2236 /* release a legacy task hold */
2238 kr
= release_task_hold(task
, TASK_HOLD_LEGACY
);
2241 is_write_lock(space
);
2242 if (is_active(space
) && IP_VALID(task
->itk_resume
) &&
2243 ipc_hash_lookup(space
, (ipc_object_t
)task
->itk_resume
, &resume_port_name
, &resume_port_entry
) == TRUE
) {
2245 * We found a suspension token in the caller's IPC space. Release a send right to indicate that
2246 * we are holding one less legacy hold on the task from this caller. If the release failed,
2247 * go ahead and drop all the rights, as someone either already released our holds or the task
2250 if (kr
== KERN_SUCCESS
)
2251 ipc_right_dealloc(space
, resume_port_name
, resume_port_entry
);
2253 ipc_right_destroy(space
, resume_port_name
, resume_port_entry
, FALSE
, 0);
2254 /* space unlocked */
2256 is_write_unlock(space
);
2257 if (kr
== KERN_SUCCESS
)
2258 printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
2259 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
2267 * Suspend the target task.
2268 * Making/holding a token/reference/port is the callers responsibility.
2271 task_suspend_internal(task_t task
)
2275 if (task
== TASK_NULL
|| task
== kernel_task
)
2276 return (KERN_INVALID_ARGUMENT
);
2279 kr
= place_task_hold(task
, TASK_HOLD_NORMAL
);
2285 * Suspend the target task, and return a suspension token. The token
2286 * represents a reference on the suspended task.
2290 register task_t task
,
2291 task_suspension_token_t
*suspend_token
)
2295 kr
= task_suspend_internal(task
);
2296 if (kr
!= KERN_SUCCESS
) {
2297 *suspend_token
= TASK_NULL
;
2302 * Take a reference on the target task and return that to the caller
2303 * as a "suspension token," which can be converted into an SO right to
2304 * the now-suspended task's resume port.
2306 task_reference_internal(task
);
2307 *suspend_token
= task
;
2309 return (KERN_SUCCESS
);
2314 * (reference/token/port management is caller's responsibility).
2317 task_resume_internal(
2318 register task_suspension_token_t task
)
2322 if (task
== TASK_NULL
|| task
== kernel_task
)
2323 return (KERN_INVALID_ARGUMENT
);
2326 kr
= release_task_hold(task
, TASK_HOLD_NORMAL
);
2332 * Resume the task using a suspension token. Consumes the token's ref.
2336 register task_suspension_token_t task
)
2340 kr
= task_resume_internal(task
);
2341 task_suspension_token_deallocate(task
);
2347 task_suspension_notify(mach_msg_header_t
*request_header
)
2349 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
2350 task_t task
= convert_port_to_task_suspension_token(port
);
2351 mach_msg_type_number_t not_count
;
2353 if (task
== TASK_NULL
|| task
== kernel_task
)
2354 return TRUE
; /* nothing to do */
2356 switch (request_header
->msgh_id
) {
2358 case MACH_NOTIFY_SEND_ONCE
:
2359 /* release the hold held by this specific send-once right */
2361 release_task_hold(task
, TASK_HOLD_NORMAL
);
2365 case MACH_NOTIFY_NO_SENDERS
:
2366 not_count
= ((mach_no_senders_notification_t
*)request_header
)->not_count
;
2370 if (port
->ip_mscount
== not_count
) {
2372 /* release all the [remaining] outstanding legacy holds */
2373 assert(port
->ip_nsrequest
== IP_NULL
);
2375 release_task_hold(task
, TASK_HOLD_LEGACY_ALL
);
2378 } else if (port
->ip_nsrequest
== IP_NULL
) {
2379 ipc_port_t old_notify
;
2382 /* new send rights, re-arm notification at current make-send count */
2383 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
2384 assert(old_notify
== IP_NULL
);
2396 task_suspension_token_deallocate(task
); /* drop token reference */
2401 task_pidsuspend_locked(task_t task
)
2405 if (task
->pidsuspended
) {
2410 task
->pidsuspended
= TRUE
;
2412 kr
= place_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
2413 if (kr
!= KERN_SUCCESS
) {
2414 task
->pidsuspended
= FALSE
;
2424 * Suspends a task by placing a hold on its threads.
2427 * The caller holds a reference to the task
2431 register task_t task
)
2435 if (task
== TASK_NULL
|| task
== kernel_task
)
2436 return (KERN_INVALID_ARGUMENT
);
2440 kr
= task_pidsuspend_locked(task
);
2447 /* If enabled, we bring all the frozen pages back in prior to resumption; otherwise, they're faulted back in on demand */
2448 #define THAW_ON_RESUME 1
2452 * Resumes a previously suspended task.
2455 * The caller holds a reference to the task
2459 register task_t task
)
2463 if (task
== TASK_NULL
|| task
== kernel_task
)
2464 return (KERN_INVALID_ARGUMENT
);
2468 #if (CONFIG_FREEZE && THAW_ON_RESUME)
2470 while (task
->changing_freeze_state
) {
2472 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
2474 thread_block(THREAD_CONTINUE_NULL
);
2478 task
->changing_freeze_state
= TRUE
;
2481 kr
= release_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
2485 #if (CONFIG_FREEZE && THAW_ON_RESUME)
2486 if ((kr
== KERN_SUCCESS
) && (task
->frozen
== TRUE
)) {
2488 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2493 kr
= vm_map_thaw(task
->map
);
2498 if (kr
== KERN_SUCCESS
)
2499 task
->frozen
= FALSE
;
2500 task
->changing_freeze_state
= FALSE
;
2501 thread_wakeup(&task
->changing_freeze_state
);
2517 * The caller holds a reference to the task
2519 extern void vm_wake_compactor_swapper();
2520 extern queue_head_t c_swapout_list_head
;
2524 register task_t task
,
2525 uint32_t *purgeable_count
,
2526 uint32_t *wired_count
,
2527 uint32_t *clean_count
,
2528 uint32_t *dirty_count
,
2529 uint32_t dirty_budget
,
2531 boolean_t walk_only
)
2535 if (task
== TASK_NULL
|| task
== kernel_task
)
2536 return (KERN_INVALID_ARGUMENT
);
2540 while (task
->changing_freeze_state
) {
2542 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
2544 thread_block(THREAD_CONTINUE_NULL
);
2550 return (KERN_FAILURE
);
2552 task
->changing_freeze_state
= TRUE
;
2557 kr
= vm_map_freeze_walk(task
->map
, purgeable_count
, wired_count
, clean_count
, dirty_count
, dirty_budget
, shared
);
2559 kr
= vm_map_freeze(task
->map
, purgeable_count
, wired_count
, clean_count
, dirty_count
, dirty_budget
, shared
);
2564 if (walk_only
== FALSE
&& kr
== KERN_SUCCESS
)
2565 task
->frozen
= TRUE
;
2566 task
->changing_freeze_state
= FALSE
;
2567 thread_wakeup(&task
->changing_freeze_state
);
2571 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2572 vm_wake_compactor_swapper();
2574 * We do an explicit wakeup of the swapout thread here
2575 * because the compact_and_swap routines don't have
2576 * knowledge about these kind of "per-task packed c_segs"
2577 * and so will not be evaluating whether we need to do
2580 thread_wakeup((event_t
)&c_swapout_list_head
);
2589 * Thaw a currently frozen task.
2592 * The caller holds a reference to the task
2596 register task_t task
)
2600 if (task
== TASK_NULL
|| task
== kernel_task
)
2601 return (KERN_INVALID_ARGUMENT
);
2605 while (task
->changing_freeze_state
) {
2607 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
2609 thread_block(THREAD_CONTINUE_NULL
);
2613 if (!task
->frozen
) {
2615 return (KERN_FAILURE
);
2617 task
->changing_freeze_state
= TRUE
;
2619 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
) {
2622 kr
= vm_map_thaw(task
->map
);
2626 if (kr
== KERN_SUCCESS
)
2627 task
->frozen
= FALSE
;
2629 task
->frozen
= FALSE
;
2633 task
->changing_freeze_state
= FALSE
;
2634 thread_wakeup(&task
->changing_freeze_state
);
2638 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2639 vm_wake_compactor_swapper();
2645 #endif /* CONFIG_FREEZE */
2648 host_security_set_task_token(
2649 host_security_t host_security
,
2651 security_token_t sec_token
,
2652 audit_token_t audit_token
,
2653 host_priv_t host_priv
)
2655 ipc_port_t host_port
;
2658 if (task
== TASK_NULL
)
2659 return(KERN_INVALID_ARGUMENT
);
2661 if (host_security
== HOST_NULL
)
2662 return(KERN_INVALID_SECURITY
);
2665 task
->sec_token
= sec_token
;
2666 task
->audit_token
= audit_token
;
2670 if (host_priv
!= HOST_PRIV_NULL
) {
2671 kr
= host_get_host_priv_port(host_priv
, &host_port
);
2673 kr
= host_get_host_port(host_priv_self(), &host_port
);
2675 assert(kr
== KERN_SUCCESS
);
2676 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
2681 task_send_trace_memory(
2683 __unused
uint32_t pid
,
2684 __unused
uint64_t uniqueid
)
2686 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
2687 if (target_task
== TASK_NULL
)
2688 return (KERN_INVALID_ARGUMENT
);
2691 kr
= atm_send_proc_inspect_notification(target_task
,
2699 * This routine was added, pretty much exclusively, for registering the
2700 * RPC glue vector for in-kernel short circuited tasks. Rather than
2701 * removing it completely, I have only disabled that feature (which was
2702 * the only feature at the time). It just appears that we are going to
2703 * want to add some user data to tasks in the future (i.e. bsd info,
2704 * task names, etc...), so I left it in the formal task interface.
2709 task_flavor_t flavor
,
2710 __unused task_info_t task_info_in
, /* pointer to IN array */
2711 __unused mach_msg_type_number_t task_info_count
)
2713 if (task
== TASK_NULL
)
2714 return(KERN_INVALID_ARGUMENT
);
2719 case TASK_TRACE_MEMORY_INFO
:
2721 if (task_info_count
!= TASK_TRACE_MEMORY_INFO_COUNT
)
2722 return (KERN_INVALID_ARGUMENT
);
2724 assert(task_info_in
!= NULL
);
2725 task_trace_memory_info_t mem_info
;
2726 mem_info
= (task_trace_memory_info_t
) task_info_in
;
2727 kern_return_t kr
= atm_register_trace_memory(task
,
2728 mem_info
->user_memory_address
,
2729 mem_info
->buffer_size
);
2736 return (KERN_INVALID_ARGUMENT
);
2738 return (KERN_SUCCESS
);
2741 int radar_20146450
= 1;
2745 task_flavor_t flavor
,
2746 task_info_t task_info_out
,
2747 mach_msg_type_number_t
*task_info_count
)
2749 kern_return_t error
= KERN_SUCCESS
;
2751 if (task
== TASK_NULL
)
2752 return (KERN_INVALID_ARGUMENT
);
2756 if ((task
!= current_task()) && (!task
->active
)) {
2758 return (KERN_INVALID_ARGUMENT
);
2763 case TASK_BASIC_INFO_32
:
2764 case TASK_BASIC2_INFO_32
:
2766 task_basic_info_32_t basic_info
;
2771 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
) {
2772 error
= KERN_INVALID_ARGUMENT
;
2776 basic_info
= (task_basic_info_32_t
)task_info_out
;
2778 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
2779 basic_info
->virtual_size
= (typeof(basic_info
->virtual_size
))map
->size
;
2780 if (flavor
== TASK_BASIC2_INFO_32
) {
2782 * The "BASIC2" flavor gets the maximum resident
2783 * size instead of the current resident size...
2785 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
2787 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
2789 basic_info
->resident_size
*= PAGE_SIZE
;
2791 basic_info
->policy
= ((task
!= kernel_task
)?
2792 POLICY_TIMESHARE
: POLICY_RR
);
2793 basic_info
->suspend_count
= task
->user_stop_count
;
2795 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
2796 basic_info
->user_time
.seconds
=
2797 (typeof(basic_info
->user_time
.seconds
))secs
;
2798 basic_info
->user_time
.microseconds
= usecs
;
2800 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
2801 basic_info
->system_time
.seconds
=
2802 (typeof(basic_info
->system_time
.seconds
))secs
;
2803 basic_info
->system_time
.microseconds
= usecs
;
2805 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
2809 case TASK_BASIC_INFO_64
:
2811 task_basic_info_64_t basic_info
;
2816 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
) {
2817 error
= KERN_INVALID_ARGUMENT
;
2821 basic_info
= (task_basic_info_64_t
)task_info_out
;
2823 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
2824 basic_info
->virtual_size
= map
->size
;
2825 basic_info
->resident_size
=
2826 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
2829 basic_info
->policy
= ((task
!= kernel_task
)?
2830 POLICY_TIMESHARE
: POLICY_RR
);
2831 basic_info
->suspend_count
= task
->user_stop_count
;
2833 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
2834 basic_info
->user_time
.seconds
=
2835 (typeof(basic_info
->user_time
.seconds
))secs
;
2836 basic_info
->user_time
.microseconds
= usecs
;
2838 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
2839 basic_info
->system_time
.seconds
=
2840 (typeof(basic_info
->system_time
.seconds
))secs
;
2841 basic_info
->system_time
.microseconds
= usecs
;
2843 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
2847 case MACH_TASK_BASIC_INFO
:
2849 mach_task_basic_info_t basic_info
;
2854 if (*task_info_count
< MACH_TASK_BASIC_INFO_COUNT
) {
2855 error
= KERN_INVALID_ARGUMENT
;
2859 basic_info
= (mach_task_basic_info_t
)task_info_out
;
2861 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
2863 basic_info
->virtual_size
= map
->size
;
2865 basic_info
->resident_size
=
2866 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
));
2867 basic_info
->resident_size
*= PAGE_SIZE_64
;
2869 basic_info
->resident_size_max
=
2870 (mach_vm_size_t
)(pmap_resident_max(map
->pmap
));
2871 basic_info
->resident_size_max
*= PAGE_SIZE_64
;
2873 basic_info
->policy
= ((task
!= kernel_task
) ?
2874 POLICY_TIMESHARE
: POLICY_RR
);
2876 basic_info
->suspend_count
= task
->user_stop_count
;
2878 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
2879 basic_info
->user_time
.seconds
=
2880 (typeof(basic_info
->user_time
.seconds
))secs
;
2881 basic_info
->user_time
.microseconds
= usecs
;
2883 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
2884 basic_info
->system_time
.seconds
=
2885 (typeof(basic_info
->system_time
.seconds
))secs
;
2886 basic_info
->system_time
.microseconds
= usecs
;
2888 *task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
2892 case TASK_THREAD_TIMES_INFO
:
2894 register task_thread_times_info_t times_info
;
2895 register thread_t thread
;
2897 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
2898 error
= KERN_INVALID_ARGUMENT
;
2902 times_info
= (task_thread_times_info_t
) task_info_out
;
2903 times_info
->user_time
.seconds
= 0;
2904 times_info
->user_time
.microseconds
= 0;
2905 times_info
->system_time
.seconds
= 0;
2906 times_info
->system_time
.microseconds
= 0;
2909 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2910 time_value_t user_time
, system_time
;
2912 if (thread
->options
& TH_OPT_IDLE_THREAD
)
2915 thread_read_times(thread
, &user_time
, &system_time
);
2917 time_value_add(×_info
->user_time
, &user_time
);
2918 time_value_add(×_info
->system_time
, &system_time
);
2921 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
2925 case TASK_ABSOLUTETIME_INFO
:
2927 task_absolutetime_info_t info
;
2928 register thread_t thread
;
2930 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
) {
2931 error
= KERN_INVALID_ARGUMENT
;
2935 info
= (task_absolutetime_info_t
)task_info_out
;
2936 info
->threads_user
= info
->threads_system
= 0;
2939 info
->total_user
= task
->total_user_time
;
2940 info
->total_system
= task
->total_system_time
;
2942 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2946 if (thread
->options
& TH_OPT_IDLE_THREAD
)
2950 thread_lock(thread
);
2952 tval
= timer_grab(&thread
->user_timer
);
2953 info
->threads_user
+= tval
;
2954 info
->total_user
+= tval
;
2956 tval
= timer_grab(&thread
->system_timer
);
2957 if (thread
->precise_user_kernel_time
) {
2958 info
->threads_system
+= tval
;
2959 info
->total_system
+= tval
;
2961 /* system_timer may represent either sys or user */
2962 info
->threads_user
+= tval
;
2963 info
->total_user
+= tval
;
2966 thread_unlock(thread
);
2971 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
2975 case TASK_DYLD_INFO
:
2977 task_dyld_info_t info
;
2980 * We added the format field to TASK_DYLD_INFO output. For
2981 * temporary backward compatibility, accept the fact that
2982 * clients may ask for the old version - distinquished by the
2983 * size of the expected result structure.
2985 #define TASK_LEGACY_DYLD_INFO_COUNT \
2986 offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
2988 if (*task_info_count
< TASK_LEGACY_DYLD_INFO_COUNT
) {
2989 error
= KERN_INVALID_ARGUMENT
;
2993 info
= (task_dyld_info_t
)task_info_out
;
2994 info
->all_image_info_addr
= task
->all_image_info_addr
;
2995 info
->all_image_info_size
= task
->all_image_info_size
;
2997 /* only set format on output for those expecting it */
2998 if (*task_info_count
>= TASK_DYLD_INFO_COUNT
) {
2999 info
->all_image_info_format
= task_has_64BitAddr(task
) ?
3000 TASK_DYLD_ALL_IMAGE_INFO_64
:
3001 TASK_DYLD_ALL_IMAGE_INFO_32
;
3002 *task_info_count
= TASK_DYLD_INFO_COUNT
;
3004 *task_info_count
= TASK_LEGACY_DYLD_INFO_COUNT
;
3009 case TASK_EXTMOD_INFO
:
3011 task_extmod_info_t info
;
3014 if (*task_info_count
< TASK_EXTMOD_INFO_COUNT
) {
3015 error
= KERN_INVALID_ARGUMENT
;
3019 info
= (task_extmod_info_t
)task_info_out
;
3021 p
= get_bsdtask_info(task
);
3023 proc_getexecutableuuid(p
, info
->task_uuid
, sizeof(info
->task_uuid
));
3025 bzero(info
->task_uuid
, sizeof(info
->task_uuid
));
3027 info
->extmod_statistics
= task
->extmod_statistics
;
3028 *task_info_count
= TASK_EXTMOD_INFO_COUNT
;
3033 case TASK_KERNELMEMORY_INFO
:
3035 task_kernelmemory_info_t tkm_info
;
3036 ledger_amount_t credit
, debit
;
3038 if (*task_info_count
< TASK_KERNELMEMORY_INFO_COUNT
) {
3039 error
= KERN_INVALID_ARGUMENT
;
3043 tkm_info
= (task_kernelmemory_info_t
) task_info_out
;
3044 tkm_info
->total_palloc
= 0;
3045 tkm_info
->total_pfree
= 0;
3046 tkm_info
->total_salloc
= 0;
3047 tkm_info
->total_sfree
= 0;
3049 if (task
== kernel_task
) {
3051 * All shared allocs/frees from other tasks count against
3052 * the kernel private memory usage. If we are looking up
3053 * info for the kernel task, gather from everywhere.
3057 /* start by accounting for all the terminated tasks against the kernel */
3058 tkm_info
->total_palloc
= tasks_tkm_private
.alloc
+ tasks_tkm_shared
.alloc
;
3059 tkm_info
->total_pfree
= tasks_tkm_private
.free
+ tasks_tkm_shared
.free
;
3061 /* count all other task/thread shared alloc/free against the kernel */
3062 lck_mtx_lock(&tasks_threads_lock
);
3064 /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
3065 queue_iterate(&tasks
, task
, task_t
, tasks
) {
3066 if (task
== kernel_task
) {
3067 if (ledger_get_entries(task
->ledger
,
3068 task_ledgers
.tkm_private
, &credit
,
3069 &debit
) == KERN_SUCCESS
) {
3070 tkm_info
->total_palloc
+= credit
;
3071 tkm_info
->total_pfree
+= debit
;
3074 if (!ledger_get_entries(task
->ledger
,
3075 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3076 tkm_info
->total_palloc
+= credit
;
3077 tkm_info
->total_pfree
+= debit
;
3080 lck_mtx_unlock(&tasks_threads_lock
);
3082 if (!ledger_get_entries(task
->ledger
,
3083 task_ledgers
.tkm_private
, &credit
, &debit
)) {
3084 tkm_info
->total_palloc
= credit
;
3085 tkm_info
->total_pfree
= debit
;
3087 if (!ledger_get_entries(task
->ledger
,
3088 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3089 tkm_info
->total_salloc
= credit
;
3090 tkm_info
->total_sfree
= debit
;
3095 *task_info_count
= TASK_KERNELMEMORY_INFO_COUNT
;
3096 return KERN_SUCCESS
;
3100 case TASK_SCHED_FIFO_INFO
:
3103 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
) {
3104 error
= KERN_INVALID_ARGUMENT
;
3108 error
= KERN_INVALID_POLICY
;
3113 case TASK_SCHED_RR_INFO
:
3115 register policy_rr_base_t rr_base
;
3116 uint32_t quantum_time
;
3117 uint64_t quantum_ns
;
3119 if (*task_info_count
< POLICY_RR_BASE_COUNT
) {
3120 error
= KERN_INVALID_ARGUMENT
;
3124 rr_base
= (policy_rr_base_t
) task_info_out
;
3126 if (task
!= kernel_task
) {
3127 error
= KERN_INVALID_POLICY
;
3131 rr_base
->base_priority
= task
->priority
;
3133 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
3134 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
3136 rr_base
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
3138 *task_info_count
= POLICY_RR_BASE_COUNT
;
3143 case TASK_SCHED_TIMESHARE_INFO
:
3145 register policy_timeshare_base_t ts_base
;
3147 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
) {
3148 error
= KERN_INVALID_ARGUMENT
;
3152 ts_base
= (policy_timeshare_base_t
) task_info_out
;
3154 if (task
== kernel_task
) {
3155 error
= KERN_INVALID_POLICY
;
3159 ts_base
->base_priority
= task
->priority
;
3161 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
3165 case TASK_SECURITY_TOKEN
:
3167 register security_token_t
*sec_token_p
;
3169 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
3170 error
= KERN_INVALID_ARGUMENT
;
3174 sec_token_p
= (security_token_t
*) task_info_out
;
3176 *sec_token_p
= task
->sec_token
;
3178 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
3182 case TASK_AUDIT_TOKEN
:
3184 register audit_token_t
*audit_token_p
;
3186 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
3187 error
= KERN_INVALID_ARGUMENT
;
3191 audit_token_p
= (audit_token_t
*) task_info_out
;
3193 *audit_token_p
= task
->audit_token
;
3195 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
3199 case TASK_SCHED_INFO
:
3200 error
= KERN_INVALID_ARGUMENT
;
3203 case TASK_EVENTS_INFO
:
3205 register task_events_info_t events_info
;
3206 register thread_t thread
;
3208 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
3209 error
= KERN_INVALID_ARGUMENT
;
3213 events_info
= (task_events_info_t
) task_info_out
;
3216 events_info
->faults
= task
->faults
;
3217 events_info
->pageins
= task
->pageins
;
3218 events_info
->cow_faults
= task
->cow_faults
;
3219 events_info
->messages_sent
= task
->messages_sent
;
3220 events_info
->messages_received
= task
->messages_received
;
3221 events_info
->syscalls_mach
= task
->syscalls_mach
;
3222 events_info
->syscalls_unix
= task
->syscalls_unix
;
3224 events_info
->csw
= task
->c_switch
;
3226 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3227 events_info
->csw
+= thread
->c_switch
;
3228 events_info
->syscalls_mach
+= thread
->syscalls_mach
;
3229 events_info
->syscalls_unix
+= thread
->syscalls_unix
;
3233 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
3236 case TASK_AFFINITY_TAG_INFO
:
3238 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
) {
3239 error
= KERN_INVALID_ARGUMENT
;
3243 error
= task_affinity_info(task
, task_info_out
, task_info_count
);
3246 case TASK_POWER_INFO
:
3248 if (*task_info_count
< TASK_POWER_INFO_COUNT
) {
3249 error
= KERN_INVALID_ARGUMENT
;
3253 task_power_info_locked(task
, (task_power_info_t
)task_info_out
, NULL
);
3257 case TASK_POWER_INFO_V2
:
3259 if (*task_info_count
< TASK_POWER_INFO_V2_COUNT
) {
3260 error
= KERN_INVALID_ARGUMENT
;
3263 task_power_info_v2_t tpiv2
= (task_power_info_v2_t
) task_info_out
;
3264 task_power_info_locked(task
, &tpiv2
->cpu_energy
, &tpiv2
->gpu_energy
);
3269 case TASK_VM_INFO_PURGEABLE
:
3271 task_vm_info_t vm_info
;
3274 if (*task_info_count
< TASK_VM_INFO_REV0_COUNT
) {
3275 error
= KERN_INVALID_ARGUMENT
;
3279 vm_info
= (task_vm_info_t
)task_info_out
;
3281 if (task
== kernel_task
) {
3286 vm_map_lock_read(map
);
3289 vm_info
->virtual_size
= (typeof(vm_info
->virtual_size
))map
->size
;
3290 vm_info
->region_count
= map
->hdr
.nentries
;
3291 vm_info
->page_size
= vm_map_page_size(map
);
3293 vm_info
->resident_size
= pmap_resident_count(map
->pmap
);
3294 vm_info
->resident_size
*= PAGE_SIZE
;
3295 vm_info
->resident_size_peak
= pmap_resident_max(map
->pmap
);
3296 vm_info
->resident_size_peak
*= PAGE_SIZE
;
3298 #define _VM_INFO(_name) \
3299 vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
3302 _VM_INFO(device_peak
);
3304 _VM_INFO(external_peak
);
3306 _VM_INFO(internal_peak
);
3308 _VM_INFO(reusable_peak
);
3309 _VM_INFO(compressed
);
3310 _VM_INFO(compressed_peak
);
3311 _VM_INFO(compressed_lifetime
);
3313 vm_info
->purgeable_volatile_pmap
= 0;
3314 vm_info
->purgeable_volatile_resident
= 0;
3315 vm_info
->purgeable_volatile_virtual
= 0;
3316 if (task
== kernel_task
) {
3318 * We do not maintain the detailed stats for the
3319 * kernel_pmap, so just count everything as
3322 vm_info
->internal
= vm_info
->resident_size
;
3324 * ... but since the memory held by the VM compressor
3325 * in the kernel address space ought to be attributed
3326 * to user-space tasks, we subtract it from "internal"
3327 * to give memory reporting tools a more accurate idea
3328 * of what the kernel itself is actually using, instead
3329 * of making it look like the kernel is leaking memory
3330 * when the system is under memory pressure.
3332 vm_info
->internal
-= (VM_PAGE_COMPRESSOR_COUNT
*
3335 mach_vm_size_t volatile_virtual_size
;
3336 mach_vm_size_t volatile_resident_size
;
3337 mach_vm_size_t volatile_compressed_size
;
3338 mach_vm_size_t volatile_pmap_size
;
3339 mach_vm_size_t volatile_compressed_pmap_size
;
3342 if (flavor
== TASK_VM_INFO_PURGEABLE
) {
3343 kr
= vm_map_query_volatile(
3345 &volatile_virtual_size
,
3346 &volatile_resident_size
,
3347 &volatile_compressed_size
,
3348 &volatile_pmap_size
,
3349 &volatile_compressed_pmap_size
);
3350 if (kr
== KERN_SUCCESS
) {
3351 vm_info
->purgeable_volatile_pmap
=
3353 if (radar_20146450
) {
3354 vm_info
->compressed
-=
3355 volatile_compressed_pmap_size
;
3357 vm_info
->purgeable_volatile_resident
=
3358 volatile_resident_size
;
3359 vm_info
->purgeable_volatile_virtual
=
3360 volatile_virtual_size
;
3363 vm_map_unlock_read(map
);
3366 if (*task_info_count
>= TASK_VM_INFO_COUNT
) {
3367 vm_info
->phys_footprint
= 0;
3368 *task_info_count
= TASK_VM_INFO_COUNT
;
3370 *task_info_count
= TASK_VM_INFO_REV0_COUNT
;
3376 case TASK_WAIT_STATE_INFO
:
3379 * Deprecated flavor. Currently allowing some results until all users
3380 * stop calling it. The results may not be accurate.
3382 task_wait_state_info_t wait_state_info
;
3383 uint64_t total_sfi_ledger_val
= 0;
3385 if (*task_info_count
< TASK_WAIT_STATE_INFO_COUNT
) {
3386 error
= KERN_INVALID_ARGUMENT
;
3390 wait_state_info
= (task_wait_state_info_t
) task_info_out
;
3392 wait_state_info
->total_wait_state_time
= 0;
3393 bzero(wait_state_info
->_reserved
, sizeof(wait_state_info
->_reserved
));
3395 #if CONFIG_SCHED_SFI
3396 int i
, prev_lentry
= -1;
3397 int64_t val_credit
, val_debit
;
3399 for (i
= 0; i
< MAX_SFI_CLASS_ID
; i
++){
3402 * checking with prev_lentry != entry ensures adjacent classes
3403 * which share the same ledger do not add wait times twice.
3404 * Note: Use ledger() call to get data for each individual sfi class.
3406 if (prev_lentry
!= task_ledgers
.sfi_wait_times
[i
] &&
3407 KERN_SUCCESS
== ledger_get_entries(task
->ledger
,
3408 task_ledgers
.sfi_wait_times
[i
], &val_credit
, &val_debit
)) {
3409 total_sfi_ledger_val
+= val_credit
;
3411 prev_lentry
= task_ledgers
.sfi_wait_times
[i
];
3414 #endif /* CONFIG_SCHED_SFI */
3415 wait_state_info
->total_wait_sfi_state_time
= total_sfi_ledger_val
;
3416 *task_info_count
= TASK_WAIT_STATE_INFO_COUNT
;
3420 case TASK_VM_INFO_PURGEABLE_ACCOUNT
:
3422 #if DEVELOPMENT || DEBUG
3423 pvm_account_info_t acnt_info
;
3425 if (*task_info_count
< PVM_ACCOUNT_INFO_COUNT
) {
3426 error
= KERN_INVALID_ARGUMENT
;
3430 if (task_info_out
== NULL
) {
3431 error
= KERN_INVALID_ARGUMENT
;
3435 acnt_info
= (pvm_account_info_t
) task_info_out
;
3437 error
= vm_purgeable_account(task
, acnt_info
);
3439 *task_info_count
= PVM_ACCOUNT_INFO_COUNT
;
3442 #else /* DEVELOPMENT || DEBUG */
3443 error
= KERN_NOT_SUPPORTED
;
3445 #endif /* DEVELOPMENT || DEBUG */
3447 case TASK_FLAGS_INFO
:
3449 task_flags_info_t flags_info
;
3451 if (*task_info_count
< TASK_FLAGS_INFO_COUNT
) {
3452 error
= KERN_INVALID_ARGUMENT
;
3456 flags_info
= (task_flags_info_t
)task_info_out
;
3458 /* only publish the 64-bit flag of the task */
3459 flags_info
->flags
= task
->t_flags
& TF_64B_ADDR
;
3461 *task_info_count
= TASK_FLAGS_INFO_COUNT
;
3465 case TASK_DEBUG_INFO_INTERNAL
:
3467 #if DEVELOPMENT || DEBUG
3468 task_debug_info_internal_t dbg_info
;
3469 if (*task_info_count
< TASK_DEBUG_INFO_INTERNAL_COUNT
) {
3470 error
= KERN_NOT_SUPPORTED
;
3474 if (task_info_out
== NULL
) {
3475 error
= KERN_INVALID_ARGUMENT
;
3478 dbg_info
= (task_debug_info_internal_t
) task_info_out
;
3479 dbg_info
->ipc_space_size
= 0;
3480 if (task
->itk_space
){
3481 dbg_info
->ipc_space_size
= task
->itk_space
->is_table_size
;
3484 error
= KERN_SUCCESS
;
3485 *task_info_count
= TASK_DEBUG_INFO_INTERNAL_COUNT
;
3487 #else /* DEVELOPMENT || DEBUG */
3488 error
= KERN_NOT_SUPPORTED
;
3490 #endif /* DEVELOPMENT || DEBUG */
3493 error
= KERN_INVALID_ARGUMENT
;
3503 * Returns power stats for the task.
3504 * Note: Called with task locked.
3507 task_power_info_locked(
3509 task_power_info_t info
,
3510 gpu_energy_data_t ginfo
)
3513 ledger_amount_t tmp
;
3515 task_lock_assert_owned(task
);
3517 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
3518 (ledger_amount_t
*)&info
->task_interrupt_wakeups
, &tmp
);
3519 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
3520 (ledger_amount_t
*)&info
->task_platform_idle_wakeups
, &tmp
);
3522 info
->task_timer_wakeups_bin_1
= task
->task_timer_wakeups_bin_1
;
3523 info
->task_timer_wakeups_bin_2
= task
->task_timer_wakeups_bin_2
;
3525 info
->total_user
= task
->total_user_time
;
3526 info
->total_system
= task
->total_system_time
;
3529 ginfo
->task_gpu_utilisation
= task
->task_gpu_ns
;
3532 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3536 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3540 thread_lock(thread
);
3542 info
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
3543 info
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
3545 tval
= timer_grab(&thread
->user_timer
);
3546 info
->total_user
+= tval
;
3548 tval
= timer_grab(&thread
->system_timer
);
3549 if (thread
->precise_user_kernel_time
) {
3550 info
->total_system
+= tval
;
3552 /* system_timer may represent either sys or user */
3553 info
->total_user
+= tval
;
3557 ginfo
->task_gpu_utilisation
+= ml_gpu_stat(thread
);
3559 thread_unlock(thread
);
3565 * task_gpu_utilisation
3567 * Returns the total gpu time used by the all the threads of the task
3568 * (both dead and alive)
3571 task_gpu_utilisation(
3574 uint64_t gpu_time
= 0;
3578 gpu_time
+= task
->task_gpu_ns
;
3580 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3583 thread_lock(thread
);
3584 gpu_time
+= ml_gpu_stat(thread
);
3585 thread_unlock(thread
);
3596 task_purgable_info_t
*stats
)
3598 if (task
== TASK_NULL
|| stats
== NULL
)
3599 return KERN_INVALID_ARGUMENT
;
3600 /* Take task reference */
3601 task_reference(task
);
3602 vm_purgeable_stats((vm_purgeable_info_t
)stats
, task
);
3603 /* Drop task reference */
3604 task_deallocate(task
);
3605 return KERN_SUCCESS
;
3616 /* assert(task == current_task()); */ /* bogus assert 4803227 4807483 */
3620 task
->vtimers
|= which
;
3624 case TASK_VTIMER_USER
:
3625 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3627 thread_lock(thread
);
3628 if (thread
->precise_user_kernel_time
)
3629 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
3631 thread
->vtimer_user_save
= timer_grab(&thread
->system_timer
);
3632 thread_unlock(thread
);
3637 case TASK_VTIMER_PROF
:
3638 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3640 thread_lock(thread
);
3641 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
3642 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
3643 thread_unlock(thread
);
3648 case TASK_VTIMER_RLIM
:
3649 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3651 thread_lock(thread
);
3652 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
3653 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
3654 thread_unlock(thread
);
3668 assert(task
== current_task());
3672 task
->vtimers
&= ~which
;
3682 uint32_t *microsecs
)
3684 thread_t thread
= current_thread();
3689 assert(task
== current_task());
3691 assert(task
->vtimers
& which
);
3697 case TASK_VTIMER_USER
:
3698 if (thread
->precise_user_kernel_time
) {
3699 tdelt
= (uint32_t)timer_delta(&thread
->user_timer
,
3700 &thread
->vtimer_user_save
);
3702 tdelt
= (uint32_t)timer_delta(&thread
->system_timer
,
3703 &thread
->vtimer_user_save
);
3705 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
3708 case TASK_VTIMER_PROF
:
3709 tsum
= timer_grab(&thread
->user_timer
);
3710 tsum
+= timer_grab(&thread
->system_timer
);
3711 tdelt
= (uint32_t)(tsum
- thread
->vtimer_prof_save
);
3712 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
3713 /* if the time delta is smaller than a usec, ignore */
3714 if (*microsecs
!= 0)
3715 thread
->vtimer_prof_save
= tsum
;
3718 case TASK_VTIMER_RLIM
:
3719 tsum
= timer_grab(&thread
->user_timer
);
3720 tsum
+= timer_grab(&thread
->system_timer
);
3721 tdelt
= (uint32_t)(tsum
- thread
->vtimer_rlim_save
);
3722 thread
->vtimer_rlim_save
= tsum
;
3723 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
3732 * Change the assigned processor set for the task
3736 __unused task_t task
,
3737 __unused processor_set_t new_pset
,
3738 __unused boolean_t assign_threads
)
3740 return(KERN_FAILURE
);
3744 * task_assign_default:
3746 * Version of task_assign to assign to default processor set.
3749 task_assign_default(
3751 boolean_t assign_threads
)
3753 return (task_assign(task
, &pset0
, assign_threads
));
3757 * task_get_assignment
3759 * Return name of processor set that task is assigned to.
3762 task_get_assignment(
3764 processor_set_t
*pset
)
3767 return(KERN_FAILURE
);
3771 return (KERN_SUCCESS
);
3775 get_task_dispatchqueue_offset(
3778 return task
->dispatchqueue_offset
;
3784 * Set scheduling policy and parameters, both base and limit, for
3785 * the given task. Policy must be a policy which is enabled for the
3786 * processor set. Change contained threads if requested.
3790 __unused task_t task
,
3791 __unused policy_t policy_id
,
3792 __unused policy_base_t base
,
3793 __unused mach_msg_type_number_t count
,
3794 __unused boolean_t set_limit
,
3795 __unused boolean_t change
)
3797 return(KERN_FAILURE
);
3803 * Set scheduling policy and parameters, both base and limit, for
3804 * the given task. Policy can be any policy implemented by the
3805 * processor set, whether enabled or not. Change contained threads
3810 __unused task_t task
,
3811 __unused processor_set_t pset
,
3812 __unused policy_t policy_id
,
3813 __unused policy_base_t base
,
3814 __unused mach_msg_type_number_t base_count
,
3815 __unused policy_limit_t limit
,
3816 __unused mach_msg_type_number_t limit_count
,
3817 __unused boolean_t change
)
3819 return(KERN_FAILURE
);
3824 __unused task_t task
,
3825 __unused vm_offset_t pc
,
3826 __unused vm_offset_t endpc
)
3828 return KERN_FAILURE
;
3832 task_synchronizer_destroy_all(task_t task
)
3834 semaphore_t semaphore
;
3837 * Destroy owned semaphores
3840 while (!queue_empty(&task
->semaphore_list
)) {
3841 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
3842 (void) semaphore_destroy_internal(task
, semaphore
);
3847 * Install default (machine-dependent) initial thread state
3848 * on the task. Subsequent thread creation will have this initial
3849 * state set on the thread by machine_thread_inherit_taskwide().
3850 * Flavors and structures are exactly the same as those to thread_set_state()
3856 thread_state_t state
,
3857 mach_msg_type_number_t state_count
)
3861 if (task
== TASK_NULL
) {
3862 return (KERN_INVALID_ARGUMENT
);
3867 if (!task
->active
) {
3869 return (KERN_FAILURE
);
3872 ret
= machine_task_set_state(task
, flavor
, state
, state_count
);
3879 * Examine the default (machine-dependent) initial thread state
3880 * on the task, as set by task_set_state(). Flavors and structures
3881 * are exactly the same as those passed to thread_get_state().
3887 thread_state_t state
,
3888 mach_msg_type_number_t
*state_count
)
3892 if (task
== TASK_NULL
) {
3893 return (KERN_INVALID_ARGUMENT
);
3898 if (!task
->active
) {
3900 return (KERN_FAILURE
);
3903 ret
= machine_task_get_state(task
, flavor
, state
, state_count
);
3910 #define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
3912 void __attribute__((noinline
))
3913 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
)
3915 task_t task
= current_task();
3917 const char *procname
= "unknown";
3918 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
3921 pid
= proc_selfpid();
3925 * Cannot have ReportCrash analyzing
3926 * a suspended initproc.
3931 if (task
->bsd_info
!= NULL
)
3932 procname
= proc_name_address(current_task()->bsd_info
);
3935 if (hwm_user_cores
) {
3937 uint64_t starttime
, end
;
3938 clock_sec_t secs
= 0;
3939 uint32_t microsecs
= 0;
3941 starttime
= mach_absolute_time();
3943 * Trigger a coredump of this process. Don't proceed unless we know we won't
3944 * be filling up the disk; and ignore the core size resource limit for this
3947 if ((error
= coredump(current_task()->bsd_info
, HWM_USERCORE_MINSPACE
, COREDUMP_IGNORE_ULIMIT
)) != 0) {
3948 printf("couldn't take coredump of %s[%d]: %d\n", procname
, pid
, error
);
3951 * coredump() leaves the task suspended.
3953 task_resume_internal(current_task());
3955 end
= mach_absolute_time();
3956 absolutetime_to_microtime(end
- starttime
, &secs
, µsecs
);
3957 printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
3958 proc_name_address(current_task()->bsd_info
), pid
, (int)secs
, microsecs
);
3961 if (disable_exc_resource
) {
3962 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
3963 "supressed by a boot-arg.\n", procname
, pid
, max_footprint_mb
);
3968 * A task that has triggered an EXC_RESOURCE, should not be
3969 * jetsammed when the device is under memory pressure. Here
3970 * we set the P_MEMSTAT_TERMINATED flag so that the process
3971 * will be skipped if the memorystatus_thread wakes up.
3973 proc_memstat_terminated(current_task()->bsd_info
, TRUE
);
3975 printf("process %s[%d] crossed memory high watermark (%d MB); sending "
3976 "EXC_RESOURCE.\n", procname
, pid
, max_footprint_mb
);
3978 code
[0] = code
[1] = 0;
3979 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_MEMORY
);
3980 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_HIGH_WATERMARK
);
3981 EXC_RESOURCE_HWM_ENCODE_LIMIT(code
[0], max_footprint_mb
);
3984 * Use the _internal_ variant so that no user-space
3985 * process can resume our task from under us.
3987 task_suspend_internal(task
);
3988 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
3989 task_resume_internal(task
);
3992 * After the EXC_RESOURCE has been handled, we must clear the
3993 * P_MEMSTAT_TERMINATED flag so that the process can again be
3994 * considered for jetsam if the memorystatus_thread wakes up.
3996 proc_memstat_terminated(current_task()->bsd_info
, FALSE
); /* clear the flag */
4000 * Callback invoked when a task exceeds its physical footprint limit.
4003 task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
4005 ledger_amount_t max_footprint
, max_footprint_mb
;
4006 ledger_amount_t footprint_after_purge
;
4009 if (warning
== LEDGER_WARNING_DIPPED_BELOW
) {
4011 * Task memory limits only provide a warning on the way up.
4016 task
= current_task();
4018 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &max_footprint
);
4019 max_footprint_mb
= max_footprint
>> 20;
4022 * Try and purge all "volatile" memory in that task first.
4024 (void) task_purge_volatile_memory(task
);
4025 /* are we still over the limit ? */
4026 ledger_get_balance(task
->ledger
,
4027 task_ledgers
.phys_footprint
,
4028 &footprint_after_purge
);
4030 footprint_after_purge
<= max_footprint
) ||
4032 footprint_after_purge
<= ((max_footprint
*
4033 PHYS_FOOTPRINT_WARNING_LEVEL
) / 100))) {
4034 /* all better now */
4035 ledger_reset_callback_state(task
->ledger
,
4036 task_ledgers
.phys_footprint
);
4039 /* still over the limit after purging... */
4042 * If this an actual violation (not a warning),
4043 * generate a non-fatal high watermark EXC_RESOURCE.
4045 if ((warning
== 0) && (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
)) {
4046 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND((int)max_footprint_mb
);
4049 memorystatus_on_ledger_footprint_exceeded((warning
== LEDGER_WARNING_ROSE_ABOVE
) ? TRUE
: FALSE
,
4050 (int)max_footprint_mb
);
4053 extern int proc_check_footprint_priv(void);
4056 task_set_phys_footprint_limit(
4061 kern_return_t error
;
4063 if ((error
= proc_check_footprint_priv())) {
4064 return (KERN_NO_ACCESS
);
4067 return task_set_phys_footprint_limit_internal(task
, new_limit_mb
, old_limit_mb
, FALSE
);
4071 task_convert_phys_footprint_limit(
4073 int *converted_limit_mb
)
4075 if (limit_mb
== -1) {
4079 if (max_task_footprint
!= 0) {
4080 *converted_limit_mb
= (int)(max_task_footprint
/ 1024 / 1024); /* bytes to MB */
4082 *converted_limit_mb
= (int)(LEDGER_LIMIT_INFINITY
>> 20);
4085 /* nothing to convert */
4086 *converted_limit_mb
= limit_mb
;
4088 return (KERN_SUCCESS
);
4093 task_set_phys_footprint_limit_internal(
4097 boolean_t trigger_exception
)
4099 ledger_amount_t old
;
4101 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &old
);
4105 * Check that limit >> 20 will not give an "unexpected" 32-bit
4106 * result. There are, however, implicit assumptions that -1 mb limit
4107 * equates to LEDGER_LIMIT_INFINITY.
4109 assert(((old
& 0xFFF0000000000000LL
) == 0) || (old
== LEDGER_LIMIT_INFINITY
));
4110 *old_limit_mb
= (int)(old
>> 20);
4113 if (new_limit_mb
== -1) {
4115 * Caller wishes to remove the limit.
4117 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
4118 max_task_footprint
? max_task_footprint
: LEDGER_LIMIT_INFINITY
,
4119 max_task_footprint
? PHYS_FOOTPRINT_WARNING_LEVEL
: 0);
4120 return (KERN_SUCCESS
);
4123 #ifdef CONFIG_NOMONITORS
4124 return (KERN_SUCCESS
);
4125 #endif /* CONFIG_NOMONITORS */
4129 if (trigger_exception
) {
4130 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
4132 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
4135 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
4136 (ledger_amount_t
)new_limit_mb
<< 20, PHYS_FOOTPRINT_WARNING_LEVEL
);
4138 if (task
== current_task()) {
4139 ledger_check_new_balance(task
->ledger
, task_ledgers
.phys_footprint
);
4144 return (KERN_SUCCESS
);
4148 task_get_phys_footprint_limit(
4152 ledger_amount_t limit
;
4154 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &limit
);
4156 * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
4157 * result. There are, however, implicit assumptions that -1 mb limit
4158 * equates to LEDGER_LIMIT_INFINITY.
4160 assert(((limit
& 0xFFF0000000000000LL
) == 0) || (limit
== LEDGER_LIMIT_INFINITY
));
4161 *limit_mb
= (int)(limit
>> 20);
4163 return (KERN_SUCCESS
);
4165 #else /* CONFIG_JETSAM */
4167 task_set_phys_footprint_limit(
4168 __unused task_t task
,
4169 __unused
int new_limit_mb
,
4170 __unused
int *old_limit_mb
)
4172 return (KERN_FAILURE
);
4176 task_get_phys_footprint_limit(
4177 __unused task_t task
,
4178 __unused
int *limit_mb
)
4180 return (KERN_FAILURE
);
4182 #endif /* CONFIG_JETSAM */
4185 * We need to export some functions to other components that
4186 * are currently implemented in macros within the osfmk
4187 * component. Just export them as functions of the same name.
4189 boolean_t
is_kerneltask(task_t t
)
4191 if (t
== kernel_task
)
4198 check_for_tasksuspend(task_t task
)
4201 if (task
== TASK_NULL
)
4204 return (task
->suspend_count
> 0);
4208 task_t
current_task(void);
4209 task_t
current_task(void)
4211 return (current_task_fast());
4214 #undef task_reference
4215 void task_reference(task_t task
);
4220 if (task
!= TASK_NULL
)
4221 task_reference_internal(task
);
4224 /* defined in bsd/kern/kern_prot.c */
4225 extern int get_audit_token_pid(audit_token_t
*audit_token
);
4227 int task_pid(task_t task
)
4230 return get_audit_token_pid(&task
->audit_token
);
4236 * This routine is called always with task lock held.
4237 * And it returns a thread handle without reference as the caller
4238 * operates on it under the task lock held.
4241 task_findtid(task_t task
, uint64_t tid
)
4243 thread_t thread
= THREAD_NULL
;
4245 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4246 if (thread
->thread_id
== tid
)
4249 return(THREAD_NULL
);
4253 * Control the CPU usage monitor for a task.
4256 task_cpu_usage_monitor_ctl(task_t task
, uint32_t *flags
)
4258 int error
= KERN_SUCCESS
;
4260 if (*flags
& CPUMON_MAKE_FATAL
) {
4261 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_CPUMON
;
4263 error
= KERN_INVALID_ARGUMENT
;
4270 * Control the wakeups monitor for a task.
4273 task_wakeups_monitor_ctl(task_t task
, uint32_t *flags
, int32_t *rate_hz
)
4275 ledger_t ledger
= task
->ledger
;
4278 if (*flags
& WAKEMON_GET_PARAMS
) {
4279 ledger_amount_t limit
;
4282 ledger_get_limit(ledger
, task_ledgers
.interrupt_wakeups
, &limit
);
4283 ledger_get_period(ledger
, task_ledgers
.interrupt_wakeups
, &period
);
4285 if (limit
!= LEDGER_LIMIT_INFINITY
) {
4287 * An active limit means the wakeups monitor is enabled.
4289 *rate_hz
= (int32_t)(limit
/ (int64_t)(period
/ NSEC_PER_SEC
));
4290 *flags
= WAKEMON_ENABLE
;
4291 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
4292 *flags
|= WAKEMON_MAKE_FATAL
;
4295 *flags
= WAKEMON_DISABLE
;
4300 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
4303 return KERN_SUCCESS
;
4306 if (*flags
& WAKEMON_ENABLE
) {
4307 if (*flags
& WAKEMON_SET_DEFAULTS
) {
4308 *rate_hz
= task_wakeups_monitor_rate
;
4311 #ifndef CONFIG_NOMONITORS
4312 if (*flags
& WAKEMON_MAKE_FATAL
) {
4313 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
4315 #endif /* CONFIG_NOMONITORS */
4319 return KERN_INVALID_ARGUMENT
;
4322 #ifndef CONFIG_NOMONITORS
4323 ledger_set_limit(ledger
, task_ledgers
.interrupt_wakeups
, *rate_hz
* task_wakeups_monitor_interval
,
4324 task_wakeups_monitor_ustackshots_trigger_pct
);
4325 ledger_set_period(ledger
, task_ledgers
.interrupt_wakeups
, task_wakeups_monitor_interval
* NSEC_PER_SEC
);
4326 ledger_enable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
4327 #endif /* CONFIG_NOMONITORS */
4328 } else if (*flags
& WAKEMON_DISABLE
) {
4330 * Caller wishes to disable wakeups monitor on the task.
4332 * Disable telemetry if it was triggered by the wakeups monitor, and
4333 * remove the limit & callback on the wakeups ledger entry.
4335 #if CONFIG_TELEMETRY
4336 telemetry_task_ctl_locked(current_task(), TF_WAKEMON_WARNING
, 0);
4338 ledger_disable_refill(ledger
, task_ledgers
.interrupt_wakeups
);
4339 ledger_disable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
4343 return KERN_SUCCESS
;
4347 task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
4349 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
4350 #if CONFIG_TELEMETRY
4352 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
4353 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
4355 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 1);
4360 #if CONFIG_TELEMETRY
4362 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
4363 * exceeded the limit, turn telemetry off for the task.
4365 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 0);
4369 THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS__SENDING_EXC_RESOURCE();
4373 void __attribute__((noinline
))
4374 THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS__SENDING_EXC_RESOURCE(void)
4376 task_t task
= current_task();
4378 const char *procname
= "unknown";
4379 uint64_t observed_wakeups_rate
;
4380 uint64_t permitted_wakeups_rate
;
4381 uint64_t observation_interval
;
4382 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
4383 struct ledger_entry_info lei
;
4386 pid
= proc_selfpid();
4387 if (task
->bsd_info
!= NULL
)
4388 procname
= proc_name_address(current_task()->bsd_info
);
4391 ledger_get_entry_info(task
->ledger
, task_ledgers
.interrupt_wakeups
, &lei
);
4394 * Disable the exception notification so we don't overwhelm
4395 * the listener with an endless stream of redundant exceptions.
4397 uint32_t flags
= WAKEMON_DISABLE
;
4398 task_wakeups_monitor_ctl(task
, &flags
, NULL
);
4400 observed_wakeups_rate
= (lei
.lei_balance
* (int64_t)NSEC_PER_SEC
) / lei
.lei_last_refill
;
4401 permitted_wakeups_rate
= lei
.lei_limit
/ task_wakeups_monitor_interval
;
4402 observation_interval
= lei
.lei_refill_period
/ NSEC_PER_SEC
;
4404 if (disable_exc_resource
) {
4405 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
4406 "supressed by a boot-arg\n", procname
, pid
);
4410 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
4411 "supressed due to audio playback\n", procname
, pid
);
4414 printf("process %s[%d] caught causing excessive wakeups. Observed wakeups rate "
4415 "(per sec): %lld; Maximum permitted wakeups rate (per sec): %lld; Observation "
4416 "period: %lld seconds; Task lifetime number of wakeups: %lld\n",
4417 procname
, pid
, observed_wakeups_rate
, permitted_wakeups_rate
,
4418 observation_interval
, lei
.lei_credit
);
4420 code
[0] = code
[1] = 0;
4421 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_WAKEUPS
);
4422 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_WAKEUPS_MONITOR
);
4423 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code
[0], task_wakeups_monitor_rate
);
4424 EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code
[0], observation_interval
);
4425 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code
[1], lei
.lei_balance
* (int64_t)NSEC_PER_SEC
/ lei
.lei_last_refill
);
4426 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
4428 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
4429 task_terminate_internal(task
);
4434 task_purge_volatile_memory(
4438 int num_object_purged
;
4440 if (task
== TASK_NULL
)
4441 return KERN_INVALID_TASK
;
4445 if (!task
->active
) {
4447 return KERN_INVALID_TASK
;
4450 if (map
== VM_MAP_NULL
) {
4452 return KERN_INVALID_TASK
;
4454 vm_map_reference(task
->map
);
4458 num_object_purged
= vm_map_purge(map
);
4459 vm_map_deallocate(map
);
4461 return KERN_SUCCESS
;
4464 /* Placeholders for the task set/get voucher interfaces */
4466 task_get_mach_voucher(
4468 mach_voucher_selector_t __unused which
,
4469 ipc_voucher_t
*voucher
)
4471 if (TASK_NULL
== task
)
4472 return KERN_INVALID_TASK
;
4475 return KERN_SUCCESS
;
4479 task_set_mach_voucher(
4481 ipc_voucher_t __unused voucher
)
4483 if (TASK_NULL
== task
)
4484 return KERN_INVALID_TASK
;
4486 return KERN_SUCCESS
;
4490 task_swap_mach_voucher(
4492 ipc_voucher_t new_voucher
,
4493 ipc_voucher_t
*in_out_old_voucher
)
4495 if (TASK_NULL
== task
)
4496 return KERN_INVALID_TASK
;
4498 *in_out_old_voucher
= new_voucher
;
4499 return KERN_SUCCESS
;
4502 void task_set_gpu_denied(task_t task
, boolean_t denied
)
4507 task
->t_flags
|= TF_GPU_DENIED
;
4509 task
->t_flags
&= ~TF_GPU_DENIED
;
4515 boolean_t
task_is_gpu_denied(task_t task
)
4517 /* We don't need the lock to read this flag */
4518 return (task
->t_flags
& TF_GPU_DENIED
) ? TRUE
: FALSE
;