2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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.
82 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
83 * support for mandatory and extensible security protections. This notice
84 * is included in support of clause 2.2 (b) of the Apple Public License,
86 * Copyright (c) 2005 SPARTA, Inc.
91 #include <platforms.h>
93 #include <mach/mach_types.h>
94 #include <mach/boolean.h>
95 #include <mach/host_priv.h>
96 #include <mach/machine/vm_types.h>
97 #include <mach/vm_param.h>
98 #include <mach/semaphore.h>
99 #include <mach/task_info.h>
100 #include <mach/task_special_ports.h>
102 #include <ipc/ipc_types.h>
103 #include <ipc/ipc_space.h>
104 #include <ipc/ipc_entry.h>
106 #include <kern/kern_types.h>
107 #include <kern/mach_param.h>
108 #include <kern/misc_protos.h>
109 #include <kern/task.h>
110 #include <kern/thread.h>
111 #include <kern/zalloc.h>
112 #include <kern/kalloc.h>
113 #include <kern/processor.h>
114 #include <kern/sched_prim.h> /* for thread_wakeup */
115 #include <kern/ipc_tt.h>
116 #include <kern/ledger.h>
117 #include <kern/host.h>
118 #include <kern/clock.h>
119 #include <kern/timer.h>
120 #include <kern/assert.h>
121 #include <kern/sync_lock.h>
122 #include <kern/affinity.h>
125 #include <vm/vm_map.h>
126 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
127 #include <vm/vm_pageout.h>
128 #include <vm/vm_protos.h>
131 #include <ddb/db_sym.h>
132 #endif /* MACH_KDB */
135 #include <ppc/exception.h>
136 #include <ppc/hw_perfmon.h>
140 * Exported interfaces
143 #include <mach/task_server.h>
144 #include <mach/mach_host_server.h>
145 #include <mach/host_security_server.h>
146 #include <mach/mach_port_server.h>
147 #include <mach/security_server.h>
149 #include <vm/vm_shared_region.h>
152 #include <security/mac_mach_internal.h>
160 void task_hold_locked(
162 void task_wait_locked(
164 void task_release_locked(
168 void task_synchronizer_destroy_all(
171 kern_return_t
task_set_ledger(
177 task_backing_store_privileged(
181 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
194 #endif /* __i386__ */
198 if (task_has_64BitAddr(task
))
201 task_set_64BitAddr(task
);
203 if ( !task_has_64BitAddr(task
))
207 * Deallocate all memory previously allocated
208 * above the 32-bit address space, since it won't
209 * be accessible anymore.
211 /* remove regular VM map entries & pmap mappings */
212 (void) vm_map_remove(task
->map
,
213 (vm_map_offset_t
) VM_MAX_ADDRESS
,
217 /* LP64todo - make this clean */
219 * PPC51: ppc64 is limited to 51-bit addresses.
220 * Memory mapped above that limit is handled specially
221 * at the pmap level, so let pmap clean the commpage mapping
224 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
225 /* ... and avoid regular pmap cleanup */
226 vm_flags
|= VM_MAP_REMOVE_NO_PMAP_CLEANUP
;
228 /* remove the higher VM mappings */
229 (void) vm_map_remove(task
->map
,
231 0xFFFFFFFFFFFFF000ULL
,
233 task_clear_64BitAddr(task
);
235 /* FIXME: On x86, the thread save state flavor can diverge from the
236 * task's 64-bit feature flag due to the 32-bit/64-bit register save
237 * state dichotomy. Since we can be pre-empted in this interval,
238 * certain routines may observe the thread as being in an inconsistent
239 * state with respect to its task's 64-bitness.
242 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
243 machine_thread_switch_addrmode(thread
);
245 #endif /* __i386__ */
253 TASK_MAX
* sizeof(struct task
),
254 TASK_CHUNK
* sizeof(struct task
),
258 * Create the kernel task as the first task.
260 if (task_create_internal(TASK_NULL
, FALSE
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
261 panic("task_init\n");
263 vm_map_deallocate(kernel_task
->map
);
264 kernel_task
->map
= kernel_map
;
268 * Create a task running in the kernel address space. It may
269 * have its own map of size mem_size and may have ipc privileges.
273 __unused task_t parent_task
,
274 __unused vm_offset_t map_base
,
275 __unused vm_size_t map_size
,
276 __unused task_t
*child_task
)
278 return (KERN_INVALID_ARGUMENT
);
284 __unused ledger_port_array_t ledger_ports
,
285 __unused mach_msg_type_number_t num_ledger_ports
,
286 __unused boolean_t inherit_memory
,
287 __unused task_t
*child_task
) /* OUT */
289 if (parent_task
== TASK_NULL
)
290 return(KERN_INVALID_ARGUMENT
);
293 * No longer supported: too many calls assume that a task has a valid
296 return(KERN_FAILURE
);
300 host_security_create_task_token(
301 host_security_t host_security
,
303 __unused security_token_t sec_token
,
304 __unused audit_token_t audit_token
,
305 __unused host_priv_t host_priv
,
306 __unused ledger_port_array_t ledger_ports
,
307 __unused mach_msg_type_number_t num_ledger_ports
,
308 __unused boolean_t inherit_memory
,
309 __unused task_t
*child_task
) /* OUT */
311 if (parent_task
== TASK_NULL
)
312 return(KERN_INVALID_ARGUMENT
);
314 if (host_security
== HOST_NULL
)
315 return(KERN_INVALID_SECURITY
);
318 * No longer supported.
320 return(KERN_FAILURE
);
324 task_create_internal(
326 boolean_t inherit_memory
,
328 task_t
*child_task
) /* OUT */
331 vm_shared_region_t shared_region
;
333 new_task
= (task_t
) zalloc(task_zone
);
335 if (new_task
== TASK_NULL
)
336 return(KERN_RESOURCE_SHORTAGE
);
338 /* one ref for just being alive; one for our caller */
339 new_task
->ref_count
= 2;
342 new_task
->map
= vm_map_fork(parent_task
->map
);
344 new_task
->map
= vm_map_create(pmap_create(0, is_64bit
),
345 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
346 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
348 /* Inherit memlock limit from parent */
350 vm_map_set_user_wire_limit(new_task
->map
, parent_task
->map
->user_wire_limit
);
352 mutex_init(&new_task
->lock
, 0);
353 queue_init(&new_task
->threads
);
354 new_task
->suspend_count
= 0;
355 new_task
->thread_count
= 0;
356 new_task
->active_thread_count
= 0;
357 new_task
->user_stop_count
= 0;
358 new_task
->pset_hint
= PROCESSOR_SET_NULL
;
359 new_task
->role
= TASK_UNSPECIFIED
;
360 new_task
->active
= TRUE
;
361 new_task
->user_data
= NULL
;
362 new_task
->faults
= 0;
363 new_task
->cow_faults
= 0;
364 new_task
->pageins
= 0;
365 new_task
->messages_sent
= 0;
366 new_task
->messages_received
= 0;
367 new_task
->syscalls_mach
= 0;
368 new_task
->priv_flags
= 0;
369 new_task
->syscalls_unix
=0;
370 new_task
->c_switch
= new_task
->p_switch
= new_task
->ps_switch
= 0;
371 new_task
->taskFeatures
[0] = 0; /* Init task features */
372 new_task
->taskFeatures
[1] = 0; /* Init task features */
375 new_task
->bsd_info
= NULL
;
376 #endif /* MACH_BSD */
379 new_task
->i386_ldt
= 0;
383 if(BootProcInfo
.pf
.Available
& pf64Bit
) new_task
->taskFeatures
[0] |= tf64BitData
; /* If 64-bit machine, show we have 64-bit registers at least */
386 queue_init(&new_task
->semaphore_list
);
387 queue_init(&new_task
->lock_set_list
);
388 new_task
->semaphores_owned
= 0;
389 new_task
->lock_sets_owned
= 0;
392 /*mutex_init(&new_task->labellock, ETAP_NO_TRACE);*/
393 new_task
->label
= labelh_new(1);
394 mac_task_label_init (&new_task
->maclabel
);
397 ipc_task_init(new_task
, parent_task
);
399 new_task
->total_user_time
= 0;
400 new_task
->total_system_time
= 0;
402 new_task
->vtimers
= 0;
404 new_task
->shared_region
= NULL
;
406 new_task
->affinity_space
= NULL
;
408 if (parent_task
!= TASK_NULL
) {
409 new_task
->sec_token
= parent_task
->sec_token
;
410 new_task
->audit_token
= parent_task
->audit_token
;
412 /* inherit the parent's shared region */
413 shared_region
= vm_shared_region_get(parent_task
);
414 vm_shared_region_set(new_task
, shared_region
);
416 new_task
->wired_ledger_port
= ledger_copy(
417 convert_port_to_ledger(parent_task
->wired_ledger_port
));
418 new_task
->paged_ledger_port
= ledger_copy(
419 convert_port_to_ledger(parent_task
->paged_ledger_port
));
420 if(task_has_64BitAddr(parent_task
))
421 task_set_64BitAddr(new_task
);
424 if (inherit_memory
&& parent_task
->i386_ldt
)
425 new_task
->i386_ldt
= user_ldt_copy(parent_task
->i386_ldt
);
427 if (inherit_memory
&& parent_task
->affinity_space
)
428 task_affinity_create(parent_task
, new_task
);
431 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
432 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
433 new_task
->wired_ledger_port
= ledger_copy(root_wired_ledger
);
434 new_task
->paged_ledger_port
= ledger_copy(root_paged_ledger
);
437 if (kernel_task
== TASK_NULL
) {
438 new_task
->priority
= BASEPRI_KERNEL
;
439 new_task
->max_priority
= MAXPRI_KERNEL
;
442 new_task
->priority
= BASEPRI_DEFAULT
;
443 new_task
->max_priority
= MAXPRI_USER
;
446 mutex_lock(&tasks_threads_lock
);
447 queue_enter(&tasks
, new_task
, task_t
, tasks
);
449 mutex_unlock(&tasks_threads_lock
);
451 if (vm_backing_store_low
&& parent_task
!= NULL
)
452 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
454 ipc_task_enable(new_task
);
456 *child_task
= new_task
;
457 return(KERN_SUCCESS
);
463 * Drop a reference on a task.
469 if (task
== TASK_NULL
)
472 if (task_deallocate_internal(task
) > 0)
475 ipc_task_terminate(task
);
477 if (task
->affinity_space
)
478 task_affinity_deallocate(task
);
480 vm_map_deallocate(task
->map
);
481 is_release(task
->itk_space
);
484 labelh_release(task
->label
);
486 zfree(task_zone
, task
);
490 * task_name_deallocate:
492 * Drop a reference on a task name.
495 task_name_deallocate(
496 task_name_t task_name
)
498 return(task_deallocate((task_t
)task_name
));
505 * Terminate the specified task. See comments on thread_terminate
506 * (kern/thread.c) about problems with terminating the "current task."
513 if (task
== TASK_NULL
)
514 return (KERN_INVALID_ARGUMENT
);
517 return (KERN_FAILURE
);
519 return (task_terminate_internal(task
));
523 task_terminate_internal(
526 thread_t thread
, self
;
528 boolean_t interrupt_save
;
530 assert(task
!= kernel_task
);
532 self
= current_thread();
533 self_task
= self
->task
;
536 * Get the task locked and make sure that we are not racing
537 * with someone else trying to terminate us.
539 if (task
== self_task
)
542 if (task
< self_task
) {
544 task_lock(self_task
);
547 task_lock(self_task
);
551 if (!task
->active
|| !self
->active
) {
553 * Task or current act is already being terminated.
554 * Just return an error. If we are dying, this will
555 * just get us to our AST special handler and that
556 * will get us to finalize the termination of ourselves.
559 if (self_task
!= task
)
560 task_unlock(self_task
);
562 return (KERN_FAILURE
);
565 if (self_task
!= task
)
566 task_unlock(self_task
);
569 * Make sure the current thread does not get aborted out of
570 * the waits inside these operations.
572 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
575 * Indicate that we want all the threads to stop executing
576 * at user space by holding the task (we would have held
577 * each thread independently in thread_terminate_internal -
578 * but this way we may be more likely to already find it
579 * held there). Mark the task inactive, and prevent
580 * further task operations via the task port.
582 task_hold_locked(task
);
583 task
->active
= FALSE
;
584 ipc_task_disable(task
);
587 * Terminate each thread in the task.
589 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
590 thread_terminate_internal(thread
);
594 * Give the machine dependent code a chance
595 * to perform cleanup before ripping apart
598 if (self_task
== task
)
599 machine_thread_terminate_self();
604 * Destroy all synchronizers owned by the task.
606 task_synchronizer_destroy_all(task
);
609 * Destroy the IPC space, leaving just a reference for it.
611 ipc_space_destroy(task
->itk_space
);
614 /* LP64todo - make this clean */
616 * PPC51: ppc64 is limited to 51-bit addresses.
618 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
621 if (vm_map_has_4GB_pagezero(task
->map
))
622 vm_map_clear_4GB_pagezero(task
->map
);
625 * If the current thread is a member of the task
626 * being terminated, then the last reference to
627 * the task will not be dropped until the thread
628 * is finally reaped. To avoid incurring the
629 * expense of removing the address space regions
630 * at reap time, we do it explictly here.
632 vm_map_remove(task
->map
,
633 task
->map
->min_offset
,
634 task
->map
->max_offset
,
637 /* release our shared region */
638 vm_shared_region_set(task
, NULL
);
640 mutex_lock(&tasks_threads_lock
);
641 queue_remove(&tasks
, task
, task_t
, tasks
);
643 mutex_unlock(&tasks_threads_lock
);
646 * We no longer need to guard against being aborted, so restore
647 * the previous interruptible state.
649 thread_interrupt_level(interrupt_save
);
652 perfmon_release_facility(task
); // notify the perfmon facility
656 * Get rid of the task active reference on itself.
658 task_deallocate(task
);
660 return (KERN_SUCCESS
);
666 * Shut the current task down (except for the current thread) in
667 * preparation for dramatic changes to the task (probably exec).
668 * We hold the task, terminate all other threads in the task and
669 * wait for them to terminate, clean up the portspace, and when
670 * all done, let the current thread go.
676 thread_t thread
, self
;
678 assert(task
!= kernel_task
);
680 self
= current_thread();
682 if (task
!= self
->task
)
683 return (KERN_INVALID_ARGUMENT
);
687 if (!task
->active
|| !self
->active
) {
689 * Task or current thread is already being terminated.
690 * Hurry up and return out of the current kernel context
691 * so that we run our AST special handler to terminate
696 return (KERN_FAILURE
);
699 if (task
->thread_count
> 1) {
701 * Mark all the threads to keep them from starting any more
702 * user-level execution. The thread_terminate_internal code
703 * would do this on a thread by thread basis anyway, but this
704 * gives us a better chance of not having to wait there.
706 task_hold_locked(task
);
709 * Terminate all the other threads in the task.
711 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
713 thread_terminate_internal(thread
);
716 task_release_locked(task
);
720 * Give the machine dependent code a chance
721 * to perform cleanup before ripping apart
724 machine_thread_terminate_self();
729 * Destroy all synchronizers owned by the task.
731 task_synchronizer_destroy_all(task
);
734 * Destroy the contents of the IPC space, leaving just
735 * a reference for it.
737 ipc_space_clean(task
->itk_space
);
740 * Clean out the address space, as we are going to be
743 vm_map_remove(task
->map
, task
->map
->min_offset
,
744 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
746 return (KERN_SUCCESS
);
752 * Suspend execution of the specified task.
753 * This is a recursive-style suspension of the task, a count of
754 * suspends is maintained.
756 * CONDITIONS: the task is locked and active.
760 register task_t task
)
762 register thread_t thread
;
764 assert(task
->active
);
766 if (task
->suspend_count
++ > 0)
770 * Iterate through all the threads and hold them.
772 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
773 thread_mtx_lock(thread
);
775 thread_mtx_unlock(thread
);
782 * Same as the internal routine above, except that is must lock
783 * and verify that the task is active. This differs from task_suspend
784 * in that it places a kernel hold on the task rather than just a
785 * user-level hold. This keeps users from over resuming and setting
786 * it running out from under the kernel.
788 * CONDITIONS: the caller holds a reference on the task
792 register task_t task
)
794 if (task
== TASK_NULL
)
795 return (KERN_INVALID_ARGUMENT
);
802 return (KERN_FAILURE
);
805 task_hold_locked(task
);
808 return (KERN_SUCCESS
);
814 * Wait for all threads in task to stop.
817 * Called with task locked, active, and held.
821 register task_t task
)
823 register thread_t thread
, self
;
825 assert(task
->active
);
826 assert(task
->suspend_count
> 0);
828 self
= current_thread();
831 * Iterate through all the threads and wait for them to
832 * stop. Do not wait for the current thread if it is within
835 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
842 * task_release_locked:
844 * Release a kernel hold on a task.
846 * CONDITIONS: the task is locked and active
850 register task_t task
)
852 register thread_t thread
;
854 assert(task
->active
);
855 assert(task
->suspend_count
> 0);
857 if (--task
->suspend_count
> 0)
860 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
861 thread_mtx_lock(thread
);
862 thread_release(thread
);
863 thread_mtx_unlock(thread
);
870 * Same as the internal routine above, except that it must lock
871 * and verify that the task is active.
873 * CONDITIONS: The caller holds a reference to the task
879 if (task
== TASK_NULL
)
880 return (KERN_INVALID_ARGUMENT
);
887 return (KERN_FAILURE
);
890 task_release_locked(task
);
893 return (KERN_SUCCESS
);
899 thread_act_array_t
*threads_out
,
900 mach_msg_type_number_t
*count
)
902 mach_msg_type_number_t actual
;
903 thread_t
*thread_list
;
905 vm_size_t size
, size_needed
;
909 if (task
== TASK_NULL
)
910 return (KERN_INVALID_ARGUMENT
);
912 size
= 0; addr
= NULL
;
922 return (KERN_FAILURE
);
925 actual
= task
->thread_count
;
927 /* do we have the memory we need? */
928 size_needed
= actual
* sizeof (mach_port_t
);
929 if (size_needed
<= size
)
932 /* unlock the task and allocate more memory */
938 assert(size_needed
> 0);
943 return (KERN_RESOURCE_SHORTAGE
);
946 /* OK, have memory and the task is locked & active */
947 thread_list
= (thread_t
*)addr
;
951 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
952 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
953 thread_reference_internal(thread
);
954 thread_list
[j
++] = thread
;
957 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
960 size_needed
= actual
* sizeof (mach_port_t
);
962 /* can unlock task now that we've got the thread refs */
966 /* no threads, so return null pointer and deallocate memory */
975 /* if we allocated too much, must copy */
977 if (size_needed
< size
) {
980 newaddr
= kalloc(size_needed
);
982 for (i
= 0; i
< actual
; ++i
)
983 thread_deallocate(thread_list
[i
]);
985 return (KERN_RESOURCE_SHORTAGE
);
988 bcopy(addr
, newaddr
, size_needed
);
990 thread_list
= (thread_t
*)newaddr
;
993 *threads_out
= thread_list
;
996 /* do the conversion that Mig should handle */
998 for (i
= 0; i
< actual
; ++i
)
999 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
1002 return (KERN_SUCCESS
);
1008 * Implement a user-level suspension on a task.
1011 * The caller holds a reference to the task
1015 register task_t task
)
1017 if (task
== TASK_NULL
|| task
== kernel_task
)
1018 return (KERN_INVALID_ARGUMENT
);
1022 if (!task
->active
) {
1025 return (KERN_FAILURE
);
1028 if (task
->user_stop_count
++ > 0) {
1030 * If the stop count was positive, the task is
1031 * already stopped and we can exit.
1035 return (KERN_SUCCESS
);
1039 * Put a kernel-level hold on the threads in the task (all
1040 * user-level task suspensions added together represent a
1041 * single kernel-level hold). We then wait for the threads
1042 * to stop executing user code.
1044 task_hold_locked(task
);
1045 task_wait_locked(task
);
1049 return (KERN_SUCCESS
);
1054 * Release a kernel hold on a task.
1057 * The caller holds a reference to the task
1061 register task_t task
)
1063 register boolean_t release
= FALSE
;
1065 if (task
== TASK_NULL
|| task
== kernel_task
)
1066 return (KERN_INVALID_ARGUMENT
);
1070 if (!task
->active
) {
1073 return (KERN_FAILURE
);
1076 if (task
->user_stop_count
> 0) {
1077 if (--task
->user_stop_count
== 0)
1083 return (KERN_FAILURE
);
1087 * Release the task if necessary.
1090 task_release_locked(task
);
1094 return (KERN_SUCCESS
);
1098 host_security_set_task_token(
1099 host_security_t host_security
,
1101 security_token_t sec_token
,
1102 audit_token_t audit_token
,
1103 host_priv_t host_priv
)
1105 ipc_port_t host_port
;
1108 if (task
== TASK_NULL
)
1109 return(KERN_INVALID_ARGUMENT
);
1111 if (host_security
== HOST_NULL
)
1112 return(KERN_INVALID_SECURITY
);
1115 task
->sec_token
= sec_token
;
1116 task
->audit_token
= audit_token
;
1119 if (host_priv
!= HOST_PRIV_NULL
) {
1120 kr
= host_get_host_priv_port(host_priv
, &host_port
);
1122 kr
= host_get_host_port(host_priv_self(), &host_port
);
1124 assert(kr
== KERN_SUCCESS
);
1125 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
1130 * Utility routine to set a ledger
1138 if (task
== TASK_NULL
)
1139 return(KERN_INVALID_ARGUMENT
);
1143 ipc_port_release_send(task
->wired_ledger_port
);
1144 task
->wired_ledger_port
= ledger_copy(wired
);
1147 ipc_port_release_send(task
->paged_ledger_port
);
1148 task
->paged_ledger_port
= ledger_copy(paged
);
1152 return(KERN_SUCCESS
);
1156 * This routine was added, pretty much exclusively, for registering the
1157 * RPC glue vector for in-kernel short circuited tasks. Rather than
1158 * removing it completely, I have only disabled that feature (which was
1159 * the only feature at the time). It just appears that we are going to
1160 * want to add some user data to tasks in the future (i.e. bsd info,
1161 * task names, etc...), so I left it in the formal task interface.
1166 task_flavor_t flavor
,
1167 __unused task_info_t task_info_in
, /* pointer to IN array */
1168 __unused mach_msg_type_number_t task_info_count
)
1170 if (task
== TASK_NULL
)
1171 return(KERN_INVALID_ARGUMENT
);
1175 return (KERN_INVALID_ARGUMENT
);
1177 return (KERN_SUCCESS
);
1183 task_flavor_t flavor
,
1184 task_info_t task_info_out
,
1185 mach_msg_type_number_t
*task_info_count
)
1187 if (task
== TASK_NULL
)
1188 return (KERN_INVALID_ARGUMENT
);
1192 case TASK_BASIC_INFO_32
:
1193 case TASK_BASIC2_INFO_32
:
1195 task_basic_info_32_t basic_info
;
1198 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
)
1199 return (KERN_INVALID_ARGUMENT
);
1201 basic_info
= (task_basic_info_32_t
)task_info_out
;
1203 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1204 basic_info
->virtual_size
= CAST_DOWN(vm_offset_t
,map
->size
);
1205 if (flavor
== TASK_BASIC2_INFO_32
) {
1207 * The "BASIC2" flavor gets the maximum resident
1208 * size instead of the current resident size...
1210 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
1212 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
1214 basic_info
->resident_size
*= PAGE_SIZE
;
1217 basic_info
->policy
= ((task
!= kernel_task
)?
1218 POLICY_TIMESHARE
: POLICY_RR
);
1219 basic_info
->suspend_count
= task
->user_stop_count
;
1221 absolutetime_to_microtime(task
->total_user_time
,
1222 (unsigned *)&basic_info
->user_time
.seconds
,
1223 (unsigned *)&basic_info
->user_time
.microseconds
);
1224 absolutetime_to_microtime(task
->total_system_time
,
1225 (unsigned *)&basic_info
->system_time
.seconds
,
1226 (unsigned *)&basic_info
->system_time
.microseconds
);
1229 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
1233 case TASK_BASIC_INFO_64
:
1235 task_basic_info_64_t basic_info
;
1238 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
)
1239 return (KERN_INVALID_ARGUMENT
);
1241 basic_info
= (task_basic_info_64_t
)task_info_out
;
1243 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1244 basic_info
->virtual_size
= map
->size
;
1245 basic_info
->resident_size
=
1246 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
1250 basic_info
->policy
= ((task
!= kernel_task
)?
1251 POLICY_TIMESHARE
: POLICY_RR
);
1252 basic_info
->suspend_count
= task
->user_stop_count
;
1254 absolutetime_to_microtime(task
->total_user_time
,
1255 (unsigned *)&basic_info
->user_time
.seconds
,
1256 (unsigned *)&basic_info
->user_time
.microseconds
);
1257 absolutetime_to_microtime(task
->total_system_time
,
1258 (unsigned *)&basic_info
->system_time
.seconds
,
1259 (unsigned *)&basic_info
->system_time
.microseconds
);
1262 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
1266 case TASK_THREAD_TIMES_INFO
:
1268 register task_thread_times_info_t times_info
;
1269 register thread_t thread
;
1271 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
)
1272 return (KERN_INVALID_ARGUMENT
);
1274 times_info
= (task_thread_times_info_t
) task_info_out
;
1275 times_info
->user_time
.seconds
= 0;
1276 times_info
->user_time
.microseconds
= 0;
1277 times_info
->system_time
.seconds
= 0;
1278 times_info
->system_time
.microseconds
= 0;
1282 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1283 time_value_t user_time
, system_time
;
1285 thread_read_times(thread
, &user_time
, &system_time
);
1287 time_value_add(×_info
->user_time
, &user_time
);
1288 time_value_add(×_info
->system_time
, &system_time
);
1293 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1297 case TASK_ABSOLUTETIME_INFO
:
1299 task_absolutetime_info_t info
;
1300 register thread_t thread
;
1302 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
)
1303 return (KERN_INVALID_ARGUMENT
);
1305 info
= (task_absolutetime_info_t
)task_info_out
;
1306 info
->threads_user
= info
->threads_system
= 0;
1310 info
->total_user
= task
->total_user_time
;
1311 info
->total_system
= task
->total_system_time
;
1313 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1316 tval
= timer_grab(&thread
->user_timer
);
1317 info
->threads_user
+= tval
;
1318 info
->total_user
+= tval
;
1320 tval
= timer_grab(&thread
->system_timer
);
1321 info
->threads_system
+= tval
;
1322 info
->total_system
+= tval
;
1327 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
1332 case TASK_SCHED_FIFO_INFO
:
1335 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
)
1336 return (KERN_INVALID_ARGUMENT
);
1338 return (KERN_INVALID_POLICY
);
1342 case TASK_SCHED_RR_INFO
:
1344 register policy_rr_base_t rr_base
;
1346 if (*task_info_count
< POLICY_RR_BASE_COUNT
)
1347 return (KERN_INVALID_ARGUMENT
);
1349 rr_base
= (policy_rr_base_t
) task_info_out
;
1352 if (task
!= kernel_task
) {
1354 return (KERN_INVALID_POLICY
);
1357 rr_base
->base_priority
= task
->priority
;
1360 rr_base
->quantum
= std_quantum_us
/ 1000;
1362 *task_info_count
= POLICY_RR_BASE_COUNT
;
1367 case TASK_SCHED_TIMESHARE_INFO
:
1369 register policy_timeshare_base_t ts_base
;
1371 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
)
1372 return (KERN_INVALID_ARGUMENT
);
1374 ts_base
= (policy_timeshare_base_t
) task_info_out
;
1377 if (task
== kernel_task
) {
1379 return (KERN_INVALID_POLICY
);
1382 ts_base
->base_priority
= task
->priority
;
1385 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
1389 case TASK_SECURITY_TOKEN
:
1391 register security_token_t
*sec_token_p
;
1393 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
)
1394 return (KERN_INVALID_ARGUMENT
);
1396 sec_token_p
= (security_token_t
*) task_info_out
;
1399 *sec_token_p
= task
->sec_token
;
1402 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
1406 case TASK_AUDIT_TOKEN
:
1408 register audit_token_t
*audit_token_p
;
1410 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
)
1411 return (KERN_INVALID_ARGUMENT
);
1413 audit_token_p
= (audit_token_t
*) task_info_out
;
1416 *audit_token_p
= task
->audit_token
;
1419 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
1423 case TASK_SCHED_INFO
:
1424 return (KERN_INVALID_ARGUMENT
);
1426 case TASK_EVENTS_INFO
:
1428 register task_events_info_t events_info
;
1429 register thread_t thread
;
1431 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
)
1432 return (KERN_INVALID_ARGUMENT
);
1434 events_info
= (task_events_info_t
) task_info_out
;
1438 events_info
->faults
= task
->faults
;
1439 events_info
->pageins
= task
->pageins
;
1440 events_info
->cow_faults
= task
->cow_faults
;
1441 events_info
->messages_sent
= task
->messages_sent
;
1442 events_info
->messages_received
= task
->messages_received
;
1443 events_info
->syscalls_mach
= task
->syscalls_mach
;
1444 events_info
->syscalls_unix
= task
->syscalls_unix
;
1446 events_info
->csw
= task
->c_switch
;
1448 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1449 events_info
->csw
+= thread
->c_switch
;
1454 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
1457 case TASK_AFFINITY_TAG_INFO
:
1459 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
)
1460 return (KERN_INVALID_ARGUMENT
);
1462 return task_affinity_info(task
, task_info_out
, task_info_count
);
1466 return (KERN_INVALID_ARGUMENT
);
1469 return (KERN_SUCCESS
);
1479 /* assert(task == current_task()); */ /* bogus assert 4803227 4807483 */
1483 task
->vtimers
|= which
;
1487 case TASK_VTIMER_USER
:
1488 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1489 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
1493 case TASK_VTIMER_PROF
:
1494 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1495 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
1496 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
1500 case TASK_VTIMER_RLIM
:
1501 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1502 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
1503 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
1516 assert(task
== current_task());
1520 task
->vtimers
&= ~which
;
1530 uint32_t *microsecs
)
1532 thread_t thread
= current_thread();
1533 uint32_t tdelt
, secs
;
1536 assert(task
== current_task());
1538 assert(task
->vtimers
& which
);
1544 case TASK_VTIMER_USER
:
1545 tdelt
= timer_delta(&thread
->user_timer
,
1546 &thread
->vtimer_user_save
);
1549 case TASK_VTIMER_PROF
:
1550 tsum
= timer_grab(&thread
->user_timer
);
1551 tsum
+= timer_grab(&thread
->system_timer
);
1552 tdelt
= tsum
- thread
->vtimer_prof_save
;
1553 thread
->vtimer_prof_save
= tsum
;
1556 case TASK_VTIMER_RLIM
:
1557 tsum
= timer_grab(&thread
->user_timer
);
1558 tsum
+= timer_grab(&thread
->system_timer
);
1559 tdelt
= tsum
- thread
->vtimer_rlim_save
;
1560 thread
->vtimer_rlim_save
= tsum
;
1564 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
1570 * Change the assigned processor set for the task
1574 __unused task_t task
,
1575 __unused processor_set_t new_pset
,
1576 __unused boolean_t assign_threads
)
1578 return(KERN_FAILURE
);
1582 * task_assign_default:
1584 * Version of task_assign to assign to default processor set.
1587 task_assign_default(
1589 boolean_t assign_threads
)
1591 return (task_assign(task
, &pset0
, assign_threads
));
1595 * task_get_assignment
1597 * Return name of processor set that task is assigned to.
1600 task_get_assignment(
1602 processor_set_t
*pset
)
1605 return(KERN_FAILURE
);
1609 return (KERN_SUCCESS
);
1616 * Set scheduling policy and parameters, both base and limit, for
1617 * the given task. Policy must be a policy which is enabled for the
1618 * processor set. Change contained threads if requested.
1622 __unused task_t task
,
1623 __unused policy_t policy_id
,
1624 __unused policy_base_t base
,
1625 __unused mach_msg_type_number_t count
,
1626 __unused boolean_t set_limit
,
1627 __unused boolean_t change
)
1629 return(KERN_FAILURE
);
1635 * Set scheduling policy and parameters, both base and limit, for
1636 * the given task. Policy can be any policy implemented by the
1637 * processor set, whether enabled or not. Change contained threads
1642 __unused task_t task
,
1643 __unused processor_set_t pset
,
1644 __unused policy_t policy_id
,
1645 __unused policy_base_t base
,
1646 __unused mach_msg_type_number_t base_count
,
1647 __unused policy_limit_t limit
,
1648 __unused mach_msg_type_number_t limit_count
,
1649 __unused boolean_t change
)
1651 return(KERN_FAILURE
);
1661 extern int fast_tas_debug
;
1663 if (fast_tas_debug
) {
1664 printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
1668 task
->fast_tas_base
= pc
;
1669 task
->fast_tas_end
= endpc
;
1671 return KERN_SUCCESS
;
1673 #else /* FAST_TAS */
1676 __unused task_t task
,
1677 __unused vm_offset_t pc
,
1678 __unused vm_offset_t endpc
)
1680 return KERN_FAILURE
;
1682 #endif /* FAST_TAS */
1685 task_synchronizer_destroy_all(task_t task
)
1687 semaphore_t semaphore
;
1688 lock_set_t lock_set
;
1691 * Destroy owned semaphores
1694 while (!queue_empty(&task
->semaphore_list
)) {
1695 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
1696 (void) semaphore_destroy(task
, semaphore
);
1700 * Destroy owned lock sets
1703 while (!queue_empty(&task
->lock_set_list
)) {
1704 lock_set
= (lock_set_t
) queue_first(&task
->lock_set_list
);
1705 (void) lock_set_destroy(task
, lock_set
);
1710 * We need to export some functions to other components that
1711 * are currently implemented in macros within the osfmk
1712 * component. Just export them as functions of the same name.
1714 boolean_t
is_kerneltask(task_t t
)
1716 if (t
== kernel_task
)
1723 task_t
current_task(void);
1724 task_t
current_task(void)
1726 return (current_task_fast());
1729 #undef task_reference
1730 void task_reference(task_t task
);
1735 if (task
!= TASK_NULL
)
1736 task_reference_internal(task
);
1739 #if CONFIG_MACF_MACH
1741 * Protect 2 task labels against modification by adding a reference on
1742 * both label handles. The locks do not actually have to be held while
1743 * using the labels as only labels with one reference can be modified
1752 labelh_reference(a
->label
);
1753 labelh_reference(b
->label
);
1761 labelh_release(a
->label
);
1762 labelh_release(b
->label
);
1766 mac_task_label_update_internal(
1771 tasklabel_lock(task
);
1772 task
->label
= labelh_modify(task
->label
);
1773 mac_task_label_update(pl
, &task
->maclabel
);
1774 tasklabel_unlock(task
);
1775 ip_lock(task
->itk_self
);
1776 mac_port_label_update_cred(pl
, &task
->itk_self
->ip_label
);
1777 ip_unlock(task
->itk_self
);
1781 mac_task_label_modify(
1784 void (*f
) (struct label
*l
, void *arg
))
1787 tasklabel_lock(task
);
1788 task
->label
= labelh_modify(task
->label
);
1789 (*f
)(&task
->maclabel
, arg
);
1790 tasklabel_unlock(task
);
1794 mac_task_get_label(struct task
*task
)
1796 return (&task
->maclabel
);