2 * Copyright (c) 2000-2010 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.
90 #include <platforms.h>
92 #include <mach/mach_types.h>
93 #include <mach/boolean.h>
94 #include <mach/host_priv.h>
95 #include <mach/machine/vm_types.h>
96 #include <mach/vm_param.h>
97 #include <mach/semaphore.h>
98 #include <mach/task_info.h>
99 #include <mach/task_special_ports.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/zalloc.h>
112 #include <kern/kalloc.h>
113 #include <kern/processor.h>
114 #include <kern/sched_prim.h> /* for thread_wakeup */
115 #include <kern/ipc_tt.h>
116 #include <kern/host.h>
117 #include <kern/clock.h>
118 #include <kern/timer.h>
119 #include <kern/assert.h>
120 #include <kern/sync_lock.h>
121 #include <kern/affinity.h>
122 #include <kern/exc_resource.h>
124 #include <kern/telemetry.h>
128 #include <vm/vm_map.h>
129 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
130 #include <vm/vm_pageout.h>
131 #include <vm/vm_protos.h>
132 #include <vm/vm_purgeable_internal.h>
134 #include <sys/resource.h>
136 * Exported interfaces
139 #include <mach/task_server.h>
140 #include <mach/mach_host_server.h>
141 #include <mach/host_security_server.h>
142 #include <mach/mach_port_server.h>
143 #include <mach/security_server.h>
145 #include <vm/vm_shared_region.h>
148 #include <security/mac_mach_internal.h>
153 #endif /* CONFIG_COUNTERS */
155 #include <libkern/OSDebug.h>
156 #include <libkern/OSAtomic.h>
160 lck_attr_t task_lck_attr
;
161 lck_grp_t task_lck_grp
;
162 lck_grp_attr_t task_lck_grp_attr
;
164 zinfo_usage_store_t tasks_tkm_private
;
165 zinfo_usage_store_t tasks_tkm_shared
;
167 /* A container to accumulate statistics for expired tasks */
168 expired_task_statistics_t dead_task_statistics
;
169 lck_spin_t dead_task_statistics_lock
;
171 static ledger_template_t task_ledger_template
= NULL
;
172 struct _task_ledger_indices task_ledgers
__attribute__((used
)) = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
173 void init_task_ledgers(void);
174 void task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
175 void task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
);
176 void __attribute__((noinline
)) THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS__SENDING_EXC_RESOURCE(void);
177 void __attribute__((noinline
)) THIS_PROCESS_CROSSED_HIGH_WATERMARK__SENDING_EXC_RESOURCE(int max_footprint_mb
);
178 int coredump(void *core_proc
, int reserve_mb
, int ignore_ulimit
);
180 kern_return_t
task_suspend_internal(task_t
);
181 kern_return_t
task_resume_internal(task_t
);
183 void proc_init_cpumon_params(void);
185 // Warn tasks when they hit 80% of their memory limit.
186 #define PHYS_FOOTPRINT_WARNING_LEVEL 80
188 #define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT 150 /* wakeups per second */
189 #define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL 300 /* in seconds. */
192 * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
194 * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
195 * stacktraces, aka micro-stackshots)
197 #define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER 70
199 int task_wakeups_monitor_interval
; /* In seconds. Time period over which wakeups rate is observed */
200 int task_wakeups_monitor_rate
; /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
202 int task_wakeups_monitor_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
204 int disable_exc_resource
; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
206 int max_task_footprint
= 0; /* Per-task limit on physical memory consumption */
207 int task_max
= CONFIG_TASK_MAX
; /* Max number of tasks */
209 int hwm_user_cores
= 0; /* high watermark violations generate user core files */
212 extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
213 extern int proc_pid(struct proc
*p
);
214 extern int proc_selfpid(void);
215 extern char *proc_name_address(struct proc
*p
);
217 extern void memorystatus_on_ledger_footprint_exceeded(int warning
, const int max_footprint_mb
);
223 void task_hold_locked(
225 void task_wait_locked(
227 boolean_t until_not_runnable
);
228 void task_release_locked(
232 void task_synchronizer_destroy_all(
235 int check_for_tasksuspend(
239 task_backing_store_privileged(
243 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
254 #if defined(__i386__) || defined(__x86_64__)
256 #endif /* defined(__i386__) || defined(__x86_64__) */
261 if (task_has_64BitAddr(task
))
263 task_set_64BitAddr(task
);
265 if ( !task_has_64BitAddr(task
))
267 task_clear_64BitAddr(task
);
269 /* FIXME: On x86, the thread save state flavor can diverge from the
270 * task's 64-bit feature flag due to the 32-bit/64-bit register save
271 * state dichotomy. Since we can be pre-empted in this interval,
272 * certain routines may observe the thread as being in an inconsistent
273 * state with respect to its task's 64-bitness.
276 #if defined(__i386__) || defined(__x86_64__)
277 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
278 thread_mtx_lock(thread
);
279 machine_thread_switch_addrmode(thread
);
280 thread_mtx_unlock(thread
);
282 #endif /* defined(__i386__) || defined(__x86_64__) */
290 task_set_dyld_info(task_t task
, mach_vm_address_t addr
, mach_vm_size_t size
)
293 task
->all_image_info_addr
= addr
;
294 task
->all_image_info_size
= size
;
298 #if TASK_REFERENCE_LEAK_DEBUG
299 #include <kern/btlog.h>
301 decl_simple_lock_data(static,task_ref_lock
);
302 static btlog_t
*task_ref_btlog
;
303 #define TASK_REF_OP_INCR 0x1
304 #define TASK_REF_OP_DECR 0x2
306 #define TASK_REF_BTDEPTH 7
309 task_ref_lock_lock(void *context
)
311 simple_lock((simple_lock_t
)context
);
314 task_ref_lock_unlock(void *context
)
316 simple_unlock((simple_lock_t
)context
);
320 task_reference_internal(task_t task
)
322 void * bt
[TASK_REF_BTDEPTH
];
325 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
327 (void)hw_atomic_add(&(task
)->ref_count
, 1);
328 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_INCR
,
333 task_deallocate_internal(task_t task
)
335 void * bt
[TASK_REF_BTDEPTH
];
338 numsaved
= OSBacktrace(bt
, TASK_REF_BTDEPTH
);
340 btlog_add_entry(task_ref_btlog
, task
, TASK_REF_OP_DECR
,
342 return hw_atomic_sub(&(task
)->ref_count
, 1);
345 #endif /* TASK_REFERENCE_LEAK_DEBUG */
351 lck_grp_attr_setdefault(&task_lck_grp_attr
);
352 lck_grp_init(&task_lck_grp
, "task", &task_lck_grp_attr
);
353 lck_attr_setdefault(&task_lck_attr
);
354 lck_mtx_init(&tasks_threads_lock
, &task_lck_grp
, &task_lck_attr
);
358 task_max
* sizeof(struct task
),
359 TASK_CHUNK
* sizeof(struct task
),
362 zone_change(task_zone
, Z_NOENCRYPT
, TRUE
);
365 * Configure per-task memory limit. The boot arg takes precedence over the
368 if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint
,
369 sizeof (max_task_footprint
))) {
370 max_task_footprint
= 0;
373 if (max_task_footprint
== 0) {
375 * No limit was found in boot-args, so go look in the device tree.
377 if (!PE_get_default("kern.max_task_pmem", &max_task_footprint
,
378 sizeof(max_task_footprint
))) {
379 max_task_footprint
= 0;
383 if (max_task_footprint
!= 0) {
385 if (max_task_footprint
< 50) {
386 printf("Warning: max_task_pmem %d below minimum.\n",
388 max_task_footprint
= 50;
390 printf("Limiting task physical memory footprint to %d MB\n",
392 max_task_footprint
*= 1024 * 1024; // Convert MB to bytes
394 printf("Warning: max_task_footprint specified, but jetsam not configured; ignoring.\n");
398 if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores
,
399 sizeof (hwm_user_cores
))) {
403 proc_init_cpumon_params();
405 if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate
, sizeof (task_wakeups_monitor_rate
))) {
406 task_wakeups_monitor_rate
= TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT
;
409 if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval
, sizeof (task_wakeups_monitor_interval
))) {
410 task_wakeups_monitor_interval
= TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL
;
413 if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct
,
414 sizeof (task_wakeups_monitor_ustackshots_trigger_pct
))) {
415 task_wakeups_monitor_ustackshots_trigger_pct
= TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER
;
418 if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource
,
419 sizeof (disable_exc_resource
))) {
420 disable_exc_resource
= 0;
425 #if TASK_REFERENCE_LEAK_DEBUG
426 simple_lock_init(&task_ref_lock
, 0);
427 task_ref_btlog
= btlog_create(100000,
430 task_ref_lock_unlock
,
432 assert(task_ref_btlog
);
436 * Create the kernel task as the first task.
439 if (task_create_internal(TASK_NULL
, FALSE
, TRUE
, &kernel_task
) != KERN_SUCCESS
)
441 if (task_create_internal(TASK_NULL
, FALSE
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
443 panic("task_init\n");
445 vm_map_deallocate(kernel_task
->map
);
446 kernel_task
->map
= kernel_map
;
447 lck_spin_init(&dead_task_statistics_lock
, &task_lck_grp
, &task_lck_attr
);
451 * Create a task running in the kernel address space. It may
452 * have its own map of size mem_size and may have ipc privileges.
456 __unused task_t parent_task
,
457 __unused vm_offset_t map_base
,
458 __unused vm_size_t map_size
,
459 __unused task_t
*child_task
)
461 return (KERN_INVALID_ARGUMENT
);
467 __unused ledger_port_array_t ledger_ports
,
468 __unused mach_msg_type_number_t num_ledger_ports
,
469 __unused boolean_t inherit_memory
,
470 __unused task_t
*child_task
) /* OUT */
472 if (parent_task
== TASK_NULL
)
473 return(KERN_INVALID_ARGUMENT
);
476 * No longer supported: too many calls assume that a task has a valid
479 return(KERN_FAILURE
);
483 host_security_create_task_token(
484 host_security_t host_security
,
486 __unused security_token_t sec_token
,
487 __unused audit_token_t audit_token
,
488 __unused host_priv_t host_priv
,
489 __unused ledger_port_array_t ledger_ports
,
490 __unused mach_msg_type_number_t num_ledger_ports
,
491 __unused boolean_t inherit_memory
,
492 __unused task_t
*child_task
) /* OUT */
494 if (parent_task
== TASK_NULL
)
495 return(KERN_INVALID_ARGUMENT
);
497 if (host_security
== HOST_NULL
)
498 return(KERN_INVALID_SECURITY
);
501 * No longer supported.
503 return(KERN_FAILURE
);
511 * Physical footprint: This is the sum of:
512 * + phys_mem [task's resident memory]
517 * IOKit mappings: The total size of all IOKit mappings in this task [regardless of clean/dirty state].
520 * Physical compressed: Amount of this task's resident memory which is held by the compressor.
521 * Such memory is no longer actually resident for the task [i.e., resident in its pmap],
522 * and could be either decompressed back into memory, or paged out to storage, depending
523 * on our implementation.
526 init_task_ledgers(void)
530 assert(task_ledger_template
== NULL
);
531 assert(kernel_task
== TASK_NULL
);
533 if ((t
= ledger_template_create("Per-task ledger")) == NULL
)
534 panic("couldn't create task ledger template");
536 task_ledgers
.cpu_time
= ledger_entry_add(t
, "cpu_time", "sched", "ns");
537 task_ledgers
.tkm_private
= ledger_entry_add(t
, "tkm_private",
539 task_ledgers
.tkm_shared
= ledger_entry_add(t
, "tkm_shared", "physmem",
541 task_ledgers
.phys_mem
= ledger_entry_add(t
, "phys_mem", "physmem",
543 task_ledgers
.wired_mem
= ledger_entry_add(t
, "wired_mem", "physmem",
545 task_ledgers
.iokit_mem
= ledger_entry_add(t
, "iokit_mem", "mappings",
547 task_ledgers
.phys_footprint
= ledger_entry_add(t
, "phys_footprint", "physmem",
549 task_ledgers
.phys_compressed
= ledger_entry_add(t
, "phys_compressed", "physmem",
551 task_ledgers
.platform_idle_wakeups
= ledger_entry_add(t
, "platform_idle_wakeups", "power",
553 task_ledgers
.interrupt_wakeups
= ledger_entry_add(t
, "interrupt_wakeups", "power",
556 if ((task_ledgers
.cpu_time
< 0) || (task_ledgers
.tkm_private
< 0) ||
557 (task_ledgers
.tkm_shared
< 0) || (task_ledgers
.phys_mem
< 0) ||
558 (task_ledgers
.wired_mem
< 0) || (task_ledgers
.iokit_mem
< 0) ||
559 (task_ledgers
.phys_footprint
< 0) || (task_ledgers
.phys_compressed
< 0) ||
560 (task_ledgers
.platform_idle_wakeups
< 0) || (task_ledgers
.interrupt_wakeups
< 0)) {
561 panic("couldn't create entries for task ledger template");
564 ledger_track_maximum(t
, task_ledgers
.phys_footprint
, 60);
567 ledger_set_callback(t
, task_ledgers
.phys_footprint
, task_footprint_exceeded
, NULL
, NULL
);
570 ledger_set_callback(t
, task_ledgers
.interrupt_wakeups
,
571 task_wakeups_rate_exceeded
, NULL
, NULL
);
573 task_ledger_template
= t
;
577 task_create_internal(
579 boolean_t inherit_memory
,
581 task_t
*child_task
) /* OUT */
584 vm_shared_region_t shared_region
;
585 ledger_t ledger
= NULL
;
587 new_task
= (task_t
) zalloc(task_zone
);
589 if (new_task
== TASK_NULL
)
590 return(KERN_RESOURCE_SHORTAGE
);
592 /* one ref for just being alive; one for our caller */
593 new_task
->ref_count
= 2;
595 /* allocate with active entries */
596 assert(task_ledger_template
!= NULL
);
597 if ((ledger
= ledger_instantiate(task_ledger_template
,
598 LEDGER_CREATE_ACTIVE_ENTRIES
)) == NULL
) {
599 zfree(task_zone
, new_task
);
600 return(KERN_RESOURCE_SHORTAGE
);
603 new_task
->ledger
= ledger
;
605 /* if inherit_memory is true, parent_task MUST not be NULL */
607 new_task
->map
= vm_map_fork(ledger
, parent_task
->map
);
609 new_task
->map
= vm_map_create(pmap_create(ledger
, 0, is_64bit
),
610 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
611 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
613 /* Inherit memlock limit from parent */
615 vm_map_set_user_wire_limit(new_task
->map
, (vm_size_t
)parent_task
->map
->user_wire_limit
);
617 lck_mtx_init(&new_task
->lock
, &task_lck_grp
, &task_lck_attr
);
618 queue_init(&new_task
->threads
);
619 new_task
->suspend_count
= 0;
620 new_task
->thread_count
= 0;
621 new_task
->active_thread_count
= 0;
622 new_task
->user_stop_count
= 0;
623 new_task
->legacy_stop_count
= 0;
624 new_task
->active
= TRUE
;
625 new_task
->halting
= FALSE
;
626 new_task
->user_data
= NULL
;
627 new_task
->faults
= 0;
628 new_task
->cow_faults
= 0;
629 new_task
->pageins
= 0;
630 new_task
->messages_sent
= 0;
631 new_task
->messages_received
= 0;
632 new_task
->syscalls_mach
= 0;
633 new_task
->priv_flags
= 0;
634 new_task
->syscalls_unix
=0;
635 new_task
->c_switch
= new_task
->p_switch
= new_task
->ps_switch
= 0;
636 new_task
->t_flags
= 0;
637 new_task
->importance
= 0;
639 zinfo_task_init(new_task
);
642 new_task
->bsd_info
= NULL
;
643 #endif /* MACH_BSD */
646 if (max_task_footprint
!= 0) {
647 ledger_set_limit(ledger
, task_ledgers
.phys_footprint
, max_task_footprint
, PHYS_FOOTPRINT_WARNING_LEVEL
);
651 if (task_wakeups_monitor_rate
!= 0) {
652 uint32_t flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
653 int32_t rate
; // Ignored because of WAKEMON_SET_DEFAULTS
654 task_wakeups_monitor_ctl(new_task
, &flags
, &rate
);
657 #if defined(__i386__) || defined(__x86_64__)
658 new_task
->i386_ldt
= 0;
661 new_task
->task_debug
= NULL
;
663 queue_init(&new_task
->semaphore_list
);
664 new_task
->semaphores_owned
= 0;
667 new_task
->label
= labelh_new(1);
668 mac_task_label_init (&new_task
->maclabel
);
671 ipc_task_init(new_task
, parent_task
);
673 new_task
->total_user_time
= 0;
674 new_task
->total_system_time
= 0;
676 new_task
->vtimers
= 0;
678 new_task
->shared_region
= NULL
;
680 new_task
->affinity_space
= NULL
;
683 new_task
->t_chud
= 0U;
686 new_task
->pidsuspended
= FALSE
;
687 new_task
->frozen
= FALSE
;
688 new_task
->changing_freeze_state
= FALSE
;
689 new_task
->rusage_cpu_flags
= 0;
690 new_task
->rusage_cpu_percentage
= 0;
691 new_task
->rusage_cpu_interval
= 0;
692 new_task
->rusage_cpu_deadline
= 0;
693 new_task
->rusage_cpu_callt
= NULL
;
695 new_task
->suspends_outstanding
= 0;
699 new_task
->low_mem_notified_warn
= 0;
700 new_task
->low_mem_notified_critical
= 0;
701 new_task
->purged_memory_warn
= 0;
702 new_task
->purged_memory_critical
= 0;
703 new_task
->mem_notify_reserved
= 0;
704 #if IMPORTANCE_INHERITANCE
705 new_task
->imp_receiver
= 0;
706 new_task
->imp_donor
= 0;
707 new_task
->imp_reserved
= 0;
708 new_task
->task_imp_assertcnt
= 0;
709 new_task
->task_imp_externcnt
= 0;
710 #endif /* IMPORTANCE_INHERITANCE */
712 #if defined(__x86_64__)
713 new_task
->uexc_range_start
= new_task
->uexc_range_size
= new_task
->uexc_handler
= 0;
716 new_task
->requested_policy
= default_task_requested_policy
;
717 new_task
->effective_policy
= default_task_effective_policy
;
718 new_task
->pended_policy
= default_task_pended_policy
;
720 if (parent_task
!= TASK_NULL
) {
721 new_task
->sec_token
= parent_task
->sec_token
;
722 new_task
->audit_token
= parent_task
->audit_token
;
724 /* inherit the parent's shared region */
725 shared_region
= vm_shared_region_get(parent_task
);
726 vm_shared_region_set(new_task
, shared_region
);
728 if(task_has_64BitAddr(parent_task
))
729 task_set_64BitAddr(new_task
);
730 new_task
->all_image_info_addr
= parent_task
->all_image_info_addr
;
731 new_task
->all_image_info_size
= parent_task
->all_image_info_size
;
733 #if defined(__i386__) || defined(__x86_64__)
734 if (inherit_memory
&& parent_task
->i386_ldt
)
735 new_task
->i386_ldt
= user_ldt_copy(parent_task
->i386_ldt
);
737 if (inherit_memory
&& parent_task
->affinity_space
)
738 task_affinity_create(parent_task
, new_task
);
740 new_task
->pset_hint
= parent_task
->pset_hint
= task_choose_pset(parent_task
);
742 #if IMPORTANCE_INHERITANCE
743 new_task
->imp_donor
= parent_task
->imp_donor
;
744 /* Embedded doesn't want this to inherit */
745 new_task
->imp_receiver
= parent_task
->imp_receiver
;
746 #endif /* IMPORTANCE_INHERITANCE */
748 new_task
->requested_policy
.t_apptype
= parent_task
->requested_policy
.t_apptype
;
750 new_task
->requested_policy
.int_darwinbg
= parent_task
->requested_policy
.int_darwinbg
;
751 new_task
->requested_policy
.ext_darwinbg
= parent_task
->requested_policy
.ext_darwinbg
;
752 new_task
->requested_policy
.int_iotier
= parent_task
->requested_policy
.int_iotier
;
753 new_task
->requested_policy
.ext_iotier
= parent_task
->requested_policy
.ext_iotier
;
754 new_task
->requested_policy
.int_iopassive
= parent_task
->requested_policy
.int_iopassive
;
755 new_task
->requested_policy
.ext_iopassive
= parent_task
->requested_policy
.ext_iopassive
;
756 new_task
->requested_policy
.bg_iotier
= parent_task
->requested_policy
.bg_iotier
;
757 new_task
->requested_policy
.terminated
= parent_task
->requested_policy
.terminated
;
759 task_policy_create(new_task
, parent_task
->requested_policy
.t_boosted
);
761 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
762 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
765 task_set_64BitAddr(new_task
);
767 new_task
->all_image_info_addr
= (mach_vm_address_t
)0;
768 new_task
->all_image_info_size
= (mach_vm_size_t
)0;
770 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
773 if (kernel_task
== TASK_NULL
) {
774 new_task
->priority
= BASEPRI_KERNEL
;
775 new_task
->max_priority
= MAXPRI_KERNEL
;
776 } else if (proc_get_effective_task_policy(new_task
, TASK_POLICY_LOWPRI_CPU
)) {
777 new_task
->priority
= MAXPRI_THROTTLE
;
778 new_task
->max_priority
= MAXPRI_THROTTLE
;
780 new_task
->priority
= BASEPRI_DEFAULT
;
781 new_task
->max_priority
= MAXPRI_USER
;
784 bzero(&new_task
->extmod_statistics
, sizeof(new_task
->extmod_statistics
));
785 new_task
->task_timer_wakeups_bin_1
= new_task
->task_timer_wakeups_bin_2
= 0;
786 lck_mtx_lock(&tasks_threads_lock
);
787 queue_enter(&tasks
, new_task
, task_t
, tasks
);
789 lck_mtx_unlock(&tasks_threads_lock
);
791 if (vm_backing_store_low
&& parent_task
!= NULL
)
792 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
794 new_task
->task_volatile_objects
= 0;
796 ipc_task_enable(new_task
);
798 *child_task
= new_task
;
799 return(KERN_SUCCESS
);
805 * Drop a reference on a task.
811 ledger_amount_t credit
, debit
, interrupt_wakeups
, platform_idle_wakeups
;
813 if (task
== TASK_NULL
)
816 if (task_deallocate_internal(task
) > 0)
819 lck_mtx_lock(&tasks_threads_lock
);
820 queue_remove(&terminated_tasks
, task
, task_t
, tasks
);
821 terminated_tasks_count
--;
822 lck_mtx_unlock(&tasks_threads_lock
);
825 * Give the machine dependent code a chance
826 * to perform cleanup before ripping apart
829 machine_task_terminate(task
);
831 ipc_task_terminate(task
);
833 if (task
->affinity_space
)
834 task_affinity_deallocate(task
);
836 vm_map_deallocate(task
->map
);
837 is_release(task
->itk_space
);
839 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
840 &interrupt_wakeups
, &debit
);
841 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
842 &platform_idle_wakeups
, &debit
);
844 /* Accumulate statistics for dead tasks */
845 lck_spin_lock(&dead_task_statistics_lock
);
846 dead_task_statistics
.total_user_time
+= task
->total_user_time
;
847 dead_task_statistics
.total_system_time
+= task
->total_system_time
;
849 dead_task_statistics
.task_interrupt_wakeups
+= interrupt_wakeups
;
850 dead_task_statistics
.task_platform_idle_wakeups
+= platform_idle_wakeups
;
852 dead_task_statistics
.task_timer_wakeups_bin_1
+= task
->task_timer_wakeups_bin_1
;
853 dead_task_statistics
.task_timer_wakeups_bin_2
+= task
->task_timer_wakeups_bin_2
;
855 lck_spin_unlock(&dead_task_statistics_lock
);
856 lck_mtx_destroy(&task
->lock
, &task_lck_grp
);
859 labelh_release(task
->label
);
862 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_private
, &credit
,
864 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_private
.alloc
);
865 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_private
.free
);
867 if (!ledger_get_entries(task
->ledger
, task_ledgers
.tkm_shared
, &credit
,
869 OSAddAtomic64(credit
, (int64_t *)&tasks_tkm_shared
.alloc
);
870 OSAddAtomic64(debit
, (int64_t *)&tasks_tkm_shared
.free
);
872 ledger_dereference(task
->ledger
);
873 zinfo_task_free(task
);
875 #if TASK_REFERENCE_LEAK_DEBUG
876 btlog_remove_entries_for_element(task_ref_btlog
, task
);
879 if (task
->task_volatile_objects
) {
881 * This task still "owns" some volatile VM objects.
882 * Disown them now to avoid leaving them pointing back at
885 vm_purgeable_disown(task
);
886 assert(task
->task_volatile_objects
== 0);
889 zfree(task_zone
, task
);
893 * task_name_deallocate:
895 * Drop a reference on a task name.
898 task_name_deallocate(
899 task_name_t task_name
)
901 return(task_deallocate((task_t
)task_name
));
905 * task_suspension_token_deallocate:
907 * Drop a reference on a task suspension token.
910 task_suspension_token_deallocate(
911 task_suspension_token_t token
)
913 return(task_deallocate((task_t
)token
));
919 * Terminate the specified task. See comments on thread_terminate
920 * (kern/thread.c) about problems with terminating the "current task."
927 if (task
== TASK_NULL
)
928 return (KERN_INVALID_ARGUMENT
);
931 return (KERN_FAILURE
);
933 return (task_terminate_internal(task
));
937 task_terminate_internal(
940 thread_t thread
, self
;
942 boolean_t interrupt_save
;
944 assert(task
!= kernel_task
);
946 self
= current_thread();
947 self_task
= self
->task
;
950 * Get the task locked and make sure that we are not racing
951 * with someone else trying to terminate us.
953 if (task
== self_task
)
956 if (task
< self_task
) {
958 task_lock(self_task
);
961 task_lock(self_task
);
967 * Task is already being terminated.
968 * Just return an error. If we are dying, this will
969 * just get us to our AST special handler and that
970 * will get us to finalize the termination of ourselves.
973 if (self_task
!= task
)
974 task_unlock(self_task
);
976 return (KERN_FAILURE
);
980 if (task
->suspends_outstanding
!= 0) {
981 printf("WARNING: %s (%d) exiting with %d outstanding suspensions\n",
982 proc_name_address(task
->bsd_info
), proc_pid(task
->bsd_info
),
983 task
->suspends_outstanding
);
987 if (self_task
!= task
)
988 task_unlock(self_task
);
991 * Make sure the current thread does not get aborted out of
992 * the waits inside these operations.
994 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
997 * Indicate that we want all the threads to stop executing
998 * at user space by holding the task (we would have held
999 * each thread independently in thread_terminate_internal -
1000 * but this way we may be more likely to already find it
1001 * held there). Mark the task inactive, and prevent
1002 * further task operations via the task port.
1004 task_hold_locked(task
);
1005 task
->active
= FALSE
;
1006 ipc_task_disable(task
);
1008 #if CONFIG_TELEMETRY
1010 * Notify telemetry that this task is going away.
1012 telemetry_task_ctl_locked(task
, TF_TELEMETRY
, 0);
1016 * Terminate each thread in the task.
1018 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1019 thread_terminate_internal(thread
);
1026 * Destroy all synchronizers owned by the task.
1028 task_synchronizer_destroy_all(task
);
1031 * Destroy the IPC space, leaving just a reference for it.
1033 ipc_space_terminate(task
->itk_space
);
1035 if (vm_map_has_4GB_pagezero(task
->map
))
1036 vm_map_clear_4GB_pagezero(task
->map
);
1039 * If the current thread is a member of the task
1040 * being terminated, then the last reference to
1041 * the task will not be dropped until the thread
1042 * is finally reaped. To avoid incurring the
1043 * expense of removing the address space regions
1044 * at reap time, we do it explictly here.
1046 vm_map_remove(task
->map
,
1047 task
->map
->min_offset
,
1048 task
->map
->max_offset
,
1051 /* release our shared region */
1052 vm_shared_region_set(task
, NULL
);
1054 lck_mtx_lock(&tasks_threads_lock
);
1055 queue_remove(&tasks
, task
, task_t
, tasks
);
1056 queue_enter(&terminated_tasks
, task
, task_t
, tasks
);
1058 terminated_tasks_count
++;
1059 lck_mtx_unlock(&tasks_threads_lock
);
1062 * We no longer need to guard against being aborted, so restore
1063 * the previous interruptible state.
1065 thread_interrupt_level(interrupt_save
);
1068 * Get rid of the task active reference on itself.
1070 task_deallocate(task
);
1072 return (KERN_SUCCESS
);
1078 * Shut the current task down (except for the current thread) in
1079 * preparation for dramatic changes to the task (probably exec).
1080 * We hold the task and mark all other threads in the task for
1087 thread_t thread
, self
;
1089 assert(task
!= kernel_task
);
1091 self
= current_thread();
1093 if (task
!= self
->task
)
1094 return (KERN_INVALID_ARGUMENT
);
1098 if (task
->halting
|| !task
->active
|| !self
->active
) {
1100 * Task or current thread is already being terminated.
1101 * Hurry up and return out of the current kernel context
1102 * so that we run our AST special handler to terminate
1107 return (KERN_FAILURE
);
1110 task
->halting
= TRUE
;
1112 if (task
->thread_count
> 1) {
1115 * Mark all the threads to keep them from starting any more
1116 * user-level execution. The thread_terminate_internal code
1117 * would do this on a thread by thread basis anyway, but this
1118 * gives us a better chance of not having to wait there.
1120 task_hold_locked(task
);
1123 * Terminate all the other threads in the task.
1125 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1127 thread_terminate_internal(thread
);
1130 task_release_locked(task
);
1133 return KERN_SUCCESS
;
1138 * task_complete_halt:
1140 * Complete task halt by waiting for threads to terminate, then clean
1141 * up task resources (VM, port namespace, etc...) and then let the
1142 * current thread go in the (practically empty) task context.
1145 task_complete_halt(task_t task
)
1148 assert(task
->halting
);
1149 assert(task
== current_task());
1152 * Wait for the other threads to get shut down.
1153 * When the last other thread is reaped, we'll be
1156 if (task
->thread_count
> 1) {
1157 assert_wait((event_t
)&task
->halting
, THREAD_UNINT
);
1159 thread_block(THREAD_CONTINUE_NULL
);
1165 * Give the machine dependent code a chance
1166 * to perform cleanup of task-level resources
1167 * associated with the current thread before
1168 * ripping apart the task.
1170 machine_task_terminate(task
);
1173 * Destroy all synchronizers owned by the task.
1175 task_synchronizer_destroy_all(task
);
1178 * Destroy the contents of the IPC space, leaving just
1179 * a reference for it.
1181 ipc_space_clean(task
->itk_space
);
1184 * Clean out the address space, as we are going to be
1185 * getting a new one.
1187 vm_map_remove(task
->map
, task
->map
->min_offset
,
1188 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
1190 task
->halting
= FALSE
;
1196 * Suspend execution of the specified task.
1197 * This is a recursive-style suspension of the task, a count of
1198 * suspends is maintained.
1200 * CONDITIONS: the task is locked and active.
1204 register task_t task
)
1206 register thread_t thread
;
1208 assert(task
->active
);
1210 if (task
->suspend_count
++ > 0)
1214 * Iterate through all the threads and hold them.
1216 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1217 thread_mtx_lock(thread
);
1218 thread_hold(thread
);
1219 thread_mtx_unlock(thread
);
1226 * Same as the internal routine above, except that is must lock
1227 * and verify that the task is active. This differs from task_suspend
1228 * in that it places a kernel hold on the task rather than just a
1229 * user-level hold. This keeps users from over resuming and setting
1230 * it running out from under the kernel.
1232 * CONDITIONS: the caller holds a reference on the task
1236 register task_t task
)
1238 if (task
== TASK_NULL
)
1239 return (KERN_INVALID_ARGUMENT
);
1243 if (!task
->active
) {
1246 return (KERN_FAILURE
);
1249 task_hold_locked(task
);
1252 return (KERN_SUCCESS
);
1258 boolean_t until_not_runnable
)
1260 if (task
== TASK_NULL
)
1261 return (KERN_INVALID_ARGUMENT
);
1265 if (!task
->active
) {
1268 return (KERN_FAILURE
);
1271 task_wait_locked(task
, until_not_runnable
);
1274 return (KERN_SUCCESS
);
1280 * Wait for all threads in task to stop.
1283 * Called with task locked, active, and held.
1287 register task_t task
,
1288 boolean_t until_not_runnable
)
1290 register thread_t thread
, self
;
1292 assert(task
->active
);
1293 assert(task
->suspend_count
> 0);
1295 self
= current_thread();
1298 * Iterate through all the threads and wait for them to
1299 * stop. Do not wait for the current thread if it is within
1302 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1304 thread_wait(thread
, until_not_runnable
);
1309 * task_release_locked:
1311 * Release a kernel hold on a task.
1313 * CONDITIONS: the task is locked and active
1316 task_release_locked(
1317 register task_t task
)
1319 register thread_t thread
;
1321 assert(task
->active
);
1322 assert(task
->suspend_count
> 0);
1324 if (--task
->suspend_count
> 0)
1327 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1328 thread_mtx_lock(thread
);
1329 thread_release(thread
);
1330 thread_mtx_unlock(thread
);
1337 * Same as the internal routine above, except that it must lock
1338 * and verify that the task is active.
1340 * CONDITIONS: The caller holds a reference to the task
1346 if (task
== TASK_NULL
)
1347 return (KERN_INVALID_ARGUMENT
);
1351 if (!task
->active
) {
1354 return (KERN_FAILURE
);
1357 task_release_locked(task
);
1360 return (KERN_SUCCESS
);
1366 thread_act_array_t
*threads_out
,
1367 mach_msg_type_number_t
*count
)
1369 mach_msg_type_number_t actual
;
1370 thread_t
*thread_list
;
1372 vm_size_t size
, size_needed
;
1376 if (task
== TASK_NULL
)
1377 return (KERN_INVALID_ARGUMENT
);
1379 size
= 0; addr
= NULL
;
1383 if (!task
->active
) {
1389 return (KERN_FAILURE
);
1392 actual
= task
->thread_count
;
1394 /* do we have the memory we need? */
1395 size_needed
= actual
* sizeof (mach_port_t
);
1396 if (size_needed
<= size
)
1399 /* unlock the task and allocate more memory */
1405 assert(size_needed
> 0);
1408 addr
= kalloc(size
);
1410 return (KERN_RESOURCE_SHORTAGE
);
1413 /* OK, have memory and the task is locked & active */
1414 thread_list
= (thread_t
*)addr
;
1418 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
1419 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
1420 thread_reference_internal(thread
);
1421 thread_list
[j
++] = thread
;
1424 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
1427 size_needed
= actual
* sizeof (mach_port_t
);
1429 /* can unlock task now that we've got the thread refs */
1433 /* no threads, so return null pointer and deallocate memory */
1435 *threads_out
= NULL
;
1442 /* if we allocated too much, must copy */
1444 if (size_needed
< size
) {
1447 newaddr
= kalloc(size_needed
);
1449 for (i
= 0; i
< actual
; ++i
)
1450 thread_deallocate(thread_list
[i
]);
1452 return (KERN_RESOURCE_SHORTAGE
);
1455 bcopy(addr
, newaddr
, size_needed
);
1457 thread_list
= (thread_t
*)newaddr
;
1460 *threads_out
= thread_list
;
1463 /* do the conversion that Mig should handle */
1465 for (i
= 0; i
< actual
; ++i
)
1466 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
1469 return (KERN_SUCCESS
);
1472 #define TASK_HOLD_NORMAL 0
1473 #define TASK_HOLD_PIDSUSPEND 1
1474 #define TASK_HOLD_LEGACY 2
1475 #define TASK_HOLD_LEGACY_ALL 3
1477 static kern_return_t
1479 register task_t task
,
1482 if (!task
->active
) {
1483 return (KERN_FAILURE
);
1486 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1487 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_SUSPEND
) | DBG_FUNC_NONE
,
1488 proc_pid(task
->bsd_info
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
1489 task
->user_stop_count
, task
->user_stop_count
+ 1, 0);
1492 current_task()->suspends_outstanding
++;
1495 if (mode
== TASK_HOLD_LEGACY
)
1496 task
->legacy_stop_count
++;
1498 if (task
->user_stop_count
++ > 0) {
1500 * If the stop count was positive, the task is
1501 * already stopped and we can exit.
1503 return (KERN_SUCCESS
);
1507 * Put a kernel-level hold on the threads in the task (all
1508 * user-level task suspensions added together represent a
1509 * single kernel-level hold). We then wait for the threads
1510 * to stop executing user code.
1512 task_hold_locked(task
);
1513 task_wait_locked(task
, FALSE
);
1515 return (KERN_SUCCESS
);
1518 static kern_return_t
1520 register task_t task
,
1523 register boolean_t release
= FALSE
;
1525 if (!task
->active
) {
1526 return (KERN_FAILURE
);
1529 if (mode
== TASK_HOLD_PIDSUSPEND
) {
1530 if (task
->pidsuspended
== FALSE
) {
1531 return (KERN_FAILURE
);
1533 task
->pidsuspended
= FALSE
;
1536 if (task
->user_stop_count
> (task
->pidsuspended
? 1 : 0)) {
1538 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1539 MACHDBG_CODE(DBG_MACH_IPC
,MACH_TASK_RESUME
) | DBG_FUNC_NONE
,
1540 proc_pid(task
->bsd_info
), ((thread_t
)queue_first(&task
->threads
))->thread_id
,
1541 task
->user_stop_count
, mode
, task
->legacy_stop_count
);
1545 * This is obviously not robust; if we suspend one task and then resume a different one,
1546 * we'll fly under the radar. This is only meant to catch the common case of a crashed
1547 * or buggy suspender.
1549 current_task()->suspends_outstanding
--;
1552 if (mode
== TASK_HOLD_LEGACY_ALL
) {
1553 if (task
->legacy_stop_count
>= task
->user_stop_count
) {
1554 task
->user_stop_count
= 0;
1557 task
->user_stop_count
-= task
->legacy_stop_count
;
1559 task
->legacy_stop_count
= 0;
1561 if (mode
== TASK_HOLD_LEGACY
&& task
->legacy_stop_count
> 0)
1562 task
->legacy_stop_count
--;
1563 if (--task
->user_stop_count
== 0)
1568 return (KERN_FAILURE
);
1572 * Release the task if necessary.
1575 task_release_locked(task
);
1577 return (KERN_SUCCESS
);
1584 * Implement an (old-fashioned) user-level suspension on a task.
1586 * Because the user isn't expecting to have to manage a suspension
1587 * token, we'll track it for him in the kernel in the form of a naked
1588 * send right to the task's resume port. All such send rights
1589 * account for a single suspension against the task (unlike task_suspend2()
1590 * where each caller gets a unique suspension count represented by a
1591 * unique send-once right).
1594 * The caller holds a reference to the task
1598 register task_t task
)
1601 mach_port_t port
, send
, old_notify
;
1602 mach_port_name_t name
;
1604 if (task
== TASK_NULL
|| task
== kernel_task
)
1605 return (KERN_INVALID_ARGUMENT
);
1610 * Claim a send right on the task resume port, and request a no-senders
1611 * notification on that port (if none outstanding).
1613 if (task
->itk_resume
== IP_NULL
) {
1614 task
->itk_resume
= ipc_port_alloc_kernel();
1615 if (!IP_VALID(task
->itk_resume
))
1616 panic("failed to create resume port");
1617 ipc_kobject_set(task
->itk_resume
, (ipc_kobject_t
)task
, IKOT_TASK_RESUME
);
1620 port
= task
->itk_resume
;
1622 assert(ip_active(port
));
1624 send
= ipc_port_make_send_locked(port
);
1625 assert(IP_VALID(send
));
1627 if (port
->ip_nsrequest
== IP_NULL
) {
1628 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
1629 assert(old_notify
== IP_NULL
);
1636 * place a legacy hold on the task.
1638 kr
= place_task_hold(task
, TASK_HOLD_LEGACY
);
1639 if (kr
!= KERN_SUCCESS
) {
1641 ipc_port_release_send(send
);
1648 * Copyout the send right into the calling task's IPC space. It won't know it is there,
1649 * but we'll look it up when calling a traditional resume. Any IPC operations that
1650 * deallocate the send right will auto-release the suspension.
1652 if ((kr
= ipc_kmsg_copyout_object(current_task()->itk_space
, (ipc_object_t
)send
,
1653 MACH_MSG_TYPE_MOVE_SEND
, &name
)) != KERN_SUCCESS
) {
1654 printf("warning: %s(%d) failed to copyout suspension token for task %s(%d) with error: %d\n",
1655 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
1656 proc_name_address(task
->bsd_info
), proc_pid(task
->bsd_info
), kr
);
1665 * Release a user hold on a task.
1668 * The caller holds a reference to the task
1672 register task_t task
)
1675 mach_port_name_t resume_port_name
;
1676 ipc_entry_t resume_port_entry
;
1677 ipc_space_t space
= current_task()->itk_space
;
1679 if (task
== TASK_NULL
|| task
== kernel_task
)
1680 return (KERN_INVALID_ARGUMENT
);
1682 /* release a legacy task hold */
1684 kr
= release_task_hold(task
, TASK_HOLD_LEGACY
);
1687 is_write_lock(space
);
1688 if (is_active(space
) && IP_VALID(task
->itk_resume
) &&
1689 ipc_hash_lookup(space
, (ipc_object_t
)task
->itk_resume
, &resume_port_name
, &resume_port_entry
) == TRUE
) {
1691 * We found a suspension token in the caller's IPC space. Release a send right to indicate that
1692 * we are holding one less legacy hold on the task from this caller. If the release failed,
1693 * go ahead and drop all the rights, as someone either already released our holds or the task
1696 if (kr
== KERN_SUCCESS
)
1697 ipc_right_dealloc(space
, resume_port_name
, resume_port_entry
);
1699 ipc_right_destroy(space
, resume_port_name
, resume_port_entry
, FALSE
, 0);
1700 /* space unlocked */
1702 is_write_unlock(space
);
1703 if (kr
== KERN_SUCCESS
)
1704 printf("warning: %s(%d) performed out-of-band resume on %s(%d)\n",
1705 proc_name_address(current_task()->bsd_info
), proc_pid(current_task()->bsd_info
),
1706 proc_name_address(task
->bsd_info
), proc_pid(task
->bsd_info
));
1713 * Suspend the target task.
1714 * Making/holding a token/reference/port is the callers responsibility.
1717 task_suspend_internal(task_t task
)
1721 if (task
== TASK_NULL
|| task
== kernel_task
)
1722 return (KERN_INVALID_ARGUMENT
);
1725 kr
= place_task_hold(task
, TASK_HOLD_NORMAL
);
1731 * Suspend the target task, and return a suspension token. The token
1732 * represents a reference on the suspended task.
1736 register task_t task
,
1737 task_suspension_token_t
*suspend_token
)
1741 kr
= task_suspend_internal(task
);
1742 if (kr
!= KERN_SUCCESS
) {
1743 *suspend_token
= TASK_NULL
;
1748 * Take a reference on the target task and return that to the caller
1749 * as a "suspension token," which can be converted into an SO right to
1750 * the now-suspended task's resume port.
1752 task_reference_internal(task
);
1753 *suspend_token
= task
;
1755 return (KERN_SUCCESS
);
1760 * (reference/token/port management is caller's responsibility).
1763 task_resume_internal(
1764 register task_suspension_token_t task
)
1768 if (task
== TASK_NULL
|| task
== kernel_task
)
1769 return (KERN_INVALID_ARGUMENT
);
1772 kr
= release_task_hold(task
, TASK_HOLD_NORMAL
);
1778 * Resume the task using a suspension token. Consumes the token's ref.
1782 register task_suspension_token_t task
)
1786 kr
= task_resume_internal(task
);
1787 task_suspension_token_deallocate(task
);
1793 task_suspension_notify(mach_msg_header_t
*request_header
)
1795 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
1796 task_t task
= convert_port_to_task_suspension_token(port
);
1797 mach_msg_type_number_t not_count
;
1799 if (task
== TASK_NULL
|| task
== kernel_task
)
1800 return TRUE
; /* nothing to do */
1802 switch (request_header
->msgh_id
) {
1804 case MACH_NOTIFY_SEND_ONCE
:
1805 /* release the hold held by this specific send-once right */
1807 release_task_hold(task
, TASK_HOLD_NORMAL
);
1811 case MACH_NOTIFY_NO_SENDERS
:
1812 not_count
= ((mach_no_senders_notification_t
*)request_header
)->not_count
;
1816 if (port
->ip_mscount
== not_count
) {
1818 /* release all the [remaining] outstanding legacy holds */
1819 assert(port
->ip_nsrequest
== IP_NULL
);
1821 release_task_hold(task
, TASK_HOLD_LEGACY_ALL
);
1824 } else if (port
->ip_nsrequest
== IP_NULL
) {
1825 ipc_port_t old_notify
;
1828 /* new send rights, re-arm notification at current make-send count */
1829 ipc_port_nsrequest(port
, port
->ip_mscount
, ipc_port_make_sonce_locked(port
), &old_notify
);
1830 assert(old_notify
== IP_NULL
);
1842 task_suspension_token_deallocate(task
); /* drop token reference */
1847 task_pidsuspend_locked(task_t task
)
1851 if (task
->pidsuspended
) {
1856 task
->pidsuspended
= TRUE
;
1858 kr
= place_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
1859 if (kr
!= KERN_SUCCESS
) {
1860 task
->pidsuspended
= FALSE
;
1870 * Suspends a task by placing a hold on its threads.
1873 * The caller holds a reference to the task
1877 register task_t task
)
1881 if (task
== TASK_NULL
|| task
== kernel_task
)
1882 return (KERN_INVALID_ARGUMENT
);
1886 kr
= task_pidsuspend_locked(task
);
1893 /* If enabled, we bring all the frozen pages back in prior to resumption; otherwise, they're faulted back in on demand */
1894 #define THAW_ON_RESUME 1
1898 * Resumes a previously suspended task.
1901 * The caller holds a reference to the task
1905 register task_t task
)
1909 if (task
== TASK_NULL
|| task
== kernel_task
)
1910 return (KERN_INVALID_ARGUMENT
);
1914 #if (CONFIG_FREEZE && THAW_ON_RESUME)
1916 while (task
->changing_freeze_state
) {
1918 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
1920 thread_block(THREAD_CONTINUE_NULL
);
1924 task
->changing_freeze_state
= TRUE
;
1927 kr
= release_task_hold(task
, TASK_HOLD_PIDSUSPEND
);
1931 #if (CONFIG_FREEZE && THAW_ON_RESUME)
1932 if ((kr
== KERN_SUCCESS
) && (task
->frozen
== TRUE
)) {
1934 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
1939 kr
= vm_map_thaw(task
->map
);
1944 if (kr
== KERN_SUCCESS
)
1945 task
->frozen
= FALSE
;
1946 task
->changing_freeze_state
= FALSE
;
1947 thread_wakeup(&task
->changing_freeze_state
);
1963 * The caller holds a reference to the task
1967 register task_t task
,
1968 uint32_t *purgeable_count
,
1969 uint32_t *wired_count
,
1970 uint32_t *clean_count
,
1971 uint32_t *dirty_count
,
1972 uint32_t dirty_budget
,
1974 boolean_t walk_only
)
1978 if (task
== TASK_NULL
|| task
== kernel_task
)
1979 return (KERN_INVALID_ARGUMENT
);
1983 while (task
->changing_freeze_state
) {
1985 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
1987 thread_block(THREAD_CONTINUE_NULL
);
1993 return (KERN_FAILURE
);
1995 task
->changing_freeze_state
= TRUE
;
2000 kr
= vm_map_freeze_walk(task
->map
, purgeable_count
, wired_count
, clean_count
, dirty_count
, dirty_budget
, shared
);
2002 kr
= vm_map_freeze(task
->map
, purgeable_count
, wired_count
, clean_count
, dirty_count
, dirty_budget
, shared
);
2007 if (walk_only
== FALSE
&& kr
== KERN_SUCCESS
)
2008 task
->frozen
= TRUE
;
2009 task
->changing_freeze_state
= FALSE
;
2010 thread_wakeup(&task
->changing_freeze_state
);
2020 * Thaw a currently frozen task.
2023 * The caller holds a reference to the task
2026 vm_consider_waking_compactor_swapper(void);
2030 register task_t task
)
2034 if (task
== TASK_NULL
|| task
== kernel_task
)
2035 return (KERN_INVALID_ARGUMENT
);
2039 while (task
->changing_freeze_state
) {
2041 assert_wait((event_t
)&task
->changing_freeze_state
, THREAD_UNINT
);
2043 thread_block(THREAD_CONTINUE_NULL
);
2047 if (!task
->frozen
) {
2049 return (KERN_FAILURE
);
2051 task
->changing_freeze_state
= TRUE
;
2053 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
) {
2056 kr
= vm_map_thaw(task
->map
);
2060 if (kr
== KERN_SUCCESS
)
2061 task
->frozen
= FALSE
;
2063 task
->frozen
= FALSE
;
2067 task
->changing_freeze_state
= FALSE
;
2068 thread_wakeup(&task
->changing_freeze_state
);
2072 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2073 vm_consider_waking_compactor_swapper();
2079 #endif /* CONFIG_FREEZE */
2082 host_security_set_task_token(
2083 host_security_t host_security
,
2085 security_token_t sec_token
,
2086 audit_token_t audit_token
,
2087 host_priv_t host_priv
)
2089 ipc_port_t host_port
;
2092 if (task
== TASK_NULL
)
2093 return(KERN_INVALID_ARGUMENT
);
2095 if (host_security
== HOST_NULL
)
2096 return(KERN_INVALID_SECURITY
);
2099 task
->sec_token
= sec_token
;
2100 task
->audit_token
= audit_token
;
2104 if (host_priv
!= HOST_PRIV_NULL
) {
2105 kr
= host_get_host_priv_port(host_priv
, &host_port
);
2107 kr
= host_get_host_port(host_priv_self(), &host_port
);
2109 assert(kr
== KERN_SUCCESS
);
2110 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
2115 * This routine was added, pretty much exclusively, for registering the
2116 * RPC glue vector for in-kernel short circuited tasks. Rather than
2117 * removing it completely, I have only disabled that feature (which was
2118 * the only feature at the time). It just appears that we are going to
2119 * want to add some user data to tasks in the future (i.e. bsd info,
2120 * task names, etc...), so I left it in the formal task interface.
2125 task_flavor_t flavor
,
2126 __unused task_info_t task_info_in
, /* pointer to IN array */
2127 __unused mach_msg_type_number_t task_info_count
)
2129 if (task
== TASK_NULL
)
2130 return(KERN_INVALID_ARGUMENT
);
2134 return (KERN_INVALID_ARGUMENT
);
2136 return (KERN_SUCCESS
);
2142 task_flavor_t flavor
,
2143 task_info_t task_info_out
,
2144 mach_msg_type_number_t
*task_info_count
)
2146 kern_return_t error
= KERN_SUCCESS
;
2148 if (task
== TASK_NULL
)
2149 return (KERN_INVALID_ARGUMENT
);
2153 if ((task
!= current_task()) && (!task
->active
)) {
2155 return (KERN_INVALID_ARGUMENT
);
2160 case TASK_BASIC_INFO_32
:
2161 case TASK_BASIC2_INFO_32
:
2163 task_basic_info_32_t basic_info
;
2168 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
) {
2169 error
= KERN_INVALID_ARGUMENT
;
2173 basic_info
= (task_basic_info_32_t
)task_info_out
;
2175 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
2176 basic_info
->virtual_size
= (typeof(basic_info
->virtual_size
))map
->size
;
2177 if (flavor
== TASK_BASIC2_INFO_32
) {
2179 * The "BASIC2" flavor gets the maximum resident
2180 * size instead of the current resident size...
2182 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
2184 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
2186 basic_info
->resident_size
*= PAGE_SIZE
;
2188 basic_info
->policy
= ((task
!= kernel_task
)?
2189 POLICY_TIMESHARE
: POLICY_RR
);
2190 basic_info
->suspend_count
= task
->user_stop_count
;
2192 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
2193 basic_info
->user_time
.seconds
=
2194 (typeof(basic_info
->user_time
.seconds
))secs
;
2195 basic_info
->user_time
.microseconds
= usecs
;
2197 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
2198 basic_info
->system_time
.seconds
=
2199 (typeof(basic_info
->system_time
.seconds
))secs
;
2200 basic_info
->system_time
.microseconds
= usecs
;
2202 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
2206 case TASK_BASIC_INFO_64
:
2208 task_basic_info_64_t basic_info
;
2213 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
) {
2214 error
= KERN_INVALID_ARGUMENT
;
2218 basic_info
= (task_basic_info_64_t
)task_info_out
;
2220 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
2221 basic_info
->virtual_size
= map
->size
;
2222 basic_info
->resident_size
=
2223 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
2226 basic_info
->policy
= ((task
!= kernel_task
)?
2227 POLICY_TIMESHARE
: POLICY_RR
);
2228 basic_info
->suspend_count
= task
->user_stop_count
;
2230 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
2231 basic_info
->user_time
.seconds
=
2232 (typeof(basic_info
->user_time
.seconds
))secs
;
2233 basic_info
->user_time
.microseconds
= usecs
;
2235 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
2236 basic_info
->system_time
.seconds
=
2237 (typeof(basic_info
->system_time
.seconds
))secs
;
2238 basic_info
->system_time
.microseconds
= usecs
;
2240 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
2244 case MACH_TASK_BASIC_INFO
:
2246 mach_task_basic_info_t basic_info
;
2251 if (*task_info_count
< MACH_TASK_BASIC_INFO_COUNT
) {
2252 error
= KERN_INVALID_ARGUMENT
;
2256 basic_info
= (mach_task_basic_info_t
)task_info_out
;
2258 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
2260 basic_info
->virtual_size
= map
->size
;
2262 basic_info
->resident_size
=
2263 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
));
2264 basic_info
->resident_size
*= PAGE_SIZE_64
;
2266 basic_info
->resident_size_max
=
2267 (mach_vm_size_t
)(pmap_resident_max(map
->pmap
));
2268 basic_info
->resident_size_max
*= PAGE_SIZE_64
;
2270 basic_info
->policy
= ((task
!= kernel_task
) ?
2271 POLICY_TIMESHARE
: POLICY_RR
);
2273 basic_info
->suspend_count
= task
->user_stop_count
;
2275 absolutetime_to_microtime(task
->total_user_time
, &secs
, &usecs
);
2276 basic_info
->user_time
.seconds
=
2277 (typeof(basic_info
->user_time
.seconds
))secs
;
2278 basic_info
->user_time
.microseconds
= usecs
;
2280 absolutetime_to_microtime(task
->total_system_time
, &secs
, &usecs
);
2281 basic_info
->system_time
.seconds
=
2282 (typeof(basic_info
->system_time
.seconds
))secs
;
2283 basic_info
->system_time
.microseconds
= usecs
;
2285 *task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
2289 case TASK_THREAD_TIMES_INFO
:
2291 register task_thread_times_info_t times_info
;
2292 register thread_t thread
;
2294 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
2295 error
= KERN_INVALID_ARGUMENT
;
2299 times_info
= (task_thread_times_info_t
) task_info_out
;
2300 times_info
->user_time
.seconds
= 0;
2301 times_info
->user_time
.microseconds
= 0;
2302 times_info
->system_time
.seconds
= 0;
2303 times_info
->system_time
.microseconds
= 0;
2306 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2307 time_value_t user_time
, system_time
;
2309 if (thread
->options
& TH_OPT_IDLE_THREAD
)
2312 thread_read_times(thread
, &user_time
, &system_time
);
2314 time_value_add(×_info
->user_time
, &user_time
);
2315 time_value_add(×_info
->system_time
, &system_time
);
2318 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
2322 case TASK_ABSOLUTETIME_INFO
:
2324 task_absolutetime_info_t info
;
2325 register thread_t thread
;
2327 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
) {
2328 error
= KERN_INVALID_ARGUMENT
;
2332 info
= (task_absolutetime_info_t
)task_info_out
;
2333 info
->threads_user
= info
->threads_system
= 0;
2336 info
->total_user
= task
->total_user_time
;
2337 info
->total_system
= task
->total_system_time
;
2339 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2343 if (thread
->options
& TH_OPT_IDLE_THREAD
)
2347 thread_lock(thread
);
2349 tval
= timer_grab(&thread
->user_timer
);
2350 info
->threads_user
+= tval
;
2351 info
->total_user
+= tval
;
2353 tval
= timer_grab(&thread
->system_timer
);
2354 if (thread
->precise_user_kernel_time
) {
2355 info
->threads_system
+= tval
;
2356 info
->total_system
+= tval
;
2358 /* system_timer may represent either sys or user */
2359 info
->threads_user
+= tval
;
2360 info
->total_user
+= tval
;
2363 thread_unlock(thread
);
2368 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
2372 case TASK_DYLD_INFO
:
2374 task_dyld_info_t info
;
2377 * We added the format field to TASK_DYLD_INFO output. For
2378 * temporary backward compatibility, accept the fact that
2379 * clients may ask for the old version - distinquished by the
2380 * size of the expected result structure.
2382 #define TASK_LEGACY_DYLD_INFO_COUNT \
2383 offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
2385 if (*task_info_count
< TASK_LEGACY_DYLD_INFO_COUNT
) {
2386 error
= KERN_INVALID_ARGUMENT
;
2390 info
= (task_dyld_info_t
)task_info_out
;
2391 info
->all_image_info_addr
= task
->all_image_info_addr
;
2392 info
->all_image_info_size
= task
->all_image_info_size
;
2394 /* only set format on output for those expecting it */
2395 if (*task_info_count
>= TASK_DYLD_INFO_COUNT
) {
2396 info
->all_image_info_format
= task_has_64BitAddr(task
) ?
2397 TASK_DYLD_ALL_IMAGE_INFO_64
:
2398 TASK_DYLD_ALL_IMAGE_INFO_32
;
2399 *task_info_count
= TASK_DYLD_INFO_COUNT
;
2401 *task_info_count
= TASK_LEGACY_DYLD_INFO_COUNT
;
2406 case TASK_EXTMOD_INFO
:
2408 task_extmod_info_t info
;
2411 if (*task_info_count
< TASK_EXTMOD_INFO_COUNT
) {
2412 error
= KERN_INVALID_ARGUMENT
;
2416 info
= (task_extmod_info_t
)task_info_out
;
2418 p
= get_bsdtask_info(task
);
2420 proc_getexecutableuuid(p
, info
->task_uuid
, sizeof(info
->task_uuid
));
2422 bzero(info
->task_uuid
, sizeof(info
->task_uuid
));
2424 info
->extmod_statistics
= task
->extmod_statistics
;
2425 *task_info_count
= TASK_EXTMOD_INFO_COUNT
;
2430 case TASK_KERNELMEMORY_INFO
:
2432 task_kernelmemory_info_t tkm_info
;
2433 ledger_amount_t credit
, debit
;
2435 if (*task_info_count
< TASK_KERNELMEMORY_INFO_COUNT
) {
2436 error
= KERN_INVALID_ARGUMENT
;
2440 tkm_info
= (task_kernelmemory_info_t
) task_info_out
;
2441 tkm_info
->total_palloc
= 0;
2442 tkm_info
->total_pfree
= 0;
2443 tkm_info
->total_salloc
= 0;
2444 tkm_info
->total_sfree
= 0;
2446 if (task
== kernel_task
) {
2448 * All shared allocs/frees from other tasks count against
2449 * the kernel private memory usage. If we are looking up
2450 * info for the kernel task, gather from everywhere.
2454 /* start by accounting for all the terminated tasks against the kernel */
2455 tkm_info
->total_palloc
= tasks_tkm_private
.alloc
+ tasks_tkm_shared
.alloc
;
2456 tkm_info
->total_pfree
= tasks_tkm_private
.free
+ tasks_tkm_shared
.free
;
2458 /* count all other task/thread shared alloc/free against the kernel */
2459 lck_mtx_lock(&tasks_threads_lock
);
2461 /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
2462 queue_iterate(&tasks
, task
, task_t
, tasks
) {
2463 if (task
== kernel_task
) {
2464 if (ledger_get_entries(task
->ledger
,
2465 task_ledgers
.tkm_private
, &credit
,
2466 &debit
) == KERN_SUCCESS
) {
2467 tkm_info
->total_palloc
+= credit
;
2468 tkm_info
->total_pfree
+= debit
;
2471 if (!ledger_get_entries(task
->ledger
,
2472 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
2473 tkm_info
->total_palloc
+= credit
;
2474 tkm_info
->total_pfree
+= debit
;
2477 lck_mtx_unlock(&tasks_threads_lock
);
2479 if (!ledger_get_entries(task
->ledger
,
2480 task_ledgers
.tkm_private
, &credit
, &debit
)) {
2481 tkm_info
->total_palloc
= credit
;
2482 tkm_info
->total_pfree
= debit
;
2484 if (!ledger_get_entries(task
->ledger
,
2485 task_ledgers
.tkm_shared
, &credit
, &debit
)) {
2486 tkm_info
->total_salloc
= credit
;
2487 tkm_info
->total_sfree
= debit
;
2492 *task_info_count
= TASK_KERNELMEMORY_INFO_COUNT
;
2493 return KERN_SUCCESS
;
2497 case TASK_SCHED_FIFO_INFO
:
2500 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
) {
2501 error
= KERN_INVALID_ARGUMENT
;
2505 error
= KERN_INVALID_POLICY
;
2510 case TASK_SCHED_RR_INFO
:
2512 register policy_rr_base_t rr_base
;
2513 uint32_t quantum_time
;
2514 uint64_t quantum_ns
;
2516 if (*task_info_count
< POLICY_RR_BASE_COUNT
) {
2517 error
= KERN_INVALID_ARGUMENT
;
2521 rr_base
= (policy_rr_base_t
) task_info_out
;
2523 if (task
!= kernel_task
) {
2524 error
= KERN_INVALID_POLICY
;
2528 rr_base
->base_priority
= task
->priority
;
2530 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
2531 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
2533 rr_base
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
2535 *task_info_count
= POLICY_RR_BASE_COUNT
;
2540 case TASK_SCHED_TIMESHARE_INFO
:
2542 register policy_timeshare_base_t ts_base
;
2544 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
) {
2545 error
= KERN_INVALID_ARGUMENT
;
2549 ts_base
= (policy_timeshare_base_t
) task_info_out
;
2551 if (task
== kernel_task
) {
2552 error
= KERN_INVALID_POLICY
;
2556 ts_base
->base_priority
= task
->priority
;
2558 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
2562 case TASK_SECURITY_TOKEN
:
2564 register security_token_t
*sec_token_p
;
2566 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
2567 error
= KERN_INVALID_ARGUMENT
;
2571 sec_token_p
= (security_token_t
*) task_info_out
;
2573 *sec_token_p
= task
->sec_token
;
2575 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
2579 case TASK_AUDIT_TOKEN
:
2581 register audit_token_t
*audit_token_p
;
2583 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
2584 error
= KERN_INVALID_ARGUMENT
;
2588 audit_token_p
= (audit_token_t
*) task_info_out
;
2590 *audit_token_p
= task
->audit_token
;
2592 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
2596 case TASK_SCHED_INFO
:
2597 error
= KERN_INVALID_ARGUMENT
;
2600 case TASK_EVENTS_INFO
:
2602 register task_events_info_t events_info
;
2603 register thread_t thread
;
2605 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
2606 error
= KERN_INVALID_ARGUMENT
;
2610 events_info
= (task_events_info_t
) task_info_out
;
2613 events_info
->faults
= task
->faults
;
2614 events_info
->pageins
= task
->pageins
;
2615 events_info
->cow_faults
= task
->cow_faults
;
2616 events_info
->messages_sent
= task
->messages_sent
;
2617 events_info
->messages_received
= task
->messages_received
;
2618 events_info
->syscalls_mach
= task
->syscalls_mach
;
2619 events_info
->syscalls_unix
= task
->syscalls_unix
;
2621 events_info
->csw
= task
->c_switch
;
2623 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2624 events_info
->csw
+= thread
->c_switch
;
2625 events_info
->syscalls_mach
+= thread
->syscalls_mach
;
2626 events_info
->syscalls_unix
+= thread
->syscalls_unix
;
2630 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
2633 case TASK_AFFINITY_TAG_INFO
:
2635 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
) {
2636 error
= KERN_INVALID_ARGUMENT
;
2640 error
= task_affinity_info(task
, task_info_out
, task_info_count
);
2643 case TASK_POWER_INFO
:
2645 if (*task_info_count
< TASK_POWER_INFO_COUNT
) {
2646 error
= KERN_INVALID_ARGUMENT
;
2650 task_power_info_locked(task
, (task_power_info_t
)task_info_out
);
2655 case TASK_VM_INFO_PURGEABLE
:
2657 task_vm_info_t vm_info
;
2660 if (*task_info_count
< TASK_VM_INFO_COUNT
) {
2661 error
= KERN_INVALID_ARGUMENT
;
2665 vm_info
= (task_vm_info_t
)task_info_out
;
2667 if (task
== kernel_task
) {
2672 vm_map_lock_read(map
);
2675 vm_info
->virtual_size
= (typeof(vm_info
->virtual_size
))map
->size
;
2676 vm_info
->region_count
= map
->hdr
.nentries
;
2677 vm_info
->page_size
= vm_map_page_size(map
);
2679 vm_info
->resident_size
= pmap_resident_count(map
->pmap
);
2680 vm_info
->resident_size
*= PAGE_SIZE
;
2681 vm_info
->resident_size_peak
= pmap_resident_max(map
->pmap
);
2682 vm_info
->resident_size_peak
*= PAGE_SIZE
;
2684 #define _VM_INFO(_name) \
2685 vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
2688 _VM_INFO(device_peak
);
2690 _VM_INFO(external_peak
);
2692 _VM_INFO(internal_peak
);
2694 _VM_INFO(reusable_peak
);
2695 _VM_INFO(compressed
);
2696 _VM_INFO(compressed_peak
);
2697 _VM_INFO(compressed_lifetime
);
2699 vm_info
->purgeable_volatile_pmap
= 0;
2700 vm_info
->purgeable_volatile_resident
= 0;
2701 vm_info
->purgeable_volatile_virtual
= 0;
2702 if (task
== kernel_task
) {
2704 * We do not maintain the detailed stats for the
2705 * kernel_pmap, so just count everything as
2708 vm_info
->internal
= vm_info
->resident_size
;
2710 * ... but since the memory held by the VM compressor
2711 * in the kernel address space ought to be attributed
2712 * to user-space tasks, we subtract it from "internal"
2713 * to give memory reporting tools a more accurate idea
2714 * of what the kernel itself is actually using, instead
2715 * of making it look like the kernel is leaking memory
2716 * when the system is under memory pressure.
2718 vm_info
->internal
-= (VM_PAGE_COMPRESSOR_COUNT
*
2721 mach_vm_size_t volatile_virtual_size
;
2722 mach_vm_size_t volatile_resident_size
;
2723 mach_vm_size_t volatile_pmap_size
;
2726 if (flavor
== TASK_VM_INFO_PURGEABLE
) {
2727 kr
= vm_map_query_volatile(
2729 &volatile_virtual_size
,
2730 &volatile_resident_size
,
2731 &volatile_pmap_size
);
2732 if (kr
== KERN_SUCCESS
) {
2733 vm_info
->purgeable_volatile_pmap
=
2735 vm_info
->purgeable_volatile_resident
=
2736 volatile_resident_size
;
2737 vm_info
->purgeable_volatile_virtual
=
2738 volatile_virtual_size
;
2741 vm_map_unlock_read(map
);
2744 *task_info_count
= TASK_VM_INFO_COUNT
;
2749 error
= KERN_INVALID_ARGUMENT
;
2759 * Returns power stats for the task.
2760 * Note: Called with task locked.
2763 task_power_info_locked(
2765 task_power_info_t info
)
2768 ledger_amount_t tmp
;
2770 task_lock_assert_owned(task
);
2772 ledger_get_entries(task
->ledger
, task_ledgers
.interrupt_wakeups
,
2773 (ledger_amount_t
*)&info
->task_interrupt_wakeups
, &tmp
);
2774 ledger_get_entries(task
->ledger
, task_ledgers
.platform_idle_wakeups
,
2775 (ledger_amount_t
*)&info
->task_platform_idle_wakeups
, &tmp
);
2777 info
->task_timer_wakeups_bin_1
= task
->task_timer_wakeups_bin_1
;
2778 info
->task_timer_wakeups_bin_2
= task
->task_timer_wakeups_bin_2
;
2780 info
->total_user
= task
->total_user_time
;
2781 info
->total_system
= task
->total_system_time
;
2783 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2787 if (thread
->options
& TH_OPT_IDLE_THREAD
)
2791 thread_lock(thread
);
2793 info
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
2794 info
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
2796 tval
= timer_grab(&thread
->user_timer
);
2797 info
->total_user
+= tval
;
2799 tval
= timer_grab(&thread
->system_timer
);
2800 if (thread
->precise_user_kernel_time
) {
2801 info
->total_system
+= tval
;
2803 /* system_timer may represent either sys or user */
2804 info
->total_user
+= tval
;
2807 thread_unlock(thread
);
2815 task_purgable_info_t
*stats
)
2817 if (task
== TASK_NULL
|| stats
== NULL
)
2818 return KERN_INVALID_ARGUMENT
;
2819 /* Take task reference */
2820 task_reference(task
);
2821 vm_purgeable_stats((vm_purgeable_info_t
)stats
, task
);
2822 /* Drop task reference */
2823 task_deallocate(task
);
2824 return KERN_SUCCESS
;
2835 /* assert(task == current_task()); */ /* bogus assert 4803227 4807483 */
2839 task
->vtimers
|= which
;
2843 case TASK_VTIMER_USER
:
2844 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2846 thread_lock(thread
);
2847 if (thread
->precise_user_kernel_time
)
2848 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
2850 thread
->vtimer_user_save
= timer_grab(&thread
->system_timer
);
2851 thread_unlock(thread
);
2856 case TASK_VTIMER_PROF
:
2857 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2859 thread_lock(thread
);
2860 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
2861 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
2862 thread_unlock(thread
);
2867 case TASK_VTIMER_RLIM
:
2868 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2870 thread_lock(thread
);
2871 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
2872 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
2873 thread_unlock(thread
);
2887 assert(task
== current_task());
2891 task
->vtimers
&= ~which
;
2901 uint32_t *microsecs
)
2903 thread_t thread
= current_thread();
2908 assert(task
== current_task());
2910 assert(task
->vtimers
& which
);
2916 case TASK_VTIMER_USER
:
2917 if (thread
->precise_user_kernel_time
) {
2918 tdelt
= (uint32_t)timer_delta(&thread
->user_timer
,
2919 &thread
->vtimer_user_save
);
2921 tdelt
= (uint32_t)timer_delta(&thread
->system_timer
,
2922 &thread
->vtimer_user_save
);
2924 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
2927 case TASK_VTIMER_PROF
:
2928 tsum
= timer_grab(&thread
->user_timer
);
2929 tsum
+= timer_grab(&thread
->system_timer
);
2930 tdelt
= (uint32_t)(tsum
- thread
->vtimer_prof_save
);
2931 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
2932 /* if the time delta is smaller than a usec, ignore */
2933 if (*microsecs
!= 0)
2934 thread
->vtimer_prof_save
= tsum
;
2937 case TASK_VTIMER_RLIM
:
2938 tsum
= timer_grab(&thread
->user_timer
);
2939 tsum
+= timer_grab(&thread
->system_timer
);
2940 tdelt
= (uint32_t)(tsum
- thread
->vtimer_rlim_save
);
2941 thread
->vtimer_rlim_save
= tsum
;
2942 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
2951 * Change the assigned processor set for the task
2955 __unused task_t task
,
2956 __unused processor_set_t new_pset
,
2957 __unused boolean_t assign_threads
)
2959 return(KERN_FAILURE
);
2963 * task_assign_default:
2965 * Version of task_assign to assign to default processor set.
2968 task_assign_default(
2970 boolean_t assign_threads
)
2972 return (task_assign(task
, &pset0
, assign_threads
));
2976 * task_get_assignment
2978 * Return name of processor set that task is assigned to.
2981 task_get_assignment(
2983 processor_set_t
*pset
)
2986 return(KERN_FAILURE
);
2990 return (KERN_SUCCESS
);
2997 * Set scheduling policy and parameters, both base and limit, for
2998 * the given task. Policy must be a policy which is enabled for the
2999 * processor set. Change contained threads if requested.
3003 __unused task_t task
,
3004 __unused policy_t policy_id
,
3005 __unused policy_base_t base
,
3006 __unused mach_msg_type_number_t count
,
3007 __unused boolean_t set_limit
,
3008 __unused boolean_t change
)
3010 return(KERN_FAILURE
);
3016 * Set scheduling policy and parameters, both base and limit, for
3017 * the given task. Policy can be any policy implemented by the
3018 * processor set, whether enabled or not. Change contained threads
3023 __unused task_t task
,
3024 __unused processor_set_t pset
,
3025 __unused policy_t policy_id
,
3026 __unused policy_base_t base
,
3027 __unused mach_msg_type_number_t base_count
,
3028 __unused policy_limit_t limit
,
3029 __unused mach_msg_type_number_t limit_count
,
3030 __unused boolean_t change
)
3032 return(KERN_FAILURE
);
3042 extern int fast_tas_debug
;
3044 if (fast_tas_debug
) {
3045 printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
3049 task
->fast_tas_base
= pc
;
3050 task
->fast_tas_end
= endpc
;
3052 return KERN_SUCCESS
;
3054 #else /* FAST_TAS */
3057 __unused task_t task
,
3058 __unused vm_offset_t pc
,
3059 __unused vm_offset_t endpc
)
3061 return KERN_FAILURE
;
3063 #endif /* FAST_TAS */
3066 task_synchronizer_destroy_all(task_t task
)
3068 semaphore_t semaphore
;
3071 * Destroy owned semaphores
3074 while (!queue_empty(&task
->semaphore_list
)) {
3075 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
3076 (void) semaphore_destroy(task
, semaphore
);
3081 * Install default (machine-dependent) initial thread state
3082 * on the task. Subsequent thread creation will have this initial
3083 * state set on the thread by machine_thread_inherit_taskwide().
3084 * Flavors and structures are exactly the same as those to thread_set_state()
3090 thread_state_t state
,
3091 mach_msg_type_number_t state_count
)
3095 if (task
== TASK_NULL
) {
3096 return (KERN_INVALID_ARGUMENT
);
3101 if (!task
->active
) {
3103 return (KERN_FAILURE
);
3106 ret
= machine_task_set_state(task
, flavor
, state
, state_count
);
3113 * Examine the default (machine-dependent) initial thread state
3114 * on the task, as set by task_set_state(). Flavors and structures
3115 * are exactly the same as those passed to thread_get_state().
3121 thread_state_t state
,
3122 mach_msg_type_number_t
*state_count
)
3126 if (task
== TASK_NULL
) {
3127 return (KERN_INVALID_ARGUMENT
);
3132 if (!task
->active
) {
3134 return (KERN_FAILURE
);
3137 ret
= machine_task_get_state(task
, flavor
, state
, state_count
);
3144 #define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
3146 void __attribute__((noinline
))
3147 THIS_PROCESS_CROSSED_HIGH_WATERMARK__SENDING_EXC_RESOURCE(int max_footprint_mb
)
3149 task_t task
= current_task();
3151 char *procname
= (char *) "unknown";
3152 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
3155 pid
= proc_selfpid();
3156 if (task
->bsd_info
!= NULL
)
3157 procname
= proc_name_address(current_task()->bsd_info
);
3160 if (hwm_user_cores
) {
3162 uint64_t starttime
, end
;
3163 clock_sec_t secs
= 0;
3164 uint32_t microsecs
= 0;
3166 starttime
= mach_absolute_time();
3168 * Trigger a coredump of this process. Don't proceed unless we know we won't
3169 * be filling up the disk; and ignore the core size resource limit for this
3172 if ((error
= coredump(current_task()->bsd_info
, HWM_USERCORE_MINSPACE
, 1)) != 0) {
3173 printf("couldn't take coredump of %s[%d]: %d\n", procname
, pid
, error
);
3176 * coredump() leaves the task suspended.
3178 task_resume_internal(current_task());
3180 end
= mach_absolute_time();
3181 absolutetime_to_microtime(end
- starttime
, &secs
, µsecs
);
3182 printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
3183 proc_name_address(current_task()->bsd_info
), pid
, (int)secs
, microsecs
);
3186 if (disable_exc_resource
) {
3187 printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
3188 "supressed by a boot-arg.\n", procname
, pid
, max_footprint_mb
);
3192 printf("process %s[%d] crossed memory high watermark (%d MB); sending "
3193 "EXC_RESOURCE.\n", procname
, pid
, max_footprint_mb
);
3195 code
[0] = code
[1] = 0;
3196 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_MEMORY
);
3197 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_HIGH_WATERMARK
);
3198 EXC_RESOURCE_HWM_ENCODE_LIMIT(code
[0], max_footprint_mb
);
3199 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
3203 * Callback invoked when a task exceeds its physical footprint limit.
3206 task_footprint_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
3208 ledger_amount_t max_footprint_mb
;
3210 if (warning
== LEDGER_WARNING_DIPPED_BELOW
) {
3212 * Task memory limits only provide a warning on the way up.
3217 ledger_get_limit(current_task()->ledger
, task_ledgers
.phys_footprint
, &max_footprint_mb
);
3218 max_footprint_mb
>>= 20;
3221 * If this an actual violation (not a warning),
3222 * generate a non-fatal high watermark EXC_RESOURCE.
3224 if ((warning
== 0) && (current_task()->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
)) {
3225 THIS_PROCESS_CROSSED_HIGH_WATERMARK__SENDING_EXC_RESOURCE((int)max_footprint_mb
);
3228 memorystatus_on_ledger_footprint_exceeded((warning
== LEDGER_WARNING_ROSE_ABOVE
) ? TRUE
: FALSE
,
3229 (int)max_footprint_mb
);
3232 extern int proc_check_footprint_priv(void);
3235 task_set_phys_footprint_limit(
3240 kern_return_t error
;
3242 if ((error
= proc_check_footprint_priv())) {
3243 return (KERN_NO_ACCESS
);
3246 return task_set_phys_footprint_limit_internal(task
, new_limit_mb
, old_limit_mb
, FALSE
);
3250 task_set_phys_footprint_limit_internal(
3254 boolean_t trigger_exception
)
3256 ledger_amount_t old
;
3258 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &old
);
3261 *old_limit_mb
= old
>> 20;
3264 if (new_limit_mb
== -1) {
3266 * Caller wishes to remove the limit.
3268 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
3269 max_task_footprint
? max_task_footprint
: LEDGER_LIMIT_INFINITY
,
3270 max_task_footprint
? PHYS_FOOTPRINT_WARNING_LEVEL
: 0);
3271 return (KERN_SUCCESS
);
3274 #ifdef CONFIG_NOMONITORS
3275 return (KERN_SUCCESS
);
3276 #endif /* CONFIG_NOMONITORS */
3280 if (trigger_exception
) {
3281 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
3283 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_PHYS_FOOTPRINT_EXCEPTION
;
3286 ledger_set_limit(task
->ledger
, task_ledgers
.phys_footprint
,
3287 (ledger_amount_t
)new_limit_mb
<< 20, PHYS_FOOTPRINT_WARNING_LEVEL
);
3291 return (KERN_SUCCESS
);
3295 task_get_phys_footprint_limit(
3299 ledger_amount_t limit
;
3301 ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &limit
);
3302 *limit_mb
= limit
>> 20;
3304 return (KERN_SUCCESS
);
3306 #else /* CONFIG_JETSAM */
3308 task_set_phys_footprint_limit(
3309 __unused task_t task
,
3310 __unused
int new_limit_mb
,
3311 __unused
int *old_limit_mb
)
3313 return (KERN_FAILURE
);
3317 task_get_phys_footprint_limit(
3318 __unused task_t task
,
3319 __unused
int *limit_mb
)
3321 return (KERN_FAILURE
);
3323 #endif /* CONFIG_JETSAM */
3326 * We need to export some functions to other components that
3327 * are currently implemented in macros within the osfmk
3328 * component. Just export them as functions of the same name.
3330 boolean_t
is_kerneltask(task_t t
)
3332 if (t
== kernel_task
)
3339 check_for_tasksuspend(task_t task
)
3342 if (task
== TASK_NULL
)
3345 return (task
->suspend_count
> 0);
3349 task_t
current_task(void);
3350 task_t
current_task(void)
3352 return (current_task_fast());
3355 #undef task_reference
3356 void task_reference(task_t task
);
3361 if (task
!= TASK_NULL
)
3362 task_reference_internal(task
);
3366 * This routine is called always with task lock held.
3367 * And it returns a thread handle without reference as the caller
3368 * operates on it under the task lock held.
3371 task_findtid(task_t task
, uint64_t tid
)
3373 thread_t thread
= THREAD_NULL
;
3375 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3376 if (thread
->thread_id
== tid
)
3379 return(THREAD_NULL
);
3383 #if CONFIG_MACF_MACH
3385 * Protect 2 task labels against modification by adding a reference on
3386 * both label handles. The locks do not actually have to be held while
3387 * using the labels as only labels with one reference can be modified
3396 labelh_reference(a
->label
);
3397 labelh_reference(b
->label
);
3405 labelh_release(a
->label
);
3406 labelh_release(b
->label
);
3410 mac_task_label_update_internal(
3415 tasklabel_lock(task
);
3416 task
->label
= labelh_modify(task
->label
);
3417 mac_task_label_update(pl
, &task
->maclabel
);
3418 tasklabel_unlock(task
);
3419 ip_lock(task
->itk_self
);
3420 mac_port_label_update_cred(pl
, &task
->itk_self
->ip_label
);
3421 ip_unlock(task
->itk_self
);
3425 mac_task_label_modify(
3428 void (*f
) (struct label
*l
, void *arg
))
3431 tasklabel_lock(task
);
3432 task
->label
= labelh_modify(task
->label
);
3433 (*f
)(&task
->maclabel
, arg
);
3434 tasklabel_unlock(task
);
3438 mac_task_get_label(struct task
*task
)
3440 return (&task
->maclabel
);
3445 * Control the CPU usage monitor for a task.
3448 task_cpu_usage_monitor_ctl(task_t task
, uint32_t *flags
)
3450 int error
= KERN_SUCCESS
;
3452 if (*flags
& CPUMON_MAKE_FATAL
) {
3453 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_CPUMON
;
3455 error
= KERN_INVALID_ARGUMENT
;
3462 * Control the wakeups monitor for a task.
3465 task_wakeups_monitor_ctl(task_t task
, uint32_t *flags
, int32_t *rate_hz
)
3467 ledger_t ledger
= task
->ledger
;
3470 if (*flags
& WAKEMON_GET_PARAMS
) {
3471 ledger_amount_t limit
;
3474 ledger_get_limit(ledger
, task_ledgers
.interrupt_wakeups
, &limit
);
3475 ledger_get_period(ledger
, task_ledgers
.interrupt_wakeups
, &period
);
3477 if (limit
!= LEDGER_LIMIT_INFINITY
) {
3479 * An active limit means the wakeups monitor is enabled.
3481 *rate_hz
= (int32_t)(limit
/ (int64_t)(period
/ NSEC_PER_SEC
));
3482 *flags
= WAKEMON_ENABLE
;
3483 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
3484 *flags
|= WAKEMON_MAKE_FATAL
;
3487 *flags
= WAKEMON_DISABLE
;
3492 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
3495 return KERN_SUCCESS
;
3498 if (*flags
& WAKEMON_ENABLE
) {
3499 if (*flags
& WAKEMON_SET_DEFAULTS
) {
3500 *rate_hz
= task_wakeups_monitor_rate
;
3503 #ifndef CONFIG_NOMONITORS
3504 if (*flags
& WAKEMON_MAKE_FATAL
) {
3505 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
;
3507 #endif /* CONFIG_NOMONITORS */
3511 return KERN_INVALID_ARGUMENT
;
3514 #ifndef CONFIG_NOMONITORS
3515 ledger_set_limit(ledger
, task_ledgers
.interrupt_wakeups
, *rate_hz
* task_wakeups_monitor_interval
,
3516 task_wakeups_monitor_ustackshots_trigger_pct
);
3517 ledger_set_period(ledger
, task_ledgers
.interrupt_wakeups
, task_wakeups_monitor_interval
* NSEC_PER_SEC
);
3518 ledger_enable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
3519 #endif /* CONFIG_NOMONITORS */
3520 } else if (*flags
& WAKEMON_DISABLE
) {
3522 * Caller wishes to disable wakeups monitor on the task.
3524 * Disable telemetry if it was triggered by the wakeups monitor, and
3525 * remove the limit & callback on the wakeups ledger entry.
3527 #if CONFIG_TELEMETRY
3528 telemetry_task_ctl_locked(current_task(), TF_WAKEMON_WARNING
, 0);
3530 ledger_disable_refill(ledger
, task_ledgers
.interrupt_wakeups
);
3531 ledger_disable_callback(ledger
, task_ledgers
.interrupt_wakeups
);
3535 return KERN_SUCCESS
;
3539 task_wakeups_rate_exceeded(int warning
, __unused
const void *param0
, __unused
const void *param1
)
3541 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
3542 #if CONFIG_TELEMETRY
3544 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
3545 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
3547 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 1);
3552 #if CONFIG_TELEMETRY
3554 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
3555 * exceeded the limit, turn telemetry off for the task.
3557 telemetry_task_ctl(current_task(), TF_WAKEMON_WARNING
, 0);
3561 THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS__SENDING_EXC_RESOURCE();
3565 void __attribute__((noinline
))
3566 THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS__SENDING_EXC_RESOURCE(void)
3568 task_t task
= current_task();
3570 char *procname
= (char *) "unknown";
3571 uint64_t observed_wakeups_rate
;
3572 uint64_t permitted_wakeups_rate
;
3573 uint64_t observation_interval
;
3574 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
3575 struct ledger_entry_info lei
;
3578 pid
= proc_selfpid();
3579 if (task
->bsd_info
!= NULL
)
3580 procname
= proc_name_address(current_task()->bsd_info
);
3583 ledger_get_entry_info(task
->ledger
, task_ledgers
.interrupt_wakeups
, &lei
);
3586 * Disable the exception notification so we don't overwhelm
3587 * the listener with an endless stream of redundant exceptions.
3589 uint32_t flags
= WAKEMON_DISABLE
;
3590 task_wakeups_monitor_ctl(task
, &flags
, NULL
);
3592 observed_wakeups_rate
= (lei
.lei_balance
* (int64_t)NSEC_PER_SEC
) / lei
.lei_last_refill
;
3593 permitted_wakeups_rate
= lei
.lei_limit
/ task_wakeups_monitor_interval
;
3594 observation_interval
= lei
.lei_refill_period
/ NSEC_PER_SEC
;
3596 if (disable_exc_resource
) {
3597 printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
3598 "supressed by a boot-arg\n", procname
, pid
);
3601 printf("process %s[%d] caught causing excessive wakeups. Observed wakeups rate "
3602 "(per sec): %lld; Maximum permitted wakeups rate (per sec): %lld; Observation "
3603 "period: %lld seconds; Task lifetime number of wakeups: %lld\n",
3604 procname
, pid
, observed_wakeups_rate
, permitted_wakeups_rate
,
3605 observation_interval
, lei
.lei_credit
);
3607 code
[0] = code
[1] = 0;
3608 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_WAKEUPS
);
3609 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_WAKEUPS_MONITOR
);
3610 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code
[0], task_wakeups_monitor_rate
);
3611 EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code
[0], observation_interval
);
3612 EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code
[1], lei
.lei_balance
* (int64_t)NSEC_PER_SEC
/ lei
.lei_last_refill
);
3613 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
3615 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON
) {
3616 task_terminate_internal(task
);