2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * @OSF_FREE_COPYRIGHT@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub,
63 * Task management primitives implementation.
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
69 * Permission to use, copy, modify and distribute this software and its
70 * documentation is hereby granted, provided that both the copyright
71 * notice and this permission notice appear in all copies of the
72 * software, derivative works or modified versions, and any portions
73 * thereof, and that both notices appear in supporting documentation.
75 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
76 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
77 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
79 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
80 * improvements that they make and grant CSL redistribution rights.
85 #include <mach_host.h>
86 #include <mach_prof.h>
88 #include <platforms.h>
90 #include <mach/mach_types.h>
91 #include <mach/boolean.h>
92 #include <mach/host_priv.h>
93 #include <mach/machine/vm_types.h>
94 #include <mach/vm_param.h>
95 #include <mach/semaphore.h>
96 #include <mach/task_info.h>
97 #include <mach/task_special_ports.h>
99 #include <ipc/ipc_types.h>
100 #include <ipc/ipc_space.h>
101 #include <ipc/ipc_entry.h>
103 #include <kern/kern_types.h>
104 #include <kern/mach_param.h>
105 #include <kern/misc_protos.h>
106 #include <kern/task.h>
107 #include <kern/thread.h>
108 #include <kern/zalloc.h>
109 #include <kern/kalloc.h>
110 #include <kern/processor.h>
111 #include <kern/sched_prim.h> /* for thread_wakeup */
112 #include <kern/ipc_tt.h>
113 #include <kern/ledger.h>
114 #include <kern/host.h>
115 #include <kern/clock.h>
116 #include <kern/timer.h>
117 #include <kern/profile.h>
118 #include <kern/assert.h>
119 #include <kern/sync_lock.h>
122 #include <vm/vm_map.h>
123 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
124 #include <vm/vm_pageout.h>
125 #include <vm/vm_protos.h> /* for vm_map_remove_commpage64 */
128 #include <ddb/db_sym.h>
129 #endif /* MACH_KDB */
132 #include <ppc/exception.h>
133 #include <ppc/hw_perfmon.h>
137 * Exported interfaces
140 #include <mach/task_server.h>
141 #include <mach/mach_host_server.h>
142 #include <mach/host_security_server.h>
143 #include <mach/mach_port_server.h>
145 #include <vm/task_working_set.h>
146 #include <vm/vm_shared_memory_server.h>
153 void task_hold_locked(
155 void task_wait_locked(
157 void task_release_locked(
161 void task_synchronizer_destroy_all(
164 kern_return_t
task_set_ledger(
170 task_backing_store_privileged(
174 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
180 task_working_set_disable(task_t task
)
185 ws
= task
->dynamic_working_set
;
186 task
->dynamic_working_set
= NULL
;
189 tws_hash_ws_flush(ws
);
190 tws_hash_destroy(ws
);
200 /* LP64todo - no task working set for 64-bit */
201 task_set_64BitAddr(task
);
202 task_working_set_disable(task
);
203 task
->map
->max_offset
= MACH_VM_MAX_ADDRESS
;
206 * Deallocate all memory previously allocated
207 * above the 32-bit address space, since it won't
208 * be accessible anymore.
210 /* LP64todo - make this clean */
212 vm_map_remove_commpage64(task
->map
);
213 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
215 (void) vm_map_remove(task
->map
,
216 (vm_map_offset_t
) VM_MAX_ADDRESS
,
219 task_clear_64BitAddr(task
);
220 task
->map
->max_offset
= (vm_map_offset_t
)VM_MAX_ADDRESS
;
229 TASK_MAX
* sizeof(struct task
),
230 TASK_CHUNK
* sizeof(struct task
),
234 * Create the kernel task as the first task.
236 if (task_create_internal(TASK_NULL
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
237 panic("task_init\n");
239 vm_map_deallocate(kernel_task
->map
);
240 kernel_task
->map
= kernel_map
;
252 * If may_assign is false, task is already being assigned,
253 * wait for that to finish.
255 while (task
->may_assign
== FALSE
) {
258 task
->assign_active
= TRUE
;
259 res
= thread_sleep_mutex((event_t
) &task
->assign_active
,
260 &task
->lock
, THREAD_UNINT
);
261 assert(res
== THREAD_AWAKENED
);
263 task
->may_assign
= FALSE
;
268 #define thread_freeze(thread) assert(task->processor_set == &default_pset)
277 assert(task
->may_assign
== FALSE
);
278 task
->may_assign
= TRUE
;
279 if (task
->assign_active
== TRUE
) {
280 task
->assign_active
= FALSE
;
281 thread_wakeup((event_t
)&task
->assign_active
);
287 #define thread_unfreeze(thread) assert(task->processor_set == &default_pset)
290 #endif /* MACH_HOST */
293 * Create a task running in the kernel address space. It may
294 * have its own map of size mem_size and may have ipc privileges.
298 __unused task_t parent_task
,
299 __unused vm_offset_t map_base
,
300 __unused vm_size_t map_size
,
301 __unused task_t
*child_task
)
303 return (KERN_INVALID_ARGUMENT
);
309 __unused ledger_port_array_t ledger_ports
,
310 __unused mach_msg_type_number_t num_ledger_ports
,
311 boolean_t inherit_memory
,
312 task_t
*child_task
) /* OUT */
314 if (parent_task
== TASK_NULL
)
315 return(KERN_INVALID_ARGUMENT
);
317 return task_create_internal(
318 parent_task
, inherit_memory
, child_task
);
322 host_security_create_task_token(
323 host_security_t host_security
,
325 security_token_t sec_token
,
326 audit_token_t audit_token
,
327 host_priv_t host_priv
,
328 __unused ledger_port_array_t ledger_ports
,
329 __unused mach_msg_type_number_t num_ledger_ports
,
330 boolean_t inherit_memory
,
331 task_t
*child_task
) /* OUT */
333 kern_return_t result
;
335 if (parent_task
== TASK_NULL
)
336 return(KERN_INVALID_ARGUMENT
);
338 if (host_security
== HOST_NULL
)
339 return(KERN_INVALID_SECURITY
);
341 result
= task_create_internal(
342 parent_task
, inherit_memory
, child_task
);
344 if (result
!= KERN_SUCCESS
)
347 result
= host_security_set_task_token(host_security
,
353 if (result
!= KERN_SUCCESS
)
360 task_create_internal(
362 boolean_t inherit_memory
,
363 task_t
*child_task
) /* OUT */
366 processor_set_t pset
;
368 new_task
= (task_t
) zalloc(task_zone
);
370 if (new_task
== TASK_NULL
)
371 return(KERN_RESOURCE_SHORTAGE
);
373 /* one ref for just being alive; one for our caller */
374 new_task
->ref_count
= 2;
377 new_task
->map
= vm_map_fork(parent_task
->map
);
379 new_task
->map
= vm_map_create(pmap_create(0),
380 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
381 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
383 mutex_init(&new_task
->lock
, 0);
384 queue_init(&new_task
->threads
);
385 new_task
->suspend_count
= 0;
386 new_task
->thread_count
= 0;
387 new_task
->active_thread_count
= 0;
388 new_task
->user_stop_count
= 0;
389 new_task
->role
= TASK_UNSPECIFIED
;
390 new_task
->active
= TRUE
;
391 new_task
->user_data
= 0;
392 new_task
->faults
= 0;
393 new_task
->cow_faults
= 0;
394 new_task
->pageins
= 0;
395 new_task
->messages_sent
= 0;
396 new_task
->messages_received
= 0;
397 new_task
->syscalls_mach
= 0;
398 new_task
->priv_flags
= 0;
399 new_task
->syscalls_unix
=0;
401 new_task
->taskFeatures
[0] = 0; /* Init task features */
402 new_task
->taskFeatures
[1] = 0; /* Init task features */
403 new_task
->dynamic_working_set
= 0;
405 task_working_set_create(new_task
, TWS_SMALL_HASH_LINE_COUNT
,
406 0, TWS_HASH_STYLE_DEFAULT
);
409 new_task
->bsd_info
= 0;
410 #endif /* MACH_BSD */
413 if(BootProcInfo
.pf
.Available
& pf64Bit
) new_task
->taskFeatures
[0] |= tf64BitData
; /* If 64-bit machine, show we have 64-bit registers at least */
416 queue_init(&new_task
->semaphore_list
);
417 queue_init(&new_task
->lock_set_list
);
418 new_task
->semaphores_owned
= 0;
419 new_task
->lock_sets_owned
= 0;
422 new_task
->may_assign
= TRUE
;
423 new_task
->assign_active
= FALSE
;
424 #endif /* MACH_HOST */
426 ipc_task_init(new_task
, parent_task
);
428 new_task
->total_user_time
= 0;
429 new_task
->total_system_time
= 0;
431 task_prof_init(new_task
);
433 if (parent_task
!= TASK_NULL
) {
436 * Freeze the parent, so that parent_task->processor_set
439 task_freeze(parent_task
);
440 #endif /* MACH_HOST */
441 pset
= parent_task
->processor_set
;
443 pset
= &default_pset
;
445 new_task
->sec_token
= parent_task
->sec_token
;
446 new_task
->audit_token
= parent_task
->audit_token
;
448 shared_region_mapping_ref(parent_task
->system_shared_region
);
449 new_task
->system_shared_region
= parent_task
->system_shared_region
;
451 new_task
->wired_ledger_port
= ledger_copy(
452 convert_port_to_ledger(parent_task
->wired_ledger_port
));
453 new_task
->paged_ledger_port
= ledger_copy(
454 convert_port_to_ledger(parent_task
->paged_ledger_port
));
455 if(task_has_64BitAddr(parent_task
))
456 task_set_64BitAddr(new_task
);
459 pset
= &default_pset
;
461 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
462 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
463 new_task
->wired_ledger_port
= ledger_copy(root_wired_ledger
);
464 new_task
->paged_ledger_port
= ledger_copy(root_paged_ledger
);
467 if (kernel_task
== TASK_NULL
) {
468 new_task
->priority
= BASEPRI_KERNEL
;
469 new_task
->max_priority
= MAXPRI_KERNEL
;
472 new_task
->priority
= BASEPRI_DEFAULT
;
473 new_task
->max_priority
= MAXPRI_USER
;
477 pset_add_task(pset
, new_task
);
480 if (parent_task
!= TASK_NULL
)
481 task_unfreeze(parent_task
);
482 #endif /* MACH_HOST */
484 if (vm_backing_store_low
&& parent_task
!= NULL
)
485 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
487 ipc_task_enable(new_task
);
489 *child_task
= new_task
;
490 return(KERN_SUCCESS
);
496 * Drop a reference on a task.
502 processor_set_t pset
;
504 if (task
== TASK_NULL
)
507 if (task_deallocate_internal(task
) > 0)
510 pset
= task
->processor_set
;
511 pset_deallocate(pset
);
513 if(task
->dynamic_working_set
)
514 tws_hash_destroy(task
->dynamic_working_set
);
516 ipc_task_terminate(task
);
518 vm_map_deallocate(task
->map
);
519 is_release(task
->itk_space
);
521 task_prof_deallocate(task
);
522 zfree(task_zone
, task
);
528 * Terminate the specified task. See comments on thread_terminate
529 * (kern/thread.c) about problems with terminating the "current task."
536 if (task
== TASK_NULL
)
537 return (KERN_INVALID_ARGUMENT
);
540 return (KERN_FAILURE
);
542 return (task_terminate_internal(task
));
546 task_terminate_internal(
549 processor_set_t pset
;
550 thread_t thread
, self
;
552 boolean_t interrupt_save
;
554 assert(task
!= kernel_task
);
556 self
= current_thread();
557 self_task
= self
->task
;
560 * Get the task locked and make sure that we are not racing
561 * with someone else trying to terminate us.
563 if (task
== self_task
)
566 if (task
< self_task
) {
568 task_lock(self_task
);
571 task_lock(self_task
);
575 if (!task
->active
|| !self
->active
) {
577 * Task or current act is already being terminated.
578 * Just return an error. If we are dying, this will
579 * just get us to our AST special handler and that
580 * will get us to finalize the termination of ourselves.
583 if (self_task
!= task
)
584 task_unlock(self_task
);
586 return (KERN_FAILURE
);
589 if (self_task
!= task
)
590 task_unlock(self_task
);
593 * Make sure the current thread does not get aborted out of
594 * the waits inside these operations.
596 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
599 * Indicate that we want all the threads to stop executing
600 * at user space by holding the task (we would have held
601 * each thread independently in thread_terminate_internal -
602 * but this way we may be more likely to already find it
603 * held there). Mark the task inactive, and prevent
604 * further task operations via the task port.
606 task_hold_locked(task
);
607 task
->active
= FALSE
;
608 ipc_task_disable(task
);
611 * Terminate each thread in the task.
613 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
614 thread_terminate_internal(thread
);
618 * Give the machine dependent code a chance
619 * to perform cleanup before ripping apart
622 if (self_task
== task
)
623 machine_thread_terminate_self();
628 * Destroy all synchronizers owned by the task.
630 task_synchronizer_destroy_all(task
);
633 * Destroy the IPC space, leaving just a reference for it.
635 ipc_space_destroy(task
->itk_space
);
637 /* LP64todo - make this clean */
639 vm_map_remove_commpage64(task
->map
);
640 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
644 * If the current thread is a member of the task
645 * being terminated, then the last reference to
646 * the task will not be dropped until the thread
647 * is finally reaped. To avoid incurring the
648 * expense of removing the address space regions
649 * at reap time, we do it explictly here.
651 vm_map_remove(task
->map
, task
->map
->min_offset
,
652 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
654 shared_region_mapping_dealloc(task
->system_shared_region
);
657 * Flush working set here to avoid I/O in reaper thread
659 if (task
->dynamic_working_set
)
660 tws_hash_ws_flush(task
->dynamic_working_set
);
662 pset
= task
->processor_set
;
664 pset_remove_task(pset
,task
);
668 * We no longer need to guard against being aborted, so restore
669 * the previous interruptible state.
671 thread_interrupt_level(interrupt_save
);
674 perfmon_release_facility(task
); // notify the perfmon facility
678 * Get rid of the task active reference on itself.
680 task_deallocate(task
);
682 return (KERN_SUCCESS
);
688 * Shut the current task down (except for the current thread) in
689 * preparation for dramatic changes to the task (probably exec).
690 * We hold the task, terminate all other threads in the task and
691 * wait for them to terminate, clean up the portspace, and when
692 * all done, let the current thread go.
698 thread_t thread
, self
;
700 assert(task
!= kernel_task
);
702 self
= current_thread();
704 if (task
!= self
->task
)
705 return (KERN_INVALID_ARGUMENT
);
709 if (!task
->active
|| !self
->active
) {
711 * Task or current thread is already being terminated.
712 * Hurry up and return out of the current kernel context
713 * so that we run our AST special handler to terminate
718 return (KERN_FAILURE
);
721 if (task
->thread_count
> 1) {
723 * Mark all the threads to keep them from starting any more
724 * user-level execution. The thread_terminate_internal code
725 * would do this on a thread by thread basis anyway, but this
726 * gives us a better chance of not having to wait there.
728 task_hold_locked(task
);
731 * Terminate all the other threads in the task.
733 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
735 thread_terminate_internal(thread
);
738 task_release_locked(task
);
742 * Give the machine dependent code a chance
743 * to perform cleanup before ripping apart
746 machine_thread_terminate_self();
751 * Destroy all synchronizers owned by the task.
753 task_synchronizer_destroy_all(task
);
756 * Destroy the contents of the IPC space, leaving just
757 * a reference for it.
759 ipc_space_clean(task
->itk_space
);
762 * Clean out the address space, as we are going to be
765 vm_map_remove(task
->map
, task
->map
->min_offset
,
766 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
768 return (KERN_SUCCESS
);
774 * Suspend execution of the specified task.
775 * This is a recursive-style suspension of the task, a count of
776 * suspends is maintained.
778 * CONDITIONS: the task is locked and active.
782 register task_t task
)
784 register thread_t thread
;
786 assert(task
->active
);
788 if (task
->suspend_count
++ > 0)
792 * Iterate through all the threads and hold them.
794 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
795 thread_mtx_lock(thread
);
797 thread_mtx_unlock(thread
);
804 * Same as the internal routine above, except that is must lock
805 * and verify that the task is active. This differs from task_suspend
806 * in that it places a kernel hold on the task rather than just a
807 * user-level hold. This keeps users from over resuming and setting
808 * it running out from under the kernel.
810 * CONDITIONS: the caller holds a reference on the task
814 register task_t task
)
816 if (task
== TASK_NULL
)
817 return (KERN_INVALID_ARGUMENT
);
824 return (KERN_FAILURE
);
827 task_hold_locked(task
);
830 return (KERN_SUCCESS
);
836 * Wait for all threads in task to stop.
839 * Called with task locked, active, and held.
843 register task_t task
)
845 register thread_t thread
, self
;
847 assert(task
->active
);
848 assert(task
->suspend_count
> 0);
850 self
= current_thread();
853 * Iterate through all the threads and wait for them to
854 * stop. Do not wait for the current thread if it is within
857 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
864 * task_release_locked:
866 * Release a kernel hold on a task.
868 * CONDITIONS: the task is locked and active
872 register task_t task
)
874 register thread_t thread
;
876 assert(task
->active
);
877 assert(task
->suspend_count
> 0);
879 if (--task
->suspend_count
> 0)
882 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
883 thread_mtx_lock(thread
);
884 thread_release(thread
);
885 thread_mtx_unlock(thread
);
892 * Same as the internal routine above, except that it must lock
893 * and verify that the task is active.
895 * CONDITIONS: The caller holds a reference to the task
901 if (task
== TASK_NULL
)
902 return (KERN_INVALID_ARGUMENT
);
909 return (KERN_FAILURE
);
912 task_release_locked(task
);
915 return (KERN_SUCCESS
);
921 thread_act_array_t
*threads_out
,
922 mach_msg_type_number_t
*count
)
924 mach_msg_type_number_t actual
;
927 vm_size_t size
, size_needed
;
931 if (task
== TASK_NULL
)
932 return (KERN_INVALID_ARGUMENT
);
944 return (KERN_FAILURE
);
947 actual
= task
->thread_count
;
949 /* do we have the memory we need? */
950 size_needed
= actual
* sizeof (mach_port_t
);
951 if (size_needed
<= size
)
954 /* unlock the task and allocate more memory */
960 assert(size_needed
> 0);
965 return (KERN_RESOURCE_SHORTAGE
);
968 /* OK, have memory and the task is locked & active */
969 threads
= (thread_t
*)addr
;
973 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
974 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
975 thread_reference_internal(thread
);
976 threads
[j
++] = thread
;
979 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
982 size_needed
= actual
* sizeof (mach_port_t
);
984 /* can unlock task now that we've got the thread refs */
988 /* no threads, so return null pointer and deallocate memory */
997 /* if we allocated too much, must copy */
999 if (size_needed
< size
) {
1002 newaddr
= kalloc(size_needed
);
1004 for (i
= 0; i
< actual
; ++i
)
1005 thread_deallocate(threads
[i
]);
1007 return (KERN_RESOURCE_SHORTAGE
);
1010 bcopy(addr
, newaddr
, size_needed
);
1012 threads
= (thread_t
*)newaddr
;
1015 *threads_out
= threads
;
1018 /* do the conversion that Mig should handle */
1020 for (i
= 0; i
< actual
; ++i
)
1021 ((ipc_port_t
*) threads
)[i
] = convert_thread_to_port(threads
[i
]);
1024 return (KERN_SUCCESS
);
1030 * Implement a user-level suspension on a task.
1033 * The caller holds a reference to the task
1037 register task_t task
)
1039 if (task
== TASK_NULL
|| task
== kernel_task
)
1040 return (KERN_INVALID_ARGUMENT
);
1044 if (!task
->active
) {
1047 return (KERN_FAILURE
);
1050 if (task
->user_stop_count
++ > 0) {
1052 * If the stop count was positive, the task is
1053 * already stopped and we can exit.
1057 return (KERN_SUCCESS
);
1061 * Put a kernel-level hold on the threads in the task (all
1062 * user-level task suspensions added together represent a
1063 * single kernel-level hold). We then wait for the threads
1064 * to stop executing user code.
1066 task_hold_locked(task
);
1067 task_wait_locked(task
);
1071 return (KERN_SUCCESS
);
1076 * Release a kernel hold on a task.
1079 * The caller holds a reference to the task
1083 register task_t task
)
1085 register boolean_t release
= FALSE
;
1087 if (task
== TASK_NULL
|| task
== kernel_task
)
1088 return (KERN_INVALID_ARGUMENT
);
1092 if (!task
->active
) {
1095 return (KERN_FAILURE
);
1098 if (task
->user_stop_count
> 0) {
1099 if (--task
->user_stop_count
== 0)
1105 return (KERN_FAILURE
);
1109 * Release the task if necessary.
1112 task_release_locked(task
);
1116 return (KERN_SUCCESS
);
1120 host_security_set_task_token(
1121 host_security_t host_security
,
1123 security_token_t sec_token
,
1124 audit_token_t audit_token
,
1125 host_priv_t host_priv
)
1127 ipc_port_t host_port
;
1130 if (task
== TASK_NULL
)
1131 return(KERN_INVALID_ARGUMENT
);
1133 if (host_security
== HOST_NULL
)
1134 return(KERN_INVALID_SECURITY
);
1137 task
->sec_token
= sec_token
;
1138 task
->audit_token
= audit_token
;
1141 if (host_priv
!= HOST_PRIV_NULL
) {
1142 kr
= host_get_host_priv_port(host_priv
, &host_port
);
1144 kr
= host_get_host_port(host_priv_self(), &host_port
);
1146 assert(kr
== KERN_SUCCESS
);
1147 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
1152 * Utility routine to set a ledger
1160 if (task
== TASK_NULL
)
1161 return(KERN_INVALID_ARGUMENT
);
1165 ipc_port_release_send(task
->wired_ledger_port
);
1166 task
->wired_ledger_port
= ledger_copy(wired
);
1169 ipc_port_release_send(task
->paged_ledger_port
);
1170 task
->paged_ledger_port
= ledger_copy(paged
);
1174 return(KERN_SUCCESS
);
1178 * This routine was added, pretty much exclusively, for registering the
1179 * RPC glue vector for in-kernel short circuited tasks. Rather than
1180 * removing it completely, I have only disabled that feature (which was
1181 * the only feature at the time). It just appears that we are going to
1182 * want to add some user data to tasks in the future (i.e. bsd info,
1183 * task names, etc...), so I left it in the formal task interface.
1188 task_flavor_t flavor
,
1189 __unused task_info_t task_info_in
, /* pointer to IN array */
1190 __unused mach_msg_type_number_t task_info_count
)
1192 if (task
== TASK_NULL
)
1193 return(KERN_INVALID_ARGUMENT
);
1197 return (KERN_INVALID_ARGUMENT
);
1199 return (KERN_SUCCESS
);
1205 task_flavor_t flavor
,
1206 task_info_t task_info_out
,
1207 mach_msg_type_number_t
*task_info_count
)
1209 if (task
== TASK_NULL
)
1210 return (KERN_INVALID_ARGUMENT
);
1214 case TASK_BASIC_INFO_32
:
1216 task_basic_info_32_t basic_info
;
1219 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
)
1220 return (KERN_INVALID_ARGUMENT
);
1222 basic_info
= (task_basic_info_32_t
)task_info_out
;
1224 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1225 basic_info
->virtual_size
= CAST_DOWN(vm_offset_t
,map
->size
);
1226 basic_info
->resident_size
= pmap_resident_count(map
->pmap
)
1230 basic_info
->policy
= ((task
!= kernel_task
)?
1231 POLICY_TIMESHARE
: POLICY_RR
);
1232 basic_info
->suspend_count
= task
->user_stop_count
;
1234 absolutetime_to_microtime(
1235 task
->total_user_time
,
1236 &basic_info
->user_time
.seconds
,
1237 &basic_info
->user_time
.microseconds
);
1238 absolutetime_to_microtime(
1239 task
->total_system_time
,
1240 &basic_info
->system_time
.seconds
,
1241 &basic_info
->system_time
.microseconds
);
1244 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
1248 case TASK_BASIC_INFO_64
:
1250 task_basic_info_64_t basic_info
;
1253 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
)
1254 return (KERN_INVALID_ARGUMENT
);
1256 basic_info
= (task_basic_info_64_t
)task_info_out
;
1258 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1259 basic_info
->virtual_size
= map
->size
;
1260 basic_info
->resident_size
= (mach_vm_size_t
)(pmap_resident_count(map
->pmap
)
1264 basic_info
->policy
= ((task
!= kernel_task
)?
1265 POLICY_TIMESHARE
: POLICY_RR
);
1266 basic_info
->suspend_count
= task
->user_stop_count
;
1268 absolutetime_to_microtime(
1269 task
->total_user_time
,
1270 &basic_info
->user_time
.seconds
,
1271 &basic_info
->user_time
.microseconds
);
1272 absolutetime_to_microtime(
1273 task
->total_system_time
,
1274 &basic_info
->system_time
.seconds
,
1275 &basic_info
->system_time
.microseconds
);
1278 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
1282 case TASK_THREAD_TIMES_INFO
:
1284 register task_thread_times_info_t times_info
;
1285 register thread_t thread
;
1287 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
)
1288 return (KERN_INVALID_ARGUMENT
);
1290 times_info
= (task_thread_times_info_t
) task_info_out
;
1291 times_info
->user_time
.seconds
= 0;
1292 times_info
->user_time
.microseconds
= 0;
1293 times_info
->system_time
.seconds
= 0;
1294 times_info
->system_time
.microseconds
= 0;
1298 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1299 time_value_t user_time
, system_time
;
1301 thread_read_times(thread
, &user_time
, &system_time
);
1303 time_value_add(×_info
->user_time
, &user_time
);
1304 time_value_add(×_info
->system_time
, &system_time
);
1309 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1313 case TASK_ABSOLUTETIME_INFO
:
1315 task_absolutetime_info_t info
;
1316 register thread_t thread
;
1318 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
)
1319 return (KERN_INVALID_ARGUMENT
);
1321 info
= (task_absolutetime_info_t
)task_info_out
;
1322 info
->threads_user
= info
->threads_system
= 0;
1326 info
->total_user
= task
->total_user_time
;
1327 info
->total_system
= task
->total_system_time
;
1329 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1332 tval
= timer_grab(&thread
->user_timer
);
1333 info
->threads_user
+= tval
;
1334 info
->total_user
+= tval
;
1336 tval
= timer_grab(&thread
->system_timer
);
1337 info
->threads_system
+= tval
;
1338 info
->total_system
+= tval
;
1343 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
1348 case TASK_SCHED_FIFO_INFO
:
1351 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
)
1352 return (KERN_INVALID_ARGUMENT
);
1354 return (KERN_INVALID_POLICY
);
1358 case TASK_SCHED_RR_INFO
:
1360 register policy_rr_base_t rr_base
;
1362 if (*task_info_count
< POLICY_RR_BASE_COUNT
)
1363 return (KERN_INVALID_ARGUMENT
);
1365 rr_base
= (policy_rr_base_t
) task_info_out
;
1368 if (task
!= kernel_task
) {
1370 return (KERN_INVALID_POLICY
);
1373 rr_base
->base_priority
= task
->priority
;
1376 rr_base
->quantum
= std_quantum_us
/ 1000;
1378 *task_info_count
= POLICY_RR_BASE_COUNT
;
1383 case TASK_SCHED_TIMESHARE_INFO
:
1385 register policy_timeshare_base_t ts_base
;
1387 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
)
1388 return (KERN_INVALID_ARGUMENT
);
1390 ts_base
= (policy_timeshare_base_t
) task_info_out
;
1393 if (task
== kernel_task
) {
1395 return (KERN_INVALID_POLICY
);
1398 ts_base
->base_priority
= task
->priority
;
1401 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
1405 case TASK_SECURITY_TOKEN
:
1407 register security_token_t
*sec_token_p
;
1409 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
)
1410 return (KERN_INVALID_ARGUMENT
);
1412 sec_token_p
= (security_token_t
*) task_info_out
;
1415 *sec_token_p
= task
->sec_token
;
1418 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
1422 case TASK_AUDIT_TOKEN
:
1424 register audit_token_t
*audit_token_p
;
1426 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
)
1427 return (KERN_INVALID_ARGUMENT
);
1429 audit_token_p
= (audit_token_t
*) task_info_out
;
1432 *audit_token_p
= task
->audit_token
;
1435 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
1439 case TASK_SCHED_INFO
:
1440 return (KERN_INVALID_ARGUMENT
);
1442 case TASK_EVENTS_INFO
:
1444 register task_events_info_t events_info
;
1446 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
)
1447 return (KERN_INVALID_ARGUMENT
);
1449 events_info
= (task_events_info_t
) task_info_out
;
1452 events_info
->faults
= task
->faults
;
1453 events_info
->pageins
= task
->pageins
;
1454 events_info
->cow_faults
= task
->cow_faults
;
1455 events_info
->messages_sent
= task
->messages_sent
;
1456 events_info
->messages_received
= task
->messages_received
;
1457 events_info
->syscalls_mach
= task
->syscalls_mach
;
1458 events_info
->syscalls_unix
= task
->syscalls_unix
;
1459 events_info
->csw
= task
->csw
;
1462 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
1467 return (KERN_INVALID_ARGUMENT
);
1470 return (KERN_SUCCESS
);
1476 * Change the assigned processor set for the task
1480 __unused task_t task
,
1481 __unused processor_set_t new_pset
,
1482 __unused boolean_t assign_threads
)
1484 return(KERN_FAILURE
);
1488 * task_assign_default:
1490 * Version of task_assign to assign to default processor set.
1493 task_assign_default(
1495 boolean_t assign_threads
)
1497 return (task_assign(task
, &default_pset
, assign_threads
));
1501 * task_get_assignment
1503 * Return name of processor set that task is assigned to.
1506 task_get_assignment(
1508 processor_set_t
*pset
)
1511 return(KERN_FAILURE
);
1513 *pset
= task
->processor_set
;
1514 pset_reference(*pset
);
1515 return(KERN_SUCCESS
);
1522 * Set scheduling policy and parameters, both base and limit, for
1523 * the given task. Policy must be a policy which is enabled for the
1524 * processor set. Change contained threads if requested.
1528 __unused task_t task
,
1529 __unused policy_t policy_id
,
1530 __unused policy_base_t base
,
1531 __unused mach_msg_type_number_t count
,
1532 __unused boolean_t set_limit
,
1533 __unused boolean_t change
)
1535 return(KERN_FAILURE
);
1541 * Set scheduling policy and parameters, both base and limit, for
1542 * the given task. Policy can be any policy implemented by the
1543 * processor set, whether enabled or not. Change contained threads
1548 __unused task_t task
,
1549 __unused processor_set_t pset
,
1550 __unused policy_t policy_id
,
1551 __unused policy_base_t base
,
1552 __unused mach_msg_type_number_t base_count
,
1553 __unused policy_limit_t limit
,
1554 __unused mach_msg_type_number_t limit_count
,
1555 __unused boolean_t change
)
1557 return(KERN_FAILURE
);
1567 extern int fast_tas_debug
;
1569 if (fast_tas_debug
) {
1570 printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
1574 task
->fast_tas_base
= pc
;
1575 task
->fast_tas_end
= endpc
;
1577 return KERN_SUCCESS
;
1579 #else /* FAST_TAS */
1582 __unused task_t task
,
1583 __unused vm_offset_t pc
,
1584 __unused vm_offset_t endpc
)
1586 return KERN_FAILURE
;
1588 #endif /* FAST_TAS */
1591 task_synchronizer_destroy_all(task_t task
)
1593 semaphore_t semaphore
;
1594 lock_set_t lock_set
;
1597 * Destroy owned semaphores
1600 while (!queue_empty(&task
->semaphore_list
)) {
1601 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
1602 (void) semaphore_destroy(task
, semaphore
);
1606 * Destroy owned lock sets
1609 while (!queue_empty(&task
->lock_set_list
)) {
1610 lock_set
= (lock_set_t
) queue_first(&task
->lock_set_list
);
1611 (void) lock_set_destroy(task
, lock_set
);
1616 * We need to export some functions to other components that
1617 * are currently implemented in macros within the osfmk
1618 * component. Just export them as functions of the same name.
1620 boolean_t
is_kerneltask(task_t t
)
1622 if (t
== kernel_task
)
1629 task_t
current_task(void);
1630 task_t
current_task(void)
1632 return (current_task_fast());
1635 #undef task_reference
1636 void task_reference(task_t task
);
1641 if (task
!= TASK_NULL
)
1642 task_reference_internal(task
);