2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
58 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub,
61 * Task management primitives implementation.
64 * Copyright (c) 1993 The University of Utah and
65 * the Computer Systems Laboratory (CSL). All rights reserved.
67 * Permission to use, copy, modify and distribute this software and its
68 * documentation is hereby granted, provided that both the copyright
69 * notice and this permission notice appear in all copies of the
70 * software, derivative works or modified versions, and any portions
71 * thereof, and that both notices appear in supporting documentation.
73 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
74 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
75 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
77 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
78 * improvements that they make and grant CSL redistribution rights.
82 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
83 * support for mandatory and extensible security protections. This notice
84 * is included in support of clause 2.2 (b) of the Apple Public License,
86 * Copyright (c) 2005 SPARTA, Inc.
89 #include <mach/mach_types.h>
90 #include <mach/boolean.h>
91 #include <mach/host_priv.h>
92 #include <mach/machine/vm_types.h>
93 #include <mach/vm_param.h>
94 #include <mach/mach_vm.h>
95 #include <mach/semaphore.h>
96 #include <mach/task_info.h>
97 #include <mach/task_special_ports.h>
100 #include <ipc/ipc_importance.h>
101 #include <ipc/ipc_types.h>
102 #include <ipc/ipc_space.h>
103 #include <ipc/ipc_entry.h>
104 #include <ipc/ipc_hash.h>
106 #include <kern/kern_types.h>
107 #include <kern/mach_param.h>
108 #include <kern/misc_protos.h>
109 #include <kern/task.h>
110 #include <kern/thread.h>
111 #include <kern/coalition.h>
112 #include <kern/zalloc.h>
113 #include <kern/kalloc.h>
114 #include <kern/kern_cdata.h>
115 #include <kern/processor.h>
116 #include <kern/sched_prim.h> /* for thread_wakeup */
117 #include <kern/ipc_tt.h>
118 #include <kern/host.h>
119 #include <kern/clock.h>
120 #include <kern/timer.h>
121 #include <kern/assert.h>
122 #include <kern/sync_lock.h>
123 #include <kern/affinity.h>
124 #include <kern/exc_resource.h>
125 #include <kern/machine.h>
126 #include <kern/policy_internal.h>
128 #include <corpses/task_corpse.h>
130 #include <kern/telemetry.h>
134 #include <vm/vm_map.h>
135 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
136 #include <vm/vm_pageout.h>
137 #include <vm/vm_protos.h>
138 #include <vm/vm_purgeable_internal.h>
140 #include <sys/resource.h>
141 #include <sys/signalvar.h> /* for coredump */
144 * Exported interfaces
147 #include <mach/task_server.h>
148 #include <mach/mach_host_server.h>
149 #include <mach/host_security_server.h>
150 #include <mach/mach_port_server.h>
152 #include <vm/vm_shared_region.h>
154 #include <libkern/OSDebug.h>
155 #include <libkern/OSAtomic.h>
158 #include <atm/atm_internal.h>
161 #include <kern/sfi.h> /* picks up ledger.h */
164 #include <security/mac_mach_internal.h>
168 extern int kpc_force_all_ctrs(task_t
, int);
173 lck_attr_t task_lck_attr
;
174 lck_grp_t task_lck_grp
;
175 lck_grp_attr_t task_lck_grp_attr
;
177 extern int exc_via_corpse_forking
;
178 extern int unify_corpse_blob_alloc
;
179 extern int corpse_for_fatal_memkill
;
181 /* Flag set by core audio when audio is playing. Used to stifle EXC_RESOURCE generation when active. */
182 int audio_active
= 0;
184 zinfo_usage_store_t tasks_tkm_private
;
185 zinfo_usage_store_t tasks_tkm_shared
;
187 /* A container to accumulate statistics for expired tasks */
188 expired_task_statistics_t dead_task_statistics
;
189 lck_spin_t dead_task_statistics_lock
;
191 ledger_template_t task_ledger_template
= NULL
;
193 struct _task_ledger_indices task_ledgers
__attribute__((used
)) =
194 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
195 { 0 /* initialized at runtime */},
202 /* System sleep state */
203 boolean_t tasks_suspend_state
;
206 void init_task_ledgers(void);
207 void task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
208 void task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
209 void task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
);
210 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void);
211 void __attribute__((noinline
)) PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
);
212 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
);
214 kern_return_t
task_suspend_internal(task_t
);
215 kern_return_t
task_resume_internal(task_t
);
216 static kern_return_t
task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
);
217 int proc_list_uptrs(void *p
, uint64_t *udata_buffer
, int size
);
219 extern kern_return_t
iokit_task_terminate(task_t task
);
221 extern kern_return_t
exception_deliver(thread_t
, exception_type_t
, mach_exception_data_t
, mach_msg_type_number_t
, struct exception_action
*, lck_mtx_t
*);
222 extern void bsd_copythreadname(void *dst_uth
, void *src_uth
);
223 extern kern_return_t
thread_resume(thread_t thread
);
225 // Warn tasks when they hit 80% of their memory limit.
226 #define PHYS_FOOTPRINT_WARNING_LEVEL 80
228 #define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT 150 /* wakeups per second */
229 #define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL 300 /* in seconds. */
232 * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
234 * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
235 * stacktraces, aka micro-stackshots)
237 #define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER 70
239 int task_wakeups_monitor_interval
; /* In seconds. Time period over which wakeups rate is observed */
240 int task_wakeups_monitor_rate
; /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
242 int task_wakeups_monitor_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
244 int disable_exc_resource
; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
246 ledger_amount_t max_task_footprint
= 0; /* Per-task limit on physical memory consumption in bytes */
247 int max_task_footprint_warning_level
= 0; /* Per-task limit warning percentage */
248 int max_task_footprint_mb
= 0; /* Per-task limit on physical memory consumption in megabytes */
250 /* I/O Monitor Limits */
251 #define IOMON_DEFAULT_LIMIT (20480ull) /* MB of logical/physical I/O */
252 #define IOMON_DEFAULT_INTERVAL (86400ull) /* in seconds */
254 uint64_t task_iomon_limit_mb
; /* Per-task I/O monitor limit in MBs */
255 uint64_t task_iomon_interval_secs
; /* Per-task I/O monitor interval in secs */
257 #define IO_TELEMETRY_DEFAULT_LIMIT (10ll * 1024ll * 1024ll)
258 int64_t io_telemetry_limit
; /* Threshold to take a microstackshot (0 indicated I/O telemetry is turned off) */
259 int64_t global_logical_writes_count
= 0; /* Global count for logical writes */
260 static boolean_t
global_update_logical_writes(int64_t);
263 int pmap_ledgers_panic
= 1;
264 #endif /* MACH_ASSERT */
266 int task_max
= CONFIG_TASK_MAX
; /* Max number of tasks */
269 int hwm_user_cores
= 0; /* high watermark violations generate user core files */
273 extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
274 extern int proc_pid(struct proc
*p
);
275 extern int proc_selfpid(void);
276 extern char *proc_name_address(struct proc
*p
);
277 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
279 #if CONFIG_MEMORYSTATUS
280 extern void proc_memstat_terminated(struct proc
* p
, boolean_t set
);
281 extern boolean_t
memorystatus_turnoff_exception_and_get_fatalness(boolean_t warning
, const int max_footprint_mb
);
282 extern void memorystatus_on_ledger_footprint_exceeded(int warning
, boolean_t is_fatal
);
283 #endif /* CONFIG_MEMORYSTATUS */
285 #endif /* MACH_BSD */
289 static void task_hold_locked(task_t task
);
290 static void task_wait_locked(task_t task
, boolean_t until_not_runnable
);
291 static void task_release_locked(task_t task
);
293 static void task_synchronizer_destroy_all(task_t task
);
296 task_backing_store_privileged(
300 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
311 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
313 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
318 if (task_has_64BitAddr(task
))
320 task_set_64BitAddr(task
);
322 if ( !task_has_64BitAddr(task
))
324 task_clear_64BitAddr(task
);
326 /* FIXME: On x86, the thread save state flavor can diverge from the
327 * task's 64-bit feature flag due to the 32-bit/64-bit register save
328 * state dichotomy. Since we can be pre-empted in this interval,
329 * certain routines may observe the thread as being in an inconsistent
330 * state with respect to its task's 64-bitness.
333 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
334 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
335 thread_mtx_lock(thread
);
336 machine_thread_switch_addrmode(thread
);
337 thread_mtx_unlock(thread
);
339 if (thread
== current_thread()) {
342 spl_t spl
= splsched();
344 * This call tell that the current thread changed it's 32bitness.
345 * Other thread were no more on core when 32bitness was changed,
346 * but current_thread() is on core and the previous call to
347 * machine_thread_going_on_core() gave 32bitness which is now wrong.
349 * This is needed for bring-up, a different callback should be used
353 urgency
= thread_get_urgency(thread
, &arg1
, &arg2
);
354 machine_thread_going_on_core(thread
, urgency
, 0);
355 thread_unlock(thread
);
359 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm64__) */
367 task_set_dyld_info(task_t task
, mach_vm_address_t addr
, mach_vm_size_t size
)
370 task
->all_image_info_addr
= addr
;
371 task
->all_image_info_size
= size
;
376 task_atm_reset(__unused task_t task
) {
379 if (task
->atm_context
!= NULL
) {
380 atm_task_descriptor_destroy(task
->atm_context
);
381 task
->atm_context
= NULL
;
388 task_bank_reset(__unused task_t task
) {
391 if (task
->bank_context
!= NULL
) {
392 bank_task_destroy(task
);
399 * NOTE: This should only be called when the P_LINTRANSIT
400 * flag is set (the proc_trans lock is held) on the
401 * proc associated with the task.
404 task_bank_init(__unused task_t task
) {
407 if (task
->bank_context
!= NULL
) {
408 panic("Task bank init called with non null bank context for task: %p and bank_context: %p", task
, task
->bank_context
);
410 bank_task_initialize(task
);
416 task_set_did_exec_flag(task_t task
)
418 task
->t_procflags
|= TPF_DID_EXEC
;
422 task_clear_exec_copy_flag(task_t task
)
424 task
->t_procflags
&= ~TPF_EXEC_COPY
;
428 * This wait event is t_procflags instead of t_flags because t_flags is volatile
430 * TODO: store the flags in the same place as the event
431 * rdar://problem/28501994
434 task_get_return_wait_event(task_t task
)
436 return (event_t
)&task
->t_procflags
;
440 task_clear_return_wait(task_t task
)
444 task
->t_flags
&= ~TF_LRETURNWAIT
;
446 if (task
->t_flags
& TF_LRETURNWAITER
) {
447 thread_wakeup(task_get_return_wait_event(task
));
448 task
->t_flags
&= ~TF_LRETURNWAITER
;
455 task_wait_to_return(void)
459 task
= current_task();
462 if (task
->t_flags
& TF_LRETURNWAIT
) {
464 task
->t_flags
|= TF_LRETURNWAITER
;
465 assert_wait(task_get_return_wait_event(task
), THREAD_UNINT
);
468 thread_block(THREAD_CONTINUE_NULL
);
471 } while (task
->t_flags
& TF_LRETURNWAIT
);
476 thread_bootstrap_return();
480 task_is_exec_copy(task_t task
)
482 return task_is_exec_copy_internal(task
);
486 task_did_exec(task_t task
)
488 return task_did_exec_internal(task
);
492 task_is_active(task_t task
)
497 #if TASK_REFERENCE_LEAK_DEBUG
498 #include <kern/btlog.h>
500 static btlog_t
*task_ref_btlog
;
501 #define TASK_REF_OP_INCR 0x1
502 #define TASK_REF_OP_DECR 0x2
504 #define TASK_REF_NUM_RECORDS 100000
505 #define TASK_REF_BTDEPTH 7
508 task_reference_internal(task_t task
)
510 void * bt
[TASK_REF_BTDEPTH
];
513 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
515 (void)hw_atomic_add(&(task
)->ref_count
, 1);
516 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_INCR
,
521 task_deallocate_internal(task_t task
)
523 void * bt
[TASK_REF_BTDEPTH
];
526 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
528 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_DECR
,
530 return hw_atomic_sub(&(task
)->ref_count
, 1);
533 #endif /* TASK_REFERENCE_LEAK_DEBUG */
539 lck_grp_attr_setdefault(&task_lck_grp_attr
);
540 lck_grp_init(&task_lck_grp
, "task", &task_lck_grp_attr
);
541 lck_attr_setdefault(&task_lck_attr
);
542 lck_mtx_init(&tasks_threads_lock
, &task_lck_grp
, &task_lck_attr
);
543 lck_mtx_init(&tasks_corpse_lock
, &task_lck_grp
, &task_lck_attr
);
547 task_max
* sizeof(struct task
),
548 TASK_CHUNK
* sizeof(struct task
),
551 zone_change(task_zone
, Z_NOENCRYPT
, TRUE
);
555 * Configure per-task memory limit.
556 * The boot-arg is interpreted as Megabytes,
557 * and takes precedence over the device tree.
558 * Setting the boot-arg to 0 disables task limits.
560 if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb
,
561 sizeof (max_task_footprint_mb
))) {
563 * No limit was found in boot-args, so go look in the device tree.
565 if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb
,
566 sizeof(max_task_footprint_mb
))) {
568 * No limit was found in device tree.
570 max_task_footprint_mb
= 0;
574 if (max_task_footprint_mb
!= 0) {
575 #if CONFIG_MEMORYSTATUS
576 if (max_task_footprint_mb
< 50) {
577 printf("Warning: max_task_pmem %d below minimum.\n",
578 max_task_footprint_mb
);
579 max_task_footprint_mb
= 50;
581 printf("Limiting task physical memory footprint to %d MB\n",
582 max_task_footprint_mb
);
584 max_task_footprint
= (ledger_amount_t
)max_task_footprint_mb
* 1024 * 1024; // Convert MB to bytes
587 * Configure the per-task memory limit warning level.
588 * This is computed as a percentage.
590 max_task_footprint_warning_level
= 0;
592 if (max_mem
< 0x40000000) {
594 * On devices with < 1GB of memory:
595 * -- set warnings to 50MB below the per-task limit.
597 if (max_task_footprint_mb
> 50) {
598 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 50) * 100) / max_task_footprint_mb
;
602 * On devices with >= 1GB of memory:
603 * -- set warnings to 100MB below the per-task limit.
605 if (max_task_footprint_mb
> 100) {
606 max_task_footprint_warning_level
= ((max_task_footprint_mb
- 100) * 100) / max_task_footprint_mb
;
611 * Never allow warning level to land below the default.
613 if (max_task_footprint_warning_level
< PHYS_FOOTPRINT_WARNING_LEVEL
) {
614 max_task_footprint_warning_level
= PHYS_FOOTPRINT_WARNING_LEVEL
;
617 printf("Limiting task physical memory warning to %d%%\n", max_task_footprint_warning_level
);
620 printf("Warning: max_task_pmem specified, but jetsam not configured; ignoring.\n");
621 #endif /* CONFIG_MEMORYSTATUS */
625 PE_parse_boot_argn("pmap_ledgers_panic", &pmap_ledgers_panic
,
626 sizeof (pmap_ledgers_panic
));
627 #endif /* MACH_ASSERT */
630 if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores
,
631 sizeof (hwm_user_cores
))) {
636 proc_init_cpumon_params();
638 if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate
, sizeof (task_wakeups_monitor_rate
))) {
639 task_wakeups_monitor_rate
= TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT
;
642 if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval
, sizeof (task_wakeups_monitor_interval
))) {
643 task_wakeups_monitor_interval
= TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL
;
646 if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct
,
647 sizeof (task_wakeups_monitor_ustackshots_trigger_pct
))) {
648 task_wakeups_monitor_ustackshots_trigger_pct
= TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER
;
651 if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource
,
652 sizeof (disable_exc_resource
))) {
653 disable_exc_resource
= 0;
656 if (!PE_parse_boot_argn("task_iomon_limit_mb", &task_iomon_limit_mb
, sizeof (task_iomon_limit_mb
))) {
657 task_iomon_limit_mb
= IOMON_DEFAULT_LIMIT
;
660 if (!PE_parse_boot_argn("task_iomon_interval_secs", &task_iomon_interval_secs
, sizeof (task_iomon_interval_secs
))) {
661 task_iomon_interval_secs
= IOMON_DEFAULT_INTERVAL
;
664 if (!PE_parse_boot_argn("io_telemetry_limit", &io_telemetry_limit
, sizeof (io_telemetry_limit
))) {
665 io_telemetry_limit
= IO_TELEMETRY_DEFAULT_LIMIT
;
669 * If we have coalitions, coalition_init() will call init_task_ledgers() as it
670 * sets up the ledgers for the default coalition. If we don't have coalitions,
671 * then we have to call it now.
673 #if CONFIG_COALITIONS
674 assert(task_ledger_template
);
675 #else /* CONFIG_COALITIONS */
677 #endif /* CONFIG_COALITIONS */
679 #if TASK_REFERENCE_LEAK_DEBUG
680 task_ref_btlog
= btlog_create(TASK_REF_NUM_RECORDS
, TASK_REF_BTDEPTH
, TRUE
/* caller_will_remove_entries_for_element? */);
681 assert(task_ref_btlog
);
685 * Create the kernel task as the first task.
688 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, TRUE
, TF_NONE
, TPF_NONE
, &kernel_task
) != KERN_SUCCESS
)
690 if (task_create_internal(TASK_NULL
, NULL
, FALSE
, FALSE
, TF_NONE
, TPF_NONE
, &kernel_task
) != KERN_SUCCESS
)
692 panic("task_init\n");
694 vm_map_deallocate(kernel_task
->map
);
695 kernel_task
->map
= kernel_map
;
696 lck_spin_init(&dead_task_statistics_lock
, &task_lck_grp
, &task_lck_attr
);
700 * Create a task running in the kernel address space. It may
701 * have its own map of size mem_size and may have ipc privileges.
705 __unused task_t parent_task
,
706 __unused vm_offset_t map_base
,
707 __unused vm_size_t map_size
,
708 __unused task_t
*child_task
)
710 return (KERN_INVALID_ARGUMENT
);
716 __unused ledger_port_array_t ledger_ports
,
717 __unused mach_msg_type_number_t num_ledger_ports
,
718 __unused boolean_t inherit_memory
,
719 __unused task_t
*child_task
) /* OUT */
721 if (parent_task
== TASK_NULL
)
722 return(KERN_INVALID_ARGUMENT
);
725 * No longer supported: too many calls assume that a task has a valid
728 return(KERN_FAILURE
);
732 host_security_create_task_token(
733 host_security_t host_security
,
735 __unused security_token_t sec_token
,
736 __unused audit_token_t audit_token
,
737 __unused host_priv_t host_priv
,
738 __unused ledger_port_array_t ledger_ports
,
739 __unused mach_msg_type_number_t num_ledger_ports
,
740 __unused boolean_t inherit_memory
,
741 __unused task_t
*child_task
) /* OUT */
743 if (parent_task
== TASK_NULL
)
744 return(KERN_INVALID_ARGUMENT
);
746 if (host_security
== HOST_NULL
)
747 return(KERN_INVALID_SECURITY
);
750 * No longer supported.
752 return(KERN_FAILURE
);
760 * Physical footprint: This is the sum of:
761 * + (internal - alternate_accounting)
762 * + (internal_compressed - alternate_accounting_compressed)
764 * + purgeable_nonvolatile
765 * + purgeable_nonvolatile_compressed
769 * The task's anonymous memory, which on iOS is always resident.
771 * internal_compressed
772 * Amount of this task's internal memory which is held by the compressor.
773 * Such memory is no longer actually resident for the task [i.e., resident in its pmap],
774 * and could be either decompressed back into memory, or paged out to storage, depending
775 * on our implementation.
778 * IOKit mappings: The total size of all IOKit mappings in this task, regardless of
779 clean/dirty or internal/external state].
781 * alternate_accounting
782 * The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
783 * are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
787 init_task_ledgers(void)
791 assert(task_ledger_template
== NULL
);
792 assert(kernel_task
== TASK_NULL
);
795 PE_parse_boot_argn("pmap_ledgers_panic", &pmap_ledgers_panic
,
796 sizeof (pmap_ledgers_panic
));
797 #endif /* MACH_ASSERT */
799 if ((t
= ledger_template_create("Per-task ledger")) == NULL
)
800 panic("couldn't create task ledger template");
802 task_ledgers
.cpu_time
= ledger_entry_add(t
, "cpu_time", "sched", "ns");
803 task_ledgers
.tkm_private
= ledger_entry_add(t
, "tkm_private",
805 task_ledgers
.tkm_shared
= ledger_entry_add(t
, "tkm_shared", "physmem",
807 task_ledgers
.phys_mem
= ledger_entry_add(t
, "phys_mem", "physmem",
809 task_ledgers
.wired_mem
= ledger_entry_add(t
, "wired_mem", "physmem",
811 task_ledgers
.internal
= ledger_entry_add(t
, "internal", "physmem",
813 task_ledgers
.iokit_mapped
= ledger_entry_add(t
, "iokit_mapped", "mappings",
815 task_ledgers
.alternate_accounting
= ledger_entry_add(t
, "alternate_accounting", "physmem",
817 task_ledgers
.alternate_accounting_compressed
= ledger_entry_add(t
, "alternate_accounting_compressed", "physmem",
819 task_ledgers
.page_table
= ledger_entry_add(t
, "page_table", "physmem",
821 task_ledgers
.phys_footprint
= ledger_entry_add(t
, "phys_footprint", "physmem",
823 task_ledgers
.internal_compressed
= ledger_entry_add(t
, "internal_compressed", "physmem",
825 task_ledgers
.purgeable_volatile
= ledger_entry_add(t
, "purgeable_volatile", "physmem", "bytes");
826 task_ledgers
.purgeable_nonvolatile
= ledger_entry_add(t
, "purgeable_nonvolatile", "physmem", "bytes");
827 task_ledgers
.purgeable_volatile_compressed
= ledger_entry_add(t
, "purgeable_volatile_compress", "physmem", "bytes");
828 task_ledgers
.purgeable_nonvolatile_compressed
= ledger_entry_add(t
, "purgeable_nonvolatile_compress", "physmem", "bytes");
829 task_ledgers
.platform_idle_wakeups
= ledger_entry_add(t
, "platform_idle_wakeups", "power",
831 task_ledgers
.interrupt_wakeups
= ledger_entry_add(t
, "interrupt_wakeups", "power",
835 sfi_class_id_t class_id
, ledger_alias
;
836 for (class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
837 task_ledgers
.sfi_wait_times
[class_id
] = -1;
840 /* don't account for UNSPECIFIED */
841 for (class_id
= SFI_CLASS_UNSPECIFIED
+ 1; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
842 ledger_alias
= sfi_get_ledger_alias_for_class(class_id
);
843 if (ledger_alias
!= SFI_CLASS_UNSPECIFIED
) {
844 /* Check to see if alias has been registered yet */
845 if (task_ledgers
.sfi_wait_times
[ledger_alias
] != -1) {
846 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
];
848 /* Otherwise, initialize it first */
849 task_ledgers
.sfi_wait_times
[class_id
] = task_ledgers
.sfi_wait_times
[ledger_alias
] = sfi_ledger_entry_add(t
, ledger_alias
);
852 task_ledgers
.sfi_wait_times
[class_id
] = sfi_ledger_entry_add(t
, class_id
);
855 if (task_ledgers
.sfi_wait_times
[class_id
] < 0) {
856 panic("couldn't create entries for task ledger template for SFI class 0x%x", class_id
);
860 assert(task_ledgers
.sfi_wait_times
[MAX_SFI_CLASS_ID
-1] != -1);
861 #endif /* CONFIG_SCHED_SFI */
864 task_ledgers
.cpu_time_billed_to_me
= ledger_entry_add(t
, "cpu_time_billed_to_me", "sched", "ns");
865 task_ledgers
.cpu_time_billed_to_others
= ledger_entry_add(t
, "cpu_time_billed_to_others", "sched", "ns");
867 task_ledgers
.physical_writes
= ledger_entry_add(t
, "physical_writes", "res", "bytes");
868 task_ledgers
.logical_writes
= ledger_entry_add(t
, "logical_writes", "res", "bytes");
870 if ((task_ledgers
.cpu_time
< 0) ||
871 (task_ledgers
.tkm_private
< 0) ||
872 (task_ledgers
.tkm_shared
< 0) ||
873 (task_ledgers
.phys_mem
< 0) ||
874 (task_ledgers
.wired_mem
< 0) ||
875 (task_ledgers
.internal
< 0) ||
876 (task_ledgers
.iokit_mapped
< 0) ||
877 (task_ledgers
.alternate_accounting
< 0) ||
878 (task_ledgers
.alternate_accounting_compressed
< 0) ||
879 (task_ledgers
.page_table
< 0) ||
880 (task_ledgers
.phys_footprint
< 0) ||
881 (task_ledgers
.internal_compressed
< 0) ||
882 (task_ledgers
.purgeable_volatile
< 0) ||
883 (task_ledgers
.purgeable_nonvolatile
< 0) ||
884 (task_ledgers
.purgeable_volatile_compressed
< 0) ||
885 (task_ledgers
.purgeable_nonvolatile_compressed
< 0) ||
886 (task_ledgers
.platform_idle_wakeups
< 0) ||
887 (task_ledgers
.interrupt_wakeups
< 0) ||
889 (task_ledgers
.cpu_time_billed_to_me
< 0) || (task_ledgers
.cpu_time_billed_to_others
< 0) ||
891 (task_ledgers
.physical_writes
< 0) ||
892 (task_ledgers
.logical_writes
< 0)
894 panic("couldn't create entries for task ledger template");
897 ledger_track_credit_only(t
, task_ledgers
.phys_footprint
);
898 ledger_track_credit_only(t
, task_ledgers
.internal
);
899 ledger_track_credit_only(t
, task_ledgers
.internal_compressed
);
900 ledger_track_credit_only(t
, task_ledgers
.iokit_mapped
);
901 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting
);
902 ledger_track_credit_only(t
, task_ledgers
.alternate_accounting_compressed
);
903 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile
);
904 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile
);
905 ledger_track_credit_only(t
, task_ledgers
.purgeable_volatile_compressed
);
906 ledger_track_credit_only(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
908 ledger_track_maximum(t
, task_ledgers
.phys_footprint
, 60);
910 if (pmap_ledgers_panic
) {
911 ledger_panic_on_negative(t
, task_ledgers
.phys_footprint
);
912 ledger_panic_on_negative(t
, task_ledgers
.page_table
);
913 ledger_panic_on_negative(t
, task_ledgers
.internal
);
914 ledger_panic_on_negative(t
, task_ledgers
.internal_compressed
);
915 ledger_panic_on_negative(t
, task_ledgers
.iokit_mapped
);
916 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting
);
917 ledger_panic_on_negative(t
, task_ledgers
.alternate_accounting_compressed
);
918 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile
);
919 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile
);
920 ledger_panic_on_negative(t
, task_ledgers
.purgeable_volatile_compressed
);
921 ledger_panic_on_negative(t
, task_ledgers
.purgeable_nonvolatile_compressed
);
923 #endif /* MACH_ASSERT */
925 #if CONFIG_MEMORYSTATUS
926 ledger_set_callback(t
, task_ledgers
.phys_footprint
, task_footprint_exceeded
, NULL
, NULL
);
927 #endif /* CONFIG_MEMORYSTATUS */
929 ledger_set_callback(t
, task_ledgers
.interrupt_wakeups
,
930 task_wakeups_rate_exceeded
, NULL
, NULL
);
931 ledger_set_callback(t
, task_ledgers
.physical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_PHYSICAL_WRITES
, NULL
);
932 ledger_set_callback(t
, task_ledgers
.logical_writes
, task_io_rate_exceeded
, (void *)FLAVOR_IO_LOGICAL_WRITES
, NULL
);
933 task_ledger_template
= t
;
937 task_create_internal(
939 coalition_t
*parent_coalitions __unused
,
940 boolean_t inherit_memory
,
943 uint32_t t_procflags
,
944 task_t
*child_task
) /* OUT */
947 vm_shared_region_t shared_region
;
948 ledger_t ledger
= NULL
;
950 new_task
= (task_t
) zalloc(task_zone
);
952 if (new_task
== TASK_NULL
)
953 return(KERN_RESOURCE_SHORTAGE
);
955 /* one ref for just being alive; one for our caller */
956 new_task
->ref_count
= 2;
958 /* allocate with active entries */
959 assert(task_ledger_template
!= NULL
);
960 if ((ledger
= ledger_instantiate(task_ledger_template
,
961 LEDGER_CREATE_ACTIVE_ENTRIES
)) == NULL
) {
962 zfree(task_zone
, new_task
);
963 return(KERN_RESOURCE_SHORTAGE
);
966 new_task
->ledger
= ledger
;
968 #if defined(CONFIG_SCHED_MULTIQ)
969 new_task
->sched_group
= sched_group_create();
972 /* if inherit_memory is true, parent_task MUST not be NULL */
973 if (!(t_flags
& TF_CORPSE_FORK
) && inherit_memory
)
974 new_task
->map
= vm_map_fork(ledger
, parent_task
->map
, 0);
976 new_task
->map
= vm_map_create(pmap_create(ledger
, 0, is_64bit
),
977 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
978 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
980 /* Inherit memlock limit from parent */
982 vm_map_set_user_wire_limit(new_task
->map
, (vm_size_t
)parent_task
->map
->user_wire_limit
);
984 lck_mtx_init(&new_task
->lock
, &task_lck_grp
, &task_lck_attr
);
985 queue_init(&new_task
->threads
);
986 new_task
->suspend_count
= 0;
987 new_task
->thread_count
= 0;
988 new_task
->active_thread_count
= 0;
989 new_task
->user_stop_count
= 0;
990 new_task
->legacy_stop_count
= 0;
991 new_task
->active
= TRUE
;
992 new_task
->halting
= FALSE
;
993 new_task
->user_data
= NULL
;
994 new_task
->priv_flags
= 0;
995 new_task
->t_flags
= t_flags
;
996 new_task
->t_procflags
= t_procflags
;
997 new_task
->importance
= 0;
998 new_task
->corpse_info_kernel
= NULL
;
999 new_task
->exec_token
= 0;
1002 new_task
->atm_context
= NULL
;
1005 new_task
->bank_context
= NULL
;
1009 new_task
->bsd_info
= NULL
;
1010 new_task
->corpse_info
= NULL
;
1011 #endif /* MACH_BSD */
1014 new_task
->crash_label
= NULL
;
1017 #if CONFIG_MEMORYSTATUS
1018 if (max_task_footprint
!= 0) {
1019 ledger_set_limit(ledger
, task_ledgers
.phys_footprint
, max_task_footprint
, PHYS_FOOTPRINT_WARNING_LEVEL
);
1021 #endif /* CONFIG_MEMORYSTATUS */
1023 if (task_wakeups_monitor_rate
!= 0) {
1024 uint32_t flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
1025 int32_t rate
; // Ignored because of WAKEMON_SET_DEFAULTS
1026 task_wakeups_monitor_ctl(new_task
, &flags
, &rate
);
1029 #if CONFIG_IO_ACCOUNTING
1030 uint32_t flags
= IOMON_ENABLE
;
1031 task_io_monitor_ctl(new_task
, &flags
);
1032 #endif /* CONFIG_IO_ACCOUNTING */
1034 #if defined(__i386__) || defined(__x86_64__)
1035 new_task
->i386_ldt
= 0;
1038 new_task
->task_debug
= NULL
;
1040 #if DEVELOPMENT || DEBUG
1041 new_task
->task_unnested
= FALSE
;
1042 new_task
->task_disconnected_count
= 0;
1044 queue_init(&new_task
->semaphore_list
);
1045 new_task
->semaphores_owned
= 0;
1047 ipc_task_init(new_task
, parent_task
);
1049 new_task
->vtimers
= 0;
1051 new_task
->shared_region
= NULL
;
1053 new_task
->affinity_space
= NULL
;
1055 new_task
->pidsuspended
= FALSE
;
1056 new_task
->frozen
= FALSE
;
1057 new_task
->changing_freeze_state
= FALSE
;
1058 new_task
->rusage_cpu_flags
= 0;
1059 new_task
->rusage_cpu_percentage
= 0;
1060 new_task
->rusage_cpu_interval
= 0;
1061 new_task
->rusage_cpu_deadline
= 0;
1062 new_task
->rusage_cpu_callt
= NULL
;
1064 new_task
->suspends_outstanding
= 0;
1068 new_task
->hv_task_target
= NULL
;
1069 #endif /* HYPERVISOR */
1072 new_task
->mem_notify_reserved
= 0;
1073 #if IMPORTANCE_INHERITANCE
1074 new_task
->task_imp_base
= NULL
;
1075 #endif /* IMPORTANCE_INHERITANCE */
1077 #if defined(__x86_64__)
1078 new_task
->uexc_range_start
= new_task
->uexc_range_size
= new_task
->uexc_handler
= 0;
1081 new_task
->requested_policy
= default_task_requested_policy
;
1082 new_task
->effective_policy
= default_task_effective_policy
;
1084 if (parent_task
!= TASK_NULL
) {
1085 new_task
->sec_token
= parent_task
->sec_token
;
1086 new_task
->audit_token
= parent_task
->audit_token
;
1088 /* inherit the parent's shared region */
1089 shared_region
= vm_shared_region_get(parent_task
);
1090 vm_shared_region_set(new_task
, shared_region
);
1092 if(task_has_64BitAddr(parent_task
))
1093 task_set_64BitAddr(new_task
);
1094 new_task
->all_image_info_addr
= parent_task
->all_image_info_addr
;
1095 new_task
->all_image_info_size
= parent_task
->all_image_info_size
;
1097 #if defined(__i386__) || defined(__x86_64__)
1098 if (inherit_memory
&& parent_task
->i386_ldt
)
1099 new_task
->i386_ldt
= user_ldt_copy(parent_task
->i386_ldt
);
1101 if (inherit_memory
&& parent_task
->affinity_space
)
1102 task_affinity_create(parent_task
, new_task
);
1104 new_task
->pset_hint
= parent_task
->pset_hint
= task_choose_pset(parent_task
);
1106 #if IMPORTANCE_INHERITANCE
1107 ipc_importance_task_t new_task_imp
= IIT_NULL
;
1108 boolean_t inherit_receive
= TRUE
;
1110 if (task_is_marked_importance_donor(parent_task
)) {
1111 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
1112 assert(IIT_NULL
!= new_task_imp
);
1113 ipc_importance_task_mark_donor(new_task_imp
, TRUE
);
1116 if (inherit_receive
) {
1117 if (task_is_marked_importance_receiver(parent_task
)) {
1118 if (IIT_NULL
== new_task_imp
)
1119 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
1120 assert(IIT_NULL
!= new_task_imp
);
1121 ipc_importance_task_mark_receiver(new_task_imp
, TRUE
);
1123 if (task_is_marked_importance_denap_receiver(parent_task
)) {
1124 if (IIT_NULL
== new_task_imp
)
1125 new_task_imp
= ipc_importance_for_task(new_task
, FALSE
);
1126 assert(IIT_NULL
!= new_task_imp
);
1127 ipc_importance_task_mark_denap_receiver(new_task_imp
, TRUE
);
1131 if (IIT_NULL
!= new_task_imp
) {
1132 assert(new_task
->task_imp_base
== new_task_imp
);
1133 ipc_importance_task_release(new_task_imp
);
1135 #endif /* IMPORTANCE_INHERITANCE */
1137 new_task
->priority
= BASEPRI_DEFAULT
;
1138 new_task
->max_priority
= MAXPRI_USER
;
1140 task_policy_create(new_task
, parent_task
);
1142 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
1143 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
1146 task_set_64BitAddr(new_task
);
1148 new_task
->all_image_info_addr
= (mach_vm_address_t
)0;
1149 new_task
->all_image_info_size
= (mach_vm_size_t
)0;
1151 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
1153 if (kernel_task
== TASK_NULL
) {
1154 new_task
->priority
= BASEPRI_KERNEL
;
1155 new_task
->max_priority
= MAXPRI_KERNEL
;
1157 new_task
->priority
= BASEPRI_DEFAULT
;
1158 new_task
->max_priority
= MAXPRI_USER
;
1162 bzero(new_task
->coalition
, sizeof(new_task
->coalition
));
1163 for (int i
= 0; i
< COALITION_NUM_TYPES
; i
++)
1164 queue_chain_init(new_task
->task_coalition
[i
]);
1166 /* Allocate I/O Statistics */
1167 new_task
->task_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
1168 assert(new_task
->task_io_stats
!= NULL
);
1169 bzero(new_task
->task_io_stats
, sizeof(struct io_stat_info
));
1171 bzero(&(new_task
->cpu_time_qos_stats
), sizeof(struct _cpu_time_qos_stats
));
1173 bzero(&new_task
->extmod_statistics
, sizeof(new_task
->extmod_statistics
));
1175 /* Copy resource acc. info from Parent for Corpe Forked task. */
1176 if (parent_task
!= NULL
&& (t_flags
& TF_CORPSE_FORK
)) {
1177 task_rollup_accounting_info(new_task
, parent_task
);
1179 /* Initialize to zero for standard fork/spawn case */
1180 new_task
->total_user_time
= 0;
1181 new_task
->total_system_time
= 0;
1182 new_task
->faults
= 0;
1183 new_task
->pageins
= 0;
1184 new_task
->cow_faults
= 0;
1185 new_task
->messages_sent
= 0;
1186 new_task
->messages_received
= 0;
1187 new_task
->syscalls_mach
= 0;
1188 new_task
->syscalls_unix
= 0;
1189 new_task
->c_switch
= 0;
1190 new_task
->p_switch
= 0;
1191 new_task
->ps_switch
= 0;
1192 new_task
->low_mem_notified_warn
= 0;
1193 new_task
->low_mem_notified_critical
= 0;
1194 new_task
->purged_memory_warn
= 0;
1195 new_task
->purged_memory_critical
= 0;
1196 new_task
->low_mem_privileged_listener
= 0;
1197 new_task
->task_timer_wakeups_bin_1
= 0;
1198 new_task
->task_timer_wakeups_bin_2
= 0;
1199 new_task
->task_gpu_ns
= 0;
1200 new_task
->task_immediate_writes
= 0;
1201 new_task
->task_deferred_writes
= 0;
1202 new_task
->task_invalidated_writes
= 0;
1203 new_task
->task_metadata_writes
= 0;
1204 new_task
->task_energy
= 0;
1208 #if CONFIG_COALITIONS
1209 if (!(t_flags
& TF_CORPSE_FORK
)) {
1210 /* TODO: there is no graceful failure path here... */
1211 if (parent_coalitions
&& parent_coalitions
[COALITION_TYPE_RESOURCE
]) {
1212 coalitions_adopt_task(parent_coalitions
, new_task
);
1213 } else if (parent_task
&& parent_task
->coalition
[COALITION_TYPE_RESOURCE
]) {
1215 * all tasks at least have a resource coalition, so
1216 * if the parent has one then inherit all coalitions
1217 * the parent is a part of
1219 coalitions_adopt_task(parent_task
->coalition
, new_task
);
1221 /* TODO: assert that new_task will be PID 1 (launchd) */
1222 coalitions_adopt_init_task(new_task
);
1225 coalitions_adopt_corpse_task(new_task
);
1228 if (new_task
->coalition
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
1229 panic("created task is not a member of a resource coalition");
1231 #endif /* CONFIG_COALITIONS */
1233 new_task
->dispatchqueue_offset
= 0;
1234 if (parent_task
!= NULL
) {
1235 new_task
->dispatchqueue_offset
= parent_task
->dispatchqueue_offset
;
1238 if (vm_backing_store_low
&& parent_task
!= NULL
)
1239 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
1241 new_task
->task_volatile_objects
= 0;
1242 new_task
->task_nonvolatile_objects
= 0;
1243 new_task
->task_purgeable_disowning
= FALSE
;
1244 new_task
->task_purgeable_disowned
= FALSE
;
1246 #if CONFIG_SECLUDED_MEMORY
1247 new_task
->task_can_use_secluded_mem
= FALSE
;
1248 new_task
->task_could_use_secluded_mem
= FALSE
;
1249 new_task
->task_could_also_use_secluded_mem
= FALSE
;
1250 #endif /* CONFIG_SECLUDED_MEMORY */
1252 queue_init(&new_task
->io_user_clients
);
1254 ipc_task_enable(new_task
);
1256 lck_mtx_lock(&tasks_threads_lock
);
1257 queue_enter(&tasks
, new_task
, task_t
, tasks
);
1259 if (tasks_suspend_state
) {
1260 task_suspend_internal(new_task
);
1262 lck_mtx_unlock(&tasks_threads_lock
);
1264 *child_task
= new_task
;
1265 return(KERN_SUCCESS
);
1269 * task_rollup_accounting_info
1271 * Roll up accounting stats. Used to rollup stats
1272 * for exec copy task and corpse fork.
1275 task_rollup_accounting_info(task_t to_task
, task_t from_task
)
1277 assert(from_task
!= to_task
);
1279 to_task
->total_user_time
= from_task
->total_user_time
;
1280 to_task
->total_system_time
= from_task
->total_system_time
;
1281 to_task
->faults
= from_task
->faults
;
1282 to_task
->pageins
= from_task
->pageins
;
1283 to_task
->cow_faults
= from_task
->cow_faults
;
1284 to_task
->messages_sent
= from_task
->messages_sent
;
1285 to_task
->messages_received
= from_task
->messages_received
;
1286 to_task
->syscalls_mach
= from_task
->syscalls_mach
;
1287 to_task
->syscalls_unix
= from_task
->syscalls_unix
;
1288 to_task
->c_switch
= from_task
->c_switch
;
1289 to_task
->p_switch
= from_task
->p_switch
;
1290 to_task
->ps_switch
= from_task
->ps_switch
;
1291 to_task
->extmod_statistics
= from_task
->extmod_statistics
;
1292 to_task
->low_mem_notified_warn
= from_task
->low_mem_notified_warn
;
1293 to_task
->low_mem_notified_critical
= from_task
->low_mem_notified_critical
;
1294 to_task
->purged_memory_warn
= from_task
->purged_memory_warn
;
1295 to_task
->purged_memory_critical
= from_task
->purged_memory_critical
;
1296 to_task
->low_mem_privileged_listener
= from_task
->low_mem_privileged_listener
;
1297 *to_task
->task_io_stats
= *from_task
->task_io_stats
;
1298 to_task
->cpu_time_qos_stats
= from_task
->cpu_time_qos_stats
;
1299 to_task
->task_timer_wakeups_bin_1
= from_task
->task_timer_wakeups_bin_1
;
1300 to_task
->task_timer_wakeups_bin_2
= from_task
->task_timer_wakeups_bin_2
;
1301 to_task
->task_gpu_ns
= from_task
->task_gpu_ns
;
1302 to_task
->task_immediate_writes
= from_task
->task_immediate_writes
;
1303 to_task
->task_deferred_writes
= from_task
->task_deferred_writes
;
1304 to_task
->task_invalidated_writes
= from_task
->task_invalidated_writes
;
1305 to_task
->task_metadata_writes
= from_task
->task_metadata_writes
;
1306 to_task
->task_energy
= from_task
->task_energy
;
1308 /* Skip ledger roll up for memory accounting entries */
1309 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time
);
1310 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.platform_idle_wakeups
);
1311 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.interrupt_wakeups
);
1312 #if CONFIG_SCHED_SFI
1313 for (sfi_class_id_t class_id
= SFI_CLASS_UNSPECIFIED
; class_id
< MAX_SFI_CLASS_ID
; class_id
++) {
1314 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.sfi_wait_times
[class_id
]);
1318 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_me
);
1319 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.cpu_time_billed_to_others
);
1321 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.physical_writes
);
1322 ledger_rollup_entry(to_task
->ledger
, from_task
->ledger
, task_ledgers
.logical_writes
);
1325 int task_dropped_imp_count
= 0;
1330 * Drop a reference on a task.
1336 ledger_amount_t credit
, debit
, interrupt_wakeups
, platform_idle_wakeups
;
1339 if (task
== TASK_NULL
)
1342 refs
= task_deallocate_internal(task
);
1344 #if IMPORTANCE_INHERITANCE
1350 * If last ref potentially comes from the task's importance,
1351 * disconnect it. But more task refs may be added before
1352 * that completes, so wait for the reference to go to zero
1353 * naturually (it may happen on a recursive task_deallocate()
1354 * from the ipc_importance_disconnect_task() call).
1356 if (IIT_NULL
!= task
->task_imp_base
)
1357 ipc_importance_disconnect_task(task
);
1363 #endif /* IMPORTANCE_INHERITANCE */
1365 lck_mtx_lock(&tasks_threads_lock
);
1366 queue_remove(&terminated_tasks
, task
, task_t
, tasks
);
1367 terminated_tasks_count
--;
1368 lck_mtx_unlock(&tasks_threads_lock
);
1371 * remove the reference on atm descriptor
1373 task_atm_reset(task
);
1376 * remove the reference on bank context
1378 task_bank_reset(task
);
1380 if (task
->task_io_stats
)
1381 kfree(task
->task_io_stats
, sizeof(struct io_stat_info
));
1384 * Give the machine dependent code a chance
1385 * to perform cleanup before ripping apart
1388 machine_task_terminate(task
);
1390 ipc_task_terminate(task
);
1392 /* let iokit know */
1393 iokit_task_terminate(task
);
1395 if (task
->affinity_space
)
1396 task_affinity_deallocate(task
);
1399 if (task
->ledger
!= NULL
&&
1400 task
->map
!= NULL
&&
1401 task
->map
->pmap
!= NULL
&&
1402 task
->map
->pmap
->ledger
!= NULL
) {
1403 assert(task
->ledger
== task
->map
->pmap
->ledger
);
1405 #endif /* MACH_ASSERT */
1407 vm_purgeable_disown(task
);
1408 assert(task
->task_purgeable_disowned
);
1409 if (task
->task_volatile_objects
!= 0 ||
1410 task
->task_nonvolatile_objects
!= 0) {
1411 panic("task_deallocate(%p): "
1412 "volatile_objects=%d nonvolatile_objects=%d\n",
1414 task
->task_volatile_objects
,
1415 task
->task_nonvolatile_objects
);
1418 vm_map_deallocate(task
->map
);
1419 is_release(task
->itk_space
);
1421 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
1422 &interrupt_wakeups
, &debit
);
1423 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
1424 &platform_idle_wakeups
, &debit
);
1426 #if defined(CONFIG_SCHED_MULTIQ)
1427 sched_group_destroy(task
->sched_group
);
1430 /* Accumulate statistics for dead tasks */
1431 lck_spin_lock(&dead_task_statistics_lock
);
1432 dead_task_statistics
.total_user_time
+= task
->total_user_time
;
1433 dead_task_statistics
.total_system_time
+= task
->total_system_time
;
1435 dead_task_statistics
.task_interrupt_wakeups
+= interrupt_wakeups
;
1436 dead_task_statistics
.task_platform_idle_wakeups
+= platform_idle_wakeups
;
1438 dead_task_statistics
.task_timer_wakeups_bin_1
+= task
->task_timer_wakeups_bin_1
;
1439 dead_task_statistics
.task_timer_wakeups_bin_2
+= task
->task_timer_wakeups_bin_2
;
1441 lck_spin_unlock(&dead_task_statistics_lock
);
1442 lck_mtx_destroy(&task
->lock
, &task_lck_grp
);
1444 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_private
, &credit
,
1446 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_private
.alloc
);
1447 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_private
.free
);
1449 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_shared
, &credit
,
1451 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_shared
.alloc
);
1452 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_shared
.free
);
1454 ledger_dereference(task
->ledger
);
1456 #if TASK_REFERENCE_LEAK_DEBUG
1457 btlog_remove_entries_for_element(task_ref_btlog
, task
);
1460 #if CONFIG_COALITIONS
1461 task_release_coalitions(task
);
1462 #endif /* CONFIG_COALITIONS */
1464 bzero(task
->coalition
, sizeof(task
->coalition
));
1467 /* clean up collected information since last reference to task is gone */
1468 if (task
->corpse_info
) {
1469 task_crashinfo_destroy(task
->corpse_info
, RELEASE_CORPSE_REF
);
1470 task
->corpse_info
= NULL
;
1473 if (task
->corpse_info_kernel
) {
1474 kfree(task
->corpse_info_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1478 if (task
->crash_label
) {
1479 mac_exc_action_label_task_destroy(task
);
1483 zfree(task_zone
, task
);
1487 * task_name_deallocate:
1489 * Drop a reference on a task name.
1492 task_name_deallocate(
1493 task_name_t task_name
)
1495 return(task_deallocate((task_t
)task_name
));
1499 * task_suspension_token_deallocate:
1501 * Drop a reference on a task suspension token.
1504 task_suspension_token_deallocate(
1505 task_suspension_token_t token
)
1507 return(task_deallocate((task_t
)token
));
1512 * task_collect_crash_info:
1514 * collect crash info from bsd and mach based data
1517 task_collect_crash_info(task_t task
, struct proc
*proc
, int is_corpse_fork
)
1519 kern_return_t kr
= KERN_SUCCESS
;
1521 kcdata_descriptor_t crash_data
= NULL
;
1522 kcdata_descriptor_t crash_data_release
= NULL
;
1523 mach_msg_type_number_t size
= CORPSEINFO_ALLOCATION_SIZE
;
1524 mach_vm_offset_t crash_data_ptr
= 0;
1525 void *crash_data_kernel
= NULL
;
1526 void *crash_data_kernel_release
= NULL
;
1527 int corpse_blob_kernel_alloc
= (is_corpse_fork
|| unify_corpse_blob_alloc
);
1529 if (!corpses_enabled()) {
1530 return KERN_NOT_SUPPORTED
;
1535 assert(is_corpse_fork
|| task
->bsd_info
!= NULL
);
1536 if (task
->corpse_info
== NULL
&& (is_corpse_fork
|| task
->bsd_info
!= NULL
)) {
1538 /* Update the corpse label, used by the exception delivery mac hook */
1539 mac_exc_action_label_task_update(task
, proc
);
1543 if (!corpse_blob_kernel_alloc
) {
1544 /* map crash data memory in task's vm map */
1545 kr
= mach_vm_allocate(task
->map
, &crash_data_ptr
, size
, (VM_MAKE_TAG(VM_MEMORY_CORPSEINFO
) | VM_FLAGS_ANYWHERE
));
1547 crash_data_kernel
= (void *) kalloc(CORPSEINFO_ALLOCATION_SIZE
);
1548 if (crash_data_kernel
== 0)
1549 kr
= KERN_RESOURCE_SHORTAGE
;
1550 bzero(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1551 crash_data_ptr
= (mach_vm_offset_t
) crash_data_kernel
;
1553 if (kr
!= KERN_SUCCESS
)
1556 /* Do not get a corpse ref for corpse fork */
1557 crash_data
= task_crashinfo_alloc_init((mach_vm_address_t
)crash_data_ptr
, size
, is_corpse_fork
? !GET_CORPSE_REF
: GET_CORPSE_REF
, corpse_blob_kernel_alloc
? KCFLAG_USE_MEMCOPY
: KCFLAG_USE_COPYOUT
);
1560 crash_data_release
= task
->corpse_info
;
1561 crash_data_kernel_release
= task
->corpse_info_kernel
;
1562 task
->corpse_info
= crash_data
;
1563 task
->corpse_info_kernel
= crash_data_kernel
;
1568 /* if failed to create corpse info, free the mapping */
1569 if (!corpse_blob_kernel_alloc
) {
1570 if (KERN_SUCCESS
!= mach_vm_deallocate(task
->map
, crash_data_ptr
, size
)) {
1571 printf("mach_vm_deallocate failed to clear corpse_data for pid %d.\n", task_pid(task
));
1574 kfree(crash_data_kernel
, CORPSEINFO_ALLOCATION_SIZE
);
1579 if (crash_data_release
!= NULL
) {
1580 task_crashinfo_destroy(crash_data_release
, is_corpse_fork
? !RELEASE_CORPSE_REF
: RELEASE_CORPSE_REF
);
1582 if (crash_data_kernel_release
!= NULL
) {
1583 kfree(crash_data_kernel_release
, CORPSEINFO_ALLOCATION_SIZE
);
1594 * task_deliver_crash_notification:
1596 * Makes outcall to registered host port for a corpse.
1599 task_deliver_crash_notification(task_t task
, thread_t thread
, mach_exception_data_type_t subcode
)
1601 kcdata_descriptor_t crash_info
= task
->corpse_info
;
1602 thread_t th_iter
= NULL
;
1603 kern_return_t kr
= KERN_SUCCESS
;
1604 wait_interrupt_t wsave
;
1605 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1606 ipc_port_t task_port
, old_notify
;
1608 if (crash_info
== NULL
)
1609 return KERN_FAILURE
;
1612 if (task_is_a_corpse_fork(task
)) {
1613 /* Populate code with EXC_RESOURCE for corpse fork */
1614 code
[0] = EXC_RESOURCE
;
1616 } else if (unify_corpse_blob_alloc
) {
1617 /* Populate code with EXC_CRASH for corpses */
1618 code
[0] = EXC_CRASH
;
1620 /* Update the code[1] if the boot-arg corpse_for_fatal_memkill is set */
1621 if (corpse_for_fatal_memkill
) {
1625 /* Populate code with address and length for EXC_CRASH */
1626 code
[0] = crash_info
->kcd_addr_begin
;
1627 code
[1] = crash_info
->kcd_length
;
1629 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1631 if (th_iter
->corpse_dup
== FALSE
) {
1632 ipc_thread_reset(th_iter
);
1637 /* Arm the no-sender notification for taskport */
1638 task_reference(task
);
1639 task_port
= convert_task_to_port(task
);
1641 assert(ip_active(task_port
));
1642 ipc_port_nsrequest(task_port
, task_port
->ip_mscount
, ipc_port_make_sonce_locked(task_port
), &old_notify
);
1644 assert(IP_NULL
== old_notify
);
1646 wsave
= thread_interrupt_level(THREAD_UNINT
);
1647 kr
= exception_triage_thread(EXC_CORPSE_NOTIFY
, code
, EXCEPTION_CODE_MAX
, thread
);
1648 if (kr
!= KERN_SUCCESS
) {
1649 printf("Failed to send exception EXC_CORPSE_NOTIFY. error code: %d for pid %d\n", kr
, task_pid(task
));
1652 (void)thread_interrupt_level(wsave
);
1655 * Drop the send right on task port, will fire the
1656 * no-sender notification if exception deliver failed.
1658 ipc_port_release_send(task_port
);
1665 * Terminate the specified task. See comments on thread_terminate
1666 * (kern/thread.c) about problems with terminating the "current task."
1673 if (task
== TASK_NULL
)
1674 return (KERN_INVALID_ARGUMENT
);
1677 return (KERN_FAILURE
);
1679 return (task_terminate_internal(task
));
1683 extern int proc_pid(struct proc
*);
1684 extern void proc_name_kdp(task_t t
, char *buf
, int size
);
1685 #endif /* MACH_ASSERT */
1687 #define VM_MAP_PARTIAL_REAP 0x54 /* 0x150 */
1689 __unused
task_partial_reap(task_t task
, __unused
int pid
)
1691 unsigned int reclaimed_resident
= 0;
1692 unsigned int reclaimed_compressed
= 0;
1693 uint64_t task_page_count
;
1695 task_page_count
= (get_task_phys_footprint(task
) / PAGE_SIZE_64
);
1697 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_START
),
1698 pid
, task_page_count
, 0, 0, 0);
1700 vm_map_partial_reap(task
->map
, &reclaimed_resident
, &reclaimed_compressed
);
1702 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_MAP_PARTIAL_REAP
) | DBG_FUNC_END
),
1703 pid
, reclaimed_resident
, reclaimed_compressed
, 0, 0);
1707 task_mark_corpse(task_t task
)
1709 kern_return_t kr
= KERN_SUCCESS
;
1710 thread_t self_thread
;
1712 wait_interrupt_t wsave
;
1714 assert(task
!= kernel_task
);
1715 assert(task
== current_task());
1716 assert(!task_is_a_corpse(task
));
1718 kr
= task_collect_crash_info(task
, (struct proc
*)task
->bsd_info
, FALSE
);
1719 if (kr
!= KERN_SUCCESS
) {
1723 self_thread
= current_thread();
1725 wsave
= thread_interrupt_level(THREAD_UNINT
);
1728 task_set_corpse_pending_report(task
);
1729 task_set_corpse(task
);
1731 kr
= task_start_halt_locked(task
, TRUE
);
1732 assert(kr
== KERN_SUCCESS
);
1734 ipc_task_reset(task
);
1735 /* Remove the naked send right for task port, needed to arm no sender notification */
1736 task_set_special_port(task
, TASK_KERNEL_PORT
, IPC_PORT_NULL
);
1737 ipc_task_enable(task
);
1740 /* terminate the ipc space */
1741 ipc_space_terminate(task
->itk_space
);
1743 /* Add it to global corpse task list */
1744 task_add_to_corpse_task_list(task
);
1746 task_start_halt(task
);
1747 thread_terminate_internal(self_thread
);
1749 (void) thread_interrupt_level(wsave
);
1750 assert(task
->halting
== TRUE
);
1757 * Clears the corpse pending bit on task.
1758 * Removes inspection bit on the threads.
1761 task_clear_corpse(task_t task
)
1763 thread_t th_iter
= NULL
;
1766 queue_iterate(&task
->threads
, th_iter
, thread_t
, task_threads
)
1768 thread_mtx_lock(th_iter
);
1769 th_iter
->inspection
= FALSE
;
1770 thread_mtx_unlock(th_iter
);
1773 thread_terminate_crashed_threads();
1774 /* remove the pending corpse report flag */
1775 task_clear_corpse_pending_report(task
);
1783 * Called whenever the Mach port system detects no-senders on
1784 * the task port of a corpse.
1785 * Each notification that comes in should terminate the task (corpse).
1788 task_port_notify(mach_msg_header_t
*msg
)
1790 mach_no_senders_notification_t
*notification
= (void *)msg
;
1791 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
1794 assert(ip_active(port
));
1795 assert(IKOT_TASK
== ip_kotype(port
));
1796 task
= (task_t
) port
->ip_kobject
;
1798 assert(task_is_a_corpse(task
));
1800 /* Remove the task from global corpse task list */
1801 task_remove_from_corpse_task_list(task
);
1803 task_clear_corpse(task
);
1804 task_terminate_internal(task
);
1808 * task_wait_till_threads_terminate_locked
1810 * Wait till all the threads in the task are terminated.
1811 * Might release the task lock and re-acquire it.
1814 task_wait_till_threads_terminate_locked(task_t task
)
1816 /* wait for all the threads in the task to terminate */
1817 while (task
->active_thread_count
!= 0) {
1818 assert_wait((event_t
)&task
->active_thread_count
, THREAD_UNINT
);
1820 thread_block(THREAD_CONTINUE_NULL
);
1827 * task_duplicate_map_and_threads
1829 * Copy vmmap of source task.
1830 * Copy active threads from source task to destination task.
1831 * Source task would be suspended during the copy.
1834 task_duplicate_map_and_threads(
1838 thread_t
*thread_ret
,
1840 uint64_t **udata_buffer
,
1844 kern_return_t kr
= KERN_SUCCESS
;
1846 thread_t thread
, self
, thread_return
= THREAD_NULL
;
1847 thread_t new_thread
= THREAD_NULL
;
1848 thread_t
*thread_array
;
1849 uint32_t active_thread_count
= 0, array_count
= 0, i
;
1851 uint64_t *buffer
= NULL
;
1853 int est_knotes
= 0, num_knotes
= 0;
1855 self
= current_thread();
1858 * Suspend the task to copy thread state, use the internal
1859 * variant so that no user-space process can resume
1860 * the task from under us
1862 kr
= task_suspend_internal(task
);
1863 if (kr
!= KERN_SUCCESS
) {
1867 if (task
->map
->disable_vmentry_reuse
== TRUE
) {
1869 * Quite likely GuardMalloc (or some debugging tool)
1870 * is being used on this task. And it has gone through
1871 * its limit. Making a corpse will likely encounter
1872 * a lot of VM entries that will need COW.
1876 task_resume_internal(task
);
1877 return KERN_FAILURE
;
1880 /* Setup new task's vmmap, switch from parent task's map to it COW map */
1881 oldmap
= new_task
->map
;
1882 new_task
->map
= vm_map_fork(new_task
->ledger
,
1884 (VM_MAP_FORK_SHARE_IF_INHERIT_NONE
|
1885 VM_MAP_FORK_PRESERVE_PURGEABLE
));
1886 vm_map_deallocate(oldmap
);
1889 vm_map_set_64bit(get_task_map(new_task
));
1891 vm_map_set_32bit(get_task_map(new_task
));
1894 /* Get all the udata pointers from kqueue */
1895 est_knotes
= proc_list_uptrs(p
, NULL
, 0);
1896 if (est_knotes
> 0) {
1897 buf_size
= (est_knotes
+ 32) * sizeof(uint64_t);
1898 buffer
= (uint64_t *) kalloc(buf_size
);
1899 num_knotes
= proc_list_uptrs(p
, buffer
, buf_size
);
1900 if (num_knotes
> est_knotes
+ 32) {
1901 num_knotes
= est_knotes
+ 32;
1905 active_thread_count
= task
->active_thread_count
;
1906 if (active_thread_count
== 0) {
1907 if (buffer
!= NULL
) {
1908 kfree(buffer
, buf_size
);
1910 task_resume_internal(task
);
1911 return KERN_FAILURE
;
1914 thread_array
= (thread_t
*) kalloc(sizeof(thread_t
) * active_thread_count
);
1916 /* Iterate all the threads and drop the task lock before calling thread_create_with_continuation */
1918 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1919 /* Skip inactive threads */
1920 active
= thread
->active
;
1925 if (array_count
>= active_thread_count
) {
1929 thread_array
[array_count
++] = thread
;
1930 thread_reference(thread
);
1934 for (i
= 0; i
< array_count
; i
++) {
1936 kr
= thread_create_with_continuation(new_task
, &new_thread
, (thread_continue_t
)thread_corpse_continue
);
1937 if (kr
!= KERN_SUCCESS
) {
1941 /* Equivalent of current thread in corpse */
1942 if (thread_array
[i
] == self
) {
1943 thread_return
= new_thread
;
1945 /* drop the extra ref returned by thread_create_with_continuation */
1946 thread_deallocate(new_thread
);
1949 kr
= thread_dup2(thread_array
[i
], new_thread
);
1950 if (kr
!= KERN_SUCCESS
) {
1951 thread_mtx_lock(new_thread
);
1952 new_thread
->corpse_dup
= TRUE
;
1953 thread_mtx_unlock(new_thread
);
1957 /* Copy thread name */
1958 bsd_copythreadname(new_thread
->uthread
, thread_array
[i
]->uthread
);
1959 thread_copy_resource_info(new_thread
, thread_array
[i
]);
1962 task_resume_internal(task
);
1964 for (i
= 0; i
< array_count
; i
++) {
1965 thread_deallocate(thread_array
[i
]);
1967 kfree(thread_array
, sizeof(thread_t
) * active_thread_count
);
1969 if (kr
== KERN_SUCCESS
) {
1970 *thread_ret
= thread_return
;
1971 *udata_buffer
= buffer
;
1973 *num_udata
= num_knotes
;
1975 if (thread_return
!= THREAD_NULL
) {
1976 thread_deallocate(thread_return
);
1978 if (buffer
!= NULL
) {
1979 kfree(buffer
, buf_size
);
1986 #if CONFIG_SECLUDED_MEMORY
1987 extern void task_set_can_use_secluded_mem_locked(
1989 boolean_t can_use_secluded_mem
);
1990 #endif /* CONFIG_SECLUDED_MEMORY */
1993 task_terminate_internal(
1996 thread_t thread
, self
;
1998 boolean_t interrupt_save
;
2001 assert(task
!= kernel_task
);
2003 self
= current_thread();
2004 self_task
= self
->task
;
2007 * Get the task locked and make sure that we are not racing
2008 * with someone else trying to terminate us.
2010 if (task
== self_task
)
2013 if (task
< self_task
) {
2015 task_lock(self_task
);
2018 task_lock(self_task
);
2022 #if CONFIG_SECLUDED_MEMORY
2023 if (task
->task_can_use_secluded_mem
) {
2024 task_set_can_use_secluded_mem_locked(task
, FALSE
);
2026 task
->task_could_use_secluded_mem
= FALSE
;
2027 task
->task_could_also_use_secluded_mem
= FALSE
;
2028 #endif /* CONFIG_SECLUDED_MEMORY */
2030 if (!task
->active
) {
2032 * Task is already being terminated.
2033 * Just return an error. If we are dying, this will
2034 * just get us to our AST special handler and that
2035 * will get us to finalize the termination of ourselves.
2038 if (self_task
!= task
)
2039 task_unlock(self_task
);
2041 return (KERN_FAILURE
);
2044 if (task_corpse_pending_report(task
)) {
2046 * Task is marked for reporting as corpse.
2047 * Just return an error. This will
2048 * just get us to our AST special handler and that
2049 * will get us to finish the path to death
2052 if (self_task
!= task
)
2053 task_unlock(self_task
);
2055 return (KERN_FAILURE
);
2058 if (self_task
!= task
)
2059 task_unlock(self_task
);
2062 * Make sure the current thread does not get aborted out of
2063 * the waits inside these operations.
2065 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
2068 * Indicate that we want all the threads to stop executing
2069 * at user space by holding the task (we would have held
2070 * each thread independently in thread_terminate_internal -
2071 * but this way we may be more likely to already find it
2072 * held there). Mark the task inactive, and prevent
2073 * further task operations via the task port.
2075 task_hold_locked(task
);
2076 task
->active
= FALSE
;
2077 ipc_task_disable(task
);
2079 #if CONFIG_TELEMETRY
2081 * Notify telemetry that this task is going away.
2083 telemetry_task_ctl_locked(task
, TF_TELEMETRY
, 0);
2087 * Terminate each thread in the task.
2089 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2090 thread_terminate_internal(thread
);
2094 if (task
->bsd_info
!= NULL
&& !task_is_exec_copy(task
)) {
2095 pid
= proc_pid(task
->bsd_info
);
2097 #endif /* MACH_BSD */
2101 proc_set_task_policy(task
, TASK_POLICY_ATTRIBUTE
,
2102 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
2104 /* Early object reap phase */
2106 // PR-17045188: Revisit implementation
2107 // task_partial_reap(task, pid);
2111 * Destroy all synchronizers owned by the task.
2113 task_synchronizer_destroy_all(task
);
2116 * Destroy the IPC space, leaving just a reference for it.
2118 ipc_space_terminate(task
->itk_space
);
2121 /* if some ledgers go negative on tear-down again... */
2122 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2123 task_ledgers
.phys_footprint
);
2124 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2125 task_ledgers
.internal
);
2126 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2127 task_ledgers
.internal_compressed
);
2128 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2129 task_ledgers
.iokit_mapped
);
2130 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2131 task_ledgers
.alternate_accounting
);
2132 ledger_disable_panic_on_negative(task
->map
->pmap
->ledger
,
2133 task_ledgers
.alternate_accounting_compressed
);
2137 * If the current thread is a member of the task
2138 * being terminated, then the last reference to
2139 * the task will not be dropped until the thread
2140 * is finally reaped. To avoid incurring the
2141 * expense of removing the address space regions
2142 * at reap time, we do it explictly here.
2145 vm_map_lock(task
->map
);
2146 vm_map_disable_hole_optimization(task
->map
);
2147 vm_map_unlock(task
->map
);
2149 vm_map_remove(task
->map
,
2150 task
->map
->min_offset
,
2151 task
->map
->max_offset
,
2152 /* no unnesting on final cleanup: */
2153 VM_MAP_REMOVE_NO_UNNESTING
);
2155 /* release our shared region */
2156 vm_shared_region_set(task
, NULL
);
2161 * Identify the pmap's process, in case the pmap ledgers drift
2162 * and we have to report it.
2165 if (task
->bsd_info
&& !task_is_exec_copy(task
)) {
2166 pid
= proc_pid(task
->bsd_info
);
2167 proc_name_kdp(task
, procname
, sizeof (procname
));
2170 strlcpy(procname
, "<unknown>", sizeof (procname
));
2172 pmap_set_process(task
->map
->pmap
, pid
, procname
);
2173 #endif /* MACH_ASSERT */
2175 lck_mtx_lock(&tasks_threads_lock
);
2176 queue_remove(&tasks
, task
, task_t
, tasks
);
2177 queue_enter(&terminated_tasks
, task
, task_t
, tasks
);
2179 terminated_tasks_count
++;
2180 lck_mtx_unlock(&tasks_threads_lock
);
2183 * We no longer need to guard against being aborted, so restore
2184 * the previous interruptible state.
2186 thread_interrupt_level(interrupt_save
);
2189 /* force the task to release all ctrs */
2190 if (task
->t_chud
& TASK_KPC_FORCED_ALL_CTRS
)
2191 kpc_force_all_ctrs(task
, 0);
2194 #if CONFIG_COALITIONS
2196 * Leave our coalitions. (drop activation but not reference)
2198 coalitions_remove_task(task
);
2202 * Get rid of the task active reference on itself.
2204 task_deallocate(task
);
2206 return (KERN_SUCCESS
);
2210 tasks_system_suspend(boolean_t suspend
)
2214 lck_mtx_lock(&tasks_threads_lock
);
2215 assert(tasks_suspend_state
!= suspend
);
2216 tasks_suspend_state
= suspend
;
2217 queue_iterate(&tasks
, task
, task_t
, tasks
) {
2218 if (task
== kernel_task
) {
2221 suspend
? task_suspend_internal(task
) : task_resume_internal(task
);
2223 lck_mtx_unlock(&tasks_threads_lock
);
2229 * Shut the current task down (except for the current thread) in
2230 * preparation for dramatic changes to the task (probably exec).
2231 * We hold the task and mark all other threads in the task for
2235 task_start_halt(task_t task
)
2237 kern_return_t kr
= KERN_SUCCESS
;
2239 kr
= task_start_halt_locked(task
, FALSE
);
2244 static kern_return_t
2245 task_start_halt_locked(task_t task
, boolean_t should_mark_corpse
)
2247 thread_t thread
, self
;
2248 uint64_t dispatchqueue_offset
;
2250 assert(task
!= kernel_task
);
2252 self
= current_thread();
2254 if (task
!= self
->task
&& !task_is_a_corpse_fork(task
))
2255 return (KERN_INVALID_ARGUMENT
);
2257 if (task
->halting
|| !task
->active
|| !self
->active
) {
2259 * Task or current thread is already being terminated.
2260 * Hurry up and return out of the current kernel context
2261 * so that we run our AST special handler to terminate
2264 return (KERN_FAILURE
);
2267 task
->halting
= TRUE
;
2270 * Mark all the threads to keep them from starting any more
2271 * user-level execution. The thread_terminate_internal code
2272 * would do this on a thread by thread basis anyway, but this
2273 * gives us a better chance of not having to wait there.
2275 task_hold_locked(task
);
2276 dispatchqueue_offset
= get_dispatchqueue_offset_from_proc(task
->bsd_info
);
2279 * Terminate all the other threads in the task.
2281 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
)
2283 if (should_mark_corpse
) {
2284 thread_mtx_lock(thread
);
2285 thread
->inspection
= TRUE
;
2286 thread_mtx_unlock(thread
);
2289 thread_terminate_internal(thread
);
2291 task
->dispatchqueue_offset
= dispatchqueue_offset
;
2293 task_release_locked(task
);
2295 return KERN_SUCCESS
;
2300 * task_complete_halt:
2302 * Complete task halt by waiting for threads to terminate, then clean
2303 * up task resources (VM, port namespace, etc...) and then let the
2304 * current thread go in the (practically empty) task context.
2306 * Note: task->halting flag is not cleared in order to avoid creation
2307 * of new thread in old exec'ed task.
2310 task_complete_halt(task_t task
)
2313 assert(task
->halting
);
2314 assert(task
== current_task());
2317 * Wait for the other threads to get shut down.
2318 * When the last other thread is reaped, we'll be
2321 if (task
->thread_count
> 1) {
2322 assert_wait((event_t
)&task
->halting
, THREAD_UNINT
);
2324 thread_block(THREAD_CONTINUE_NULL
);
2330 * Give the machine dependent code a chance
2331 * to perform cleanup of task-level resources
2332 * associated with the current thread before
2333 * ripping apart the task.
2335 machine_task_terminate(task
);
2338 * Destroy all synchronizers owned by the task.
2340 task_synchronizer_destroy_all(task
);
2343 * Destroy the contents of the IPC space, leaving just
2344 * a reference for it.
2346 ipc_space_clean(task
->itk_space
);
2349 * Clean out the address space, as we are going to be
2350 * getting a new one.
2352 vm_map_remove(task
->map
, task
->map
->min_offset
,
2353 task
->map
->max_offset
,
2354 /* no unnesting on final cleanup: */
2355 VM_MAP_REMOVE_NO_UNNESTING
);
2358 * Kick out any IOKitUser handles to the task. At best they're stale,
2359 * at worst someone is racing a SUID exec.
2361 iokit_task_terminate(task
);
2367 * Suspend execution of the specified task.
2368 * This is a recursive-style suspension of the task, a count of
2369 * suspends is maintained.
2371 * CONDITIONS: the task is locked and active.
2379 assert(task
->active
);
2381 if (task
->suspend_count
++ > 0)
2385 * Iterate through all the threads and hold them.
2387 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2388 thread_mtx_lock(thread
);
2389 thread_hold(thread
);
2390 thread_mtx_unlock(thread
);
2397 * Same as the internal routine above, except that is must lock
2398 * and verify that the task is active. This differs from task_suspend
2399 * in that it places a kernel hold on the task rather than just a
2400 * user-level hold. This keeps users from over resuming and setting
2401 * it running out from under the kernel.
2403 * CONDITIONS: the caller holds a reference on the task
2409 if (task
== TASK_NULL
)
2410 return (KERN_INVALID_ARGUMENT
);
2414 if (!task
->active
) {
2417 return (KERN_FAILURE
);
2420 task_hold_locked(task
);
2423 return (KERN_SUCCESS
);
2429 boolean_t until_not_runnable
)
2431 if (task
== TASK_NULL
)
2432 return (KERN_INVALID_ARGUMENT
);
2436 if (!task
->active
) {
2439 return (KERN_FAILURE
);
2442 task_wait_locked(task
, until_not_runnable
);
2445 return (KERN_SUCCESS
);
2451 * Wait for all threads in task to stop.
2454 * Called with task locked, active, and held.
2459 boolean_t until_not_runnable
)
2461 thread_t thread
, self
;
2463 assert(task
->active
);
2464 assert(task
->suspend_count
> 0);
2466 self
= current_thread();
2469 * Iterate through all the threads and wait for them to
2470 * stop. Do not wait for the current thread if it is within
2473 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2475 thread_wait(thread
, until_not_runnable
);
2480 * task_release_locked:
2482 * Release a kernel hold on a task.
2484 * CONDITIONS: the task is locked and active
2487 task_release_locked(
2492 assert(task
->active
);
2493 assert(task
->suspend_count
> 0);
2495 if (--task
->suspend_count
> 0)
2498 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2499 thread_mtx_lock(thread
);
2500 thread_release(thread
);
2501 thread_mtx_unlock(thread
);
2508 * Same as the internal routine above, except that it must lock
2509 * and verify that the task is active.
2511 * CONDITIONS: The caller holds a reference to the task
2517 if (task
== TASK_NULL
)
2518 return (KERN_INVALID_ARGUMENT
);
2522 if (!task
->active
) {
2525 return (KERN_FAILURE
);
2528 task_release_locked(task
);
2531 return (KERN_SUCCESS
);
2537 thread_act_array_t
*threads_out
,
2538 mach_msg_type_number_t
*count
)
2540 mach_msg_type_number_t actual
;
2541 thread_t
*thread_list
;
2543 vm_size_t size
, size_needed
;
2547 if (task
== TASK_NULL
)
2548 return (KERN_INVALID_ARGUMENT
);
2550 size
= 0; addr
= NULL
;
2554 if (!task
->active
) {
2560 return (KERN_FAILURE
);
2563 actual
= task
->thread_count
;
2565 /* do we have the memory we need? */
2566 size_needed
= actual
* sizeof (mach_port_t
);
2567 if (size_needed
<= size
)
2570 /* unlock the task and allocate more memory */
2576 assert(size_needed
> 0);
2579 addr
= kalloc(size
);
2581 return (KERN_RESOURCE_SHORTAGE
);
2584 /* OK, have memory and the task is locked & active */
2585 thread_list
= (thread_t
*)addr
;
2589 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
2590 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
2591 thread_reference_internal(thread
);
2592 thread_list
[j
++] = thread
;
2595 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
2598 size_needed
= actual
* sizeof (mach_port_t
);
2600 /* can unlock task now that we've got the thread refs */
2604 /* no threads, so return null pointer and deallocate memory */
2606 *threads_out
= NULL
;
2613 /* if we allocated too much, must copy */
2615 if (size_needed
< size
) {
2618 newaddr
= kalloc(size_needed
);
2620 for (i
= 0; i
< actual
; ++i
)
2621 thread_deallocate(thread_list
[i
]);
2623 return (KERN_RESOURCE_SHORTAGE
);
2626 bcopy(addr
, newaddr
, size_needed
);
2628 thread_list
= (thread_t
*)newaddr
;
2631 *threads_out
= thread_list
;
2634 /* do the conversion that Mig should handle */
2636 for (i
= 0; i
< actual
; ++i
)
2637 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
2640 return (KERN_SUCCESS
);
2643 #define TASK_HOLD_NORMAL 0
2644 #define TASK_HOLD_PIDSUSPEND 1
2645 #define TASK_HOLD_LEGACY 2
2646 #define TASK_HOLD_LEGACY_ALL 3
2648 static kern_return_t
2653 if (!task
->active
&& !task_is_a_corpse(task
)) {
2654 return (KERN_FAILURE
);
2657 /* Return success for corpse task */
2658 if (task_is_a_corpse(task
)) {
2659 return KERN_SUCCESS
;
2662 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2663 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_SUSPEND
) | DBG_FUNC_NONE
,
2664 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2665 task
->user_stop_count
, task
->user_stop_count
+ 1, 0);
2668 current_task()->suspends_outstanding
++;
2671 if (mode
== TASK_HOLD_LEGACY
)
2672 task
->legacy_stop_count
++;
2674 if (task
->user_stop_count
++ > 0) {
2676 * If the stop count was positive, the task is
2677 * already stopped and we can exit.
2679 return (KERN_SUCCESS
);
2683 * Put a kernel-level hold on the threads in the task (all
2684 * user-level task suspensions added together represent a
2685 * single kernel-level hold). We then wait for the threads
2686 * to stop executing user code.
2688 task_hold_locked(task
);
2689 task_wait_locked(task
, FALSE
);
2691 return (KERN_SUCCESS
);
2694 static kern_return_t
2699 boolean_t release
= FALSE
;
2701 if (!task
->active
&& !task_is_a_corpse(task
)) {
2702 return (KERN_FAILURE
);
2705 /* Return success for corpse task */
2706 if (task_is_a_corpse(task
)) {
2707 return KERN_SUCCESS
;
2710 if (mode
== TASK_HOLD_PIDSUSPEND
) {
2711 if (task
->pidsuspended
== FALSE
) {
2712 return (KERN_FAILURE
);
2714 task
->pidsuspended
= FALSE
;
2717 if (task
->user_stop_count
> (task
->pidsuspended
? 1 : 0)) {
2719 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2720 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_RESUME
) | DBG_FUNC_NONE
,
2721 task_pid(task
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
2722 task
->user_stop_count
, mode
, task
->legacy_stop_count
);
2726 * This is obviously not robust; if we suspend one task and then resume a different one,
2727 * we'll fly under the radar. This is only meant to catch the common case of a crashed
2728 * or buggy suspender.
2730 current_task()->suspends_outstanding
--;
2733 if (mode
== TASK_HOLD_LEGACY_ALL
) {
2734 if (task
->legacy_stop_count
>= task
->user_stop_count
) {
2735 task
->user_stop_count
= 0;
2738 task
->user_stop_count
-= task
->legacy_stop_count
;
2740 task
->legacy_stop_count
= 0;
2742 if (mode
== TASK_HOLD_LEGACY
&& task
->legacy_stop_count
> 0)
2743 task
->legacy_stop_count
--;
2744 if (--task
->user_stop_count
== 0)
2749 return (KERN_FAILURE
);
2753 * Release the task if necessary.
2756 task_release_locked(task
);
2758 return (KERN_SUCCESS
);
2765 * Implement an (old-fashioned) user-level suspension on a task.
2767 * Because the user isn't expecting to have to manage a suspension
2768 * token, we'll track it for him in the kernel in the form of a naked
2769 * send right to the task's resume port. All such send rights
2770 * account for a single suspension against the task (unlike task_suspend2()
2771 * where each caller gets a unique suspension count represented by a
2772 * unique send-once right).
2775 * The caller holds a reference to the task
2782 mach_port_t port
, send
, old_notify
;
2783 mach_port_name_t name
;
2785 if (task
== TASK_NULL
|| task
== kernel_task
)
2786 return (KERN_INVALID_ARGUMENT
);
2791 * Claim a send right on the task resume port, and request a no-senders
2792 * notification on that port (if none outstanding).
2794 if (task
->itk_resume
== IP_NULL
) {
2795 task
->itk_resume
= ipc_port_alloc_kernel();
2796 if (!IP_VALID(task
->itk_resume
))
2797 panic("failed to create resume port");
2798 ipc_kobject_set(task
->itk_resume
, (ipc_kobject_t
)task
, IKOT_TASK_RESUME
);
2801 port
= task
->itk_resume
;
2803 assert(ip_active(port
));
2805 send
= ipc_port_make_send_locked(port
);
2806 assert(IP_VALID(send
));
2808 if (port
->ip_nsrequest
== IP_NULL
) {
2809 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
2810 assert(old_notify
== IP_NULL
);
2817 * place a legacy hold on the task.
2819 kr
= place_task_hold(task
, TASK_HOLD_LEGACY
);
2820 if (kr
!= KERN_SUCCESS
) {
2822 ipc_port_release_send(send
);
2829 * Copyout the send right into the calling task's IPC space. It won't know it is there,
2830 * but we'll look it up when calling a traditional resume. Any IPC operations that
2831 * deallocate the send right will auto-release the suspension.
2833 if ((kr
= ipc_kmsg_copyout_object(current_task()->itk_space
, (ipc_object_t
)send
,
2834 MACH_MSG_TYPE_MOVE_SEND
, &name
)) != KERN_SUCCESS
) {
2835 printf("warning: %s(%d) failed to copyout suspension token for pid %d with error: %d\n",
2836 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
2837 task_pid(task
), kr
);
2846 * Release a user hold on a task.
2849 * The caller holds a reference to the task
2856 mach_port_name_t resume_port_name
;
2857 ipc_entry_t resume_port_entry
;
2858 ipc_space_t space
= current_task()->itk_space
;
2860 if (task
== TASK_NULL
|| task
== kernel_task
)
2861 return (KERN_INVALID_ARGUMENT
);
2863 /* release a legacy task hold */
2865 kr
= release_task_hold(task
, TASK_HOLD_LEGACY
);
2868 is_write_lock(space
);
2869 if (is_active(space
) && IP_VALID(task
->itk_resume
) &&
2870 ipc_hash_lookup(space
, (ipc_object_t
)task
->itk_resume
, &resume_port_name
, &resume_port_entry
) == TRUE
) {
2872 * We found a suspension token in the caller's IPC space. Release a send right to indicate that
2873 * we are holding one less legacy hold on the task from this caller. If the release failed,
2874 * go ahead and drop all the rights, as someone either already released our holds or the task
2877 if (kr
== KERN_SUCCESS
)
2878 ipc_right_dealloc(space
, resume_port_name
, resume_port_entry
);
2880 ipc_right_destroy(space
, resume_port_name
, resume_port_entry
, FALSE
, 0);
2881 /* space unlocked */
2883 is_write_unlock(space
);
2884 if (kr
== KERN_SUCCESS
)
2885 printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
2886 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
2894 * Suspend the target task.
2895 * Making/holding a token/reference/port is the callers responsibility.
2898 task_suspend_internal(task_t task
)
2902 if (task
== TASK_NULL
|| task
== kernel_task
)
2903 return (KERN_INVALID_ARGUMENT
);
2906 kr
= place_task_hold(task
, TASK_HOLD_NORMAL
);
2912 * Suspend the target task, and return a suspension token. The token
2913 * represents a reference on the suspended task.
2918 task_suspension_token_t
*suspend_token
)
2922 kr
= task_suspend_internal(task
);
2923 if (kr
!= KERN_SUCCESS
) {
2924 *suspend_token
= TASK_NULL
;
2929 * Take a reference on the target task and return that to the caller
2930 * as a "suspension token," which can be converted into an SO right to
2931 * the now-suspended task's resume port.
2933 task_reference_internal(task
);
2934 *suspend_token
= task
;
2936 return (KERN_SUCCESS
);
2941 * (reference/token/port management is caller's responsibility).
2944 task_resume_internal(
2945 task_suspension_token_t task
)
2949 if (task
== TASK_NULL
|| task
== kernel_task
)
2950 return (KERN_INVALID_ARGUMENT
);
2953 kr
= release_task_hold(task
, TASK_HOLD_NORMAL
);
2959 * Resume the task using a suspension token. Consumes the token's ref.
2963 task_suspension_token_t task
)
2967 kr
= task_resume_internal(task
);
2968 task_suspension_token_deallocate(task
);
2974 task_suspension_notify(mach_msg_header_t
*request_header
)
2976 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
2977 task_t task
= convert_port_to_task_suspension_token(port
);
2978 mach_msg_type_number_t not_count
;
2980 if (task
== TASK_NULL
|| task
== kernel_task
)
2981 return TRUE
; /* nothing to do */
2983 switch (request_header
->msgh_id
) {
2985 case MACH_NOTIFY_SEND_ONCE
:
2986 /* release the hold held by this specific send-once right */
2988 release_task_hold(task
, TASK_HOLD_NORMAL
);
2992 case MACH_NOTIFY_NO_SENDERS
:
2993 not_count
= ((mach_no_senders_notification_t
*)request_header
)->not_count
;
2997 if (port
->ip_mscount
== not_count
) {
2999 /* release all the [remaining] outstanding legacy holds */
3000 assert(port
->ip_nsrequest
== IP_NULL
);
3002 release_task_hold(task
, TASK_HOLD_LEGACY_ALL
);
3005 } else if (port
->ip_nsrequest
== IP_NULL
) {
3006 ipc_port_t old_notify
;
3009 /* new send rights, re-arm notification at current make-send count */
3010 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
3011 assert(old_notify
== IP_NULL
);
3023 task_suspension_token_deallocate(task
); /* drop token reference */
3028 task_pidsuspend_locked(task_t task
)
3032 if (task
->pidsuspended
) {
3037 task
->pidsuspended
= TRUE
;
3039 kr
= place_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3040 if (kr
!= KERN_SUCCESS
) {
3041 task
->pidsuspended
= FALSE
;
3051 * Suspends a task by placing a hold on its threads.
3054 * The caller holds a reference to the task
3062 if (task
== TASK_NULL
|| task
== kernel_task
)
3063 return (KERN_INVALID_ARGUMENT
);
3067 kr
= task_pidsuspend_locked(task
);
3076 * Resumes a previously suspended task.
3079 * The caller holds a reference to the task
3087 if (task
== TASK_NULL
|| task
== kernel_task
)
3088 return (KERN_INVALID_ARGUMENT
);
3094 while (task
->changing_freeze_state
) {
3096 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3098 thread_block(THREAD_CONTINUE_NULL
);
3102 task
->changing_freeze_state
= TRUE
;
3105 kr
= release_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
3113 if (kr
== KERN_SUCCESS
)
3114 task
->frozen
= FALSE
;
3115 task
->changing_freeze_state
= FALSE
;
3116 thread_wakeup(&task
->changing_freeze_state
);
3125 #if DEVELOPMENT || DEBUG
3127 extern void IOSleep(int);
3130 task_disconnect_page_mappings(task_t task
)
3134 if (task
== TASK_NULL
|| task
== kernel_task
)
3135 return (KERN_INVALID_ARGUMENT
);
3138 * this function is used to strip all of the mappings from
3139 * the pmap for the specified task to force the task to
3140 * re-fault all of the pages it is actively using... this
3141 * allows us to approximate the true working set of the
3142 * specified task. We only engage if at least 1 of the
3143 * threads in the task is runnable, but we want to continuously
3144 * sweep (at least for a while - I've arbitrarily set the limit at
3145 * 100 sweeps to be re-looked at as we gain experience) to get a better
3146 * view into what areas within a page are being visited (as opposed to only
3147 * seeing the first fault of a page after the task becomes
3148 * runnable)... in the future I may
3149 * try to block until awakened by a thread in this task
3150 * being made runnable, but for now we'll periodically poll from the
3151 * user level debug tool driving the sysctl
3153 for (n
= 0; n
< 100; n
++) {
3156 boolean_t do_unnest
;
3164 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3166 if (thread
->state
& TH_RUN
) {
3172 task
->task_disconnected_count
++;
3174 if (task
->task_unnested
== FALSE
) {
3175 if (runnable
== TRUE
) {
3176 task
->task_unnested
= TRUE
;
3182 if (runnable
== FALSE
)
3185 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_START
,
3186 task
, do_unnest
, task
->task_disconnected_count
, 0, 0);
3188 page_count
= vm_map_disconnect_page_mappings(task
->map
, do_unnest
);
3190 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_TASK_PAGE_MAPPINGS
)) | DBG_FUNC_END
,
3191 task
, page_count
, 0, 0, 0);
3196 return (KERN_SUCCESS
);
3210 * The caller holds a reference to the task
3212 extern void vm_wake_compactor_swapper();
3213 extern queue_head_t c_swapout_list_head
;
3218 uint32_t *purgeable_count
,
3219 uint32_t *wired_count
,
3220 uint32_t *clean_count
,
3221 uint32_t *dirty_count
,
3222 uint32_t dirty_budget
,
3224 boolean_t walk_only
)
3226 kern_return_t kr
= KERN_SUCCESS
;
3228 if (task
== TASK_NULL
|| task
== kernel_task
)
3229 return (KERN_INVALID_ARGUMENT
);
3233 while (task
->changing_freeze_state
) {
3235 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3237 thread_block(THREAD_CONTINUE_NULL
);
3243 return (KERN_FAILURE
);
3245 task
->changing_freeze_state
= TRUE
;
3250 panic("task_freeze - walk_only == TRUE");
3252 kr
= vm_map_freeze(task
->map
, purgeable_count
, wired_count
, clean_count
, dirty_count
, dirty_budget
, shared
);
3257 if (walk_only
== FALSE
&& kr
== KERN_SUCCESS
)
3258 task
->frozen
= TRUE
;
3259 task
->changing_freeze_state
= FALSE
;
3260 thread_wakeup(&task
->changing_freeze_state
);
3264 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
) {
3265 vm_wake_compactor_swapper();
3267 * We do an explicit wakeup of the swapout thread here
3268 * because the compact_and_swap routines don't have
3269 * knowledge about these kind of "per-task packed c_segs"
3270 * and so will not be evaluating whether we need to do
3273 thread_wakeup((event_t
)&c_swapout_list_head
);
3282 * Thaw a currently frozen task.
3285 * The caller holds a reference to the task
3291 if (task
== TASK_NULL
|| task
== kernel_task
)
3292 return (KERN_INVALID_ARGUMENT
);
3296 while (task
->changing_freeze_state
) {
3298 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
3300 thread_block(THREAD_CONTINUE_NULL
);
3304 if (!task
->frozen
) {
3306 return (KERN_FAILURE
);
3308 task
->frozen
= FALSE
;
3312 return (KERN_SUCCESS
);
3315 #endif /* CONFIG_FREEZE */
3318 host_security_set_task_token(
3319 host_security_t host_security
,
3321 security_token_t sec_token
,
3322 audit_token_t audit_token
,
3323 host_priv_t host_priv
)
3325 ipc_port_t host_port
;
3328 if (task
== TASK_NULL
)
3329 return(KERN_INVALID_ARGUMENT
);
3331 if (host_security
== HOST_NULL
)
3332 return(KERN_INVALID_SECURITY
);
3335 task
->sec_token
= sec_token
;
3336 task
->audit_token
= audit_token
;
3340 if (host_priv
!= HOST_PRIV_NULL
) {
3341 kr
= host_get_host_priv_port(host_priv
, &host_port
);
3343 kr
= host_get_host_port(host_priv_self(), &host_port
);
3345 assert(kr
== KERN_SUCCESS
);
3346 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
3351 task_send_trace_memory(
3353 __unused
uint32_t pid
,
3354 __unused
uint64_t uniqueid
)
3356 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
3357 if (target_task
== TASK_NULL
)
3358 return (KERN_INVALID_ARGUMENT
);
3361 kr
= atm_send_proc_inspect_notification(target_task
,
3369 * This routine was added, pretty much exclusively, for registering the
3370 * RPC glue vector for in-kernel short circuited tasks. Rather than
3371 * removing it completely, I have only disabled that feature (which was
3372 * the only feature at the time). It just appears that we are going to
3373 * want to add some user data to tasks in the future (i.e. bsd info,
3374 * task names, etc...), so I left it in the formal task interface.
3379 task_flavor_t flavor
,
3380 __unused task_info_t task_info_in
, /* pointer to IN array */
3381 __unused mach_msg_type_number_t task_info_count
)
3383 if (task
== TASK_NULL
)
3384 return(KERN_INVALID_ARGUMENT
);
3389 case TASK_TRACE_MEMORY_INFO
:
3391 if (task_info_count
!= TASK_TRACE_MEMORY_INFO_COUNT
)
3392 return (KERN_INVALID_ARGUMENT
);
3394 assert(task_info_in
!= NULL
);
3395 task_trace_memory_info_t mem_info
;
3396 mem_info
= (task_trace_memory_info_t
) task_info_in
;
3397 kern_return_t kr
= atm_register_trace_memory(task
,
3398 mem_info
->user_memory_address
,
3399 mem_info
->buffer_size
);
3405 return (KERN_INVALID_ARGUMENT
);
3407 return (KERN_SUCCESS
);
3410 int radar_20146450
= 1;
3414 task_flavor_t flavor
,
3415 task_info_t task_info_out
,
3416 mach_msg_type_number_t
*task_info_count
)
3418 kern_return_t error
= KERN_SUCCESS
;
3419 mach_msg_type_number_t original_task_info_count
;
3421 if (task
== TASK_NULL
)
3422 return (KERN_INVALID_ARGUMENT
);
3424 original_task_info_count
= *task_info_count
;
3427 if ((task
!= current_task()) && (!task
->active
)) {
3429 return (KERN_INVALID_ARGUMENT
);
3434 case TASK_BASIC_INFO_32
:
3435 case TASK_BASIC2_INFO_32
:
3437 task_basic_info_32_t basic_info
;
3442 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
) {
3443 error
= KERN_INVALID_ARGUMENT
;
3447 basic_info
= (task_basic_info_32_t
)task_info_out
;
3449 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3450 basic_info
->virtual_size
= (typeof(basic_info
->virtual_size
))map
->size
;
3451 if (flavor
== TASK_BASIC2_INFO_32
) {
3453 * The "BASIC2" flavor gets the maximum resident
3454 * size instead of the current resident size...
3456 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
3458 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
3460 basic_info
->resident_size
*= PAGE_SIZE
;
3462 basic_info
->policy
= ((task
!= kernel_task
)?
3463 POLICY_TIMESHARE
: POLICY_RR
);
3464 basic_info
->suspend_count
= task
->user_stop_count
;
3466 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3467 basic_info
->user_time
.seconds
=
3468 (typeof(basic_info
->user_time
.seconds
))secs
;
3469 basic_info
->user_time
.microseconds
= usecs
;
3471 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3472 basic_info
->system_time
.seconds
=
3473 (typeof(basic_info
->system_time
.seconds
))secs
;
3474 basic_info
->system_time
.microseconds
= usecs
;
3476 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
3480 case TASK_BASIC_INFO_64
:
3482 task_basic_info_64_t basic_info
;
3487 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
) {
3488 error
= KERN_INVALID_ARGUMENT
;
3492 basic_info
= (task_basic_info_64_t
)task_info_out
;
3494 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
3495 basic_info
->virtual_size
= map
->size
;
3496 basic_info
->resident_size
=
3497 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
3500 basic_info
->policy
= ((task
!= kernel_task
)?
3501 POLICY_TIMESHARE
: POLICY_RR
);
3502 basic_info
->suspend_count
= task
->user_stop_count
;
3504 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3505 basic_info
->user_time
.seconds
=
3506 (typeof(basic_info
->user_time
.seconds
))secs
;
3507 basic_info
->user_time
.microseconds
= usecs
;
3509 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3510 basic_info
->system_time
.seconds
=
3511 (typeof(basic_info
->system_time
.seconds
))secs
;
3512 basic_info
->system_time
.microseconds
= usecs
;
3514 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
3518 case MACH_TASK_BASIC_INFO
:
3520 mach_task_basic_info_t basic_info
;
3525 if (*task_info_count
< MACH_TASK_BASIC_INFO_COUNT
) {
3526 error
= KERN_INVALID_ARGUMENT
;
3530 basic_info
= (mach_task_basic_info_t
)task_info_out
;
3532 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
3534 basic_info
->virtual_size
= map
->size
;
3536 basic_info
->resident_size
=
3537 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
));
3538 basic_info
->resident_size
*= PAGE_SIZE_64
;
3540 basic_info
->resident_size_max
=
3541 (mach_vm_size_t
)(pmap_resident_max(map
->pmap
));
3542 basic_info
->resident_size_max
*= PAGE_SIZE_64
;
3544 basic_info
->policy
= ((task
!= kernel_task
) ?
3545 POLICY_TIMESHARE
: POLICY_RR
);
3547 basic_info
->suspend_count
= task
->user_stop_count
;
3549 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
3550 basic_info
->user_time
.seconds
=
3551 (typeof(basic_info
->user_time
.seconds
))secs
;
3552 basic_info
->user_time
.microseconds
= usecs
;
3554 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
3555 basic_info
->system_time
.seconds
=
3556 (typeof(basic_info
->system_time
.seconds
))secs
;
3557 basic_info
->system_time
.microseconds
= usecs
;
3559 *task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
3563 case TASK_THREAD_TIMES_INFO
:
3565 task_thread_times_info_t times_info
;
3568 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
3569 error
= KERN_INVALID_ARGUMENT
;
3573 times_info
= (task_thread_times_info_t
) task_info_out
;
3574 times_info
->user_time
.seconds
= 0;
3575 times_info
->user_time
.microseconds
= 0;
3576 times_info
->system_time
.seconds
= 0;
3577 times_info
->system_time
.microseconds
= 0;
3580 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3581 time_value_t user_time
, system_time
;
3583 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3586 thread_read_times(thread
, &user_time
, &system_time
);
3588 time_value_add(×_info
->user_time
, &user_time
);
3589 time_value_add(×_info
->system_time
, &system_time
);
3592 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
3596 case TASK_ABSOLUTETIME_INFO
:
3598 task_absolutetime_info_t info
;
3601 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
) {
3602 error
= KERN_INVALID_ARGUMENT
;
3606 info
= (task_absolutetime_info_t
)task_info_out
;
3607 info
->threads_user
= info
->threads_system
= 0;
3610 info
->total_user
= task
->total_user_time
;
3611 info
->total_system
= task
->total_system_time
;
3613 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3617 if (thread
->options
& TH_OPT_IDLE_THREAD
)
3621 thread_lock(thread
);
3623 tval
= timer_grab(&thread
->user_timer
);
3624 info
->threads_user
+= tval
;
3625 info
->total_user
+= tval
;
3627 tval
= timer_grab(&thread
->system_timer
);
3628 if (thread
->precise_user_kernel_time
) {
3629 info
->threads_system
+= tval
;
3630 info
->total_system
+= tval
;
3632 /* system_timer may represent either sys or user */
3633 info
->threads_user
+= tval
;
3634 info
->total_user
+= tval
;
3637 thread_unlock(thread
);
3642 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
3646 case TASK_DYLD_INFO
:
3648 task_dyld_info_t info
;
3651 * We added the format field to TASK_DYLD_INFO output. For
3652 * temporary backward compatibility, accept the fact that
3653 * clients may ask for the old version - distinquished by the
3654 * size of the expected result structure.
3656 #define TASK_LEGACY_DYLD_INFO_COUNT \
3657 offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
3659 if (*task_info_count
< TASK_LEGACY_DYLD_INFO_COUNT
) {
3660 error
= KERN_INVALID_ARGUMENT
;
3664 info
= (task_dyld_info_t
)task_info_out
;
3665 info
->all_image_info_addr
= task
->all_image_info_addr
;
3666 info
->all_image_info_size
= task
->all_image_info_size
;
3668 /* only set format on output for those expecting it */
3669 if (*task_info_count
>= TASK_DYLD_INFO_COUNT
) {
3670 info
->all_image_info_format
= task_has_64BitAddr(task
) ?
3671 TASK_DYLD_ALL_IMAGE_INFO_64
:
3672 TASK_DYLD_ALL_IMAGE_INFO_32
;
3673 *task_info_count
= TASK_DYLD_INFO_COUNT
;
3675 *task_info_count
= TASK_LEGACY_DYLD_INFO_COUNT
;
3680 case TASK_EXTMOD_INFO
:
3682 task_extmod_info_t info
;
3685 if (*task_info_count
< TASK_EXTMOD_INFO_COUNT
) {
3686 error
= KERN_INVALID_ARGUMENT
;
3690 info
= (task_extmod_info_t
)task_info_out
;
3692 p
= get_bsdtask_info(task
);
3694 proc_getexecutableuuid(p
, info
->task_uuid
, sizeof(info
->task_uuid
));
3696 bzero(info
->task_uuid
, sizeof(info
->task_uuid
));
3698 info
->extmod_statistics
= task
->extmod_statistics
;
3699 *task_info_count
= TASK_EXTMOD_INFO_COUNT
;
3704 case TASK_KERNELMEMORY_INFO
:
3706 task_kernelmemory_info_t tkm_info
;
3707 ledger_amount_t credit
, debit
;
3709 if (*task_info_count
< TASK_KERNELMEMORY_INFO_COUNT
) {
3710 error
= KERN_INVALID_ARGUMENT
;
3714 tkm_info
= (task_kernelmemory_info_t
) task_info_out
;
3715 tkm_info
->total_palloc
= 0;
3716 tkm_info
->total_pfree
= 0;
3717 tkm_info
->total_salloc
= 0;
3718 tkm_info
->total_sfree
= 0;
3720 if (task
== kernel_task
) {
3722 * All shared allocs/frees from other tasks count against
3723 * the kernel private memory usage. If we are looking up
3724 * info for the kernel task, gather from everywhere.
3728 /* start by accounting for all the terminated tasks against the kernel */
3729 tkm_info
->total_palloc
= tasks_tkm_private
.alloc
+ tasks_tkm_shared
.alloc
;
3730 tkm_info
->total_pfree
= tasks_tkm_private
.free
+ tasks_tkm_shared
.free
;
3732 /* count all other task/thread shared alloc/free against the kernel */
3733 lck_mtx_lock(&tasks_threads_lock
);
3735 /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
3736 queue_iterate(&tasks
, task
, task_t
, tasks
) {
3737 if (task
== kernel_task
) {
3738 if (ledger_get_entries(task
->ledger
,
3739 task_ledgers
.tkm_private
, &credit
,
3740 &debit
) == KERN_SUCCESS
) {
3741 tkm_info
->total_palloc
+= credit
;
3742 tkm_info
->total_pfree
+= debit
;
3745 if (!ledger_get_entries(task
->ledger
,
3746 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3747 tkm_info
->total_palloc
+= credit
;
3748 tkm_info
->total_pfree
+= debit
;
3751 lck_mtx_unlock(&tasks_threads_lock
);
3753 if (!ledger_get_entries(task
->ledger
,
3754 task_ledgers
.tkm_private
, &credit
, &debit
)) {
3755 tkm_info
->total_palloc
= credit
;
3756 tkm_info
->total_pfree
= debit
;
3758 if (!ledger_get_entries(task
->ledger
,
3759 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
3760 tkm_info
->total_salloc
= credit
;
3761 tkm_info
->total_sfree
= debit
;
3766 *task_info_count
= TASK_KERNELMEMORY_INFO_COUNT
;
3767 return KERN_SUCCESS
;
3771 case TASK_SCHED_FIFO_INFO
:
3774 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
) {
3775 error
= KERN_INVALID_ARGUMENT
;
3779 error
= KERN_INVALID_POLICY
;
3784 case TASK_SCHED_RR_INFO
:
3786 policy_rr_base_t rr_base
;
3787 uint32_t quantum_time
;
3788 uint64_t quantum_ns
;
3790 if (*task_info_count
< POLICY_RR_BASE_COUNT
) {
3791 error
= KERN_INVALID_ARGUMENT
;
3795 rr_base
= (policy_rr_base_t
) task_info_out
;
3797 if (task
!= kernel_task
) {
3798 error
= KERN_INVALID_POLICY
;
3802 rr_base
->base_priority
= task
->priority
;
3804 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
3805 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
3807 rr_base
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
3809 *task_info_count
= POLICY_RR_BASE_COUNT
;
3814 case TASK_SCHED_TIMESHARE_INFO
:
3816 policy_timeshare_base_t ts_base
;
3818 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
) {
3819 error
= KERN_INVALID_ARGUMENT
;
3823 ts_base
= (policy_timeshare_base_t
) task_info_out
;
3825 if (task
== kernel_task
) {
3826 error
= KERN_INVALID_POLICY
;
3830 ts_base
->base_priority
= task
->priority
;
3832 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
3836 case TASK_SECURITY_TOKEN
:
3838 security_token_t
*sec_token_p
;
3840 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
3841 error
= KERN_INVALID_ARGUMENT
;
3845 sec_token_p
= (security_token_t
*) task_info_out
;
3847 *sec_token_p
= task
->sec_token
;
3849 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
3853 case TASK_AUDIT_TOKEN
:
3855 audit_token_t
*audit_token_p
;
3857 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
3858 error
= KERN_INVALID_ARGUMENT
;
3862 audit_token_p
= (audit_token_t
*) task_info_out
;
3864 *audit_token_p
= task
->audit_token
;
3866 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
3870 case TASK_SCHED_INFO
:
3871 error
= KERN_INVALID_ARGUMENT
;
3874 case TASK_EVENTS_INFO
:
3876 task_events_info_t events_info
;
3879 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
3880 error
= KERN_INVALID_ARGUMENT
;
3884 events_info
= (task_events_info_t
) task_info_out
;
3887 events_info
->faults
= task
->faults
;
3888 events_info
->pageins
= task
->pageins
;
3889 events_info
->cow_faults
= task
->cow_faults
;
3890 events_info
->messages_sent
= task
->messages_sent
;
3891 events_info
->messages_received
= task
->messages_received
;
3892 events_info
->syscalls_mach
= task
->syscalls_mach
;
3893 events_info
->syscalls_unix
= task
->syscalls_unix
;
3895 events_info
->csw
= task
->c_switch
;
3897 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3898 events_info
->csw
+= thread
->c_switch
;
3899 events_info
->syscalls_mach
+= thread
->syscalls_mach
;
3900 events_info
->syscalls_unix
+= thread
->syscalls_unix
;
3904 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
3907 case TASK_AFFINITY_TAG_INFO
:
3909 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
) {
3910 error
= KERN_INVALID_ARGUMENT
;
3914 error
= task_affinity_info(task
, task_info_out
, task_info_count
);
3917 case TASK_POWER_INFO
:
3919 if (*task_info_count
< TASK_POWER_INFO_COUNT
) {
3920 error
= KERN_INVALID_ARGUMENT
;
3924 task_power_info_locked(task
, (task_power_info_t
)task_info_out
, NULL
, NULL
);
3928 case TASK_POWER_INFO_V2
:
3930 if (*task_info_count
< TASK_POWER_INFO_V2_COUNT
) {
3931 error
= KERN_INVALID_ARGUMENT
;
3934 task_power_info_v2_t tpiv2
= (task_power_info_v2_t
) task_info_out
;
3936 uint64_t *task_energy
= NULL
;
3937 task_power_info_locked(task
, &tpiv2
->cpu_energy
, &tpiv2
->gpu_energy
, task_energy
);
3942 case TASK_VM_INFO_PURGEABLE
:
3944 task_vm_info_t vm_info
;
3947 if (*task_info_count
< TASK_VM_INFO_REV0_COUNT
) {
3948 error
= KERN_INVALID_ARGUMENT
;
3952 vm_info
= (task_vm_info_t
)task_info_out
;
3954 if (task
== kernel_task
) {
3959 vm_map_lock_read(map
);
3962 vm_info
->virtual_size
= (typeof(vm_info
->virtual_size
))map
->size
;
3963 vm_info
->region_count
= map
->hdr
.nentries
;
3964 vm_info
->page_size
= vm_map_page_size(map
);
3966 vm_info
->resident_size
= pmap_resident_count(map
->pmap
);
3967 vm_info
->resident_size
*= PAGE_SIZE
;
3968 vm_info
->resident_size_peak
= pmap_resident_max(map
->pmap
);
3969 vm_info
->resident_size_peak
*= PAGE_SIZE
;
3971 #define _VM_INFO(_name) \
3972 vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
3975 _VM_INFO(device_peak
);
3977 _VM_INFO(external_peak
);
3979 _VM_INFO(internal_peak
);
3981 _VM_INFO(reusable_peak
);
3982 _VM_INFO(compressed
);
3983 _VM_INFO(compressed_peak
);
3984 _VM_INFO(compressed_lifetime
);
3986 vm_info
->purgeable_volatile_pmap
= 0;
3987 vm_info
->purgeable_volatile_resident
= 0;
3988 vm_info
->purgeable_volatile_virtual
= 0;
3989 if (task
== kernel_task
) {
3991 * We do not maintain the detailed stats for the
3992 * kernel_pmap, so just count everything as
3995 vm_info
->internal
= vm_info
->resident_size
;
3997 * ... but since the memory held by the VM compressor
3998 * in the kernel address space ought to be attributed
3999 * to user-space tasks, we subtract it from "internal"
4000 * to give memory reporting tools a more accurate idea
4001 * of what the kernel itself is actually using, instead
4002 * of making it look like the kernel is leaking memory
4003 * when the system is under memory pressure.
4005 vm_info
->internal
-= (VM_PAGE_COMPRESSOR_COUNT
*
4008 mach_vm_size_t volatile_virtual_size
;
4009 mach_vm_size_t volatile_resident_size
;
4010 mach_vm_size_t volatile_compressed_size
;
4011 mach_vm_size_t volatile_pmap_size
;
4012 mach_vm_size_t volatile_compressed_pmap_size
;
4015 if (flavor
== TASK_VM_INFO_PURGEABLE
) {
4016 kr
= vm_map_query_volatile(
4018 &volatile_virtual_size
,
4019 &volatile_resident_size
,
4020 &volatile_compressed_size
,
4021 &volatile_pmap_size
,
4022 &volatile_compressed_pmap_size
);
4023 if (kr
== KERN_SUCCESS
) {
4024 vm_info
->purgeable_volatile_pmap
=
4026 if (radar_20146450
) {
4027 vm_info
->compressed
-=
4028 volatile_compressed_pmap_size
;
4030 vm_info
->purgeable_volatile_resident
=
4031 volatile_resident_size
;
4032 vm_info
->purgeable_volatile_virtual
=
4033 volatile_virtual_size
;
4037 *task_info_count
= TASK_VM_INFO_REV0_COUNT
;
4039 if (original_task_info_count
>= TASK_VM_INFO_REV1_COUNT
) {
4040 vm_info
->phys_footprint
=
4041 (mach_vm_size_t
) get_task_phys_footprint(task
);
4042 *task_info_count
= TASK_VM_INFO_REV1_COUNT
;
4044 if (original_task_info_count
>= TASK_VM_INFO_REV2_COUNT
) {
4045 vm_info
->min_address
= map
->min_offset
;
4046 vm_info
->max_address
= map
->max_offset
;
4047 *task_info_count
= TASK_VM_INFO_REV2_COUNT
;
4050 if (task
!= kernel_task
) {
4051 vm_map_unlock_read(map
);
4057 case TASK_WAIT_STATE_INFO
:
4060 * Deprecated flavor. Currently allowing some results until all users
4061 * stop calling it. The results may not be accurate.
4063 task_wait_state_info_t wait_state_info
;
4064 uint64_t total_sfi_ledger_val
= 0;
4066 if (*task_info_count
< TASK_WAIT_STATE_INFO_COUNT
) {
4067 error
= KERN_INVALID_ARGUMENT
;
4071 wait_state_info
= (task_wait_state_info_t
) task_info_out
;
4073 wait_state_info
->total_wait_state_time
= 0;
4074 bzero(wait_state_info
->_reserved
, sizeof(wait_state_info
->_reserved
));
4076 #if CONFIG_SCHED_SFI
4077 int i
, prev_lentry
= -1;
4078 int64_t val_credit
, val_debit
;
4080 for (i
= 0; i
< MAX_SFI_CLASS_ID
; i
++){
4083 * checking with prev_lentry != entry ensures adjacent classes
4084 * which share the same ledger do not add wait times twice.
4085 * Note: Use ledger() call to get data for each individual sfi class.
4087 if (prev_lentry
!= task_ledgers
.sfi_wait_times
[i
] &&
4088 KERN_SUCCESS
== ledger_get_entries(task
->ledger
,
4089 task_ledgers
.sfi_wait_times
[i
], &val_credit
, &val_debit
)) {
4090 total_sfi_ledger_val
+= val_credit
;
4092 prev_lentry
= task_ledgers
.sfi_wait_times
[i
];
4095 #endif /* CONFIG_SCHED_SFI */
4096 wait_state_info
->total_wait_sfi_state_time
= total_sfi_ledger_val
;
4097 *task_info_count
= TASK_WAIT_STATE_INFO_COUNT
;
4101 case TASK_VM_INFO_PURGEABLE_ACCOUNT
:
4103 #if DEVELOPMENT || DEBUG
4104 pvm_account_info_t acnt_info
;
4106 if (*task_info_count
< PVM_ACCOUNT_INFO_COUNT
) {
4107 error
= KERN_INVALID_ARGUMENT
;
4111 if (task_info_out
== NULL
) {
4112 error
= KERN_INVALID_ARGUMENT
;
4116 acnt_info
= (pvm_account_info_t
) task_info_out
;
4118 error
= vm_purgeable_account(task
, acnt_info
);
4120 *task_info_count
= PVM_ACCOUNT_INFO_COUNT
;
4123 #else /* DEVELOPMENT || DEBUG */
4124 error
= KERN_NOT_SUPPORTED
;
4126 #endif /* DEVELOPMENT || DEBUG */
4128 case TASK_FLAGS_INFO
:
4130 task_flags_info_t flags_info
;
4132 if (*task_info_count
< TASK_FLAGS_INFO_COUNT
) {
4133 error
= KERN_INVALID_ARGUMENT
;
4137 flags_info
= (task_flags_info_t
)task_info_out
;
4139 /* only publish the 64-bit flag of the task */
4140 flags_info
->flags
= task
->t_flags
& TF_64B_ADDR
;
4142 *task_info_count
= TASK_FLAGS_INFO_COUNT
;
4146 case TASK_DEBUG_INFO_INTERNAL
:
4148 #if DEVELOPMENT || DEBUG
4149 task_debug_info_internal_t dbg_info
;
4150 if (*task_info_count
< TASK_DEBUG_INFO_INTERNAL_COUNT
) {
4151 error
= KERN_NOT_SUPPORTED
;
4155 if (task_info_out
== NULL
) {
4156 error
= KERN_INVALID_ARGUMENT
;
4159 dbg_info
= (task_debug_info_internal_t
) task_info_out
;
4160 dbg_info
->ipc_space_size
= 0;
4161 if (task
->itk_space
){
4162 dbg_info
->ipc_space_size
= task
->itk_space
->is_table_size
;
4165 error
= KERN_SUCCESS
;
4166 *task_info_count
= TASK_DEBUG_INFO_INTERNAL_COUNT
;
4168 #else /* DEVELOPMENT || DEBUG */
4169 error
= KERN_NOT_SUPPORTED
;
4171 #endif /* DEVELOPMENT || DEBUG */
4174 error
= KERN_INVALID_ARGUMENT
;
4184 * Returns power stats for the task.
4185 * Note: Called with task locked.
4188 task_power_info_locked(
4190 task_power_info_t info
,
4191 gpu_energy_data_t ginfo
,
4192 uint64_t *task_energy
)
4195 ledger_amount_t tmp
;
4197 task_lock_assert_owned(task
);
4199 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
4200 (ledger_amount_t
*)&info
->task_interrupt_wakeups
, &tmp
);
4201 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
4202 (ledger_amount_t
*)&info
->task_platform_idle_wakeups
, &tmp
);
4204 info
->task_timer_wakeups_bin_1
= task
->task_timer_wakeups_bin_1
;
4205 info
->task_timer_wakeups_bin_2
= task
->task_timer_wakeups_bin_2
;
4207 info
->total_user
= task
->total_user_time
;
4208 info
->total_system
= task
->total_system_time
;
4211 *task_energy
= task
->task_energy
;
4215 ginfo
->task_gpu_utilisation
= task
->task_gpu_ns
;
4218 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4222 if (thread
->options
& TH_OPT_IDLE_THREAD
)
4226 thread_lock(thread
);
4228 info
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
4229 info
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
4232 *task_energy
+= ml_energy_stat(thread
);
4235 tval
= timer_grab(&thread
->user_timer
);
4236 info
->total_user
+= tval
;
4238 tval
= timer_grab(&thread
->system_timer
);
4239 if (thread
->precise_user_kernel_time
) {
4240 info
->total_system
+= tval
;
4242 /* system_timer may represent either sys or user */
4243 info
->total_user
+= tval
;
4247 ginfo
->task_gpu_utilisation
+= ml_gpu_stat(thread
);
4249 thread_unlock(thread
);
4255 * task_gpu_utilisation
4257 * Returns the total gpu time used by the all the threads of the task
4258 * (both dead and alive)
4261 task_gpu_utilisation(
4264 uint64_t gpu_time
= 0;
4268 gpu_time
+= task
->task_gpu_ns
;
4270 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4273 thread_lock(thread
);
4274 gpu_time
+= ml_gpu_stat(thread
);
4275 thread_unlock(thread
);
4286 * Returns the total energy used by the all the threads of the task
4287 * (both dead and alive)
4293 uint64_t energy
= 0;
4297 energy
+= task
->task_energy
;
4299 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4302 thread_lock(thread
);
4303 energy
+= ml_energy_stat(thread
);
4304 thread_unlock(thread
);
4315 task_purgable_info_t
*stats
)
4317 if (task
== TASK_NULL
|| stats
== NULL
)
4318 return KERN_INVALID_ARGUMENT
;
4319 /* Take task reference */
4320 task_reference(task
);
4321 vm_purgeable_stats((vm_purgeable_info_t
)stats
, task
);
4322 /* Drop task reference */
4323 task_deallocate(task
);
4324 return KERN_SUCCESS
;
4337 task
->vtimers
|= which
;
4341 case TASK_VTIMER_USER
:
4342 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4344 thread_lock(thread
);
4345 if (thread
->precise_user_kernel_time
)
4346 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
4348 thread
->vtimer_user_save
= timer_grab(&thread
->system_timer
);
4349 thread_unlock(thread
);
4354 case TASK_VTIMER_PROF
:
4355 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4357 thread_lock(thread
);
4358 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
4359 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
4360 thread_unlock(thread
);
4365 case TASK_VTIMER_RLIM
:
4366 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
4368 thread_lock(thread
);
4369 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
4370 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
4371 thread_unlock(thread
);
4385 assert(task
== current_task());
4389 task
->vtimers
&= ~which
;
4399 uint32_t *microsecs
)
4401 thread_t thread
= current_thread();
4403 clock_sec_t secs
= 0;
4406 assert(task
== current_task());
4408 spl_t s
= splsched();
4409 thread_lock(thread
);
4411 if ((task
->vtimers
& which
) != (uint32_t)which
) {
4412 thread_unlock(thread
);
4419 case TASK_VTIMER_USER
:
4420 if (thread
->precise_user_kernel_time
) {
4421 tdelt
= (uint32_t)timer_delta(&thread
->user_timer
,
4422 &thread
->vtimer_user_save
);
4424 tdelt
= (uint32_t)timer_delta(&thread
->system_timer
,
4425 &thread
->vtimer_user_save
);
4427 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4430 case TASK_VTIMER_PROF
:
4431 tsum
= timer_grab(&thread
->user_timer
);
4432 tsum
+= timer_grab(&thread
->system_timer
);
4433 tdelt
= (uint32_t)(tsum
- thread
->vtimer_prof_save
);
4434 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4435 /* if the time delta is smaller than a usec, ignore */
4436 if (*microsecs
!= 0)
4437 thread
->vtimer_prof_save
= tsum
;
4440 case TASK_VTIMER_RLIM
:
4441 tsum
= timer_grab(&thread
->user_timer
);
4442 tsum
+= timer_grab(&thread
->system_timer
);
4443 tdelt
= (uint32_t)(tsum
- thread
->vtimer_rlim_save
);
4444 thread
->vtimer_rlim_save
= tsum
;
4445 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
4449 thread_unlock(thread
);
4456 * Change the assigned processor set for the task
4460 __unused task_t task
,
4461 __unused processor_set_t new_pset
,
4462 __unused boolean_t assign_threads
)
4464 return(KERN_FAILURE
);
4468 * task_assign_default:
4470 * Version of task_assign to assign to default processor set.
4473 task_assign_default(
4475 boolean_t assign_threads
)
4477 return (task_assign(task
, &pset0
, assign_threads
));
4481 * task_get_assignment
4483 * Return name of processor set that task is assigned to.
4486 task_get_assignment(
4488 processor_set_t
*pset
)
4490 if (!task
|| !task
->active
)
4491 return KERN_FAILURE
;
4495 return KERN_SUCCESS
;
4499 get_task_dispatchqueue_offset(
4502 return task
->dispatchqueue_offset
;
4508 * Set scheduling policy and parameters, both base and limit, for
4509 * the given task. Policy must be a policy which is enabled for the
4510 * processor set. Change contained threads if requested.
4514 __unused task_t task
,
4515 __unused policy_t policy_id
,
4516 __unused policy_base_t base
,
4517 __unused mach_msg_type_number_t count
,
4518 __unused boolean_t set_limit
,
4519 __unused boolean_t change
)
4521 return(KERN_FAILURE
);
4527 * Set scheduling policy and parameters, both base and limit, for
4528 * the given task. Policy can be any policy implemented by the
4529 * processor set, whether enabled or not. Change contained threads
4534 __unused task_t task
,
4535 __unused processor_set_t pset
,
4536 __unused policy_t policy_id
,
4537 __unused policy_base_t base
,
4538 __unused mach_msg_type_number_t base_count
,
4539 __unused policy_limit_t limit
,
4540 __unused mach_msg_type_number_t limit_count
,
4541 __unused boolean_t change
)
4543 return(KERN_FAILURE
);
4548 __unused task_t task
,
4549 __unused vm_offset_t pc
,
4550 __unused vm_offset_t endpc
)
4552 return KERN_FAILURE
;
4556 task_synchronizer_destroy_all(task_t task
)
4559 * Destroy owned semaphores
4561 semaphore_destroy_all(task
);
4565 * Install default (machine-dependent) initial thread state
4566 * on the task. Subsequent thread creation will have this initial
4567 * state set on the thread by machine_thread_inherit_taskwide().
4568 * Flavors and structures are exactly the same as those to thread_set_state()
4574 thread_state_t state
,
4575 mach_msg_type_number_t state_count
)
4579 if (task
== TASK_NULL
) {
4580 return (KERN_INVALID_ARGUMENT
);
4585 if (!task
->active
) {
4587 return (KERN_FAILURE
);
4590 ret
= machine_task_set_state(task
, flavor
, state
, state_count
);
4597 * Examine the default (machine-dependent) initial thread state
4598 * on the task, as set by task_set_state(). Flavors and structures
4599 * are exactly the same as those passed to thread_get_state().
4605 thread_state_t state
,
4606 mach_msg_type_number_t
*state_count
)
4610 if (task
== TASK_NULL
) {
4611 return (KERN_INVALID_ARGUMENT
);
4616 if (!task
->active
) {
4618 return (KERN_FAILURE
);
4621 ret
= machine_task_get_state(task
, flavor
, state
, state_count
);
4627 #if CONFIG_MEMORYSTATUS
4628 #define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
4630 void __attribute__((noinline
))
4631 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb
, boolean_t is_fatal
)
4633 task_t task
= current_task();
4635 const char *procname
= "unknown";
4636 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
4639 pid
= proc_selfpid();
4643 * Cannot have ReportCrash analyzing
4644 * a suspended initproc.
4649 if (task
->bsd_info
!= NULL
)
4650 procname
= proc_name_address(current_task()->bsd_info
);
4653 if (hwm_user_cores
) {
4655 uint64_t starttime
, end
;
4656 clock_sec_t secs
= 0;
4657 uint32_t microsecs
= 0;
4659 starttime
= mach_absolute_time();
4661 * Trigger a coredump of this process. Don't proceed unless we know we won't
4662 * be filling up the disk; and ignore the core size resource limit for this
4665 if ((error
= coredump(current_task()->bsd_info
, HWM_USERCORE_MINSPACE
, COREDUMP_IGNORE_ULIMIT
)) != 0) {
4666 printf("couldn't take coredump of %s[%d]: %d\n", procname
, pid
, error
);
4669 * coredump() leaves the task suspended.
4671 task_resume_internal(current_task());
4673 end
= mach_absolute_time();
4674 absolutetime_to_microtime(end
- starttime
, &secs
, µsecs
);
4675 printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
4676 proc_name_address(current_task()->bsd_info
), pid
, (int)secs
, microsecs
);
4678 #endif /* CONFIG_COREDUMP */
4680 if (disable_exc_resource
) {
4681 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
4682 "supressed by a boot-arg.\n", procname
, pid
, max_footprint_mb
);
4687 * A task that has triggered an EXC_RESOURCE, should not be
4688 * jetsammed when the device is under memory pressure. Here
4689 * we set the P_MEMSTAT_TERMINATED flag so that the process
4690 * will be skipped if the memorystatus_thread wakes up.
4692 proc_memstat_terminated(current_task()->bsd_info
, TRUE
);
4694 printf("process %s[%d] crossed memory high watermark (%d MB); sending "
4695 "EXC_RESOURCE.\n", procname
, pid
, max_footprint_mb
);
4697 code
[0] = code
[1] = 0;
4698 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_MEMORY
);
4699 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_HIGH_WATERMARK
);
4700 EXC_RESOURCE_HWM_ENCODE_LIMIT(code
[0], max_footprint_mb
);
4702 /* Do not generate a corpse fork if the violation is a fatal one */
4703 if (is_fatal
|| exc_via_corpse_forking
== 0) {
4704 /* Do not send a EXC_RESOURCE is corpse_for_fatal_memkill is set */
4705 if (corpse_for_fatal_memkill
== 0) {
4707 * Use the _internal_ variant so that no user-space
4708 * process can resume our task from under us.
4710 task_suspend_internal(task
);
4711 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
4712 task_resume_internal(task
);
4715 task_enqueue_exception_with_corpse(task
, code
, EXCEPTION_CODE_MAX
);
4719 * After the EXC_RESOURCE has been handled, we must clear the
4720 * P_MEMSTAT_TERMINATED flag so that the process can again be
4721 * considered for jetsam if the memorystatus_thread wakes up.
4723 proc_memstat_terminated(current_task()->bsd_info
, FALSE
); /* clear the flag */
4727 * Callback invoked when a task exceeds its physical footprint limit.
4730 task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
4732 ledger_amount_t max_footprint
, max_footprint_mb
;
4735 boolean_t trigger_exception
;
4737 if (warning
== LEDGER_WARNING_DIPPED_BELOW
) {
4739 * Task memory limits only provide a warning on the way up.
4744 task
= current_task();
4746 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &max_footprint
);
4747 max_footprint_mb
= max_footprint
>> 20;
4750 * Capture the trigger exception flag before turning off the exception.
4752 trigger_exception
= task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
? TRUE
: FALSE
;
4754 is_fatal
= memorystatus_turnoff_exception_and_get_fatalness((warning
== LEDGER_WARNING_ROSE_ABOVE
) ? TRUE
: FALSE
, (int)max_footprint_mb
);
4757 * If this an actual violation (not a warning),
4758 * generate a non-fatal high watermark EXC_RESOURCE.
4760 if ((warning
== 0) && trigger_exception
) {
4761 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND((int)max_footprint_mb
, is_fatal
);
4764 memorystatus_on_ledger_footprint_exceeded((warning
== LEDGER_WARNING_ROSE_ABOVE
) ? TRUE
: FALSE
,
4768 extern int proc_check_footprint_priv(void);
4771 task_set_phys_footprint_limit(
4776 kern_return_t error
;
4778 if ((error
= proc_check_footprint_priv())) {
4779 return (KERN_NO_ACCESS
);
4782 return task_set_phys_footprint_limit_internal(task
, new_limit_mb
, old_limit_mb
, FALSE
);
4786 task_convert_phys_footprint_limit(
4788 int *converted_limit_mb
)
4790 if (limit_mb
== -1) {
4794 if (max_task_footprint
!= 0) {
4795 *converted_limit_mb
= (int)(max_task_footprint
/ 1024 / 1024); /* bytes to MB */
4797 *converted_limit_mb
= (int)(LEDGER_LIMIT_INFINITY
>> 20);
4800 /* nothing to convert */
4801 *converted_limit_mb
= limit_mb
;
4803 return (KERN_SUCCESS
);
4808 task_set_phys_footprint_limit_internal(
4812 boolean_t trigger_exception
)
4814 ledger_amount_t old
;
4816 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &old
);
4820 * Check that limit >> 20 will not give an "unexpected" 32-bit
4821 * result. There are, however, implicit assumptions that -1 mb limit
4822 * equates to LEDGER_LIMIT_INFINITY.
4824 assert(((old
& 0xFFF0000000000000LL
) == 0) || (old
== LEDGER_LIMIT_INFINITY
));
4825 *old_limit_mb
= (int)(old
>> 20);
4828 if (new_limit_mb
== -1) {
4830 * Caller wishes to remove the limit.
4832 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
4833 max_task_footprint
? max_task_footprint
: LEDGER_LIMIT_INFINITY
,
4834 max_task_footprint
? max_task_footprint_warning_level
: 0);
4835 return (KERN_SUCCESS
);
4838 #ifdef CONFIG_NOMONITORS
4839 return (KERN_SUCCESS
);
4840 #endif /* CONFIG_NOMONITORS */
4844 if (trigger_exception
) {
4845 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
4847 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
4850 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
4851 (ledger_amount_t
)new_limit_mb
<< 20, PHYS_FOOTPRINT_WARNING_LEVEL
);
4853 if (task
== current_task()) {
4854 ledger_check_new_balance(task
->ledger
, task_ledgers
.phys_footprint
);
4859 return (KERN_SUCCESS
);
4863 task_get_phys_footprint_limit(
4867 ledger_amount_t limit
;
4869 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &limit
);
4871 * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
4872 * result. There are, however, implicit assumptions that -1 mb limit
4873 * equates to LEDGER_LIMIT_INFINITY.
4875 assert(((limit
& 0xFFF0000000000000LL
) == 0) || (limit
== LEDGER_LIMIT_INFINITY
));
4876 *limit_mb
= (int)(limit
>> 20);
4878 return (KERN_SUCCESS
);
4880 #else /* CONFIG_MEMORYSTATUS */
4882 task_set_phys_footprint_limit(
4883 __unused task_t task
,
4884 __unused
int new_limit_mb
,
4885 __unused
int *old_limit_mb
)
4887 return (KERN_FAILURE
);
4891 task_get_phys_footprint_limit(
4892 __unused task_t task
,
4893 __unused
int *limit_mb
)
4895 return (KERN_FAILURE
);
4897 #endif /* CONFIG_MEMORYSTATUS */
4900 * We need to export some functions to other components that
4901 * are currently implemented in macros within the osfmk
4902 * component. Just export them as functions of the same name.
4904 boolean_t
is_kerneltask(task_t t
)
4906 if (t
== kernel_task
)
4912 boolean_t
is_corpsetask(task_t t
)
4914 return (task_is_a_corpse(t
));
4918 task_t
current_task(void);
4919 task_t
current_task(void)
4921 return (current_task_fast());
4924 #undef task_reference
4925 void task_reference(task_t task
);
4930 if (task
!= TASK_NULL
)
4931 task_reference_internal(task
);
4934 /* defined in bsd/kern/kern_prot.c */
4935 extern int get_audit_token_pid(audit_token_t
*audit_token
);
4937 int task_pid(task_t task
)
4940 return get_audit_token_pid(&task
->audit_token
);
4946 * This routine finds a thread in a task by its unique id
4947 * Returns a referenced thread or THREAD_NULL if the thread was not found
4949 * TODO: This is super inefficient - it's an O(threads in task) list walk!
4950 * We should make a tid hash, or transition all tid clients to thread ports
4952 * Precondition: No locks held (will take task lock)
4955 task_findtid(task_t task
, uint64_t tid
)
4957 thread_t self
= current_thread();
4958 thread_t found_thread
= THREAD_NULL
;
4959 thread_t iter_thread
= THREAD_NULL
;
4961 /* Short-circuit the lookup if we're looking up ourselves */
4962 if (tid
== self
->thread_id
|| tid
== TID_NULL
) {
4963 assert(self
->task
== task
);
4965 thread_reference(self
);
4972 queue_iterate(&task
->threads
, iter_thread
, thread_t
, task_threads
) {
4973 if (iter_thread
->thread_id
== tid
) {
4974 found_thread
= iter_thread
;
4975 thread_reference(found_thread
);
4982 return (found_thread
);
4987 * Control the CPU usage monitor for a task.
4990 task_cpu_usage_monitor_ctl(task_t task
, uint32_t *flags
)
4992 int error
= KERN_SUCCESS
;
4994 if (*flags
& CPUMON_MAKE_FATAL
) {
4995 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_CPUMON
;
4997 error
= KERN_INVALID_ARGUMENT
;
5004 * Control the wakeups monitor for a task.
5007 task_wakeups_monitor_ctl(task_t task
, uint32_t *flags
, int32_t *rate_hz
)
5009 ledger_t ledger
= task
->ledger
;
5012 if (*flags
& WAKEMON_GET_PARAMS
) {
5013 ledger_amount_t limit
;
5016 ledger_get_limit(ledger
, task_ledgers
.interrupt_wakeups
, &limit
);
5017 ledger_get_period(ledger
, task_ledgers
.interrupt_wakeups
, &period
);
5019 if (limit
!= LEDGER_LIMIT_INFINITY
) {
5021 * An active limit means the wakeups monitor is enabled.
5023 *rate_hz
= (int32_t)(limit
/ (int64_t)(period
/ NSEC_PER_SEC
));
5024 *flags
= WAKEMON_ENABLE
;
5025 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
5026 *flags
|= WAKEMON_MAKE_FATAL
;
5029 *flags
= WAKEMON_DISABLE
;
5034 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
5037 return KERN_SUCCESS
;
5040 if (*flags
& WAKEMON_ENABLE
) {
5041 if (*flags
& WAKEMON_SET_DEFAULTS
) {
5042 *rate_hz
= task_wakeups_monitor_rate
;
5045 #ifndef CONFIG_NOMONITORS
5046 if (*flags
& WAKEMON_MAKE_FATAL
) {
5047 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
5049 #endif /* CONFIG_NOMONITORS */
5051 if (*rate_hz
<= 0) {
5053 return KERN_INVALID_ARGUMENT
;
5056 #ifndef CONFIG_NOMONITORS
5057 ledger_set_limit(ledger
, task_ledgers
.interrupt_wakeups
, *rate_hz
* task_wakeups_monitor_interval
,
5058 task_wakeups_monitor_ustackshots_trigger_pct
);
5059 ledger_set_period(ledger
, task_ledgers
.interrupt_wakeups
, task_wakeups_monitor_interval
* NSEC_PER_SEC
);
5060 ledger_enable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
5061 #endif /* CONFIG_NOMONITORS */
5062 } else if (*flags
& WAKEMON_DISABLE
) {
5064 * Caller wishes to disable wakeups monitor on the task.
5066 * Disable telemetry if it was triggered by the wakeups monitor, and
5067 * remove the limit & callback on the wakeups ledger entry.
5069 #if CONFIG_TELEMETRY
5070 telemetry_task_ctl_locked(task
, TF_WAKEMON_WARNING
, 0);
5072 ledger_disable_refill(ledger
, task_ledgers
.interrupt_wakeups
);
5073 ledger_disable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
5077 return KERN_SUCCESS
;
5081 task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
5083 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
5084 #if CONFIG_TELEMETRY
5086 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
5087 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
5089 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 1);
5094 #if CONFIG_TELEMETRY
5096 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
5097 * exceeded the limit, turn telemetry off for the task.
5099 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 0);
5103 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS();
5107 void __attribute__((noinline
))
5108 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
5110 task_t task
= current_task();
5112 const char *procname
= "unknown";
5115 #ifdef EXC_RESOURCE_MONITORS
5116 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5117 #endif /* EXC_RESOURCE_MONITORS */
5118 struct ledger_entry_info lei
;
5121 pid
= proc_selfpid();
5122 if (task
->bsd_info
!= NULL
)
5123 procname
= proc_name_address(current_task()->bsd_info
);
5126 ledger_get_entry_info(task
->ledger
, task_ledgers
.interrupt_wakeups
, &lei
);
5129 * Disable the exception notification so we don't overwhelm
5130 * the listener with an endless stream of redundant exceptions.
5131 * TODO: detect whether another thread is already reporting the violation.
5133 uint32_t flags
= WAKEMON_DISABLE
;
5134 task_wakeups_monitor_ctl(task
, &flags
, NULL
);
5136 fatal
= task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
5137 trace_resource_violation(RMON_CPUWAKES_VIOLATED
, &lei
);
5138 printf("process %s[%d] caught waking the CPU %llu times "
5139 "over ~%llu seconds, averaging %llu wakes / second and "
5140 "violating a %slimit of %llu wakes over %llu seconds.\n",
5142 lei
.lei_balance
, lei
.lei_last_refill
/ NSEC_PER_SEC
,
5143 lei
.lei_last_refill
== 0 ? 0 :
5144 (NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
),
5145 fatal
? "FATAL " : "",
5146 lei
.lei_limit
, lei
.lei_refill_period
/ NSEC_PER_SEC
);
5148 kr
= send_resource_violation(send_cpu_wakes_violation
, task
, &lei
,
5149 fatal
? kRNFatalLimitFlag
: 0);
5151 printf("send_resource_violation(CPU wakes, ...): error %#x\n", kr
);
5154 #ifdef EXC_RESOURCE_MONITORS
5155 if (disable_exc_resource
) {
5156 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5157 "supressed by a boot-arg\n", procname
, pid
);
5161 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5162 "supressed due to audio playback\n", procname
, pid
);
5165 if (lei
.lei_last_refill
== 0) {
5166 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
5167 "supressed due to lei.lei_last_refill = 0 \n", procname
, pid
);
5170 code
[0] = code
[1] = 0;
5171 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_WAKEUPS
);
5172 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_WAKEUPS_MONITOR
);
5173 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code
[0],
5174 NSEC_PER_SEC
* lei
.lei_limit
/ lei
.lei_refill_period
);
5175 EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code
[0],
5176 lei
.lei_last_refill
);
5177 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code
[1],
5178 NSEC_PER_SEC
* lei
.lei_balance
/ lei
.lei_last_refill
);
5179 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5180 #endif /* EXC_RESOURCE_MONITORS */
5183 task_terminate_internal(task
);
5188 global_update_logical_writes(int64_t io_delta
)
5190 int64_t old_count
, new_count
;
5191 boolean_t needs_telemetry
;
5194 new_count
= old_count
= global_logical_writes_count
;
5195 new_count
+= io_delta
;
5196 if (new_count
>= io_telemetry_limit
) {
5198 needs_telemetry
= TRUE
;
5200 needs_telemetry
= FALSE
;
5202 } while(!OSCompareAndSwap64(old_count
, new_count
, &global_logical_writes_count
));
5203 return needs_telemetry
;
5206 void task_update_logical_writes(task_t task
, uint32_t io_size
, int flags
, void *vp
)
5208 int64_t io_delta
= 0;
5209 boolean_t needs_telemetry
= FALSE
;
5211 if ((!task
) || (!io_size
) || (!vp
))
5214 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_DATA_WRITE
)) | DBG_FUNC_NONE
,
5215 task_pid(task
), io_size
, flags
, (uintptr_t)VM_KERNEL_ADDRPERM(vp
), 0);
5216 DTRACE_IO4(logical_writes
, struct task
*, task
, uint32_t, io_size
, int, flags
, vnode
*, vp
);
5218 case TASK_WRITE_IMMEDIATE
:
5219 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_immediate_writes
));
5220 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5222 case TASK_WRITE_DEFERRED
:
5223 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_deferred_writes
));
5224 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5226 case TASK_WRITE_INVALIDATED
:
5227 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_invalidated_writes
));
5228 ledger_debit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5230 case TASK_WRITE_METADATA
:
5231 OSAddAtomic64(io_size
, (SInt64
*)&(task
->task_metadata_writes
));
5232 ledger_credit(task
->ledger
, task_ledgers
.logical_writes
, io_size
);
5236 io_delta
= (flags
== TASK_WRITE_INVALIDATED
) ? ((int64_t)io_size
* -1ll) : ((int64_t)io_size
);
5237 if (io_telemetry_limit
!= 0) {
5238 /* If io_telemetry_limit is 0, disable global updates and I/O telemetry */
5239 needs_telemetry
= global_update_logical_writes(io_delta
);
5240 if (needs_telemetry
) {
5241 act_set_io_telemetry_ast(current_thread());
5247 * Control the I/O monitor for a task.
5250 task_io_monitor_ctl(task_t task
, uint32_t *flags
)
5252 ledger_t ledger
= task
->ledger
;
5255 if (*flags
& IOMON_ENABLE
) {
5256 /* Configure the physical I/O ledger */
5257 ledger_set_limit(ledger
, task_ledgers
.physical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5258 ledger_set_period(ledger
, task_ledgers
.physical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5260 /* Configure the logical I/O ledger */
5261 ledger_set_limit(ledger
, task_ledgers
.logical_writes
, (task_iomon_limit_mb
* 1024 * 1024), 0);
5262 ledger_set_period(ledger
, task_ledgers
.logical_writes
, (task_iomon_interval_secs
* NSEC_PER_SEC
));
5264 } else if (*flags
& IOMON_DISABLE
) {
5266 * Caller wishes to disable I/O monitor on the task.
5268 ledger_disable_refill(ledger
, task_ledgers
.physical_writes
);
5269 ledger_disable_callback(ledger
, task_ledgers
.physical_writes
);
5270 ledger_disable_refill(ledger
, task_ledgers
.logical_writes
);
5271 ledger_disable_callback(ledger
, task_ledgers
.logical_writes
);
5275 return KERN_SUCCESS
;
5279 task_io_rate_exceeded(int warning
, const void *param0
, __unused
const void *param1
)
5282 SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO((int)param0
);
5286 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor
)
5289 task_t task
= current_task();
5290 #ifdef EXC_RESOURCE_MONITORS
5291 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
5292 #endif /* EXC_RESOURCE_MONITORS */
5293 struct ledger_entry_info lei
;
5297 pid
= proc_selfpid();
5300 * Get the ledger entry info. We need to do this before disabling the exception
5301 * to get correct values for all fields.
5304 case FLAVOR_IO_PHYSICAL_WRITES
:
5305 ledger_get_entry_info(task
->ledger
, task_ledgers
.physical_writes
, &lei
);
5307 case FLAVOR_IO_LOGICAL_WRITES
:
5308 ledger_get_entry_info(task
->ledger
, task_ledgers
.logical_writes
, &lei
);
5314 * Disable the exception notification so we don't overwhelm
5315 * the listener with an endless stream of redundant exceptions.
5316 * TODO: detect whether another thread is already reporting the violation.
5318 uint32_t flags
= IOMON_DISABLE
;
5319 task_io_monitor_ctl(task
, &flags
);
5321 if (flavor
== FLAVOR_IO_LOGICAL_WRITES
) {
5322 trace_resource_violation(RMON_LOGWRITES_VIOLATED
, &lei
);
5324 printf("process [%d] caught causing excessive I/O (flavor: %d). Task I/O: %lld MB. [Limit : %lld MB per %lld secs]\n",
5325 pid
, flavor
, (lei
.lei_balance
/ (1024 * 1024)), (lei
.lei_limit
/ (1024 * 1024)), (lei
.lei_refill_period
/ NSEC_PER_SEC
));
5327 kr
= send_resource_violation(send_disk_writes_violation
, task
, &lei
, kRNFlagsNone
);
5329 printf("send_resource_violation(disk_writes, ...): error %#x\n", kr
);
5332 #ifdef EXC_RESOURCE_MONITORS
5333 code
[0] = code
[1] = 0;
5334 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_IO
);
5335 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], flavor
);
5336 EXC_RESOURCE_IO_ENCODE_INTERVAL(code
[0], (lei
.lei_refill_period
/ NSEC_PER_SEC
));
5337 EXC_RESOURCE_IO_ENCODE_LIMIT(code
[0], (lei
.lei_limit
/ (1024 * 1024)));
5338 EXC_RESOURCE_IO_ENCODE_OBSERVED(code
[1], (lei
.lei_balance
/ (1024 * 1024)));
5339 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
5340 #endif /* EXC_RESOURCE_MONITORS */
5343 /* Placeholders for the task set/get voucher interfaces */
5345 task_get_mach_voucher(
5347 mach_voucher_selector_t __unused which
,
5348 ipc_voucher_t
*voucher
)
5350 if (TASK_NULL
== task
)
5351 return KERN_INVALID_TASK
;
5354 return KERN_SUCCESS
;
5358 task_set_mach_voucher(
5360 ipc_voucher_t __unused voucher
)
5362 if (TASK_NULL
== task
)
5363 return KERN_INVALID_TASK
;
5365 return KERN_SUCCESS
;
5369 task_swap_mach_voucher(
5371 ipc_voucher_t new_voucher
,
5372 ipc_voucher_t
*in_out_old_voucher
)
5374 if (TASK_NULL
== task
)
5375 return KERN_INVALID_TASK
;
5377 *in_out_old_voucher
= new_voucher
;
5378 return KERN_SUCCESS
;
5381 void task_set_gpu_denied(task_t task
, boolean_t denied
)
5386 task
->t_flags
|= TF_GPU_DENIED
;
5388 task
->t_flags
&= ~TF_GPU_DENIED
;
5394 boolean_t
task_is_gpu_denied(task_t task
)
5396 /* We don't need the lock to read this flag */
5397 return (task
->t_flags
& TF_GPU_DENIED
) ? TRUE
: FALSE
;
5401 uint64_t get_task_memory_region_count(task_t task
)
5404 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
5405 return((uint64_t)get_map_nentries(map
));
5409 kdebug_trace_dyld_internal(uint32_t base_code
,
5410 struct dyld_kernel_image_info
*info
)
5412 static_assert(sizeof(info
->uuid
) >= 16);
5414 #if defined(__LP64__)
5415 uint64_t *uuid
= (uint64_t *)&(info
->uuid
);
5417 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5418 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
), uuid
[0],
5419 uuid
[1], info
->load_addr
,
5420 (uint64_t)info
->fsid
.val
[0] | ((uint64_t)info
->fsid
.val
[1] << 32),
5422 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5423 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 1),
5424 (uint64_t)info
->fsobjid
.fid_objno
|
5425 ((uint64_t)info
->fsobjid
.fid_generation
<< 32),
5427 #else /* defined(__LP64__) */
5428 uint32_t *uuid
= (uint32_t *)&(info
->uuid
);
5430 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5431 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 2), uuid
[0],
5432 uuid
[1], uuid
[2], uuid
[3], 0);
5433 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5434 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 3),
5435 (uint32_t)info
->load_addr
, info
->fsid
.val
[0], info
->fsid
.val
[1],
5436 info
->fsobjid
.fid_objno
, 0);
5437 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5438 KDBG_EVENTID(DBG_DYLD
, DBG_DYLD_UUID
, base_code
+ 4),
5439 info
->fsobjid
.fid_generation
, 0, 0, 0, 0);
5440 #endif /* !defined(__LP64__) */
5443 static kern_return_t
5444 kdebug_trace_dyld(task_t task
, uint32_t base_code
,
5445 vm_map_copy_t infos_copy
, mach_msg_type_number_t infos_len
)
5448 dyld_kernel_image_info_array_t infos
;
5449 vm_map_offset_t map_data
;
5452 assert(infos_copy
!= NULL
);
5454 if (task
== NULL
|| task
!= current_task()) {
5455 return KERN_INVALID_TASK
;
5458 kr
= vm_map_copyout(ipc_kernel_map
, &map_data
, (vm_map_copy_t
)infos_copy
);
5459 if (kr
!= KERN_SUCCESS
) {
5463 infos
= CAST_DOWN(dyld_kernel_image_info_array_t
, map_data
);
5465 for (mach_msg_type_number_t i
= 0; i
< infos_len
; i
++) {
5466 kdebug_trace_dyld_internal(base_code
, &(infos
[i
]));
5469 data
= CAST_DOWN(vm_offset_t
, map_data
);
5470 mach_vm_deallocate(ipc_kernel_map
, data
, infos_len
* sizeof(infos
[0]));
5471 return KERN_SUCCESS
;
5475 task_register_dyld_image_infos(task_t task
,
5476 dyld_kernel_image_info_array_t infos_copy
,
5477 mach_msg_type_number_t infos_len
)
5479 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_MAP_A
,
5480 (vm_map_copy_t
)infos_copy
, infos_len
);
5484 task_unregister_dyld_image_infos(task_t task
,
5485 dyld_kernel_image_info_array_t infos_copy
,
5486 mach_msg_type_number_t infos_len
)
5488 return kdebug_trace_dyld(task
, DBG_DYLD_UUID_UNMAP_A
,
5489 (vm_map_copy_t
)infos_copy
, infos_len
);
5493 task_get_dyld_image_infos(__unused task_t task
,
5494 __unused dyld_kernel_image_info_array_t
* dyld_images
,
5495 __unused mach_msg_type_number_t
* dyld_imagesCnt
)
5497 return KERN_NOT_SUPPORTED
;
5501 task_register_dyld_shared_cache_image_info(task_t task
,
5502 dyld_kernel_image_info_t cache_img
,
5503 __unused boolean_t no_cache
,
5504 __unused boolean_t private_cache
)
5506 if (task
== NULL
|| task
!= current_task()) {
5507 return KERN_INVALID_TASK
;
5510 kdebug_trace_dyld_internal(DBG_DYLD_UUID_SHARED_CACHE_A
, &cache_img
);
5511 return KERN_SUCCESS
;
5515 task_register_dyld_set_dyld_state(__unused task_t task
,
5516 __unused
uint8_t dyld_state
)
5518 return KERN_NOT_SUPPORTED
;
5522 task_register_dyld_get_process_state(__unused task_t task
,
5523 __unused dyld_kernel_process_info_t
* dyld_process_state
)
5525 return KERN_NOT_SUPPORTED
;
5528 #if CONFIG_SECLUDED_MEMORY
5529 int num_tasks_can_use_secluded_mem
= 0;
5532 task_set_can_use_secluded_mem(
5534 boolean_t can_use_secluded_mem
)
5536 if (!task
->task_could_use_secluded_mem
) {
5540 task_set_can_use_secluded_mem_locked(task
, can_use_secluded_mem
);
5545 task_set_can_use_secluded_mem_locked(
5547 boolean_t can_use_secluded_mem
)
5549 assert(task
->task_could_use_secluded_mem
);
5550 if (can_use_secluded_mem
&&
5551 secluded_for_apps
&& /* global boot-arg */
5552 !task
->task_can_use_secluded_mem
) {
5553 assert(num_tasks_can_use_secluded_mem
>= 0);
5555 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
5556 task
->task_can_use_secluded_mem
= TRUE
;
5557 } else if (!can_use_secluded_mem
&&
5558 task
->task_can_use_secluded_mem
) {
5559 assert(num_tasks_can_use_secluded_mem
> 0);
5561 (volatile SInt32
*)&num_tasks_can_use_secluded_mem
);
5562 task
->task_can_use_secluded_mem
= FALSE
;
5567 task_set_could_use_secluded_mem(
5569 boolean_t could_use_secluded_mem
)
5571 task
->task_could_use_secluded_mem
= could_use_secluded_mem
;
5575 task_set_could_also_use_secluded_mem(
5577 boolean_t could_also_use_secluded_mem
)
5579 task
->task_could_also_use_secluded_mem
= could_also_use_secluded_mem
;
5583 task_can_use_secluded_mem(
5586 if (task
->task_can_use_secluded_mem
) {
5587 assert(task
->task_could_use_secluded_mem
);
5588 assert(num_tasks_can_use_secluded_mem
> 0);
5591 if (task
->task_could_also_use_secluded_mem
&&
5592 num_tasks_can_use_secluded_mem
> 0) {
5593 assert(num_tasks_can_use_secluded_mem
> 0);
5600 task_could_use_secluded_mem(
5603 return task
->task_could_use_secluded_mem
;
5605 #endif /* CONFIG_SECLUDED_MEMORY */
5608 task_io_user_clients(task_t task
)
5610 return (&task
->io_user_clients
);