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@
28 #include <mach/mach_types.h>
29 #include <mach/machine/vm_param.h>
30 #include <mach/task.h>
32 #include <kern/kern_types.h>
33 #include <kern/ledger.h>
34 #include <kern/processor.h>
35 #include <kern/thread.h>
36 #include <kern/task.h>
39 #include <ipc/ipc_port.h>
40 #include <ipc/ipc_object.h>
41 #include <vm/vm_map.h>
42 #include <vm/vm_kern.h>
44 #include <vm/vm_protos.h> /* last */
45 #include <sys/resource.h>
47 #undef thread_should_halt
49 /* BSD KERN COMPONENT INTERFACE */
51 task_t bsd_init_task
= TASK_NULL
;
52 char init_task_failure_data
[1024];
53 extern unsigned int not_in_kdp
; /* Skip acquiring locks if we're in kdp */
55 thread_t
get_firstthread(task_t
);
56 int get_task_userstop(task_t
);
57 int get_thread_userstop(thread_t
);
58 boolean_t
current_thread_aborted(void);
59 void task_act_iterate_wth_args(task_t
, void(*)(thread_t
, void *), void *);
60 kern_return_t
get_signalact(task_t
, thread_t
*, int);
61 int get_vmsubmap_entries(vm_map_t
, vm_object_offset_t
, vm_object_offset_t
);
62 int fill_task_rusage(task_t task
, rusage_info_current
*ri
);
63 int fill_task_io_rusage(task_t task
, rusage_info_current
*ri
);
64 int fill_task_qos_rusage(task_t task
, rusage_info_current
*ri
);
65 void fill_task_billed_usage(task_t task
, rusage_info_current
*ri
);
70 void *get_bsdtask_info(task_t t
)
78 void *get_bsdthreadtask_info(thread_t th
)
80 return(th
->task
!= TASK_NULL
? th
->task
->bsd_info
: NULL
);
86 void set_bsdtask_info(task_t t
,void * v
)
94 void *get_bsdthread_info(thread_t th
)
102 int get_thread_lock_count(thread_t th
); /* forced forward */
103 int get_thread_lock_count(thread_t th
)
105 return(th
->mutex_count
);
109 * XXX: wait for BSD to fix signal code
110 * Until then, we cannot block here. We know the task
111 * can't go away, so we make sure it is still active after
112 * retrieving the first thread for extra safety.
114 thread_t
get_firstthread(task_t task
)
116 thread_t thread
= (thread_t
)(void *)queue_first(&task
->threads
);
118 if (queue_end(&task
->threads
, (queue_entry_t
)thread
))
119 thread
= THREAD_NULL
;
122 return (THREAD_NULL
);
130 thread_t
*result_out
,
133 kern_return_t result
= KERN_SUCCESS
;
134 thread_t inc
, thread
= THREAD_NULL
;
141 return (KERN_FAILURE
);
144 for (inc
= (thread_t
)(void *)queue_first(&task
->threads
);
145 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
146 thread_mtx_lock(inc
);
148 (inc
->sched_flags
& TH_SFLAG_ABORTED_MASK
) != TH_SFLAG_ABORT
) {
152 thread_mtx_unlock(inc
);
154 inc
= (thread_t
)(void *)queue_next(&inc
->task_threads
);
158 *result_out
= thread
;
162 act_set_astbsd(thread
);
164 thread_mtx_unlock(thread
);
167 result
= KERN_FAILURE
;
181 kern_return_t result
= KERN_FAILURE
;
189 return (KERN_FAILURE
);
192 for (inc
= (thread_t
)(void *)queue_first(&task
->threads
);
193 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
195 thread_mtx_lock(inc
);
198 (inc
->sched_flags
& TH_SFLAG_ABORTED_MASK
) != TH_SFLAG_ABORT
) {
199 result
= KERN_SUCCESS
;
203 thread_mtx_unlock(inc
);
207 inc
= (thread_t
)(void *)queue_next(&inc
->task_threads
);
210 if (result
== KERN_SUCCESS
) {
212 act_set_astbsd(thread
);
214 thread_mtx_unlock(thread
);
222 ledger_t
get_task_ledger(task_t t
)
228 * This is only safe to call from a thread executing in
229 * in the task's context or if the task is locked Otherwise,
230 * the map could be switched for the task (and freed) before
231 * we to return it here.
233 vm_map_t
get_task_map(task_t t
)
238 vm_map_t
get_task_map_reference(task_t t
)
251 vm_map_reference_swap(m
);
259 ipc_space_t
get_task_ipcspace(task_t t
)
261 return(t
->itk_space
);
264 int get_task_numactivethreads(task_t task
)
267 int num_active_thr
=0;
270 for (inc
= (thread_t
)(void *)queue_first(&task
->threads
);
271 !queue_end(&task
->threads
, (queue_entry_t
)inc
); inc
= (thread_t
)(void *)queue_next(&inc
->task_threads
))
277 return num_active_thr
;
280 int get_task_numacts(task_t t
)
282 return(t
->thread_count
);
285 /* does this machine need 64bit register set for signal handler */
286 int is_64signalregset(void)
288 if (task_has_64BitData(current_task())) {
296 * Swap in a new map for the task/thread pair; the old map reference is
300 swap_task_map(task_t task
, thread_t thread
, vm_map_t map
, boolean_t doswitch
)
304 if (task
!= thread
->task
)
305 panic("swap_task_map");
308 mp_disable_preemption();
310 thread
->map
= task
->map
= map
;
312 pmap_switch(map
->pmap
);
314 mp_enable_preemption();
317 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
318 inval_copy_windows(thread
);
327 pmap_t
get_task_pmap(task_t t
)
329 return(t
->map
->pmap
);
335 uint64_t get_task_resident_size(task_t task
)
339 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
340 return((uint64_t)pmap_resident_count(map
->pmap
) * PAGE_SIZE_64
);
343 uint64_t get_task_compressed(task_t task
)
347 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
348 return((uint64_t)pmap_compressed(map
->pmap
) * PAGE_SIZE_64
);
351 uint64_t get_task_resident_max(task_t task
)
355 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
356 return((uint64_t)pmap_resident_max(map
->pmap
) * PAGE_SIZE_64
);
359 uint64_t get_task_purgeable_size(task_t task
)
362 mach_vm_size_t volatile_virtual_size
;
363 mach_vm_size_t volatile_resident_size
;
364 mach_vm_size_t volatile_pmap_size
;
366 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
367 vm_map_query_volatile(map
, &volatile_virtual_size
, &volatile_resident_size
, &volatile_pmap_size
);
369 return((uint64_t)volatile_resident_size
);
374 uint64_t get_task_phys_footprint(task_t task
)
377 ledger_amount_t credit
, debit
;
379 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.phys_footprint
, &credit
, &debit
);
380 if (KERN_SUCCESS
== ret
) {
381 return (credit
- debit
);
390 uint64_t get_task_phys_footprint_max(task_t task
)
395 ret
= ledger_get_maximum(task
->ledger
, task_ledgers
.phys_footprint
, &max
);
396 if (KERN_SUCCESS
== ret
) {
403 uint64_t get_task_cpu_time(task_t task
)
406 ledger_amount_t credit
, debit
;
408 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.cpu_time
, &credit
, &debit
);
409 if (KERN_SUCCESS
== ret
) {
410 return (credit
- debit
);
419 pmap_t
get_map_pmap(vm_map_t map
)
426 task_t
get_threadtask(thread_t th
)
438 return(vm_map_min(map
));
448 return(vm_map_max(map
));
458 get_vmsubmap_entries(
460 vm_object_offset_t start
,
461 vm_object_offset_t end
)
463 int total_entries
= 0;
464 vm_map_entry_t entry
;
468 entry
= vm_map_first_entry(map
);
469 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< start
)) {
470 entry
= entry
->vme_next
;
473 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
474 if(entry
->is_sub_map
) {
476 get_vmsubmap_entries(entry
->object
.sub_map
,
479 (entry
->vme_end
- entry
->vme_start
));
483 entry
= entry
->vme_next
;
487 return(total_entries
);
494 int total_entries
= 0;
495 vm_map_entry_t entry
;
499 entry
= vm_map_first_entry(map
);
501 while(entry
!= vm_map_to_entry(map
)) {
502 if(entry
->is_sub_map
) {
504 get_vmsubmap_entries(entry
->object
.sub_map
,
507 (entry
->vme_end
- entry
->vme_start
));
511 entry
= entry
->vme_next
;
515 return(total_entries
);
528 return(task
->user_stop_count
);
538 return(th
->user_stop_count
);
545 get_task_pidsuspended(
548 return (task
->pidsuspended
);
558 return (task
->frozen
);
568 return ((th
->sched_flags
& TH_SFLAG_ABORTED_MASK
) == TH_SFLAG_ABORT
);
572 * This routine is like thread_should_abort() above. It checks to
573 * see if the current thread is aborted. But unlike above, it also
574 * checks to see if thread is safely aborted. If so, it returns
575 * that fact, and clears the condition (safe aborts only should
576 * have a single effect, and a poll of the abort status
580 current_thread_aborted (
583 thread_t th
= current_thread();
586 if ((th
->sched_flags
& TH_SFLAG_ABORTED_MASK
) == TH_SFLAG_ABORT
&&
587 (th
->options
& TH_OPT_INTMASK
) != THREAD_UNINT
)
589 if (th
->sched_flags
& TH_SFLAG_ABORTSAFELY
) {
592 if (th
->sched_flags
& TH_SFLAG_ABORTSAFELY
)
593 th
->sched_flags
&= ~TH_SFLAG_ABORTED_MASK
;
604 task_act_iterate_wth_args(
606 void (*func_callback
)(thread_t
, void *),
613 for (inc
= (thread_t
)(void *)queue_first(&task
->threads
);
614 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
615 (void) (*func_callback
)(inc
, func_arg
);
616 inc
= (thread_t
)(void *)queue_next(&inc
->task_threads
);
628 reenable
= ml_set_interrupts_enabled(FALSE
);
629 ast_on_fast(AST_BSD
);
630 (void)ml_set_interrupts_enabled(reenable
);
634 #include <sys/bsdtask_info.h>
637 fill_taskprocinfo(task_t task
, struct proc_taskinfo_internal
* ptinfo
)
640 task_absolutetime_info_data_t tinfo
;
642 uint32_t cswitch
= 0, numrunning
= 0;
643 uint32_t syscalls_unix
= 0;
644 uint32_t syscalls_mach
= 0;
646 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
648 ptinfo
->pti_virtual_size
= map
->size
;
649 ptinfo
->pti_resident_size
=
650 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
655 ptinfo
->pti_policy
= ((task
!= kernel_task
)?
656 POLICY_TIMESHARE
: POLICY_RR
);
658 tinfo
.threads_user
= tinfo
.threads_system
= 0;
659 tinfo
.total_user
= task
->total_user_time
;
660 tinfo
.total_system
= task
->total_system_time
;
662 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
666 if (thread
->options
& TH_OPT_IDLE_THREAD
)
672 if ((thread
->state
& TH_RUN
) == TH_RUN
)
674 cswitch
+= thread
->c_switch
;
675 tval
= timer_grab(&thread
->user_timer
);
676 tinfo
.threads_user
+= tval
;
677 tinfo
.total_user
+= tval
;
679 tval
= timer_grab(&thread
->system_timer
);
681 if (thread
->precise_user_kernel_time
) {
682 tinfo
.threads_system
+= tval
;
683 tinfo
.total_system
+= tval
;
685 /* system_timer may represent either sys or user */
686 tinfo
.threads_user
+= tval
;
687 tinfo
.total_user
+= tval
;
690 syscalls_unix
+= thread
->syscalls_unix
;
691 syscalls_mach
+= thread
->syscalls_mach
;
693 thread_unlock(thread
);
697 ptinfo
->pti_total_system
= tinfo
.total_system
;
698 ptinfo
->pti_total_user
= tinfo
.total_user
;
699 ptinfo
->pti_threads_system
= tinfo
.threads_system
;
700 ptinfo
->pti_threads_user
= tinfo
.threads_user
;
702 ptinfo
->pti_faults
= task
->faults
;
703 ptinfo
->pti_pageins
= task
->pageins
;
704 ptinfo
->pti_cow_faults
= task
->cow_faults
;
705 ptinfo
->pti_messages_sent
= task
->messages_sent
;
706 ptinfo
->pti_messages_received
= task
->messages_received
;
707 ptinfo
->pti_syscalls_mach
= task
->syscalls_mach
+ syscalls_mach
;
708 ptinfo
->pti_syscalls_unix
= task
->syscalls_unix
+ syscalls_unix
;
709 ptinfo
->pti_csw
= task
->c_switch
+ cswitch
;
710 ptinfo
->pti_threadnum
= task
->thread_count
;
711 ptinfo
->pti_numrunning
= numrunning
;
712 ptinfo
->pti_priority
= task
->priority
;
718 fill_taskthreadinfo(task_t task
, uint64_t thaddr
, int thuniqueid
, struct proc_threadinfo_internal
* ptinfo
, void * vpp
, int *vidp
)
722 mach_msg_type_number_t count
;
723 thread_basic_info_data_t basic_info
;
729 for (thact
= (thread_t
)(void *)queue_first(&task
->threads
);
730 !queue_end(&task
->threads
, (queue_entry_t
)thact
); ) {
731 addr
= (thuniqueid
==0)?thact
->machine
.cthread_self
: thact
->thread_id
;
735 count
= THREAD_BASIC_INFO_COUNT
;
736 if ((kret
= thread_info_internal(thact
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &count
)) != KERN_SUCCESS
) {
740 ptinfo
->pth_user_time
= ((basic_info
.user_time
.seconds
* (integer_t
)NSEC_PER_SEC
) + (basic_info
.user_time
.microseconds
* (integer_t
)NSEC_PER_USEC
));
741 ptinfo
->pth_system_time
= ((basic_info
.system_time
.seconds
* (integer_t
)NSEC_PER_SEC
) + (basic_info
.system_time
.microseconds
* (integer_t
)NSEC_PER_USEC
));
743 ptinfo
->pth_cpu_usage
= basic_info
.cpu_usage
;
744 ptinfo
->pth_policy
= basic_info
.policy
;
745 ptinfo
->pth_run_state
= basic_info
.run_state
;
746 ptinfo
->pth_flags
= basic_info
.flags
;
747 ptinfo
->pth_sleep_time
= basic_info
.sleep_time
;
748 ptinfo
->pth_curpri
= thact
->sched_pri
;
749 ptinfo
->pth_priority
= thact
->priority
;
750 ptinfo
->pth_maxpriority
= thact
->max_priority
;
752 if ((vpp
!= NULL
) && (thact
->uthread
!= NULL
))
753 bsd_threadcdir(thact
->uthread
, vpp
, vidp
);
754 bsd_getthreadname(thact
->uthread
,ptinfo
->pth_name
);
758 thact
= (thread_t
)(void *)queue_next(&thact
->task_threads
);
768 fill_taskthreadlist(task_t task
, void * buffer
, int thcount
)
775 uptr
= (uint64_t *)buffer
;
779 for (thact
= (thread_t
)(void *)queue_first(&task
->threads
);
780 !queue_end(&task
->threads
, (queue_entry_t
)thact
); ) {
781 thaddr
= thact
->machine
.cthread_self
;
784 if (numthr
>= thcount
)
786 thact
= (thread_t
)(void *)queue_next(&thact
->task_threads
);
791 return (int)(numthr
* sizeof(uint64_t));
796 get_numthreads(task_t task
)
798 return(task
->thread_count
);
802 * Gather the various pieces of info about the designated task,
803 * and collect it all into a single rusage_info.
806 fill_task_rusage(task_t task
, rusage_info_current
*ri
)
808 struct task_power_info powerinfo
;
810 assert(task
!= TASK_NULL
);
813 task_power_info_locked(task
, &powerinfo
, NULL
);
814 ri
->ri_pkg_idle_wkups
= powerinfo
.task_platform_idle_wakeups
;
815 ri
->ri_interrupt_wkups
= powerinfo
.task_interrupt_wakeups
;
816 ri
->ri_user_time
= powerinfo
.total_user
;
817 ri
->ri_system_time
= powerinfo
.total_system
;
819 ledger_get_balance(task
->ledger
, task_ledgers
.phys_footprint
,
820 (ledger_amount_t
*)&ri
->ri_phys_footprint
);
821 ledger_get_balance(task
->ledger
, task_ledgers
.phys_mem
,
822 (ledger_amount_t
*)&ri
->ri_resident_size
);
823 ledger_get_balance(task
->ledger
, task_ledgers
.wired_mem
,
824 (ledger_amount_t
*)&ri
->ri_wired_size
);
826 ri
->ri_pageins
= task
->pageins
;
833 fill_task_billed_usage(task_t task __unused
, rusage_info_current
*ri
)
836 ri
->ri_billed_system_time
= bank_billed_time(task
->bank_context
);
837 ri
->ri_serviced_system_time
= bank_serviced_time(task
->bank_context
);
839 ri
->ri_billed_system_time
= 0;
840 ri
->ri_serviced_system_time
= 0;
845 fill_task_io_rusage(task_t task
, rusage_info_current
*ri
)
847 assert(task
!= TASK_NULL
);
850 if (task
->task_io_stats
) {
851 ri
->ri_diskio_bytesread
= task
->task_io_stats
->disk_reads
.size
;
852 ri
->ri_diskio_byteswritten
= (task
->task_io_stats
->total_io
.size
- task
->task_io_stats
->disk_reads
.size
);
854 /* I/O Stats unavailable */
855 ri
->ri_diskio_bytesread
= 0;
856 ri
->ri_diskio_byteswritten
= 0;
863 fill_task_qos_rusage(task_t task
, rusage_info_current
*ri
)
867 assert(task
!= TASK_NULL
);
870 /* Rollup Qos time of all the threads to task */
871 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
872 if (thread
->options
& TH_OPT_IDLE_THREAD
)
875 thread_mtx_lock(thread
);
876 thread_update_qos_cpu_time(thread
, TRUE
);
877 thread_mtx_unlock(thread
);
880 ri
->ri_cpu_time_qos_default
= task
->cpu_time_qos_stats
.cpu_time_qos_default
;
881 ri
->ri_cpu_time_qos_maintenance
= task
->cpu_time_qos_stats
.cpu_time_qos_maintenance
;
882 ri
->ri_cpu_time_qos_background
= task
->cpu_time_qos_stats
.cpu_time_qos_background
;
883 ri
->ri_cpu_time_qos_utility
= task
->cpu_time_qos_stats
.cpu_time_qos_utility
;
884 ri
->ri_cpu_time_qos_legacy
= task
->cpu_time_qos_stats
.cpu_time_qos_legacy
;
885 ri
->ri_cpu_time_qos_user_initiated
= task
->cpu_time_qos_stats
.cpu_time_qos_user_initiated
;
886 ri
->ri_cpu_time_qos_user_interactive
= task
->cpu_time_qos_stats
.cpu_time_qos_user_interactive
;