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
46 #undef ipc_port_release
48 /* BSD KERN COMPONENT INTERFACE */
50 task_t bsd_init_task
= TASK_NULL
;
51 char init_task_failure_data
[1024];
52 extern unsigned int not_in_kdp
; /* Skip acquiring locks if we're in kdp */
54 thread_t
get_firstthread(task_t
);
55 int get_task_userstop(task_t
);
56 int get_thread_userstop(thread_t
);
57 boolean_t
thread_should_abort(thread_t
);
58 boolean_t
current_thread_aborted(void);
59 void task_act_iterate_wth_args(task_t
, void(*)(thread_t
, void *), void *);
60 void ipc_port_release(ipc_port_t
);
61 kern_return_t
get_signalact(task_t
, thread_t
*, int);
62 int get_vmsubmap_entries(vm_map_t
, vm_object_offset_t
, vm_object_offset_t
);
63 void syscall_exit_funnelcheck(void);
69 void *get_bsdtask_info(task_t t
)
77 void *get_bsdthreadtask_info(thread_t th
)
79 return(th
->task
!= TASK_NULL
? th
->task
->bsd_info
: NULL
);
85 void set_bsdtask_info(task_t t
,void * v
)
93 void *get_bsdthread_info(thread_t th
)
101 int get_thread_lock_count(thread_t th
); /* forced forward */
102 int get_thread_lock_count(thread_t th
)
104 return(th
->mutex_count
);
108 * XXX: wait for BSD to fix signal code
109 * Until then, we cannot block here. We know the task
110 * can't go away, so we make sure it is still active after
111 * retrieving the first thread for extra safety.
113 thread_t
get_firstthread(task_t task
)
115 thread_t thread
= (thread_t
)queue_first(&task
->threads
);
117 if (queue_end(&task
->threads
, (queue_entry_t
)thread
))
118 thread
= THREAD_NULL
;
121 return (THREAD_NULL
);
129 thread_t
*result_out
,
132 kern_return_t result
= KERN_SUCCESS
;
133 thread_t inc
, thread
= THREAD_NULL
;
140 return (KERN_FAILURE
);
143 for (inc
= (thread_t
)queue_first(&task
->threads
);
144 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
145 thread_mtx_lock(inc
);
147 (inc
->sched_flags
& TH_SFLAG_ABORTED_MASK
) != TH_SFLAG_ABORT
) {
151 thread_mtx_unlock(inc
);
153 inc
= (thread_t
)queue_next(&inc
->task_threads
);
157 *result_out
= thread
;
161 act_set_astbsd(thread
);
163 thread_mtx_unlock(thread
);
166 result
= KERN_FAILURE
;
180 kern_return_t result
= KERN_FAILURE
;
188 return (KERN_FAILURE
);
191 for (inc
= (thread_t
)queue_first(&task
->threads
);
192 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
194 thread_mtx_lock(inc
);
197 (inc
->sched_flags
& TH_SFLAG_ABORTED_MASK
) != TH_SFLAG_ABORT
) {
198 result
= KERN_SUCCESS
;
202 thread_mtx_unlock(inc
);
206 inc
= (thread_t
)queue_next(&inc
->task_threads
);
209 if (result
== KERN_SUCCESS
) {
211 act_set_astbsd(thread
);
213 thread_mtx_unlock(thread
);
222 * This is only safe to call from a thread executing in
223 * in the task's context or if the task is locked Otherwise,
224 * the map could be switched for the task (and freed) before
225 * we to return it here.
227 vm_map_t
get_task_map(task_t t
)
232 vm_map_t
get_task_map_reference(task_t t
)
245 vm_map_reference_swap(m
);
253 ipc_space_t
get_task_ipcspace(task_t t
)
255 return(t
->itk_space
);
258 int get_task_numactivethreads(task_t task
)
261 int num_active_thr
=0;
264 for (inc
= (thread_t
)queue_first(&task
->threads
);
265 !queue_end(&task
->threads
, (queue_entry_t
)inc
); inc
= (thread_t
)queue_next(&inc
->task_threads
))
271 return num_active_thr
;
274 int get_task_numacts(task_t t
)
276 return(t
->thread_count
);
279 /* does this machine need 64bit register set for signal handler */
280 int is_64signalregset(void)
282 task_t t
= current_task();
283 if(t
->taskFeatures
[0] & tf64BitData
)
290 * Swap in a new map for the task/thread pair; the old map reference is
294 swap_task_map(task_t task
, thread_t thread
, vm_map_t map
, boolean_t doswitch
)
298 if (task
!= thread
->task
)
299 panic("swap_task_map");
302 mp_disable_preemption();
304 thread
->map
= task
->map
= map
;
306 pmap_switch(map
->pmap
);
307 mp_enable_preemption();
310 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
311 inval_copy_windows(thread
);
320 pmap_t
get_task_pmap(task_t t
)
322 return(t
->map
->pmap
);
328 uint64_t get_task_resident_size(task_t task
)
332 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
333 return((uint64_t)pmap_resident_count(map
->pmap
) * PAGE_SIZE_64
);
339 pmap_t
get_map_pmap(vm_map_t map
)
346 task_t
get_threadtask(thread_t th
)
358 return(vm_map_min(map
));
368 return(vm_map_max(map
));
378 get_vmsubmap_entries(
380 vm_object_offset_t start
,
381 vm_object_offset_t end
)
383 int total_entries
= 0;
384 vm_map_entry_t entry
;
388 entry
= vm_map_first_entry(map
);
389 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< start
)) {
390 entry
= entry
->vme_next
;
393 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
394 if(entry
->is_sub_map
) {
396 get_vmsubmap_entries(entry
->object
.sub_map
,
399 (entry
->vme_end
- entry
->vme_start
));
403 entry
= entry
->vme_next
;
407 return(total_entries
);
414 int total_entries
= 0;
415 vm_map_entry_t entry
;
419 entry
= vm_map_first_entry(map
);
421 while(entry
!= vm_map_to_entry(map
)) {
422 if(entry
->is_sub_map
) {
424 get_vmsubmap_entries(entry
->object
.sub_map
,
427 (entry
->vme_end
- entry
->vme_start
));
431 entry
= entry
->vme_next
;
435 return(total_entries
);
448 return(task
->user_stop_count
);
458 return(th
->user_stop_count
);
468 return ((th
->sched_flags
& TH_SFLAG_ABORTED_MASK
) == TH_SFLAG_ABORT
);
472 * This routine is like thread_should_abort() above. It checks to
473 * see if the current thread is aborted. But unlike above, it also
474 * checks to see if thread is safely aborted. If so, it returns
475 * that fact, and clears the condition (safe aborts only should
476 * have a single effect, and a poll of the abort status
480 current_thread_aborted (
483 thread_t th
= current_thread();
486 if ((th
->sched_flags
& TH_SFLAG_ABORTED_MASK
) == TH_SFLAG_ABORT
&&
487 (th
->options
& TH_OPT_INTMASK
) != THREAD_UNINT
)
489 if (th
->sched_flags
& TH_SFLAG_ABORTSAFELY
) {
492 if (th
->sched_flags
& TH_SFLAG_ABORTSAFELY
)
493 th
->sched_flags
&= ~TH_SFLAG_ABORTED_MASK
;
504 task_act_iterate_wth_args(
506 void (*func_callback
)(thread_t
, void *),
513 for (inc
= (thread_t
)queue_first(&task
->threads
);
514 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
515 (void) (*func_callback
)(inc
, func_arg
);
516 inc
= (thread_t
)queue_next(&inc
->task_threads
);
526 ipc_object_release(&(port
)->ip_object
);
534 reenable
= ml_set_interrupts_enabled(FALSE
);
535 ast_on_fast(AST_BSD
);
536 (void)ml_set_interrupts_enabled(reenable
);
540 #include <sys/bsdtask_info.h>
543 fill_taskprocinfo(task_t task
, struct proc_taskinfo_internal
* ptinfo
)
546 task_absolutetime_info_data_t tinfo
;
548 uint32_t cswitch
= 0, numrunning
= 0;
549 uint32_t syscalls_unix
= 0;
550 uint32_t syscalls_mach
= 0;
552 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
554 ptinfo
->pti_virtual_size
= map
->size
;
555 ptinfo
->pti_resident_size
=
556 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
561 ptinfo
->pti_policy
= ((task
!= kernel_task
)?
562 POLICY_TIMESHARE
: POLICY_RR
);
564 tinfo
.threads_user
= tinfo
.threads_system
= 0;
565 tinfo
.total_user
= task
->total_user_time
;
566 tinfo
.total_system
= task
->total_system_time
;
568 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
571 if ((thread
->state
& TH_RUN
) == TH_RUN
)
573 cswitch
+= thread
->c_switch
;
574 tval
= timer_grab(&thread
->user_timer
);
575 tinfo
.threads_user
+= tval
;
576 tinfo
.total_user
+= tval
;
578 tval
= timer_grab(&thread
->system_timer
);
579 tinfo
.threads_system
+= tval
;
580 tinfo
.total_system
+= tval
;
582 syscalls_unix
+= thread
->syscalls_unix
;
583 syscalls_mach
+= thread
->syscalls_mach
;
586 ptinfo
->pti_total_system
= tinfo
.total_system
;
587 ptinfo
->pti_total_user
= tinfo
.total_user
;
588 ptinfo
->pti_threads_system
= tinfo
.threads_system
;
589 ptinfo
->pti_threads_user
= tinfo
.threads_user
;
591 ptinfo
->pti_faults
= task
->faults
;
592 ptinfo
->pti_pageins
= task
->pageins
;
593 ptinfo
->pti_cow_faults
= task
->cow_faults
;
594 ptinfo
->pti_messages_sent
= task
->messages_sent
;
595 ptinfo
->pti_messages_received
= task
->messages_received
;
596 ptinfo
->pti_syscalls_mach
= task
->syscalls_mach
+ syscalls_mach
;
597 ptinfo
->pti_syscalls_unix
= task
->syscalls_unix
+ syscalls_unix
;
598 ptinfo
->pti_csw
= task
->c_switch
+ cswitch
;
599 ptinfo
->pti_threadnum
= task
->thread_count
;
600 ptinfo
->pti_numrunning
= numrunning
;
601 ptinfo
->pti_priority
= task
->priority
;
607 fill_taskthreadinfo(task_t task
, uint64_t thaddr
, struct proc_threadinfo_internal
* ptinfo
, void * vpp
, int *vidp
)
611 mach_msg_type_number_t count
;
612 thread_basic_info_data_t basic_info
;
617 for (thact
= (thread_t
)queue_first(&task
->threads
);
618 !queue_end(&task
->threads
, (queue_entry_t
)thact
); ) {
619 if (thact
->machine
.cthread_self
== thaddr
)
622 count
= THREAD_BASIC_INFO_COUNT
;
623 if ((kret
= thread_info_internal(thact
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &count
)) != KERN_SUCCESS
) {
628 ptinfo
->pth_user_time
= timer_grab(&basic_info
.user_time
);
629 ptinfo
->pth_system_time
= timer_grab(&basic_info
.system_time
);
631 ptinfo
->pth_user_time
= ((basic_info
.user_time
.seconds
* NSEC_PER_SEC
) + (basic_info
.user_time
.microseconds
* NSEC_PER_USEC
));
632 ptinfo
->pth_system_time
= ((basic_info
.system_time
.seconds
* NSEC_PER_SEC
) + (basic_info
.system_time
.microseconds
* NSEC_PER_USEC
));
635 ptinfo
->pth_cpu_usage
= basic_info
.cpu_usage
;
636 ptinfo
->pth_policy
= basic_info
.policy
;
637 ptinfo
->pth_run_state
= basic_info
.run_state
;
638 ptinfo
->pth_flags
= basic_info
.flags
;
639 ptinfo
->pth_sleep_time
= basic_info
.sleep_time
;
640 ptinfo
->pth_curpri
= thact
->sched_pri
;
641 ptinfo
->pth_priority
= thact
->priority
;
642 ptinfo
->pth_maxpriority
= thact
->max_priority
;
644 if ((vpp
!= NULL
) && (thact
->uthread
!= NULL
))
645 bsd_threadcdir(thact
->uthread
, vpp
, vidp
);
646 bsd_getthreadname(thact
->uthread
,ptinfo
->pth_name
);
650 thact
= (thread_t
)queue_next(&thact
->task_threads
);
660 fill_taskthreadlist(task_t task
, void * buffer
, int thcount
)
667 uptr
= (uint64_t *)buffer
;
671 for (thact
= (thread_t
)queue_first(&task
->threads
);
672 !queue_end(&task
->threads
, (queue_entry_t
)thact
); ) {
673 thaddr
= thact
->machine
.cthread_self
;
676 if (numthr
>= thcount
)
678 thact
= (thread_t
)queue_next(&thact
->task_threads
);
683 return (int)(numthr
* sizeof(uint64_t));
688 get_numthreads(task_t task
)
690 return(task
->thread_count
);
694 syscall_exit_funnelcheck(void)
698 thread
= current_thread();
700 if (thread
->funnel_lock
)
701 panic("syscall exit with funnel held\n");