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>
31 #include <kern/kern_types.h>
32 #include <kern/processor.h>
33 #include <kern/thread.h>
34 #include <kern/task.h>
36 #include <kern/lock.h>
38 #include <ipc/ipc_port.h>
39 #include <ipc/ipc_object.h>
40 #include <vm/vm_map.h>
41 #include <vm/vm_kern.h>
43 #include <vm/vm_protos.h> /* last */
45 #undef thread_should_halt
47 /* BSD KERN COMPONENT INTERFACE */
49 task_t bsd_init_task
= TASK_NULL
;
50 char init_task_failure_data
[1024];
51 extern unsigned int not_in_kdp
; /* Skip acquiring locks if we're in kdp */
53 thread_t
get_firstthread(task_t
);
54 int get_task_userstop(task_t
);
55 int get_thread_userstop(thread_t
);
56 boolean_t
current_thread_aborted(void);
57 void task_act_iterate_wth_args(task_t
, void(*)(thread_t
, void *), void *);
58 kern_return_t
get_signalact(task_t
, thread_t
*, int);
59 int get_vmsubmap_entries(vm_map_t
, vm_object_offset_t
, vm_object_offset_t
);
60 void syscall_exit_funnelcheck(void);
66 void *get_bsdtask_info(task_t t
)
74 void *get_bsdthreadtask_info(thread_t th
)
76 return(th
->task
!= TASK_NULL
? th
->task
->bsd_info
: NULL
);
82 void set_bsdtask_info(task_t t
,void * v
)
90 void *get_bsdthread_info(thread_t th
)
98 int get_thread_lock_count(thread_t th
); /* forced forward */
99 int get_thread_lock_count(thread_t th
)
101 return(th
->mutex_count
);
105 * XXX: wait for BSD to fix signal code
106 * Until then, we cannot block here. We know the task
107 * can't go away, so we make sure it is still active after
108 * retrieving the first thread for extra safety.
110 thread_t
get_firstthread(task_t task
)
112 thread_t thread
= (thread_t
)queue_first(&task
->threads
);
114 if (queue_end(&task
->threads
, (queue_entry_t
)thread
))
115 thread
= THREAD_NULL
;
118 return (THREAD_NULL
);
126 thread_t
*result_out
,
129 kern_return_t result
= KERN_SUCCESS
;
130 thread_t inc
, thread
= THREAD_NULL
;
137 return (KERN_FAILURE
);
140 for (inc
= (thread_t
)queue_first(&task
->threads
);
141 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
142 thread_mtx_lock(inc
);
144 (inc
->sched_flags
& TH_SFLAG_ABORTED_MASK
) != TH_SFLAG_ABORT
) {
148 thread_mtx_unlock(inc
);
150 inc
= (thread_t
)queue_next(&inc
->task_threads
);
154 *result_out
= thread
;
158 act_set_astbsd(thread
);
160 thread_mtx_unlock(thread
);
163 result
= KERN_FAILURE
;
177 kern_return_t result
= KERN_FAILURE
;
185 return (KERN_FAILURE
);
188 for (inc
= (thread_t
)queue_first(&task
->threads
);
189 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
191 thread_mtx_lock(inc
);
194 (inc
->sched_flags
& TH_SFLAG_ABORTED_MASK
) != TH_SFLAG_ABORT
) {
195 result
= KERN_SUCCESS
;
199 thread_mtx_unlock(inc
);
203 inc
= (thread_t
)queue_next(&inc
->task_threads
);
206 if (result
== KERN_SUCCESS
) {
208 act_set_astbsd(thread
);
210 thread_mtx_unlock(thread
);
218 ledger_t
get_task_ledger(task_t t
)
224 * This is only safe to call from a thread executing in
225 * in the task's context or if the task is locked Otherwise,
226 * the map could be switched for the task (and freed) before
227 * we to return it here.
229 vm_map_t
get_task_map(task_t t
)
234 vm_map_t
get_task_map_reference(task_t t
)
247 vm_map_reference_swap(m
);
255 ipc_space_t
get_task_ipcspace(task_t t
)
257 return(t
->itk_space
);
260 int get_task_numactivethreads(task_t task
)
263 int num_active_thr
=0;
266 for (inc
= (thread_t
)queue_first(&task
->threads
);
267 !queue_end(&task
->threads
, (queue_entry_t
)inc
); inc
= (thread_t
)queue_next(&inc
->task_threads
))
273 return num_active_thr
;
276 int get_task_numacts(task_t t
)
278 return(t
->thread_count
);
281 /* does this machine need 64bit register set for signal handler */
282 int is_64signalregset(void)
284 task_t t
= current_task();
285 if(t
->taskFeatures
[0] & tf64BitData
)
292 * Swap in a new map for the task/thread pair; the old map reference is
296 swap_task_map(task_t task
, thread_t thread
, vm_map_t map
, boolean_t doswitch
)
300 if (task
!= thread
->task
)
301 panic("swap_task_map");
304 mp_disable_preemption();
306 thread
->map
= task
->map
= map
;
308 pmap_switch(map
->pmap
);
310 mp_enable_preemption();
313 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
314 inval_copy_windows(thread
);
323 pmap_t
get_task_pmap(task_t t
)
325 return(t
->map
->pmap
);
331 uint64_t get_task_resident_size(task_t task
)
335 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
336 return((uint64_t)pmap_resident_count(map
->pmap
) * PAGE_SIZE_64
);
342 pmap_t
get_map_pmap(vm_map_t map
)
349 task_t
get_threadtask(thread_t th
)
361 return(vm_map_min(map
));
371 return(vm_map_max(map
));
381 get_vmsubmap_entries(
383 vm_object_offset_t start
,
384 vm_object_offset_t end
)
386 int total_entries
= 0;
387 vm_map_entry_t entry
;
391 entry
= vm_map_first_entry(map
);
392 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< start
)) {
393 entry
= entry
->vme_next
;
396 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
397 if(entry
->is_sub_map
) {
399 get_vmsubmap_entries(entry
->object
.sub_map
,
402 (entry
->vme_end
- entry
->vme_start
));
406 entry
= entry
->vme_next
;
410 return(total_entries
);
417 int total_entries
= 0;
418 vm_map_entry_t entry
;
422 entry
= vm_map_first_entry(map
);
424 while(entry
!= vm_map_to_entry(map
)) {
425 if(entry
->is_sub_map
) {
427 get_vmsubmap_entries(entry
->object
.sub_map
,
430 (entry
->vme_end
- entry
->vme_start
));
434 entry
= entry
->vme_next
;
438 return(total_entries
);
451 return(task
->user_stop_count
);
461 return(th
->user_stop_count
);
468 get_task_pidsuspended(
471 return (task
->pidsuspended
);
481 return (task
->frozen
);
491 return ((th
->sched_flags
& TH_SFLAG_ABORTED_MASK
) == TH_SFLAG_ABORT
);
495 * This routine is like thread_should_abort() above. It checks to
496 * see if the current thread is aborted. But unlike above, it also
497 * checks to see if thread is safely aborted. If so, it returns
498 * that fact, and clears the condition (safe aborts only should
499 * have a single effect, and a poll of the abort status
503 current_thread_aborted (
506 thread_t th
= current_thread();
509 if ((th
->sched_flags
& TH_SFLAG_ABORTED_MASK
) == TH_SFLAG_ABORT
&&
510 (th
->options
& TH_OPT_INTMASK
) != THREAD_UNINT
)
512 if (th
->sched_flags
& TH_SFLAG_ABORTSAFELY
) {
515 if (th
->sched_flags
& TH_SFLAG_ABORTSAFELY
)
516 th
->sched_flags
&= ~TH_SFLAG_ABORTED_MASK
;
527 task_act_iterate_wth_args(
529 void (*func_callback
)(thread_t
, void *),
536 for (inc
= (thread_t
)queue_first(&task
->threads
);
537 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
538 (void) (*func_callback
)(inc
, func_arg
);
539 inc
= (thread_t
)queue_next(&inc
->task_threads
);
551 reenable
= ml_set_interrupts_enabled(FALSE
);
552 ast_on_fast(AST_BSD
);
553 (void)ml_set_interrupts_enabled(reenable
);
557 #include <sys/bsdtask_info.h>
560 fill_taskprocinfo(task_t task
, struct proc_taskinfo_internal
* ptinfo
)
563 task_absolutetime_info_data_t tinfo
;
565 uint32_t cswitch
= 0, numrunning
= 0;
566 uint32_t syscalls_unix
= 0;
567 uint32_t syscalls_mach
= 0;
569 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
571 ptinfo
->pti_virtual_size
= map
->size
;
572 ptinfo
->pti_resident_size
=
573 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
578 ptinfo
->pti_policy
= ((task
!= kernel_task
)?
579 POLICY_TIMESHARE
: POLICY_RR
);
581 tinfo
.threads_user
= tinfo
.threads_system
= 0;
582 tinfo
.total_user
= task
->total_user_time
;
583 tinfo
.total_system
= task
->total_system_time
;
585 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
592 if ((thread
->state
& TH_RUN
) == TH_RUN
)
594 cswitch
+= thread
->c_switch
;
595 tval
= timer_grab(&thread
->user_timer
);
596 tinfo
.threads_user
+= tval
;
597 tinfo
.total_user
+= tval
;
599 tval
= timer_grab(&thread
->system_timer
);
601 if (thread
->precise_user_kernel_time
) {
602 tinfo
.threads_system
+= tval
;
603 tinfo
.total_system
+= tval
;
605 /* system_timer may represent either sys or user */
606 tinfo
.threads_user
+= tval
;
607 tinfo
.total_user
+= tval
;
610 syscalls_unix
+= thread
->syscalls_unix
;
611 syscalls_mach
+= thread
->syscalls_mach
;
613 thread_unlock(thread
);
617 ptinfo
->pti_total_system
= tinfo
.total_system
;
618 ptinfo
->pti_total_user
= tinfo
.total_user
;
619 ptinfo
->pti_threads_system
= tinfo
.threads_system
;
620 ptinfo
->pti_threads_user
= tinfo
.threads_user
;
622 ptinfo
->pti_faults
= task
->faults
;
623 ptinfo
->pti_pageins
= task
->pageins
;
624 ptinfo
->pti_cow_faults
= task
->cow_faults
;
625 ptinfo
->pti_messages_sent
= task
->messages_sent
;
626 ptinfo
->pti_messages_received
= task
->messages_received
;
627 ptinfo
->pti_syscalls_mach
= task
->syscalls_mach
+ syscalls_mach
;
628 ptinfo
->pti_syscalls_unix
= task
->syscalls_unix
+ syscalls_unix
;
629 ptinfo
->pti_csw
= task
->c_switch
+ cswitch
;
630 ptinfo
->pti_threadnum
= task
->thread_count
;
631 ptinfo
->pti_numrunning
= numrunning
;
632 ptinfo
->pti_priority
= task
->priority
;
638 fill_taskthreadinfo(task_t task
, uint64_t thaddr
, int thuniqueid
, struct proc_threadinfo_internal
* ptinfo
, void * vpp
, int *vidp
)
642 mach_msg_type_number_t count
;
643 thread_basic_info_data_t basic_info
;
649 for (thact
= (thread_t
)queue_first(&task
->threads
);
650 !queue_end(&task
->threads
, (queue_entry_t
)thact
); ) {
651 addr
= (thuniqueid
==0)?thact
->machine
.cthread_self
: thact
->thread_id
;
655 count
= THREAD_BASIC_INFO_COUNT
;
656 if ((kret
= thread_info_internal(thact
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &count
)) != KERN_SUCCESS
) {
660 ptinfo
->pth_user_time
= ((basic_info
.user_time
.seconds
* NSEC_PER_SEC
) + (basic_info
.user_time
.microseconds
* NSEC_PER_USEC
));
661 ptinfo
->pth_system_time
= ((basic_info
.system_time
.seconds
* NSEC_PER_SEC
) + (basic_info
.system_time
.microseconds
* NSEC_PER_USEC
));
663 ptinfo
->pth_cpu_usage
= basic_info
.cpu_usage
;
664 ptinfo
->pth_policy
= basic_info
.policy
;
665 ptinfo
->pth_run_state
= basic_info
.run_state
;
666 ptinfo
->pth_flags
= basic_info
.flags
;
667 ptinfo
->pth_sleep_time
= basic_info
.sleep_time
;
668 ptinfo
->pth_curpri
= thact
->sched_pri
;
669 ptinfo
->pth_priority
= thact
->priority
;
670 ptinfo
->pth_maxpriority
= thact
->max_priority
;
672 if ((vpp
!= NULL
) && (thact
->uthread
!= NULL
))
673 bsd_threadcdir(thact
->uthread
, vpp
, vidp
);
674 bsd_getthreadname(thact
->uthread
,ptinfo
->pth_name
);
678 thact
= (thread_t
)queue_next(&thact
->task_threads
);
688 fill_taskthreadlist(task_t task
, void * buffer
, int thcount
)
695 uptr
= (uint64_t *)buffer
;
699 for (thact
= (thread_t
)queue_first(&task
->threads
);
700 !queue_end(&task
->threads
, (queue_entry_t
)thact
); ) {
701 thaddr
= thact
->machine
.cthread_self
;
704 if (numthr
>= thcount
)
706 thact
= (thread_t
)queue_next(&thact
->task_threads
);
711 return (int)(numthr
* sizeof(uint64_t));
716 get_numthreads(task_t task
)
718 return(task
->thread_count
);
722 syscall_exit_funnelcheck(void)
726 thread
= current_thread();
728 if (thread
->funnel_lock
)
729 panic("syscall exit with funnel held\n");