2 * Copyright (c) 2000-2005 Apple Computer, 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.
83 #include <mach_host.h>
84 #include <mach_prof.h>
86 #include <platforms.h>
88 #include <mach/mach_types.h>
89 #include <mach/boolean.h>
90 #include <mach/host_priv.h>
91 #include <mach/machine/vm_types.h>
92 #include <mach/vm_param.h>
93 #include <mach/semaphore.h>
94 #include <mach/task_info.h>
95 #include <mach/task_special_ports.h>
97 #include <ipc/ipc_types.h>
98 #include <ipc/ipc_space.h>
99 #include <ipc/ipc_entry.h>
101 #include <kern/kern_types.h>
102 #include <kern/mach_param.h>
103 #include <kern/misc_protos.h>
104 #include <kern/task.h>
105 #include <kern/thread.h>
106 #include <kern/zalloc.h>
107 #include <kern/kalloc.h>
108 #include <kern/processor.h>
109 #include <kern/sched_prim.h> /* for thread_wakeup */
110 #include <kern/ipc_tt.h>
111 #include <kern/ledger.h>
112 #include <kern/host.h>
113 #include <kern/clock.h>
114 #include <kern/timer.h>
115 #include <kern/profile.h>
116 #include <kern/assert.h>
117 #include <kern/sync_lock.h>
120 #include <vm/vm_map.h>
121 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
122 #include <vm/vm_pageout.h>
123 #include <vm/vm_protos.h> /* for vm_map_remove_commpage64 */
126 #include <ddb/db_sym.h>
127 #endif /* MACH_KDB */
130 #include <ppc/exception.h>
131 #include <ppc/hw_perfmon.h>
135 * Exported interfaces
138 #include <mach/task_server.h>
139 #include <mach/mach_host_server.h>
140 #include <mach/host_security_server.h>
141 #include <mach/mach_port_server.h>
143 #include <vm/task_working_set.h>
144 #include <vm/vm_shared_memory_server.h>
151 void task_hold_locked(
153 void task_wait_locked(
155 void task_release_locked(
159 void task_synchronizer_destroy_all(
162 kern_return_t
task_set_ledger(
168 task_backing_store_privileged(
172 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
178 task_working_set_disable(task_t task
)
183 ws
= task
->dynamic_working_set
;
184 task
->dynamic_working_set
= NULL
;
187 tws_hash_ws_flush(ws
);
188 tws_hash_destroy(ws
);
198 /* LP64todo - no task working set for 64-bit */
199 task_set_64BitAddr(task
);
200 task_working_set_disable(task
);
201 task
->map
->max_offset
= MACH_VM_MAX_ADDRESS
;
204 * Deallocate all memory previously allocated
205 * above the 32-bit address space, since it won't
206 * be accessible anymore.
208 /* LP64todo - make this clean */
210 vm_map_remove_commpage64(task
->map
);
211 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
213 (void) vm_map_remove(task
->map
,
214 (vm_map_offset_t
) VM_MAX_ADDRESS
,
217 task_clear_64BitAddr(task
);
218 task
->map
->max_offset
= (vm_map_offset_t
)VM_MAX_ADDRESS
;
227 TASK_MAX
* sizeof(struct task
),
228 TASK_CHUNK
* sizeof(struct task
),
232 * Create the kernel task as the first task.
234 if (task_create_internal(TASK_NULL
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
235 panic("task_init\n");
237 vm_map_deallocate(kernel_task
->map
);
238 kernel_task
->map
= kernel_map
;
250 * If may_assign is false, task is already being assigned,
251 * wait for that to finish.
253 while (task
->may_assign
== FALSE
) {
256 task
->assign_active
= TRUE
;
257 res
= thread_sleep_mutex((event_t
) &task
->assign_active
,
258 &task
->lock
, THREAD_UNINT
);
259 assert(res
== THREAD_AWAKENED
);
261 task
->may_assign
= FALSE
;
266 #define thread_freeze(thread) assert(task->processor_set == &default_pset)
275 assert(task
->may_assign
== FALSE
);
276 task
->may_assign
= TRUE
;
277 if (task
->assign_active
== TRUE
) {
278 task
->assign_active
= FALSE
;
279 thread_wakeup((event_t
)&task
->assign_active
);
285 #define thread_unfreeze(thread) assert(task->processor_set == &default_pset)
288 #endif /* MACH_HOST */
291 * Create a task running in the kernel address space. It may
292 * have its own map of size mem_size and may have ipc privileges.
296 __unused task_t parent_task
,
297 __unused vm_offset_t map_base
,
298 __unused vm_size_t map_size
,
299 __unused task_t
*child_task
)
301 return (KERN_INVALID_ARGUMENT
);
307 __unused ledger_port_array_t ledger_ports
,
308 __unused mach_msg_type_number_t num_ledger_ports
,
309 boolean_t inherit_memory
,
310 task_t
*child_task
) /* OUT */
312 if (parent_task
== TASK_NULL
)
313 return(KERN_INVALID_ARGUMENT
);
315 return task_create_internal(
316 parent_task
, inherit_memory
, child_task
);
320 host_security_create_task_token(
321 host_security_t host_security
,
323 security_token_t sec_token
,
324 audit_token_t audit_token
,
325 host_priv_t host_priv
,
326 __unused ledger_port_array_t ledger_ports
,
327 __unused mach_msg_type_number_t num_ledger_ports
,
328 boolean_t inherit_memory
,
329 task_t
*child_task
) /* OUT */
331 kern_return_t result
;
333 if (parent_task
== TASK_NULL
)
334 return(KERN_INVALID_ARGUMENT
);
336 if (host_security
== HOST_NULL
)
337 return(KERN_INVALID_SECURITY
);
339 result
= task_create_internal(
340 parent_task
, inherit_memory
, child_task
);
342 if (result
!= KERN_SUCCESS
)
345 result
= host_security_set_task_token(host_security
,
351 if (result
!= KERN_SUCCESS
)
358 task_create_internal(
360 boolean_t inherit_memory
,
361 task_t
*child_task
) /* OUT */
364 processor_set_t pset
;
366 new_task
= (task_t
) zalloc(task_zone
);
368 if (new_task
== TASK_NULL
)
369 return(KERN_RESOURCE_SHORTAGE
);
371 /* one ref for just being alive; one for our caller */
372 new_task
->ref_count
= 2;
375 new_task
->map
= vm_map_fork(parent_task
->map
);
377 new_task
->map
= vm_map_create(pmap_create(0),
378 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
379 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
381 mutex_init(&new_task
->lock
, 0);
382 queue_init(&new_task
->threads
);
383 new_task
->suspend_count
= 0;
384 new_task
->thread_count
= 0;
385 new_task
->active_thread_count
= 0;
386 new_task
->user_stop_count
= 0;
387 new_task
->role
= TASK_UNSPECIFIED
;
388 new_task
->active
= TRUE
;
389 new_task
->user_data
= 0;
390 new_task
->faults
= 0;
391 new_task
->cow_faults
= 0;
392 new_task
->pageins
= 0;
393 new_task
->messages_sent
= 0;
394 new_task
->messages_received
= 0;
395 new_task
->syscalls_mach
= 0;
396 new_task
->priv_flags
= 0;
397 new_task
->syscalls_unix
=0;
399 new_task
->taskFeatures
[0] = 0; /* Init task features */
400 new_task
->taskFeatures
[1] = 0; /* Init task features */
401 new_task
->dynamic_working_set
= 0;
403 task_working_set_create(new_task
, TWS_SMALL_HASH_LINE_COUNT
,
404 0, TWS_HASH_STYLE_DEFAULT
);
407 new_task
->bsd_info
= 0;
408 #endif /* MACH_BSD */
411 if(BootProcInfo
.pf
.Available
& pf64Bit
) new_task
->taskFeatures
[0] |= tf64BitData
; /* If 64-bit machine, show we have 64-bit registers at least */
414 queue_init(&new_task
->semaphore_list
);
415 queue_init(&new_task
->lock_set_list
);
416 new_task
->semaphores_owned
= 0;
417 new_task
->lock_sets_owned
= 0;
420 new_task
->may_assign
= TRUE
;
421 new_task
->assign_active
= FALSE
;
422 #endif /* MACH_HOST */
424 ipc_task_init(new_task
, parent_task
);
426 new_task
->total_user_time
= 0;
427 new_task
->total_system_time
= 0;
429 task_prof_init(new_task
);
431 if (parent_task
!= TASK_NULL
) {
434 * Freeze the parent, so that parent_task->processor_set
437 task_freeze(parent_task
);
438 #endif /* MACH_HOST */
439 pset
= parent_task
->processor_set
;
441 pset
= &default_pset
;
443 new_task
->sec_token
= parent_task
->sec_token
;
444 new_task
->audit_token
= parent_task
->audit_token
;
446 shared_region_mapping_ref(parent_task
->system_shared_region
);
447 new_task
->system_shared_region
= parent_task
->system_shared_region
;
449 new_task
->wired_ledger_port
= ledger_copy(
450 convert_port_to_ledger(parent_task
->wired_ledger_port
));
451 new_task
->paged_ledger_port
= ledger_copy(
452 convert_port_to_ledger(parent_task
->paged_ledger_port
));
453 if(task_has_64BitAddr(parent_task
))
454 task_set_64BitAddr(new_task
);
457 pset
= &default_pset
;
459 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
460 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
461 new_task
->wired_ledger_port
= ledger_copy(root_wired_ledger
);
462 new_task
->paged_ledger_port
= ledger_copy(root_paged_ledger
);
465 if (kernel_task
== TASK_NULL
) {
466 new_task
->priority
= BASEPRI_KERNEL
;
467 new_task
->max_priority
= MAXPRI_KERNEL
;
470 new_task
->priority
= BASEPRI_DEFAULT
;
471 new_task
->max_priority
= MAXPRI_USER
;
475 pset_add_task(pset
, new_task
);
478 if (parent_task
!= TASK_NULL
)
479 task_unfreeze(parent_task
);
480 #endif /* MACH_HOST */
482 if (vm_backing_store_low
&& parent_task
!= NULL
)
483 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
485 ipc_task_enable(new_task
);
487 *child_task
= new_task
;
488 return(KERN_SUCCESS
);
494 * Drop a reference on a task.
500 processor_set_t pset
;
502 if (task
== TASK_NULL
)
505 if (task_deallocate_internal(task
) > 0)
508 pset
= task
->processor_set
;
509 pset_deallocate(pset
);
511 if(task
->dynamic_working_set
)
512 tws_hash_destroy(task
->dynamic_working_set
);
514 ipc_task_terminate(task
);
516 vm_map_deallocate(task
->map
);
517 is_release(task
->itk_space
);
519 task_prof_deallocate(task
);
520 zfree(task_zone
, task
);
526 * Terminate the specified task. See comments on thread_terminate
527 * (kern/thread.c) about problems with terminating the "current task."
534 if (task
== TASK_NULL
)
535 return (KERN_INVALID_ARGUMENT
);
538 return (KERN_FAILURE
);
540 return (task_terminate_internal(task
));
544 task_terminate_internal(
547 processor_set_t pset
;
548 thread_t thread
, self
;
550 boolean_t interrupt_save
;
552 assert(task
!= kernel_task
);
554 self
= current_thread();
555 self_task
= self
->task
;
558 * Get the task locked and make sure that we are not racing
559 * with someone else trying to terminate us.
561 if (task
== self_task
)
564 if (task
< self_task
) {
566 task_lock(self_task
);
569 task_lock(self_task
);
573 if (!task
->active
|| !self
->active
) {
575 * Task or current act is already being terminated.
576 * Just return an error. If we are dying, this will
577 * just get us to our AST special handler and that
578 * will get us to finalize the termination of ourselves.
581 if (self_task
!= task
)
582 task_unlock(self_task
);
584 return (KERN_FAILURE
);
587 if (self_task
!= task
)
588 task_unlock(self_task
);
591 * Make sure the current thread does not get aborted out of
592 * the waits inside these operations.
594 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
597 * Indicate that we want all the threads to stop executing
598 * at user space by holding the task (we would have held
599 * each thread independently in thread_terminate_internal -
600 * but this way we may be more likely to already find it
601 * held there). Mark the task inactive, and prevent
602 * further task operations via the task port.
604 task_hold_locked(task
);
605 task
->active
= FALSE
;
606 ipc_task_disable(task
);
609 * Terminate each thread in the task.
611 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
612 thread_terminate_internal(thread
);
616 * Give the machine dependent code a chance
617 * to perform cleanup before ripping apart
620 if (self_task
== task
)
621 machine_thread_terminate_self();
626 * Destroy all synchronizers owned by the task.
628 task_synchronizer_destroy_all(task
);
631 * Destroy the IPC space, leaving just a reference for it.
633 ipc_space_destroy(task
->itk_space
);
635 /* LP64todo - make this clean */
637 vm_map_remove_commpage64(task
->map
);
638 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
642 * If the current thread is a member of the task
643 * being terminated, then the last reference to
644 * the task will not be dropped until the thread
645 * is finally reaped. To avoid incurring the
646 * expense of removing the address space regions
647 * at reap time, we do it explictly here.
649 vm_map_remove(task
->map
, task
->map
->min_offset
,
650 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
652 shared_region_mapping_dealloc(task
->system_shared_region
);
655 * Flush working set here to avoid I/O in reaper thread
657 if (task
->dynamic_working_set
)
658 tws_hash_ws_flush(task
->dynamic_working_set
);
660 pset
= task
->processor_set
;
662 pset_remove_task(pset
,task
);
666 * We no longer need to guard against being aborted, so restore
667 * the previous interruptible state.
669 thread_interrupt_level(interrupt_save
);
672 perfmon_release_facility(task
); // notify the perfmon facility
676 * Get rid of the task active reference on itself.
678 task_deallocate(task
);
680 return (KERN_SUCCESS
);
686 * Shut the current task down (except for the current thread) in
687 * preparation for dramatic changes to the task (probably exec).
688 * We hold the task, terminate all other threads in the task and
689 * wait for them to terminate, clean up the portspace, and when
690 * all done, let the current thread go.
696 thread_t thread
, self
;
698 assert(task
!= kernel_task
);
700 self
= current_thread();
702 if (task
!= self
->task
)
703 return (KERN_INVALID_ARGUMENT
);
707 if (!task
->active
|| !self
->active
) {
709 * Task or current thread is already being terminated.
710 * Hurry up and return out of the current kernel context
711 * so that we run our AST special handler to terminate
716 return (KERN_FAILURE
);
719 if (task
->thread_count
> 1) {
721 * Mark all the threads to keep them from starting any more
722 * user-level execution. The thread_terminate_internal code
723 * would do this on a thread by thread basis anyway, but this
724 * gives us a better chance of not having to wait there.
726 task_hold_locked(task
);
729 * Terminate all the other threads in the task.
731 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
733 thread_terminate_internal(thread
);
736 task_release_locked(task
);
740 * Give the machine dependent code a chance
741 * to perform cleanup before ripping apart
744 machine_thread_terminate_self();
749 * Destroy all synchronizers owned by the task.
751 task_synchronizer_destroy_all(task
);
754 * Destroy the contents of the IPC space, leaving just
755 * a reference for it.
757 ipc_space_clean(task
->itk_space
);
760 * Clean out the address space, as we are going to be
763 vm_map_remove(task
->map
, task
->map
->min_offset
,
764 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
766 return (KERN_SUCCESS
);
772 * Suspend execution of the specified task.
773 * This is a recursive-style suspension of the task, a count of
774 * suspends is maintained.
776 * CONDITIONS: the task is locked and active.
780 register task_t task
)
782 register thread_t thread
;
784 assert(task
->active
);
786 if (task
->suspend_count
++ > 0)
790 * Iterate through all the threads and hold them.
792 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
793 thread_mtx_lock(thread
);
795 thread_mtx_unlock(thread
);
802 * Same as the internal routine above, except that is must lock
803 * and verify that the task is active. This differs from task_suspend
804 * in that it places a kernel hold on the task rather than just a
805 * user-level hold. This keeps users from over resuming and setting
806 * it running out from under the kernel.
808 * CONDITIONS: the caller holds a reference on the task
812 register task_t task
)
814 if (task
== TASK_NULL
)
815 return (KERN_INVALID_ARGUMENT
);
822 return (KERN_FAILURE
);
825 task_hold_locked(task
);
828 return (KERN_SUCCESS
);
834 * Wait for all threads in task to stop.
837 * Called with task locked, active, and held.
841 register task_t task
)
843 register thread_t thread
, self
;
845 assert(task
->active
);
846 assert(task
->suspend_count
> 0);
848 self
= current_thread();
851 * Iterate through all the threads and wait for them to
852 * stop. Do not wait for the current thread if it is within
855 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
862 * task_release_locked:
864 * Release a kernel hold on a task.
866 * CONDITIONS: the task is locked and active
870 register task_t task
)
872 register thread_t thread
;
874 assert(task
->active
);
875 assert(task
->suspend_count
> 0);
877 if (--task
->suspend_count
> 0)
880 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
881 thread_mtx_lock(thread
);
882 thread_release(thread
);
883 thread_mtx_unlock(thread
);
890 * Same as the internal routine above, except that it must lock
891 * and verify that the task is active.
893 * CONDITIONS: The caller holds a reference to the task
899 if (task
== TASK_NULL
)
900 return (KERN_INVALID_ARGUMENT
);
907 return (KERN_FAILURE
);
910 task_release_locked(task
);
913 return (KERN_SUCCESS
);
919 thread_act_array_t
*threads_out
,
920 mach_msg_type_number_t
*count
)
922 mach_msg_type_number_t actual
;
925 vm_size_t size
, size_needed
;
929 if (task
== TASK_NULL
)
930 return (KERN_INVALID_ARGUMENT
);
942 return (KERN_FAILURE
);
945 actual
= task
->thread_count
;
947 /* do we have the memory we need? */
948 size_needed
= actual
* sizeof (mach_port_t
);
949 if (size_needed
<= size
)
952 /* unlock the task and allocate more memory */
958 assert(size_needed
> 0);
963 return (KERN_RESOURCE_SHORTAGE
);
966 /* OK, have memory and the task is locked & active */
967 threads
= (thread_t
*)addr
;
971 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
972 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
973 thread_reference_internal(thread
);
974 threads
[j
++] = thread
;
977 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
980 size_needed
= actual
* sizeof (mach_port_t
);
982 /* can unlock task now that we've got the thread refs */
986 /* no threads, so return null pointer and deallocate memory */
995 /* if we allocated too much, must copy */
997 if (size_needed
< size
) {
1000 newaddr
= kalloc(size_needed
);
1002 for (i
= 0; i
< actual
; ++i
)
1003 thread_deallocate(threads
[i
]);
1005 return (KERN_RESOURCE_SHORTAGE
);
1008 bcopy(addr
, newaddr
, size_needed
);
1010 threads
= (thread_t
*)newaddr
;
1013 *threads_out
= threads
;
1016 /* do the conversion that Mig should handle */
1018 for (i
= 0; i
< actual
; ++i
)
1019 ((ipc_port_t
*) threads
)[i
] = convert_thread_to_port(threads
[i
]);
1022 return (KERN_SUCCESS
);
1028 * Implement a user-level suspension on a task.
1031 * The caller holds a reference to the task
1035 register task_t task
)
1037 if (task
== TASK_NULL
|| task
== kernel_task
)
1038 return (KERN_INVALID_ARGUMENT
);
1042 if (!task
->active
) {
1045 return (KERN_FAILURE
);
1048 if (task
->user_stop_count
++ > 0) {
1050 * If the stop count was positive, the task is
1051 * already stopped and we can exit.
1055 return (KERN_SUCCESS
);
1059 * Put a kernel-level hold on the threads in the task (all
1060 * user-level task suspensions added together represent a
1061 * single kernel-level hold). We then wait for the threads
1062 * to stop executing user code.
1064 task_hold_locked(task
);
1065 task_wait_locked(task
);
1069 return (KERN_SUCCESS
);
1074 * Release a kernel hold on a task.
1077 * The caller holds a reference to the task
1081 register task_t task
)
1083 register boolean_t release
= FALSE
;
1085 if (task
== TASK_NULL
|| task
== kernel_task
)
1086 return (KERN_INVALID_ARGUMENT
);
1090 if (!task
->active
) {
1093 return (KERN_FAILURE
);
1096 if (task
->user_stop_count
> 0) {
1097 if (--task
->user_stop_count
== 0)
1103 return (KERN_FAILURE
);
1107 * Release the task if necessary.
1110 task_release_locked(task
);
1114 return (KERN_SUCCESS
);
1118 host_security_set_task_token(
1119 host_security_t host_security
,
1121 security_token_t sec_token
,
1122 audit_token_t audit_token
,
1123 host_priv_t host_priv
)
1125 ipc_port_t host_port
;
1128 if (task
== TASK_NULL
)
1129 return(KERN_INVALID_ARGUMENT
);
1131 if (host_security
== HOST_NULL
)
1132 return(KERN_INVALID_SECURITY
);
1135 task
->sec_token
= sec_token
;
1136 task
->audit_token
= audit_token
;
1139 if (host_priv
!= HOST_PRIV_NULL
) {
1140 kr
= host_get_host_priv_port(host_priv
, &host_port
);
1142 kr
= host_get_host_port(host_priv_self(), &host_port
);
1144 assert(kr
== KERN_SUCCESS
);
1145 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
1150 * Utility routine to set a ledger
1158 if (task
== TASK_NULL
)
1159 return(KERN_INVALID_ARGUMENT
);
1163 ipc_port_release_send(task
->wired_ledger_port
);
1164 task
->wired_ledger_port
= ledger_copy(wired
);
1167 ipc_port_release_send(task
->paged_ledger_port
);
1168 task
->paged_ledger_port
= ledger_copy(paged
);
1172 return(KERN_SUCCESS
);
1176 * This routine was added, pretty much exclusively, for registering the
1177 * RPC glue vector for in-kernel short circuited tasks. Rather than
1178 * removing it completely, I have only disabled that feature (which was
1179 * the only feature at the time). It just appears that we are going to
1180 * want to add some user data to tasks in the future (i.e. bsd info,
1181 * task names, etc...), so I left it in the formal task interface.
1186 task_flavor_t flavor
,
1187 __unused task_info_t task_info_in
, /* pointer to IN array */
1188 __unused mach_msg_type_number_t task_info_count
)
1190 if (task
== TASK_NULL
)
1191 return(KERN_INVALID_ARGUMENT
);
1195 return (KERN_INVALID_ARGUMENT
);
1197 return (KERN_SUCCESS
);
1203 task_flavor_t flavor
,
1204 task_info_t task_info_out
,
1205 mach_msg_type_number_t
*task_info_count
)
1207 if (task
== TASK_NULL
)
1208 return (KERN_INVALID_ARGUMENT
);
1212 case TASK_BASIC_INFO_32
:
1214 task_basic_info_32_t basic_info
;
1217 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
)
1218 return (KERN_INVALID_ARGUMENT
);
1220 basic_info
= (task_basic_info_32_t
)task_info_out
;
1222 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1223 basic_info
->virtual_size
= CAST_DOWN(vm_offset_t
,map
->size
);
1224 basic_info
->resident_size
= pmap_resident_count(map
->pmap
)
1228 basic_info
->policy
= ((task
!= kernel_task
)?
1229 POLICY_TIMESHARE
: POLICY_RR
);
1230 basic_info
->suspend_count
= task
->user_stop_count
;
1232 absolutetime_to_microtime(
1233 task
->total_user_time
,
1234 &basic_info
->user_time
.seconds
,
1235 &basic_info
->user_time
.microseconds
);
1236 absolutetime_to_microtime(
1237 task
->total_system_time
,
1238 &basic_info
->system_time
.seconds
,
1239 &basic_info
->system_time
.microseconds
);
1242 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
1246 case TASK_BASIC_INFO_64
:
1248 task_basic_info_64_t basic_info
;
1251 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
)
1252 return (KERN_INVALID_ARGUMENT
);
1254 basic_info
= (task_basic_info_64_t
)task_info_out
;
1256 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1257 basic_info
->virtual_size
= map
->size
;
1258 basic_info
->resident_size
= (mach_vm_size_t
)(pmap_resident_count(map
->pmap
)
1262 basic_info
->policy
= ((task
!= kernel_task
)?
1263 POLICY_TIMESHARE
: POLICY_RR
);
1264 basic_info
->suspend_count
= task
->user_stop_count
;
1266 absolutetime_to_microtime(
1267 task
->total_user_time
,
1268 &basic_info
->user_time
.seconds
,
1269 &basic_info
->user_time
.microseconds
);
1270 absolutetime_to_microtime(
1271 task
->total_system_time
,
1272 &basic_info
->system_time
.seconds
,
1273 &basic_info
->system_time
.microseconds
);
1276 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
1280 case TASK_THREAD_TIMES_INFO
:
1282 register task_thread_times_info_t times_info
;
1283 register thread_t thread
;
1285 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
)
1286 return (KERN_INVALID_ARGUMENT
);
1288 times_info
= (task_thread_times_info_t
) task_info_out
;
1289 times_info
->user_time
.seconds
= 0;
1290 times_info
->user_time
.microseconds
= 0;
1291 times_info
->system_time
.seconds
= 0;
1292 times_info
->system_time
.microseconds
= 0;
1296 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1297 time_value_t user_time
, system_time
;
1299 thread_read_times(thread
, &user_time
, &system_time
);
1301 time_value_add(×_info
->user_time
, &user_time
);
1302 time_value_add(×_info
->system_time
, &system_time
);
1307 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1311 case TASK_ABSOLUTETIME_INFO
:
1313 task_absolutetime_info_t info
;
1314 register thread_t thread
;
1316 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
)
1317 return (KERN_INVALID_ARGUMENT
);
1319 info
= (task_absolutetime_info_t
)task_info_out
;
1320 info
->threads_user
= info
->threads_system
= 0;
1324 info
->total_user
= task
->total_user_time
;
1325 info
->total_system
= task
->total_system_time
;
1327 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1330 tval
= timer_grab(&thread
->user_timer
);
1331 info
->threads_user
+= tval
;
1332 info
->total_user
+= tval
;
1334 tval
= timer_grab(&thread
->system_timer
);
1335 info
->threads_system
+= tval
;
1336 info
->total_system
+= tval
;
1341 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
1346 case TASK_SCHED_FIFO_INFO
:
1349 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
)
1350 return (KERN_INVALID_ARGUMENT
);
1352 return (KERN_INVALID_POLICY
);
1356 case TASK_SCHED_RR_INFO
:
1358 register policy_rr_base_t rr_base
;
1360 if (*task_info_count
< POLICY_RR_BASE_COUNT
)
1361 return (KERN_INVALID_ARGUMENT
);
1363 rr_base
= (policy_rr_base_t
) task_info_out
;
1366 if (task
!= kernel_task
) {
1368 return (KERN_INVALID_POLICY
);
1371 rr_base
->base_priority
= task
->priority
;
1374 rr_base
->quantum
= std_quantum_us
/ 1000;
1376 *task_info_count
= POLICY_RR_BASE_COUNT
;
1381 case TASK_SCHED_TIMESHARE_INFO
:
1383 register policy_timeshare_base_t ts_base
;
1385 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
)
1386 return (KERN_INVALID_ARGUMENT
);
1388 ts_base
= (policy_timeshare_base_t
) task_info_out
;
1391 if (task
== kernel_task
) {
1393 return (KERN_INVALID_POLICY
);
1396 ts_base
->base_priority
= task
->priority
;
1399 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
1403 case TASK_SECURITY_TOKEN
:
1405 register security_token_t
*sec_token_p
;
1407 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
)
1408 return (KERN_INVALID_ARGUMENT
);
1410 sec_token_p
= (security_token_t
*) task_info_out
;
1413 *sec_token_p
= task
->sec_token
;
1416 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
1420 case TASK_AUDIT_TOKEN
:
1422 register audit_token_t
*audit_token_p
;
1424 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
)
1425 return (KERN_INVALID_ARGUMENT
);
1427 audit_token_p
= (audit_token_t
*) task_info_out
;
1430 *audit_token_p
= task
->audit_token
;
1433 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
1437 case TASK_SCHED_INFO
:
1438 return (KERN_INVALID_ARGUMENT
);
1440 case TASK_EVENTS_INFO
:
1442 register task_events_info_t events_info
;
1444 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
)
1445 return (KERN_INVALID_ARGUMENT
);
1447 events_info
= (task_events_info_t
) task_info_out
;
1450 events_info
->faults
= task
->faults
;
1451 events_info
->pageins
= task
->pageins
;
1452 events_info
->cow_faults
= task
->cow_faults
;
1453 events_info
->messages_sent
= task
->messages_sent
;
1454 events_info
->messages_received
= task
->messages_received
;
1455 events_info
->syscalls_mach
= task
->syscalls_mach
;
1456 events_info
->syscalls_unix
= task
->syscalls_unix
;
1457 events_info
->csw
= task
->csw
;
1460 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
1465 return (KERN_INVALID_ARGUMENT
);
1468 return (KERN_SUCCESS
);
1474 * Change the assigned processor set for the task
1478 __unused task_t task
,
1479 __unused processor_set_t new_pset
,
1480 __unused boolean_t assign_threads
)
1482 return(KERN_FAILURE
);
1486 * task_assign_default:
1488 * Version of task_assign to assign to default processor set.
1491 task_assign_default(
1493 boolean_t assign_threads
)
1495 return (task_assign(task
, &default_pset
, assign_threads
));
1499 * task_get_assignment
1501 * Return name of processor set that task is assigned to.
1504 task_get_assignment(
1506 processor_set_t
*pset
)
1509 return(KERN_FAILURE
);
1511 *pset
= task
->processor_set
;
1512 pset_reference(*pset
);
1513 return(KERN_SUCCESS
);
1520 * Set scheduling policy and parameters, both base and limit, for
1521 * the given task. Policy must be a policy which is enabled for the
1522 * processor set. Change contained threads if requested.
1526 __unused task_t task
,
1527 __unused policy_t policy_id
,
1528 __unused policy_base_t base
,
1529 __unused mach_msg_type_number_t count
,
1530 __unused boolean_t set_limit
,
1531 __unused boolean_t change
)
1533 return(KERN_FAILURE
);
1539 * Set scheduling policy and parameters, both base and limit, for
1540 * the given task. Policy can be any policy implemented by the
1541 * processor set, whether enabled or not. Change contained threads
1546 __unused task_t task
,
1547 __unused processor_set_t pset
,
1548 __unused policy_t policy_id
,
1549 __unused policy_base_t base
,
1550 __unused mach_msg_type_number_t base_count
,
1551 __unused policy_limit_t limit
,
1552 __unused mach_msg_type_number_t limit_count
,
1553 __unused boolean_t change
)
1555 return(KERN_FAILURE
);
1565 extern int fast_tas_debug
;
1567 if (fast_tas_debug
) {
1568 printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
1572 task
->fast_tas_base
= pc
;
1573 task
->fast_tas_end
= endpc
;
1575 return KERN_SUCCESS
;
1577 #else /* FAST_TAS */
1580 __unused task_t task
,
1581 __unused vm_offset_t pc
,
1582 __unused vm_offset_t endpc
)
1584 return KERN_FAILURE
;
1586 #endif /* FAST_TAS */
1589 task_synchronizer_destroy_all(task_t task
)
1591 semaphore_t semaphore
;
1592 lock_set_t lock_set
;
1595 * Destroy owned semaphores
1598 while (!queue_empty(&task
->semaphore_list
)) {
1599 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
1600 (void) semaphore_destroy(task
, semaphore
);
1604 * Destroy owned lock sets
1607 while (!queue_empty(&task
->lock_set_list
)) {
1608 lock_set
= (lock_set_t
) queue_first(&task
->lock_set_list
);
1609 (void) lock_set_destroy(task
, lock_set
);
1614 * We need to export some functions to other components that
1615 * are currently implemented in macros within the osfmk
1616 * component. Just export them as functions of the same name.
1618 boolean_t
is_kerneltask(task_t t
)
1620 if (t
== kernel_task
)
1627 task_t
current_task(void);
1628 task_t
current_task(void)
1630 return (current_task_fast());
1633 #undef task_reference
1634 void task_reference(task_t task
);
1639 if (task
!= TASK_NULL
)
1640 task_reference_internal(task
);