2 * Copyright (c) 2000-2007 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
->role
= TASK_UNSPECIFIED
;
359 new_task
->active
= TRUE
;
360 new_task
->user_data
= NULL
;
361 new_task
->faults
= 0;
362 new_task
->cow_faults
= 0;
363 new_task
->pageins
= 0;
364 new_task
->messages_sent
= 0;
365 new_task
->messages_received
= 0;
366 new_task
->syscalls_mach
= 0;
367 new_task
->priv_flags
= 0;
368 new_task
->syscalls_unix
=0;
369 new_task
->c_switch
= new_task
->p_switch
= new_task
->ps_switch
= 0;
370 new_task
->taskFeatures
[0] = 0; /* Init task features */
371 new_task
->taskFeatures
[1] = 0; /* Init task features */
374 new_task
->bsd_info
= NULL
;
375 #endif /* MACH_BSD */
378 new_task
->i386_ldt
= 0;
382 if(BootProcInfo
.pf
.Available
& pf64Bit
) new_task
->taskFeatures
[0] |= tf64BitData
; /* If 64-bit machine, show we have 64-bit registers at least */
385 queue_init(&new_task
->semaphore_list
);
386 queue_init(&new_task
->lock_set_list
);
387 new_task
->semaphores_owned
= 0;
388 new_task
->lock_sets_owned
= 0;
391 /*mutex_init(&new_task->labellock, ETAP_NO_TRACE);*/
392 new_task
->label
= labelh_new(1);
393 mac_task_label_init (&new_task
->maclabel
);
396 ipc_task_init(new_task
, parent_task
);
398 new_task
->total_user_time
= 0;
399 new_task
->total_system_time
= 0;
401 new_task
->vtimers
= 0;
403 new_task
->shared_region
= NULL
;
405 new_task
->affinity_space
= NULL
;
407 if (parent_task
!= TASK_NULL
) {
408 new_task
->sec_token
= parent_task
->sec_token
;
409 new_task
->audit_token
= parent_task
->audit_token
;
411 /* inherit the parent's shared region */
412 shared_region
= vm_shared_region_get(parent_task
);
413 vm_shared_region_set(new_task
, shared_region
);
415 new_task
->wired_ledger_port
= ledger_copy(
416 convert_port_to_ledger(parent_task
->wired_ledger_port
));
417 new_task
->paged_ledger_port
= ledger_copy(
418 convert_port_to_ledger(parent_task
->paged_ledger_port
));
419 if(task_has_64BitAddr(parent_task
))
420 task_set_64BitAddr(new_task
);
423 if (inherit_memory
&& parent_task
->i386_ldt
)
424 new_task
->i386_ldt
= user_ldt_copy(parent_task
->i386_ldt
);
426 if (inherit_memory
&& parent_task
->affinity_space
)
427 task_affinity_create(parent_task
, new_task
);
430 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
431 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
432 new_task
->wired_ledger_port
= ledger_copy(root_wired_ledger
);
433 new_task
->paged_ledger_port
= ledger_copy(root_paged_ledger
);
436 if (kernel_task
== TASK_NULL
) {
437 new_task
->priority
= BASEPRI_KERNEL
;
438 new_task
->max_priority
= MAXPRI_KERNEL
;
441 new_task
->priority
= BASEPRI_DEFAULT
;
442 new_task
->max_priority
= MAXPRI_USER
;
445 mutex_lock(&tasks_threads_lock
);
446 queue_enter(&tasks
, new_task
, task_t
, tasks
);
448 mutex_unlock(&tasks_threads_lock
);
450 if (vm_backing_store_low
&& parent_task
!= NULL
)
451 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
453 ipc_task_enable(new_task
);
455 *child_task
= new_task
;
456 return(KERN_SUCCESS
);
462 * Drop a reference on a task.
468 if (task
== TASK_NULL
)
471 if (task_deallocate_internal(task
) > 0)
474 ipc_task_terminate(task
);
476 if (task
->affinity_space
)
477 task_affinity_deallocate(task
);
479 vm_map_deallocate(task
->map
);
480 is_release(task
->itk_space
);
483 labelh_release(task
->label
);
485 zfree(task_zone
, task
);
489 * task_name_deallocate:
491 * Drop a reference on a task name.
494 task_name_deallocate(
495 task_name_t task_name
)
497 return(task_deallocate((task_t
)task_name
));
504 * Terminate the specified task. See comments on thread_terminate
505 * (kern/thread.c) about problems with terminating the "current task."
512 if (task
== TASK_NULL
)
513 return (KERN_INVALID_ARGUMENT
);
516 return (KERN_FAILURE
);
518 return (task_terminate_internal(task
));
522 task_terminate_internal(
525 thread_t thread
, self
;
527 boolean_t interrupt_save
;
529 assert(task
!= kernel_task
);
531 self
= current_thread();
532 self_task
= self
->task
;
535 * Get the task locked and make sure that we are not racing
536 * with someone else trying to terminate us.
538 if (task
== self_task
)
541 if (task
< self_task
) {
543 task_lock(self_task
);
546 task_lock(self_task
);
550 if (!task
->active
|| !self
->active
) {
552 * Task or current act is already being terminated.
553 * Just return an error. If we are dying, this will
554 * just get us to our AST special handler and that
555 * will get us to finalize the termination of ourselves.
558 if (self_task
!= task
)
559 task_unlock(self_task
);
561 return (KERN_FAILURE
);
564 if (self_task
!= task
)
565 task_unlock(self_task
);
568 * Make sure the current thread does not get aborted out of
569 * the waits inside these operations.
571 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
574 * Indicate that we want all the threads to stop executing
575 * at user space by holding the task (we would have held
576 * each thread independently in thread_terminate_internal -
577 * but this way we may be more likely to already find it
578 * held there). Mark the task inactive, and prevent
579 * further task operations via the task port.
581 task_hold_locked(task
);
582 task
->active
= FALSE
;
583 ipc_task_disable(task
);
586 * Terminate each thread in the task.
588 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
589 thread_terminate_internal(thread
);
593 * Give the machine dependent code a chance
594 * to perform cleanup before ripping apart
597 if (self_task
== task
)
598 machine_thread_terminate_self();
603 * Destroy all synchronizers owned by the task.
605 task_synchronizer_destroy_all(task
);
608 * Destroy the IPC space, leaving just a reference for it.
610 ipc_space_destroy(task
->itk_space
);
613 /* LP64todo - make this clean */
615 * PPC51: ppc64 is limited to 51-bit addresses.
617 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
620 if (vm_map_has_4GB_pagezero(task
->map
))
621 vm_map_clear_4GB_pagezero(task
->map
);
624 * If the current thread is a member of the task
625 * being terminated, then the last reference to
626 * the task will not be dropped until the thread
627 * is finally reaped. To avoid incurring the
628 * expense of removing the address space regions
629 * at reap time, we do it explictly here.
631 vm_map_remove(task
->map
,
632 task
->map
->min_offset
,
633 task
->map
->max_offset
,
636 /* release our shared region */
637 vm_shared_region_set(task
, NULL
);
639 mutex_lock(&tasks_threads_lock
);
640 queue_remove(&tasks
, task
, task_t
, tasks
);
642 mutex_unlock(&tasks_threads_lock
);
645 * We no longer need to guard against being aborted, so restore
646 * the previous interruptible state.
648 thread_interrupt_level(interrupt_save
);
651 perfmon_release_facility(task
); // notify the perfmon facility
655 * Get rid of the task active reference on itself.
657 task_deallocate(task
);
659 return (KERN_SUCCESS
);
665 * Shut the current task down (except for the current thread) in
666 * preparation for dramatic changes to the task (probably exec).
667 * We hold the task, terminate all other threads in the task and
668 * wait for them to terminate, clean up the portspace, and when
669 * all done, let the current thread go.
675 thread_t thread
, self
;
677 assert(task
!= kernel_task
);
679 self
= current_thread();
681 if (task
!= self
->task
)
682 return (KERN_INVALID_ARGUMENT
);
686 if (!task
->active
|| !self
->active
) {
688 * Task or current thread is already being terminated.
689 * Hurry up and return out of the current kernel context
690 * so that we run our AST special handler to terminate
695 return (KERN_FAILURE
);
698 if (task
->thread_count
> 1) {
700 * Mark all the threads to keep them from starting any more
701 * user-level execution. The thread_terminate_internal code
702 * would do this on a thread by thread basis anyway, but this
703 * gives us a better chance of not having to wait there.
705 task_hold_locked(task
);
708 * Terminate all the other threads in the task.
710 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
712 thread_terminate_internal(thread
);
715 task_release_locked(task
);
719 * Give the machine dependent code a chance
720 * to perform cleanup before ripping apart
723 machine_thread_terminate_self();
728 * Destroy all synchronizers owned by the task.
730 task_synchronizer_destroy_all(task
);
733 * Destroy the contents of the IPC space, leaving just
734 * a reference for it.
736 ipc_space_clean(task
->itk_space
);
739 * Clean out the address space, as we are going to be
742 vm_map_remove(task
->map
, task
->map
->min_offset
,
743 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
745 return (KERN_SUCCESS
);
751 * Suspend execution of the specified task.
752 * This is a recursive-style suspension of the task, a count of
753 * suspends is maintained.
755 * CONDITIONS: the task is locked and active.
759 register task_t task
)
761 register thread_t thread
;
763 assert(task
->active
);
765 if (task
->suspend_count
++ > 0)
769 * Iterate through all the threads and hold them.
771 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
772 thread_mtx_lock(thread
);
774 thread_mtx_unlock(thread
);
781 * Same as the internal routine above, except that is must lock
782 * and verify that the task is active. This differs from task_suspend
783 * in that it places a kernel hold on the task rather than just a
784 * user-level hold. This keeps users from over resuming and setting
785 * it running out from under the kernel.
787 * CONDITIONS: the caller holds a reference on the task
791 register task_t task
)
793 if (task
== TASK_NULL
)
794 return (KERN_INVALID_ARGUMENT
);
801 return (KERN_FAILURE
);
804 task_hold_locked(task
);
807 return (KERN_SUCCESS
);
813 * Wait for all threads in task to stop.
816 * Called with task locked, active, and held.
820 register task_t task
)
822 register thread_t thread
, self
;
824 assert(task
->active
);
825 assert(task
->suspend_count
> 0);
827 self
= current_thread();
830 * Iterate through all the threads and wait for them to
831 * stop. Do not wait for the current thread if it is within
834 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
841 * task_release_locked:
843 * Release a kernel hold on a task.
845 * CONDITIONS: the task is locked and active
849 register task_t task
)
851 register thread_t thread
;
853 assert(task
->active
);
854 assert(task
->suspend_count
> 0);
856 if (--task
->suspend_count
> 0)
859 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
860 thread_mtx_lock(thread
);
861 thread_release(thread
);
862 thread_mtx_unlock(thread
);
869 * Same as the internal routine above, except that it must lock
870 * and verify that the task is active.
872 * CONDITIONS: The caller holds a reference to the task
878 if (task
== TASK_NULL
)
879 return (KERN_INVALID_ARGUMENT
);
886 return (KERN_FAILURE
);
889 task_release_locked(task
);
892 return (KERN_SUCCESS
);
898 thread_act_array_t
*threads_out
,
899 mach_msg_type_number_t
*count
)
901 mach_msg_type_number_t actual
;
902 thread_t
*thread_list
;
904 vm_size_t size
, size_needed
;
908 if (task
== TASK_NULL
)
909 return (KERN_INVALID_ARGUMENT
);
911 size
= 0; addr
= NULL
;
921 return (KERN_FAILURE
);
924 actual
= task
->thread_count
;
926 /* do we have the memory we need? */
927 size_needed
= actual
* sizeof (mach_port_t
);
928 if (size_needed
<= size
)
931 /* unlock the task and allocate more memory */
937 assert(size_needed
> 0);
942 return (KERN_RESOURCE_SHORTAGE
);
945 /* OK, have memory and the task is locked & active */
946 thread_list
= (thread_t
*)addr
;
950 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
951 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
952 thread_reference_internal(thread
);
953 thread_list
[j
++] = thread
;
956 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
959 size_needed
= actual
* sizeof (mach_port_t
);
961 /* can unlock task now that we've got the thread refs */
965 /* no threads, so return null pointer and deallocate memory */
974 /* if we allocated too much, must copy */
976 if (size_needed
< size
) {
979 newaddr
= kalloc(size_needed
);
981 for (i
= 0; i
< actual
; ++i
)
982 thread_deallocate(thread_list
[i
]);
984 return (KERN_RESOURCE_SHORTAGE
);
987 bcopy(addr
, newaddr
, size_needed
);
989 thread_list
= (thread_t
*)newaddr
;
992 *threads_out
= thread_list
;
995 /* do the conversion that Mig should handle */
997 for (i
= 0; i
< actual
; ++i
)
998 ((ipc_port_t
*) thread_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
1001 return (KERN_SUCCESS
);
1007 * Implement a user-level suspension on a task.
1010 * The caller holds a reference to the task
1014 register task_t task
)
1016 if (task
== TASK_NULL
|| task
== kernel_task
)
1017 return (KERN_INVALID_ARGUMENT
);
1021 if (!task
->active
) {
1024 return (KERN_FAILURE
);
1027 if (task
->user_stop_count
++ > 0) {
1029 * If the stop count was positive, the task is
1030 * already stopped and we can exit.
1034 return (KERN_SUCCESS
);
1038 * Put a kernel-level hold on the threads in the task (all
1039 * user-level task suspensions added together represent a
1040 * single kernel-level hold). We then wait for the threads
1041 * to stop executing user code.
1043 task_hold_locked(task
);
1044 task_wait_locked(task
);
1048 return (KERN_SUCCESS
);
1053 * Release a kernel hold on a task.
1056 * The caller holds a reference to the task
1060 register task_t task
)
1062 register boolean_t release
= FALSE
;
1064 if (task
== TASK_NULL
|| task
== kernel_task
)
1065 return (KERN_INVALID_ARGUMENT
);
1069 if (!task
->active
) {
1072 return (KERN_FAILURE
);
1075 if (task
->user_stop_count
> 0) {
1076 if (--task
->user_stop_count
== 0)
1082 return (KERN_FAILURE
);
1086 * Release the task if necessary.
1089 task_release_locked(task
);
1093 return (KERN_SUCCESS
);
1097 host_security_set_task_token(
1098 host_security_t host_security
,
1100 security_token_t sec_token
,
1101 audit_token_t audit_token
,
1102 host_priv_t host_priv
)
1104 ipc_port_t host_port
;
1107 if (task
== TASK_NULL
)
1108 return(KERN_INVALID_ARGUMENT
);
1110 if (host_security
== HOST_NULL
)
1111 return(KERN_INVALID_SECURITY
);
1114 task
->sec_token
= sec_token
;
1115 task
->audit_token
= audit_token
;
1118 if (host_priv
!= HOST_PRIV_NULL
) {
1119 kr
= host_get_host_priv_port(host_priv
, &host_port
);
1121 kr
= host_get_host_port(host_priv_self(), &host_port
);
1123 assert(kr
== KERN_SUCCESS
);
1124 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
1129 * Utility routine to set a ledger
1137 if (task
== TASK_NULL
)
1138 return(KERN_INVALID_ARGUMENT
);
1142 ipc_port_release_send(task
->wired_ledger_port
);
1143 task
->wired_ledger_port
= ledger_copy(wired
);
1146 ipc_port_release_send(task
->paged_ledger_port
);
1147 task
->paged_ledger_port
= ledger_copy(paged
);
1151 return(KERN_SUCCESS
);
1155 * This routine was added, pretty much exclusively, for registering the
1156 * RPC glue vector for in-kernel short circuited tasks. Rather than
1157 * removing it completely, I have only disabled that feature (which was
1158 * the only feature at the time). It just appears that we are going to
1159 * want to add some user data to tasks in the future (i.e. bsd info,
1160 * task names, etc...), so I left it in the formal task interface.
1165 task_flavor_t flavor
,
1166 __unused task_info_t task_info_in
, /* pointer to IN array */
1167 __unused mach_msg_type_number_t task_info_count
)
1169 if (task
== TASK_NULL
)
1170 return(KERN_INVALID_ARGUMENT
);
1174 return (KERN_INVALID_ARGUMENT
);
1176 return (KERN_SUCCESS
);
1182 task_flavor_t flavor
,
1183 task_info_t task_info_out
,
1184 mach_msg_type_number_t
*task_info_count
)
1186 if (task
== TASK_NULL
)
1187 return (KERN_INVALID_ARGUMENT
);
1191 case TASK_BASIC_INFO_32
:
1192 case TASK_BASIC2_INFO_32
:
1194 task_basic_info_32_t basic_info
;
1197 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
)
1198 return (KERN_INVALID_ARGUMENT
);
1200 basic_info
= (task_basic_info_32_t
)task_info_out
;
1202 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1203 basic_info
->virtual_size
= CAST_DOWN(vm_offset_t
,map
->size
);
1204 if (flavor
== TASK_BASIC2_INFO_32
) {
1206 * The "BASIC2" flavor gets the maximum resident
1207 * size instead of the current resident size...
1209 basic_info
->resident_size
= pmap_resident_max(map
->pmap
);
1211 basic_info
->resident_size
= pmap_resident_count(map
->pmap
);
1213 basic_info
->resident_size
*= PAGE_SIZE
;
1216 basic_info
->policy
= ((task
!= kernel_task
)?
1217 POLICY_TIMESHARE
: POLICY_RR
);
1218 basic_info
->suspend_count
= task
->user_stop_count
;
1220 absolutetime_to_microtime(task
->total_user_time
,
1221 (unsigned *)&basic_info
->user_time
.seconds
,
1222 (unsigned *)&basic_info
->user_time
.microseconds
);
1223 absolutetime_to_microtime(task
->total_system_time
,
1224 (unsigned *)&basic_info
->system_time
.seconds
,
1225 (unsigned *)&basic_info
->system_time
.microseconds
);
1228 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
1232 case TASK_BASIC_INFO_64
:
1234 task_basic_info_64_t basic_info
;
1237 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
)
1238 return (KERN_INVALID_ARGUMENT
);
1240 basic_info
= (task_basic_info_64_t
)task_info_out
;
1242 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1243 basic_info
->virtual_size
= map
->size
;
1244 basic_info
->resident_size
=
1245 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
1249 basic_info
->policy
= ((task
!= kernel_task
)?
1250 POLICY_TIMESHARE
: POLICY_RR
);
1251 basic_info
->suspend_count
= task
->user_stop_count
;
1253 absolutetime_to_microtime(task
->total_user_time
,
1254 (unsigned *)&basic_info
->user_time
.seconds
,
1255 (unsigned *)&basic_info
->user_time
.microseconds
);
1256 absolutetime_to_microtime(task
->total_system_time
,
1257 (unsigned *)&basic_info
->system_time
.seconds
,
1258 (unsigned *)&basic_info
->system_time
.microseconds
);
1261 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
1265 case TASK_THREAD_TIMES_INFO
:
1267 register task_thread_times_info_t times_info
;
1268 register thread_t thread
;
1270 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
)
1271 return (KERN_INVALID_ARGUMENT
);
1273 times_info
= (task_thread_times_info_t
) task_info_out
;
1274 times_info
->user_time
.seconds
= 0;
1275 times_info
->user_time
.microseconds
= 0;
1276 times_info
->system_time
.seconds
= 0;
1277 times_info
->system_time
.microseconds
= 0;
1281 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1282 time_value_t user_time
, system_time
;
1284 thread_read_times(thread
, &user_time
, &system_time
);
1286 time_value_add(×_info
->user_time
, &user_time
);
1287 time_value_add(×_info
->system_time
, &system_time
);
1292 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1296 case TASK_ABSOLUTETIME_INFO
:
1298 task_absolutetime_info_t info
;
1299 register thread_t thread
;
1301 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
)
1302 return (KERN_INVALID_ARGUMENT
);
1304 info
= (task_absolutetime_info_t
)task_info_out
;
1305 info
->threads_user
= info
->threads_system
= 0;
1309 info
->total_user
= task
->total_user_time
;
1310 info
->total_system
= task
->total_system_time
;
1312 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1315 tval
= timer_grab(&thread
->user_timer
);
1316 info
->threads_user
+= tval
;
1317 info
->total_user
+= tval
;
1319 tval
= timer_grab(&thread
->system_timer
);
1320 info
->threads_system
+= tval
;
1321 info
->total_system
+= tval
;
1326 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
1331 case TASK_SCHED_FIFO_INFO
:
1334 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
)
1335 return (KERN_INVALID_ARGUMENT
);
1337 return (KERN_INVALID_POLICY
);
1341 case TASK_SCHED_RR_INFO
:
1343 register policy_rr_base_t rr_base
;
1345 if (*task_info_count
< POLICY_RR_BASE_COUNT
)
1346 return (KERN_INVALID_ARGUMENT
);
1348 rr_base
= (policy_rr_base_t
) task_info_out
;
1351 if (task
!= kernel_task
) {
1353 return (KERN_INVALID_POLICY
);
1356 rr_base
->base_priority
= task
->priority
;
1359 rr_base
->quantum
= std_quantum_us
/ 1000;
1361 *task_info_count
= POLICY_RR_BASE_COUNT
;
1366 case TASK_SCHED_TIMESHARE_INFO
:
1368 register policy_timeshare_base_t ts_base
;
1370 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
)
1371 return (KERN_INVALID_ARGUMENT
);
1373 ts_base
= (policy_timeshare_base_t
) task_info_out
;
1376 if (task
== kernel_task
) {
1378 return (KERN_INVALID_POLICY
);
1381 ts_base
->base_priority
= task
->priority
;
1384 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
1388 case TASK_SECURITY_TOKEN
:
1390 register security_token_t
*sec_token_p
;
1392 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
)
1393 return (KERN_INVALID_ARGUMENT
);
1395 sec_token_p
= (security_token_t
*) task_info_out
;
1398 *sec_token_p
= task
->sec_token
;
1401 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
1405 case TASK_AUDIT_TOKEN
:
1407 register audit_token_t
*audit_token_p
;
1409 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
)
1410 return (KERN_INVALID_ARGUMENT
);
1412 audit_token_p
= (audit_token_t
*) task_info_out
;
1415 *audit_token_p
= task
->audit_token
;
1418 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
1422 case TASK_SCHED_INFO
:
1423 return (KERN_INVALID_ARGUMENT
);
1425 case TASK_EVENTS_INFO
:
1427 register task_events_info_t events_info
;
1428 register thread_t thread
;
1430 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
)
1431 return (KERN_INVALID_ARGUMENT
);
1433 events_info
= (task_events_info_t
) task_info_out
;
1437 events_info
->faults
= task
->faults
;
1438 events_info
->pageins
= task
->pageins
;
1439 events_info
->cow_faults
= task
->cow_faults
;
1440 events_info
->messages_sent
= task
->messages_sent
;
1441 events_info
->messages_received
= task
->messages_received
;
1442 events_info
->syscalls_mach
= task
->syscalls_mach
;
1443 events_info
->syscalls_unix
= task
->syscalls_unix
;
1445 events_info
->csw
= task
->c_switch
;
1447 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1448 events_info
->csw
+= thread
->c_switch
;
1453 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
1456 case TASK_AFFINITY_TAG_INFO
:
1458 if (*task_info_count
< TASK_AFFINITY_TAG_INFO_COUNT
)
1459 return (KERN_INVALID_ARGUMENT
);
1461 return task_affinity_info(task
, task_info_out
, task_info_count
);
1465 return (KERN_INVALID_ARGUMENT
);
1468 return (KERN_SUCCESS
);
1478 /* assert(task == current_task()); */ /* bogus assert 4803227 4807483 */
1482 task
->vtimers
|= which
;
1486 case TASK_VTIMER_USER
:
1487 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1488 thread
->vtimer_user_save
= timer_grab(&thread
->user_timer
);
1492 case TASK_VTIMER_PROF
:
1493 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1494 thread
->vtimer_prof_save
= timer_grab(&thread
->user_timer
);
1495 thread
->vtimer_prof_save
+= timer_grab(&thread
->system_timer
);
1499 case TASK_VTIMER_RLIM
:
1500 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1501 thread
->vtimer_rlim_save
= timer_grab(&thread
->user_timer
);
1502 thread
->vtimer_rlim_save
+= timer_grab(&thread
->system_timer
);
1515 assert(task
== current_task());
1519 task
->vtimers
&= ~which
;
1529 uint32_t *microsecs
)
1531 thread_t thread
= current_thread();
1532 uint32_t tdelt
, secs
;
1535 assert(task
== current_task());
1537 assert(task
->vtimers
& which
);
1543 case TASK_VTIMER_USER
:
1544 tdelt
= timer_delta(&thread
->user_timer
,
1545 &thread
->vtimer_user_save
);
1548 case TASK_VTIMER_PROF
:
1549 tsum
= timer_grab(&thread
->user_timer
);
1550 tsum
+= timer_grab(&thread
->system_timer
);
1551 tdelt
= tsum
- thread
->vtimer_prof_save
;
1552 thread
->vtimer_prof_save
= tsum
;
1555 case TASK_VTIMER_RLIM
:
1556 tsum
= timer_grab(&thread
->user_timer
);
1557 tsum
+= timer_grab(&thread
->system_timer
);
1558 tdelt
= tsum
- thread
->vtimer_rlim_save
;
1559 thread
->vtimer_rlim_save
= tsum
;
1563 absolutetime_to_microtime(tdelt
, &secs
, microsecs
);
1569 * Change the assigned processor set for the task
1573 __unused task_t task
,
1574 __unused processor_set_t new_pset
,
1575 __unused boolean_t assign_threads
)
1577 return(KERN_FAILURE
);
1581 * task_assign_default:
1583 * Version of task_assign to assign to default processor set.
1586 task_assign_default(
1588 boolean_t assign_threads
)
1590 return (task_assign(task
, &pset0
, assign_threads
));
1594 * task_get_assignment
1596 * Return name of processor set that task is assigned to.
1599 task_get_assignment(
1601 processor_set_t
*pset
)
1604 return(KERN_FAILURE
);
1608 return (KERN_SUCCESS
);
1615 * Set scheduling policy and parameters, both base and limit, for
1616 * the given task. Policy must be a policy which is enabled for the
1617 * processor set. Change contained threads if requested.
1621 __unused task_t task
,
1622 __unused policy_t policy_id
,
1623 __unused policy_base_t base
,
1624 __unused mach_msg_type_number_t count
,
1625 __unused boolean_t set_limit
,
1626 __unused boolean_t change
)
1628 return(KERN_FAILURE
);
1634 * Set scheduling policy and parameters, both base and limit, for
1635 * the given task. Policy can be any policy implemented by the
1636 * processor set, whether enabled or not. Change contained threads
1641 __unused task_t task
,
1642 __unused processor_set_t pset
,
1643 __unused policy_t policy_id
,
1644 __unused policy_base_t base
,
1645 __unused mach_msg_type_number_t base_count
,
1646 __unused policy_limit_t limit
,
1647 __unused mach_msg_type_number_t limit_count
,
1648 __unused boolean_t change
)
1650 return(KERN_FAILURE
);
1660 extern int fast_tas_debug
;
1662 if (fast_tas_debug
) {
1663 printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
1667 task
->fast_tas_base
= pc
;
1668 task
->fast_tas_end
= endpc
;
1670 return KERN_SUCCESS
;
1672 #else /* FAST_TAS */
1675 __unused task_t task
,
1676 __unused vm_offset_t pc
,
1677 __unused vm_offset_t endpc
)
1679 return KERN_FAILURE
;
1681 #endif /* FAST_TAS */
1684 task_synchronizer_destroy_all(task_t task
)
1686 semaphore_t semaphore
;
1687 lock_set_t lock_set
;
1690 * Destroy owned semaphores
1693 while (!queue_empty(&task
->semaphore_list
)) {
1694 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
1695 (void) semaphore_destroy(task
, semaphore
);
1699 * Destroy owned lock sets
1702 while (!queue_empty(&task
->lock_set_list
)) {
1703 lock_set
= (lock_set_t
) queue_first(&task
->lock_set_list
);
1704 (void) lock_set_destroy(task
, lock_set
);
1709 * We need to export some functions to other components that
1710 * are currently implemented in macros within the osfmk
1711 * component. Just export them as functions of the same name.
1713 boolean_t
is_kerneltask(task_t t
)
1715 if (t
== kernel_task
)
1722 task_t
current_task(void);
1723 task_t
current_task(void)
1725 return (current_task_fast());
1728 #undef task_reference
1729 void task_reference(task_t task
);
1734 if (task
!= TASK_NULL
)
1735 task_reference_internal(task
);
1738 #if CONFIG_MACF_MACH
1740 * Protect 2 task labels against modification by adding a reference on
1741 * both label handles. The locks do not actually have to be held while
1742 * using the labels as only labels with one reference can be modified
1751 labelh_reference(a
->label
);
1752 labelh_reference(b
->label
);
1760 labelh_release(a
->label
);
1761 labelh_release(b
->label
);
1765 mac_task_label_update_internal(
1770 tasklabel_lock(task
);
1771 task
->label
= labelh_modify(task
->label
);
1772 mac_task_label_update(pl
, &task
->maclabel
);
1773 tasklabel_unlock(task
);
1774 ip_lock(task
->itk_self
);
1775 mac_port_label_update_cred(pl
, &task
->itk_self
->ip_label
);
1776 ip_unlock(task
->itk_self
);
1780 mac_task_label_modify(
1783 void (*f
) (struct label
*l
, void *arg
))
1786 tasklabel_lock(task
);
1787 task
->label
= labelh_modify(task
->label
);
1788 (*f
)(&task
->maclabel
, arg
);
1789 tasklabel_unlock(task
);
1793 mac_task_get_label(struct task
*task
)
1795 return (&task
->maclabel
);