2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * @OSF_FREE_COPYRIGHT@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
52 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub,
55 * Task management primitives implementation.
58 * Copyright (c) 1993 The University of Utah and
59 * the Computer Systems Laboratory (CSL). All rights reserved.
61 * Permission to use, copy, modify and distribute this software and its
62 * documentation is hereby granted, provided that both the copyright
63 * notice and this permission notice appear in all copies of the
64 * software, derivative works or modified versions, and any portions
65 * thereof, and that both notices appear in supporting documentation.
67 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
68 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
69 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
71 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
72 * improvements that they make and grant CSL redistribution rights.
77 #include <mach_host.h>
78 #include <mach_prof.h>
80 #include <platforms.h>
82 #include <mach/mach_types.h>
83 #include <mach/boolean.h>
84 #include <mach/host_priv.h>
85 #include <mach/machine/vm_types.h>
86 #include <mach/vm_param.h>
87 #include <mach/semaphore.h>
88 #include <mach/task_info.h>
89 #include <mach/task_special_ports.h>
91 #include <ipc/ipc_types.h>
92 #include <ipc/ipc_space.h>
93 #include <ipc/ipc_entry.h>
95 #include <kern/kern_types.h>
96 #include <kern/mach_param.h>
97 #include <kern/misc_protos.h>
98 #include <kern/task.h>
99 #include <kern/thread.h>
100 #include <kern/zalloc.h>
101 #include <kern/kalloc.h>
102 #include <kern/processor.h>
103 #include <kern/sched_prim.h> /* for thread_wakeup */
104 #include <kern/ipc_tt.h>
105 #include <kern/ledger.h>
106 #include <kern/host.h>
107 #include <kern/clock.h>
108 #include <kern/timer.h>
109 #include <kern/profile.h>
110 #include <kern/assert.h>
111 #include <kern/sync_lock.h>
114 #include <vm/vm_map.h>
115 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
116 #include <vm/vm_pageout.h>
117 #include <vm/vm_protos.h> /* for vm_map_remove_commpage */
120 #include <ddb/db_sym.h>
121 #endif /* MACH_KDB */
124 #include <ppc/exception.h>
125 #include <ppc/hw_perfmon.h>
129 * Exported interfaces
132 #include <mach/task_server.h>
133 #include <mach/mach_host_server.h>
134 #include <mach/host_security_server.h>
135 #include <mach/mach_port_server.h>
137 #include <vm/task_working_set.h>
138 #include <vm/vm_shared_memory_server.h>
145 void task_hold_locked(
147 void task_wait_locked(
149 void task_release_locked(
153 void task_synchronizer_destroy_all(
156 kern_return_t
task_set_ledger(
162 task_backing_store_privileged(
166 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
172 task_working_set_disable(task_t task
)
177 ws
= task
->dynamic_working_set
;
178 task
->dynamic_working_set
= NULL
;
181 tws_hash_ws_flush(ws
);
182 tws_hash_destroy(ws
);
194 if (task_has_64BitAddr(task
))
197 /* LP64todo - no task working set for 64-bit */
198 task_set_64BitAddr(task
);
199 task_working_set_disable(task
);
201 if ( !task_has_64BitAddr(task
))
205 * Deallocate all memory previously allocated
206 * above the 32-bit address space, since it won't
207 * be accessible anymore.
209 /* LP64todo - make this clean */
210 vm_map_remove_commpage(task
->map
);
211 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
212 (void) vm_map_remove(task
->map
,
213 (vm_map_offset_t
) VM_MAX_ADDRESS
,
216 task_clear_64BitAddr(task
);
218 /* FIXME: On x86, the thread save state flavor can diverge from the
219 * task's 64-bit feature flag due to the 32-bit/64-bit register save
220 * state dichotomy. Since we can be pre-empted in this interval,
221 * certain routines may observe the thread as being in an inconsistent
222 * state with respect to its task's 64-bitness.
225 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
226 machine_thread_switch_addrmode(thread
, !is64bit
);
236 TASK_MAX
* sizeof(struct task
),
237 TASK_CHUNK
* sizeof(struct task
),
241 * Create the kernel task as the first task.
243 if (task_create_internal(TASK_NULL
, FALSE
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
244 panic("task_init\n");
246 vm_map_deallocate(kernel_task
->map
);
247 kernel_task
->map
= kernel_map
;
259 * If may_assign is false, task is already being assigned,
260 * wait for that to finish.
262 while (task
->may_assign
== FALSE
) {
265 task
->assign_active
= TRUE
;
266 res
= thread_sleep_mutex((event_t
) &task
->assign_active
,
267 &task
->lock
, THREAD_UNINT
);
268 assert(res
== THREAD_AWAKENED
);
270 task
->may_assign
= FALSE
;
275 #define thread_freeze(thread) assert(task->processor_set == &default_pset)
284 assert(task
->may_assign
== FALSE
);
285 task
->may_assign
= TRUE
;
286 if (task
->assign_active
== TRUE
) {
287 task
->assign_active
= FALSE
;
288 thread_wakeup((event_t
)&task
->assign_active
);
294 #define thread_unfreeze(thread) assert(task->processor_set == &default_pset)
297 #endif /* MACH_HOST */
300 * Create a task running in the kernel address space. It may
301 * have its own map of size mem_size and may have ipc privileges.
305 __unused task_t parent_task
,
306 __unused vm_offset_t map_base
,
307 __unused vm_size_t map_size
,
308 __unused task_t
*child_task
)
310 return (KERN_INVALID_ARGUMENT
);
316 __unused ledger_port_array_t ledger_ports
,
317 __unused mach_msg_type_number_t num_ledger_ports
,
318 boolean_t inherit_memory
,
319 task_t
*child_task
) /* OUT */
321 if (parent_task
== TASK_NULL
)
322 return(KERN_INVALID_ARGUMENT
);
324 return task_create_internal(
325 parent_task
, inherit_memory
, task_has_64BitAddr(parent_task
), child_task
);
329 host_security_create_task_token(
330 host_security_t host_security
,
332 security_token_t sec_token
,
333 audit_token_t audit_token
,
334 host_priv_t host_priv
,
335 __unused ledger_port_array_t ledger_ports
,
336 __unused mach_msg_type_number_t num_ledger_ports
,
337 boolean_t inherit_memory
,
338 task_t
*child_task
) /* OUT */
340 kern_return_t result
;
342 if (parent_task
== TASK_NULL
)
343 return(KERN_INVALID_ARGUMENT
);
345 if (host_security
== HOST_NULL
)
346 return(KERN_INVALID_SECURITY
);
348 result
= task_create_internal(
349 parent_task
, inherit_memory
, task_has_64BitAddr(parent_task
), child_task
);
351 if (result
!= KERN_SUCCESS
)
354 result
= host_security_set_task_token(host_security
,
360 if (result
!= KERN_SUCCESS
)
367 task_create_internal(
369 boolean_t inherit_memory
,
371 task_t
*child_task
) /* OUT */
374 processor_set_t pset
;
376 new_task
= (task_t
) zalloc(task_zone
);
378 if (new_task
== TASK_NULL
)
379 return(KERN_RESOURCE_SHORTAGE
);
381 /* one ref for just being alive; one for our caller */
382 new_task
->ref_count
= 2;
385 new_task
->map
= vm_map_fork(parent_task
->map
);
387 new_task
->map
= vm_map_create(pmap_create(0, is_64bit
),
388 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
389 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
391 mutex_init(&new_task
->lock
, 0);
392 queue_init(&new_task
->threads
);
393 new_task
->suspend_count
= 0;
394 new_task
->thread_count
= 0;
395 new_task
->active_thread_count
= 0;
396 new_task
->user_stop_count
= 0;
397 new_task
->role
= TASK_UNSPECIFIED
;
398 new_task
->active
= TRUE
;
399 new_task
->user_data
= 0;
400 new_task
->faults
= 0;
401 new_task
->cow_faults
= 0;
402 new_task
->pageins
= 0;
403 new_task
->messages_sent
= 0;
404 new_task
->messages_received
= 0;
405 new_task
->syscalls_mach
= 0;
406 new_task
->priv_flags
= 0;
407 new_task
->syscalls_unix
=0;
409 new_task
->taskFeatures
[0] = 0; /* Init task features */
410 new_task
->taskFeatures
[1] = 0; /* Init task features */
411 new_task
->dynamic_working_set
= 0;
413 task_working_set_create(new_task
, TWS_SMALL_HASH_LINE_COUNT
,
414 0, TWS_HASH_STYLE_DEFAULT
);
417 new_task
->bsd_info
= 0;
418 #endif /* MACH_BSD */
421 new_task
->i386_ldt
= 0;
425 if(BootProcInfo
.pf
.Available
& pf64Bit
) new_task
->taskFeatures
[0] |= tf64BitData
; /* If 64-bit machine, show we have 64-bit registers at least */
428 queue_init(&new_task
->semaphore_list
);
429 queue_init(&new_task
->lock_set_list
);
430 new_task
->semaphores_owned
= 0;
431 new_task
->lock_sets_owned
= 0;
434 new_task
->may_assign
= TRUE
;
435 new_task
->assign_active
= FALSE
;
436 #endif /* MACH_HOST */
438 ipc_task_init(new_task
, parent_task
);
440 new_task
->total_user_time
= 0;
441 new_task
->total_system_time
= 0;
443 task_prof_init(new_task
);
445 if (parent_task
!= TASK_NULL
) {
448 * Freeze the parent, so that parent_task->processor_set
451 task_freeze(parent_task
);
452 #endif /* MACH_HOST */
453 pset
= parent_task
->processor_set
;
455 pset
= &default_pset
;
457 new_task
->sec_token
= parent_task
->sec_token
;
458 new_task
->audit_token
= parent_task
->audit_token
;
460 shared_region_mapping_ref(parent_task
->system_shared_region
);
461 new_task
->system_shared_region
= parent_task
->system_shared_region
;
463 new_task
->wired_ledger_port
= ledger_copy(
464 convert_port_to_ledger(parent_task
->wired_ledger_port
));
465 new_task
->paged_ledger_port
= ledger_copy(
466 convert_port_to_ledger(parent_task
->paged_ledger_port
));
467 if(task_has_64BitAddr(parent_task
))
468 task_set_64BitAddr(new_task
);
471 if (inherit_memory
&& parent_task
->i386_ldt
)
472 new_task
->i386_ldt
= user_ldt_copy(parent_task
->i386_ldt
);
476 pset
= &default_pset
;
478 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
479 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
480 new_task
->wired_ledger_port
= ledger_copy(root_wired_ledger
);
481 new_task
->paged_ledger_port
= ledger_copy(root_paged_ledger
);
484 if (kernel_task
== TASK_NULL
) {
485 new_task
->priority
= BASEPRI_KERNEL
;
486 new_task
->max_priority
= MAXPRI_KERNEL
;
489 new_task
->priority
= BASEPRI_DEFAULT
;
490 new_task
->max_priority
= MAXPRI_USER
;
494 pset_add_task(pset
, new_task
);
497 if (parent_task
!= TASK_NULL
)
498 task_unfreeze(parent_task
);
499 #endif /* MACH_HOST */
501 if (vm_backing_store_low
&& parent_task
!= NULL
)
502 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
504 ipc_task_enable(new_task
);
506 *child_task
= new_task
;
507 return(KERN_SUCCESS
);
513 * Drop a reference on a task.
519 processor_set_t pset
;
521 if (task
== TASK_NULL
)
524 if (task_deallocate_internal(task
) > 0)
527 pset
= task
->processor_set
;
528 pset_deallocate(pset
);
530 if(task
->dynamic_working_set
)
531 tws_hash_destroy(task
->dynamic_working_set
);
533 ipc_task_terminate(task
);
535 vm_map_deallocate(task
->map
);
536 is_release(task
->itk_space
);
538 task_prof_deallocate(task
);
539 zfree(task_zone
, task
);
543 * task_name_deallocate:
545 * Drop a reference on a task name.
548 task_name_deallocate(
549 task_name_t task_name
)
551 return(task_deallocate((task_t
)task_name
));
558 * Terminate the specified task. See comments on thread_terminate
559 * (kern/thread.c) about problems with terminating the "current task."
566 if (task
== TASK_NULL
)
567 return (KERN_INVALID_ARGUMENT
);
570 return (KERN_FAILURE
);
572 return (task_terminate_internal(task
));
576 task_terminate_internal(
579 processor_set_t pset
;
580 thread_t thread
, self
;
582 boolean_t interrupt_save
;
584 assert(task
!= kernel_task
);
586 self
= current_thread();
587 self_task
= self
->task
;
590 * Get the task locked and make sure that we are not racing
591 * with someone else trying to terminate us.
593 if (task
== self_task
)
596 if (task
< self_task
) {
598 task_lock(self_task
);
601 task_lock(self_task
);
605 if (!task
->active
|| !self
->active
) {
607 * Task or current act is already being terminated.
608 * Just return an error. If we are dying, this will
609 * just get us to our AST special handler and that
610 * will get us to finalize the termination of ourselves.
613 if (self_task
!= task
)
614 task_unlock(self_task
);
616 return (KERN_FAILURE
);
619 if (self_task
!= task
)
620 task_unlock(self_task
);
623 * Make sure the current thread does not get aborted out of
624 * the waits inside these operations.
626 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
629 * Indicate that we want all the threads to stop executing
630 * at user space by holding the task (we would have held
631 * each thread independently in thread_terminate_internal -
632 * but this way we may be more likely to already find it
633 * held there). Mark the task inactive, and prevent
634 * further task operations via the task port.
636 task_hold_locked(task
);
637 task
->active
= FALSE
;
638 ipc_task_disable(task
);
641 * Terminate each thread in the task.
643 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
644 thread_terminate_internal(thread
);
648 * Give the machine dependent code a chance
649 * to perform cleanup before ripping apart
652 if (self_task
== task
)
653 machine_thread_terminate_self();
658 * Destroy all synchronizers owned by the task.
660 task_synchronizer_destroy_all(task
);
663 * Destroy the IPC space, leaving just a reference for it.
665 ipc_space_destroy(task
->itk_space
);
667 /* LP64todo - make this clean */
668 vm_map_remove_commpage(task
->map
);
669 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
671 if (vm_map_has_4GB_pagezero(task
->map
))
672 vm_map_clear_4GB_pagezero(task
->map
);
675 * If the current thread is a member of the task
676 * being terminated, then the last reference to
677 * the task will not be dropped until the thread
678 * is finally reaped. To avoid incurring the
679 * expense of removing the address space regions
680 * at reap time, we do it explictly here.
682 vm_map_remove(task
->map
, task
->map
->min_offset
,
683 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
685 shared_region_mapping_dealloc(task
->system_shared_region
);
688 * Flush working set here to avoid I/O in reaper thread
690 if (task
->dynamic_working_set
)
691 tws_hash_ws_flush(task
->dynamic_working_set
);
693 pset
= task
->processor_set
;
695 pset_remove_task(pset
,task
);
699 * We no longer need to guard against being aborted, so restore
700 * the previous interruptible state.
702 thread_interrupt_level(interrupt_save
);
705 perfmon_release_facility(task
); // notify the perfmon facility
709 * Get rid of the task active reference on itself.
711 task_deallocate(task
);
713 return (KERN_SUCCESS
);
719 * Shut the current task down (except for the current thread) in
720 * preparation for dramatic changes to the task (probably exec).
721 * We hold the task, terminate all other threads in the task and
722 * wait for them to terminate, clean up the portspace, and when
723 * all done, let the current thread go.
729 thread_t thread
, self
;
731 assert(task
!= kernel_task
);
733 self
= current_thread();
735 if (task
!= self
->task
)
736 return (KERN_INVALID_ARGUMENT
);
740 if (!task
->active
|| !self
->active
) {
742 * Task or current thread is already being terminated.
743 * Hurry up and return out of the current kernel context
744 * so that we run our AST special handler to terminate
749 return (KERN_FAILURE
);
752 if (task
->thread_count
> 1) {
754 * Mark all the threads to keep them from starting any more
755 * user-level execution. The thread_terminate_internal code
756 * would do this on a thread by thread basis anyway, but this
757 * gives us a better chance of not having to wait there.
759 task_hold_locked(task
);
762 * Terminate all the other threads in the task.
764 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
766 thread_terminate_internal(thread
);
769 task_release_locked(task
);
773 * Give the machine dependent code a chance
774 * to perform cleanup before ripping apart
777 machine_thread_terminate_self();
782 * Destroy all synchronizers owned by the task.
784 task_synchronizer_destroy_all(task
);
787 * Destroy the contents of the IPC space, leaving just
788 * a reference for it.
790 ipc_space_clean(task
->itk_space
);
793 * Clean out the address space, as we are going to be
796 vm_map_remove(task
->map
, task
->map
->min_offset
,
797 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
799 return (KERN_SUCCESS
);
805 * Suspend execution of the specified task.
806 * This is a recursive-style suspension of the task, a count of
807 * suspends is maintained.
809 * CONDITIONS: the task is locked and active.
813 register task_t task
)
815 register thread_t thread
;
817 assert(task
->active
);
819 if (task
->suspend_count
++ > 0)
823 * Iterate through all the threads and hold them.
825 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
826 thread_mtx_lock(thread
);
828 thread_mtx_unlock(thread
);
835 * Same as the internal routine above, except that is must lock
836 * and verify that the task is active. This differs from task_suspend
837 * in that it places a kernel hold on the task rather than just a
838 * user-level hold. This keeps users from over resuming and setting
839 * it running out from under the kernel.
841 * CONDITIONS: the caller holds a reference on the task
845 register task_t task
)
847 if (task
== TASK_NULL
)
848 return (KERN_INVALID_ARGUMENT
);
855 return (KERN_FAILURE
);
858 task_hold_locked(task
);
861 return (KERN_SUCCESS
);
867 * Wait for all threads in task to stop.
870 * Called with task locked, active, and held.
874 register task_t task
)
876 register thread_t thread
, self
;
878 assert(task
->active
);
879 assert(task
->suspend_count
> 0);
881 self
= current_thread();
884 * Iterate through all the threads and wait for them to
885 * stop. Do not wait for the current thread if it is within
888 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
895 * task_release_locked:
897 * Release a kernel hold on a task.
899 * CONDITIONS: the task is locked and active
903 register task_t task
)
905 register thread_t thread
;
907 assert(task
->active
);
908 assert(task
->suspend_count
> 0);
910 if (--task
->suspend_count
> 0)
913 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
914 thread_mtx_lock(thread
);
915 thread_release(thread
);
916 thread_mtx_unlock(thread
);
923 * Same as the internal routine above, except that it must lock
924 * and verify that the task is active.
926 * CONDITIONS: The caller holds a reference to the task
932 if (task
== TASK_NULL
)
933 return (KERN_INVALID_ARGUMENT
);
940 return (KERN_FAILURE
);
943 task_release_locked(task
);
946 return (KERN_SUCCESS
);
952 thread_act_array_t
*threads_out
,
953 mach_msg_type_number_t
*count
)
955 mach_msg_type_number_t actual
;
958 vm_size_t size
, size_needed
;
962 if (task
== TASK_NULL
)
963 return (KERN_INVALID_ARGUMENT
);
975 return (KERN_FAILURE
);
978 actual
= task
->thread_count
;
980 /* do we have the memory we need? */
981 size_needed
= actual
* sizeof (mach_port_t
);
982 if (size_needed
<= size
)
985 /* unlock the task and allocate more memory */
991 assert(size_needed
> 0);
996 return (KERN_RESOURCE_SHORTAGE
);
999 /* OK, have memory and the task is locked & active */
1000 threads
= (thread_t
*)addr
;
1004 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
1005 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
1006 thread_reference_internal(thread
);
1007 threads
[j
++] = thread
;
1010 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
1013 size_needed
= actual
* sizeof (mach_port_t
);
1015 /* can unlock task now that we've got the thread refs */
1019 /* no threads, so return null pointer and deallocate memory */
1028 /* if we allocated too much, must copy */
1030 if (size_needed
< size
) {
1033 newaddr
= kalloc(size_needed
);
1035 for (i
= 0; i
< actual
; ++i
)
1036 thread_deallocate(threads
[i
]);
1038 return (KERN_RESOURCE_SHORTAGE
);
1041 bcopy(addr
, newaddr
, size_needed
);
1043 threads
= (thread_t
*)newaddr
;
1046 *threads_out
= threads
;
1049 /* do the conversion that Mig should handle */
1051 for (i
= 0; i
< actual
; ++i
)
1052 ((ipc_port_t
*) threads
)[i
] = convert_thread_to_port(threads
[i
]);
1055 return (KERN_SUCCESS
);
1061 * Implement a user-level suspension on a task.
1064 * The caller holds a reference to the task
1068 register task_t task
)
1070 if (task
== TASK_NULL
|| task
== kernel_task
)
1071 return (KERN_INVALID_ARGUMENT
);
1075 if (!task
->active
) {
1078 return (KERN_FAILURE
);
1081 if (task
->user_stop_count
++ > 0) {
1083 * If the stop count was positive, the task is
1084 * already stopped and we can exit.
1088 return (KERN_SUCCESS
);
1092 * Put a kernel-level hold on the threads in the task (all
1093 * user-level task suspensions added together represent a
1094 * single kernel-level hold). We then wait for the threads
1095 * to stop executing user code.
1097 task_hold_locked(task
);
1098 task_wait_locked(task
);
1102 return (KERN_SUCCESS
);
1107 * Release a kernel hold on a task.
1110 * The caller holds a reference to the task
1114 register task_t task
)
1116 register boolean_t release
= FALSE
;
1118 if (task
== TASK_NULL
|| task
== kernel_task
)
1119 return (KERN_INVALID_ARGUMENT
);
1123 if (!task
->active
) {
1126 return (KERN_FAILURE
);
1129 if (task
->user_stop_count
> 0) {
1130 if (--task
->user_stop_count
== 0)
1136 return (KERN_FAILURE
);
1140 * Release the task if necessary.
1143 task_release_locked(task
);
1147 return (KERN_SUCCESS
);
1151 host_security_set_task_token(
1152 host_security_t host_security
,
1154 security_token_t sec_token
,
1155 audit_token_t audit_token
,
1156 host_priv_t host_priv
)
1158 ipc_port_t host_port
;
1161 if (task
== TASK_NULL
)
1162 return(KERN_INVALID_ARGUMENT
);
1164 if (host_security
== HOST_NULL
)
1165 return(KERN_INVALID_SECURITY
);
1168 task
->sec_token
= sec_token
;
1169 task
->audit_token
= audit_token
;
1172 if (host_priv
!= HOST_PRIV_NULL
) {
1173 kr
= host_get_host_priv_port(host_priv
, &host_port
);
1175 kr
= host_get_host_port(host_priv_self(), &host_port
);
1177 assert(kr
== KERN_SUCCESS
);
1178 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
1183 * Utility routine to set a ledger
1191 if (task
== TASK_NULL
)
1192 return(KERN_INVALID_ARGUMENT
);
1196 ipc_port_release_send(task
->wired_ledger_port
);
1197 task
->wired_ledger_port
= ledger_copy(wired
);
1200 ipc_port_release_send(task
->paged_ledger_port
);
1201 task
->paged_ledger_port
= ledger_copy(paged
);
1205 return(KERN_SUCCESS
);
1209 * This routine was added, pretty much exclusively, for registering the
1210 * RPC glue vector for in-kernel short circuited tasks. Rather than
1211 * removing it completely, I have only disabled that feature (which was
1212 * the only feature at the time). It just appears that we are going to
1213 * want to add some user data to tasks in the future (i.e. bsd info,
1214 * task names, etc...), so I left it in the formal task interface.
1219 task_flavor_t flavor
,
1220 __unused task_info_t task_info_in
, /* pointer to IN array */
1221 __unused mach_msg_type_number_t task_info_count
)
1223 if (task
== TASK_NULL
)
1224 return(KERN_INVALID_ARGUMENT
);
1228 return (KERN_INVALID_ARGUMENT
);
1230 return (KERN_SUCCESS
);
1236 task_flavor_t flavor
,
1237 task_info_t task_info_out
,
1238 mach_msg_type_number_t
*task_info_count
)
1240 if (task
== TASK_NULL
)
1241 return (KERN_INVALID_ARGUMENT
);
1245 case TASK_BASIC_INFO_32
:
1247 task_basic_info_32_t basic_info
;
1250 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
)
1251 return (KERN_INVALID_ARGUMENT
);
1253 basic_info
= (task_basic_info_32_t
)task_info_out
;
1255 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1256 basic_info
->virtual_size
= CAST_DOWN(vm_offset_t
,map
->size
);
1257 basic_info
->resident_size
= pmap_resident_count(map
->pmap
)
1261 basic_info
->policy
= ((task
!= kernel_task
)?
1262 POLICY_TIMESHARE
: POLICY_RR
);
1263 basic_info
->suspend_count
= task
->user_stop_count
;
1265 absolutetime_to_microtime(
1266 task
->total_user_time
,
1267 &basic_info
->user_time
.seconds
,
1268 &basic_info
->user_time
.microseconds
);
1269 absolutetime_to_microtime(
1270 task
->total_system_time
,
1271 &basic_info
->system_time
.seconds
,
1272 &basic_info
->system_time
.microseconds
);
1275 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
1279 case TASK_BASIC_INFO_64
:
1281 task_basic_info_64_t basic_info
;
1284 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
)
1285 return (KERN_INVALID_ARGUMENT
);
1287 basic_info
= (task_basic_info_64_t
)task_info_out
;
1289 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1290 basic_info
->virtual_size
= map
->size
;
1291 basic_info
->resident_size
= (mach_vm_size_t
)(pmap_resident_count(map
->pmap
)
1295 basic_info
->policy
= ((task
!= kernel_task
)?
1296 POLICY_TIMESHARE
: POLICY_RR
);
1297 basic_info
->suspend_count
= task
->user_stop_count
;
1299 absolutetime_to_microtime(
1300 task
->total_user_time
,
1301 &basic_info
->user_time
.seconds
,
1302 &basic_info
->user_time
.microseconds
);
1303 absolutetime_to_microtime(
1304 task
->total_system_time
,
1305 &basic_info
->system_time
.seconds
,
1306 &basic_info
->system_time
.microseconds
);
1309 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
1313 case TASK_THREAD_TIMES_INFO
:
1315 register task_thread_times_info_t times_info
;
1316 register thread_t thread
;
1318 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
)
1319 return (KERN_INVALID_ARGUMENT
);
1321 times_info
= (task_thread_times_info_t
) task_info_out
;
1322 times_info
->user_time
.seconds
= 0;
1323 times_info
->user_time
.microseconds
= 0;
1324 times_info
->system_time
.seconds
= 0;
1325 times_info
->system_time
.microseconds
= 0;
1329 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1330 time_value_t user_time
, system_time
;
1332 thread_read_times(thread
, &user_time
, &system_time
);
1334 time_value_add(×_info
->user_time
, &user_time
);
1335 time_value_add(×_info
->system_time
, &system_time
);
1340 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1344 case TASK_ABSOLUTETIME_INFO
:
1346 task_absolutetime_info_t info
;
1347 register thread_t thread
;
1349 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
)
1350 return (KERN_INVALID_ARGUMENT
);
1352 info
= (task_absolutetime_info_t
)task_info_out
;
1353 info
->threads_user
= info
->threads_system
= 0;
1357 info
->total_user
= task
->total_user_time
;
1358 info
->total_system
= task
->total_system_time
;
1360 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1363 tval
= timer_grab(&thread
->user_timer
);
1364 info
->threads_user
+= tval
;
1365 info
->total_user
+= tval
;
1367 tval
= timer_grab(&thread
->system_timer
);
1368 info
->threads_system
+= tval
;
1369 info
->total_system
+= tval
;
1374 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
1379 case TASK_SCHED_FIFO_INFO
:
1382 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
)
1383 return (KERN_INVALID_ARGUMENT
);
1385 return (KERN_INVALID_POLICY
);
1389 case TASK_SCHED_RR_INFO
:
1391 register policy_rr_base_t rr_base
;
1393 if (*task_info_count
< POLICY_RR_BASE_COUNT
)
1394 return (KERN_INVALID_ARGUMENT
);
1396 rr_base
= (policy_rr_base_t
) task_info_out
;
1399 if (task
!= kernel_task
) {
1401 return (KERN_INVALID_POLICY
);
1404 rr_base
->base_priority
= task
->priority
;
1407 rr_base
->quantum
= std_quantum_us
/ 1000;
1409 *task_info_count
= POLICY_RR_BASE_COUNT
;
1414 case TASK_SCHED_TIMESHARE_INFO
:
1416 register policy_timeshare_base_t ts_base
;
1418 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
)
1419 return (KERN_INVALID_ARGUMENT
);
1421 ts_base
= (policy_timeshare_base_t
) task_info_out
;
1424 if (task
== kernel_task
) {
1426 return (KERN_INVALID_POLICY
);
1429 ts_base
->base_priority
= task
->priority
;
1432 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
1436 case TASK_SECURITY_TOKEN
:
1438 register security_token_t
*sec_token_p
;
1440 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
)
1441 return (KERN_INVALID_ARGUMENT
);
1443 sec_token_p
= (security_token_t
*) task_info_out
;
1446 *sec_token_p
= task
->sec_token
;
1449 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
1453 case TASK_AUDIT_TOKEN
:
1455 register audit_token_t
*audit_token_p
;
1457 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
)
1458 return (KERN_INVALID_ARGUMENT
);
1460 audit_token_p
= (audit_token_t
*) task_info_out
;
1463 *audit_token_p
= task
->audit_token
;
1466 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
1470 case TASK_SCHED_INFO
:
1471 return (KERN_INVALID_ARGUMENT
);
1473 case TASK_EVENTS_INFO
:
1475 register task_events_info_t events_info
;
1477 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
)
1478 return (KERN_INVALID_ARGUMENT
);
1480 events_info
= (task_events_info_t
) task_info_out
;
1483 events_info
->faults
= task
->faults
;
1484 events_info
->pageins
= task
->pageins
;
1485 events_info
->cow_faults
= task
->cow_faults
;
1486 events_info
->messages_sent
= task
->messages_sent
;
1487 events_info
->messages_received
= task
->messages_received
;
1488 events_info
->syscalls_mach
= task
->syscalls_mach
;
1489 events_info
->syscalls_unix
= task
->syscalls_unix
;
1490 events_info
->csw
= task
->csw
;
1493 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
1498 return (KERN_INVALID_ARGUMENT
);
1501 return (KERN_SUCCESS
);
1507 * Change the assigned processor set for the task
1511 __unused task_t task
,
1512 __unused processor_set_t new_pset
,
1513 __unused boolean_t assign_threads
)
1515 return(KERN_FAILURE
);
1519 * task_assign_default:
1521 * Version of task_assign to assign to default processor set.
1524 task_assign_default(
1526 boolean_t assign_threads
)
1528 return (task_assign(task
, &default_pset
, assign_threads
));
1532 * task_get_assignment
1534 * Return name of processor set that task is assigned to.
1537 task_get_assignment(
1539 processor_set_t
*pset
)
1542 return(KERN_FAILURE
);
1544 *pset
= task
->processor_set
;
1545 pset_reference(*pset
);
1546 return(KERN_SUCCESS
);
1553 * Set scheduling policy and parameters, both base and limit, for
1554 * the given task. Policy must be a policy which is enabled for the
1555 * processor set. Change contained threads if requested.
1559 __unused task_t task
,
1560 __unused policy_t policy_id
,
1561 __unused policy_base_t base
,
1562 __unused mach_msg_type_number_t count
,
1563 __unused boolean_t set_limit
,
1564 __unused boolean_t change
)
1566 return(KERN_FAILURE
);
1572 * Set scheduling policy and parameters, both base and limit, for
1573 * the given task. Policy can be any policy implemented by the
1574 * processor set, whether enabled or not. Change contained threads
1579 __unused task_t task
,
1580 __unused processor_set_t pset
,
1581 __unused policy_t policy_id
,
1582 __unused policy_base_t base
,
1583 __unused mach_msg_type_number_t base_count
,
1584 __unused policy_limit_t limit
,
1585 __unused mach_msg_type_number_t limit_count
,
1586 __unused boolean_t change
)
1588 return(KERN_FAILURE
);
1598 extern int fast_tas_debug
;
1600 if (fast_tas_debug
) {
1601 printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
1605 task
->fast_tas_base
= pc
;
1606 task
->fast_tas_end
= endpc
;
1608 return KERN_SUCCESS
;
1610 #else /* FAST_TAS */
1613 __unused task_t task
,
1614 __unused vm_offset_t pc
,
1615 __unused vm_offset_t endpc
)
1617 return KERN_FAILURE
;
1619 #endif /* FAST_TAS */
1622 task_synchronizer_destroy_all(task_t task
)
1624 semaphore_t semaphore
;
1625 lock_set_t lock_set
;
1628 * Destroy owned semaphores
1631 while (!queue_empty(&task
->semaphore_list
)) {
1632 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
1633 (void) semaphore_destroy(task
, semaphore
);
1637 * Destroy owned lock sets
1640 while (!queue_empty(&task
->lock_set_list
)) {
1641 lock_set
= (lock_set_t
) queue_first(&task
->lock_set_list
);
1642 (void) lock_set_destroy(task
, lock_set
);
1647 * We need to export some functions to other components that
1648 * are currently implemented in macros within the osfmk
1649 * component. Just export them as functions of the same name.
1651 boolean_t
is_kerneltask(task_t t
)
1653 if (t
== kernel_task
)
1660 task_t
current_task(void);
1661 task_t
current_task(void)
1663 return (current_task_fast());
1666 #undef task_reference
1667 void task_reference(task_t task
);
1672 if (task
!= TASK_NULL
)
1673 task_reference_internal(task
);