2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
58 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub,
61 * Task management primitives implementation.
64 * Copyright (c) 1993 The University of Utah and
65 * the Computer Systems Laboratory (CSL). All rights reserved.
67 * Permission to use, copy, modify and distribute this software and its
68 * documentation is hereby granted, provided that both the copyright
69 * notice and this permission notice appear in all copies of the
70 * software, derivative works or modified versions, and any portions
71 * thereof, and that both notices appear in supporting documentation.
73 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
74 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
75 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
77 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
78 * improvements that they make and grant CSL redistribution rights.
83 #include <mach_host.h>
84 #include <mach_prof.h>
86 #include <platforms.h>
88 #include <mach/mach_types.h>
89 #include <mach/boolean.h>
90 #include <mach/host_priv.h>
91 #include <mach/machine/vm_types.h>
92 #include <mach/vm_param.h>
93 #include <mach/semaphore.h>
94 #include <mach/task_info.h>
95 #include <mach/task_special_ports.h>
97 #include <ipc/ipc_types.h>
98 #include <ipc/ipc_space.h>
99 #include <ipc/ipc_entry.h>
101 #include <kern/kern_types.h>
102 #include <kern/mach_param.h>
103 #include <kern/misc_protos.h>
104 #include <kern/task.h>
105 #include <kern/thread.h>
106 #include <kern/zalloc.h>
107 #include <kern/kalloc.h>
108 #include <kern/processor.h>
109 #include <kern/sched_prim.h> /* for thread_wakeup */
110 #include <kern/ipc_tt.h>
111 #include <kern/ledger.h>
112 #include <kern/host.h>
113 #include <kern/clock.h>
114 #include <kern/timer.h>
115 #include <kern/profile.h>
116 #include <kern/assert.h>
117 #include <kern/sync_lock.h>
120 #include <vm/vm_map.h>
121 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
122 #include <vm/vm_pageout.h>
123 #include <vm/vm_protos.h> /* for vm_map_remove_commpage */
126 #include <ddb/db_sym.h>
127 #endif /* MACH_KDB */
130 #include <ppc/exception.h>
131 #include <ppc/hw_perfmon.h>
135 * Exported interfaces
138 #include <mach/task_server.h>
139 #include <mach/mach_host_server.h>
140 #include <mach/host_security_server.h>
141 #include <mach/mach_port_server.h>
143 #include <vm/task_working_set.h>
144 #include <vm/vm_shared_memory_server.h>
151 void task_hold_locked(
153 void task_wait_locked(
155 void task_release_locked(
159 void task_synchronizer_destroy_all(
162 kern_return_t
task_set_ledger(
168 task_backing_store_privileged(
172 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
178 task_working_set_disable(task_t task
)
183 ws
= task
->dynamic_working_set
;
184 task
->dynamic_working_set
= NULL
;
187 tws_hash_ws_flush(ws
);
188 tws_hash_destroy(ws
);
200 if (task_has_64BitAddr(task
))
203 /* LP64todo - no task working set for 64-bit */
204 task_set_64BitAddr(task
);
205 task_working_set_disable(task
);
207 if ( !task_has_64BitAddr(task
))
211 * Deallocate all memory previously allocated
212 * above the 32-bit address space, since it won't
213 * be accessible anymore.
215 /* LP64todo - make this clean */
216 vm_map_remove_commpage(task
->map
);
217 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
218 (void) vm_map_remove(task
->map
,
219 (vm_map_offset_t
) VM_MAX_ADDRESS
,
222 task_clear_64BitAddr(task
);
224 /* FIXME: On x86, the thread save state flavor can diverge from the
225 * task's 64-bit feature flag due to the 32-bit/64-bit register save
226 * state dichotomy. Since we can be pre-empted in this interval,
227 * certain routines may observe the thread as being in an inconsistent
228 * state with respect to its task's 64-bitness.
231 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
232 machine_thread_switch_addrmode(thread
, !is64bit
);
242 TASK_MAX
* sizeof(struct task
),
243 TASK_CHUNK
* sizeof(struct task
),
247 * Create the kernel task as the first task.
249 if (task_create_internal(TASK_NULL
, FALSE
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
250 panic("task_init\n");
252 vm_map_deallocate(kernel_task
->map
);
253 kernel_task
->map
= kernel_map
;
265 * If may_assign is false, task is already being assigned,
266 * wait for that to finish.
268 while (task
->may_assign
== FALSE
) {
271 task
->assign_active
= TRUE
;
272 res
= thread_sleep_mutex((event_t
) &task
->assign_active
,
273 &task
->lock
, THREAD_UNINT
);
274 assert(res
== THREAD_AWAKENED
);
276 task
->may_assign
= FALSE
;
281 #define thread_freeze(thread) assert(task->processor_set == &default_pset)
290 assert(task
->may_assign
== FALSE
);
291 task
->may_assign
= TRUE
;
292 if (task
->assign_active
== TRUE
) {
293 task
->assign_active
= FALSE
;
294 thread_wakeup((event_t
)&task
->assign_active
);
300 #define thread_unfreeze(thread) assert(task->processor_set == &default_pset)
303 #endif /* MACH_HOST */
306 * Create a task running in the kernel address space. It may
307 * have its own map of size mem_size and may have ipc privileges.
311 __unused task_t parent_task
,
312 __unused vm_offset_t map_base
,
313 __unused vm_size_t map_size
,
314 __unused task_t
*child_task
)
316 return (KERN_INVALID_ARGUMENT
);
322 __unused ledger_port_array_t ledger_ports
,
323 __unused mach_msg_type_number_t num_ledger_ports
,
324 boolean_t inherit_memory
,
325 task_t
*child_task
) /* OUT */
327 if (parent_task
== TASK_NULL
)
328 return(KERN_INVALID_ARGUMENT
);
330 return task_create_internal(
331 parent_task
, inherit_memory
, task_has_64BitAddr(parent_task
), child_task
);
335 host_security_create_task_token(
336 host_security_t host_security
,
338 security_token_t sec_token
,
339 audit_token_t audit_token
,
340 host_priv_t host_priv
,
341 __unused ledger_port_array_t ledger_ports
,
342 __unused mach_msg_type_number_t num_ledger_ports
,
343 boolean_t inherit_memory
,
344 task_t
*child_task
) /* OUT */
346 kern_return_t result
;
348 if (parent_task
== TASK_NULL
)
349 return(KERN_INVALID_ARGUMENT
);
351 if (host_security
== HOST_NULL
)
352 return(KERN_INVALID_SECURITY
);
354 result
= task_create_internal(
355 parent_task
, inherit_memory
, task_has_64BitAddr(parent_task
), child_task
);
357 if (result
!= KERN_SUCCESS
)
360 result
= host_security_set_task_token(host_security
,
366 if (result
!= KERN_SUCCESS
)
373 task_create_internal(
375 boolean_t inherit_memory
,
377 task_t
*child_task
) /* OUT */
380 processor_set_t pset
;
382 new_task
= (task_t
) zalloc(task_zone
);
384 if (new_task
== TASK_NULL
)
385 return(KERN_RESOURCE_SHORTAGE
);
387 /* one ref for just being alive; one for our caller */
388 new_task
->ref_count
= 2;
391 new_task
->map
= vm_map_fork(parent_task
->map
);
393 new_task
->map
= vm_map_create(pmap_create(0, is_64bit
),
394 (vm_map_offset_t
)(VM_MIN_ADDRESS
),
395 (vm_map_offset_t
)(VM_MAX_ADDRESS
), TRUE
);
397 mutex_init(&new_task
->lock
, 0);
398 queue_init(&new_task
->threads
);
399 new_task
->suspend_count
= 0;
400 new_task
->thread_count
= 0;
401 new_task
->active_thread_count
= 0;
402 new_task
->user_stop_count
= 0;
403 new_task
->role
= TASK_UNSPECIFIED
;
404 new_task
->active
= TRUE
;
405 new_task
->user_data
= 0;
406 new_task
->faults
= 0;
407 new_task
->cow_faults
= 0;
408 new_task
->pageins
= 0;
409 new_task
->messages_sent
= 0;
410 new_task
->messages_received
= 0;
411 new_task
->syscalls_mach
= 0;
412 new_task
->priv_flags
= 0;
413 new_task
->syscalls_unix
=0;
415 new_task
->taskFeatures
[0] = 0; /* Init task features */
416 new_task
->taskFeatures
[1] = 0; /* Init task features */
417 new_task
->dynamic_working_set
= 0;
419 task_working_set_create(new_task
, TWS_SMALL_HASH_LINE_COUNT
,
420 0, TWS_HASH_STYLE_DEFAULT
);
423 new_task
->bsd_info
= 0;
424 #endif /* MACH_BSD */
427 new_task
->i386_ldt
= 0;
431 if(BootProcInfo
.pf
.Available
& pf64Bit
) new_task
->taskFeatures
[0] |= tf64BitData
; /* If 64-bit machine, show we have 64-bit registers at least */
434 queue_init(&new_task
->semaphore_list
);
435 queue_init(&new_task
->lock_set_list
);
436 new_task
->semaphores_owned
= 0;
437 new_task
->lock_sets_owned
= 0;
440 new_task
->may_assign
= TRUE
;
441 new_task
->assign_active
= FALSE
;
442 #endif /* MACH_HOST */
444 ipc_task_init(new_task
, parent_task
);
446 new_task
->total_user_time
= 0;
447 new_task
->total_system_time
= 0;
449 task_prof_init(new_task
);
451 if (parent_task
!= TASK_NULL
) {
454 * Freeze the parent, so that parent_task->processor_set
457 task_freeze(parent_task
);
458 #endif /* MACH_HOST */
459 pset
= parent_task
->processor_set
;
461 pset
= &default_pset
;
463 new_task
->sec_token
= parent_task
->sec_token
;
464 new_task
->audit_token
= parent_task
->audit_token
;
466 shared_region_mapping_ref(parent_task
->system_shared_region
);
467 new_task
->system_shared_region
= parent_task
->system_shared_region
;
469 new_task
->wired_ledger_port
= ledger_copy(
470 convert_port_to_ledger(parent_task
->wired_ledger_port
));
471 new_task
->paged_ledger_port
= ledger_copy(
472 convert_port_to_ledger(parent_task
->paged_ledger_port
));
473 if(task_has_64BitAddr(parent_task
))
474 task_set_64BitAddr(new_task
);
477 if (inherit_memory
&& parent_task
->i386_ldt
)
478 new_task
->i386_ldt
= user_ldt_copy(parent_task
->i386_ldt
);
482 pset
= &default_pset
;
484 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
485 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
486 new_task
->wired_ledger_port
= ledger_copy(root_wired_ledger
);
487 new_task
->paged_ledger_port
= ledger_copy(root_paged_ledger
);
490 if (kernel_task
== TASK_NULL
) {
491 new_task
->priority
= BASEPRI_KERNEL
;
492 new_task
->max_priority
= MAXPRI_KERNEL
;
495 new_task
->priority
= BASEPRI_DEFAULT
;
496 new_task
->max_priority
= MAXPRI_USER
;
500 pset_add_task(pset
, new_task
);
503 if (parent_task
!= TASK_NULL
)
504 task_unfreeze(parent_task
);
505 #endif /* MACH_HOST */
507 if (vm_backing_store_low
&& parent_task
!= NULL
)
508 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
510 ipc_task_enable(new_task
);
512 *child_task
= new_task
;
513 return(KERN_SUCCESS
);
519 * Drop a reference on a task.
525 processor_set_t pset
;
527 if (task
== TASK_NULL
)
530 if (task_deallocate_internal(task
) > 0)
533 pset
= task
->processor_set
;
534 pset_deallocate(pset
);
536 if(task
->dynamic_working_set
)
537 tws_hash_destroy(task
->dynamic_working_set
);
539 ipc_task_terminate(task
);
541 vm_map_deallocate(task
->map
);
542 is_release(task
->itk_space
);
544 task_prof_deallocate(task
);
545 zfree(task_zone
, task
);
549 * task_name_deallocate:
551 * Drop a reference on a task name.
554 task_name_deallocate(
555 task_name_t task_name
)
557 return(task_deallocate((task_t
)task_name
));
564 * Terminate the specified task. See comments on thread_terminate
565 * (kern/thread.c) about problems with terminating the "current task."
572 if (task
== TASK_NULL
)
573 return (KERN_INVALID_ARGUMENT
);
576 return (KERN_FAILURE
);
578 return (task_terminate_internal(task
));
582 task_terminate_internal(
585 processor_set_t pset
;
586 thread_t thread
, self
;
588 boolean_t interrupt_save
;
590 assert(task
!= kernel_task
);
592 self
= current_thread();
593 self_task
= self
->task
;
596 * Get the task locked and make sure that we are not racing
597 * with someone else trying to terminate us.
599 if (task
== self_task
)
602 if (task
< self_task
) {
604 task_lock(self_task
);
607 task_lock(self_task
);
611 if (!task
->active
|| !self
->active
) {
613 * Task or current act is already being terminated.
614 * Just return an error. If we are dying, this will
615 * just get us to our AST special handler and that
616 * will get us to finalize the termination of ourselves.
619 if (self_task
!= task
)
620 task_unlock(self_task
);
622 return (KERN_FAILURE
);
625 if (self_task
!= task
)
626 task_unlock(self_task
);
629 * Make sure the current thread does not get aborted out of
630 * the waits inside these operations.
632 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
635 * Indicate that we want all the threads to stop executing
636 * at user space by holding the task (we would have held
637 * each thread independently in thread_terminate_internal -
638 * but this way we may be more likely to already find it
639 * held there). Mark the task inactive, and prevent
640 * further task operations via the task port.
642 task_hold_locked(task
);
643 task
->active
= FALSE
;
644 ipc_task_disable(task
);
647 * Terminate each thread in the task.
649 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
650 thread_terminate_internal(thread
);
654 * Give the machine dependent code a chance
655 * to perform cleanup before ripping apart
658 if (self_task
== task
)
659 machine_thread_terminate_self();
664 * Destroy all synchronizers owned by the task.
666 task_synchronizer_destroy_all(task
);
669 * Destroy the IPC space, leaving just a reference for it.
671 ipc_space_destroy(task
->itk_space
);
673 /* LP64todo - make this clean */
674 vm_map_remove_commpage(task
->map
);
675 pmap_unmap_sharedpage(task
->map
->pmap
); /* Unmap commpage */
677 if (vm_map_has_4GB_pagezero(task
->map
))
678 vm_map_clear_4GB_pagezero(task
->map
);
681 * If the current thread is a member of the task
682 * being terminated, then the last reference to
683 * the task will not be dropped until the thread
684 * is finally reaped. To avoid incurring the
685 * expense of removing the address space regions
686 * at reap time, we do it explictly here.
688 vm_map_remove(task
->map
, task
->map
->min_offset
,
689 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
691 shared_region_mapping_dealloc(task
->system_shared_region
);
694 * Flush working set here to avoid I/O in reaper thread
696 if (task
->dynamic_working_set
)
697 tws_hash_ws_flush(task
->dynamic_working_set
);
699 pset
= task
->processor_set
;
701 pset_remove_task(pset
,task
);
705 * We no longer need to guard against being aborted, so restore
706 * the previous interruptible state.
708 thread_interrupt_level(interrupt_save
);
711 perfmon_release_facility(task
); // notify the perfmon facility
715 * Get rid of the task active reference on itself.
717 task_deallocate(task
);
719 return (KERN_SUCCESS
);
725 * Shut the current task down (except for the current thread) in
726 * preparation for dramatic changes to the task (probably exec).
727 * We hold the task, terminate all other threads in the task and
728 * wait for them to terminate, clean up the portspace, and when
729 * all done, let the current thread go.
735 thread_t thread
, self
;
737 assert(task
!= kernel_task
);
739 self
= current_thread();
741 if (task
!= self
->task
)
742 return (KERN_INVALID_ARGUMENT
);
746 if (!task
->active
|| !self
->active
) {
748 * Task or current thread is already being terminated.
749 * Hurry up and return out of the current kernel context
750 * so that we run our AST special handler to terminate
755 return (KERN_FAILURE
);
758 if (task
->thread_count
> 1) {
760 * Mark all the threads to keep them from starting any more
761 * user-level execution. The thread_terminate_internal code
762 * would do this on a thread by thread basis anyway, but this
763 * gives us a better chance of not having to wait there.
765 task_hold_locked(task
);
768 * Terminate all the other threads in the task.
770 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
772 thread_terminate_internal(thread
);
775 task_release_locked(task
);
779 * Give the machine dependent code a chance
780 * to perform cleanup before ripping apart
783 machine_thread_terminate_self();
788 * Destroy all synchronizers owned by the task.
790 task_synchronizer_destroy_all(task
);
793 * Destroy the contents of the IPC space, leaving just
794 * a reference for it.
796 ipc_space_clean(task
->itk_space
);
799 * Clean out the address space, as we are going to be
802 vm_map_remove(task
->map
, task
->map
->min_offset
,
803 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
805 return (KERN_SUCCESS
);
811 * Suspend execution of the specified task.
812 * This is a recursive-style suspension of the task, a count of
813 * suspends is maintained.
815 * CONDITIONS: the task is locked and active.
819 register task_t task
)
821 register thread_t thread
;
823 assert(task
->active
);
825 if (task
->suspend_count
++ > 0)
829 * Iterate through all the threads and hold them.
831 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
832 thread_mtx_lock(thread
);
834 thread_mtx_unlock(thread
);
841 * Same as the internal routine above, except that is must lock
842 * and verify that the task is active. This differs from task_suspend
843 * in that it places a kernel hold on the task rather than just a
844 * user-level hold. This keeps users from over resuming and setting
845 * it running out from under the kernel.
847 * CONDITIONS: the caller holds a reference on the task
851 register task_t task
)
853 if (task
== TASK_NULL
)
854 return (KERN_INVALID_ARGUMENT
);
861 return (KERN_FAILURE
);
864 task_hold_locked(task
);
867 return (KERN_SUCCESS
);
873 * Wait for all threads in task to stop.
876 * Called with task locked, active, and held.
880 register task_t task
)
882 register thread_t thread
, self
;
884 assert(task
->active
);
885 assert(task
->suspend_count
> 0);
887 self
= current_thread();
890 * Iterate through all the threads and wait for them to
891 * stop. Do not wait for the current thread if it is within
894 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
901 * task_release_locked:
903 * Release a kernel hold on a task.
905 * CONDITIONS: the task is locked and active
909 register task_t task
)
911 register thread_t thread
;
913 assert(task
->active
);
914 assert(task
->suspend_count
> 0);
916 if (--task
->suspend_count
> 0)
919 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
920 thread_mtx_lock(thread
);
921 thread_release(thread
);
922 thread_mtx_unlock(thread
);
929 * Same as the internal routine above, except that it must lock
930 * and verify that the task is active.
932 * CONDITIONS: The caller holds a reference to the task
938 if (task
== TASK_NULL
)
939 return (KERN_INVALID_ARGUMENT
);
946 return (KERN_FAILURE
);
949 task_release_locked(task
);
952 return (KERN_SUCCESS
);
958 thread_act_array_t
*threads_out
,
959 mach_msg_type_number_t
*count
)
961 mach_msg_type_number_t actual
;
964 vm_size_t size
, size_needed
;
968 if (task
== TASK_NULL
)
969 return (KERN_INVALID_ARGUMENT
);
981 return (KERN_FAILURE
);
984 actual
= task
->thread_count
;
986 /* do we have the memory we need? */
987 size_needed
= actual
* sizeof (mach_port_t
);
988 if (size_needed
<= size
)
991 /* unlock the task and allocate more memory */
997 assert(size_needed
> 0);
1000 addr
= kalloc(size
);
1002 return (KERN_RESOURCE_SHORTAGE
);
1005 /* OK, have memory and the task is locked & active */
1006 threads
= (thread_t
*)addr
;
1010 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
1011 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
1012 thread_reference_internal(thread
);
1013 threads
[j
++] = thread
;
1016 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
1019 size_needed
= actual
* sizeof (mach_port_t
);
1021 /* can unlock task now that we've got the thread refs */
1025 /* no threads, so return null pointer and deallocate memory */
1034 /* if we allocated too much, must copy */
1036 if (size_needed
< size
) {
1039 newaddr
= kalloc(size_needed
);
1041 for (i
= 0; i
< actual
; ++i
)
1042 thread_deallocate(threads
[i
]);
1044 return (KERN_RESOURCE_SHORTAGE
);
1047 bcopy(addr
, newaddr
, size_needed
);
1049 threads
= (thread_t
*)newaddr
;
1052 *threads_out
= threads
;
1055 /* do the conversion that Mig should handle */
1057 for (i
= 0; i
< actual
; ++i
)
1058 ((ipc_port_t
*) threads
)[i
] = convert_thread_to_port(threads
[i
]);
1061 return (KERN_SUCCESS
);
1067 * Implement a user-level suspension on a task.
1070 * The caller holds a reference to the task
1074 register task_t task
)
1076 if (task
== TASK_NULL
|| task
== kernel_task
)
1077 return (KERN_INVALID_ARGUMENT
);
1081 if (!task
->active
) {
1084 return (KERN_FAILURE
);
1087 if (task
->user_stop_count
++ > 0) {
1089 * If the stop count was positive, the task is
1090 * already stopped and we can exit.
1094 return (KERN_SUCCESS
);
1098 * Put a kernel-level hold on the threads in the task (all
1099 * user-level task suspensions added together represent a
1100 * single kernel-level hold). We then wait for the threads
1101 * to stop executing user code.
1103 task_hold_locked(task
);
1104 task_wait_locked(task
);
1108 return (KERN_SUCCESS
);
1113 * Release a kernel hold on a task.
1116 * The caller holds a reference to the task
1120 register task_t task
)
1122 register boolean_t release
= FALSE
;
1124 if (task
== TASK_NULL
|| task
== kernel_task
)
1125 return (KERN_INVALID_ARGUMENT
);
1129 if (!task
->active
) {
1132 return (KERN_FAILURE
);
1135 if (task
->user_stop_count
> 0) {
1136 if (--task
->user_stop_count
== 0)
1142 return (KERN_FAILURE
);
1146 * Release the task if necessary.
1149 task_release_locked(task
);
1153 return (KERN_SUCCESS
);
1157 host_security_set_task_token(
1158 host_security_t host_security
,
1160 security_token_t sec_token
,
1161 audit_token_t audit_token
,
1162 host_priv_t host_priv
)
1164 ipc_port_t host_port
;
1167 if (task
== TASK_NULL
)
1168 return(KERN_INVALID_ARGUMENT
);
1170 if (host_security
== HOST_NULL
)
1171 return(KERN_INVALID_SECURITY
);
1174 task
->sec_token
= sec_token
;
1175 task
->audit_token
= audit_token
;
1178 if (host_priv
!= HOST_PRIV_NULL
) {
1179 kr
= host_get_host_priv_port(host_priv
, &host_port
);
1181 kr
= host_get_host_port(host_priv_self(), &host_port
);
1183 assert(kr
== KERN_SUCCESS
);
1184 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
1189 * Utility routine to set a ledger
1197 if (task
== TASK_NULL
)
1198 return(KERN_INVALID_ARGUMENT
);
1202 ipc_port_release_send(task
->wired_ledger_port
);
1203 task
->wired_ledger_port
= ledger_copy(wired
);
1206 ipc_port_release_send(task
->paged_ledger_port
);
1207 task
->paged_ledger_port
= ledger_copy(paged
);
1211 return(KERN_SUCCESS
);
1215 * This routine was added, pretty much exclusively, for registering the
1216 * RPC glue vector for in-kernel short circuited tasks. Rather than
1217 * removing it completely, I have only disabled that feature (which was
1218 * the only feature at the time). It just appears that we are going to
1219 * want to add some user data to tasks in the future (i.e. bsd info,
1220 * task names, etc...), so I left it in the formal task interface.
1225 task_flavor_t flavor
,
1226 __unused task_info_t task_info_in
, /* pointer to IN array */
1227 __unused mach_msg_type_number_t task_info_count
)
1229 if (task
== TASK_NULL
)
1230 return(KERN_INVALID_ARGUMENT
);
1234 return (KERN_INVALID_ARGUMENT
);
1236 return (KERN_SUCCESS
);
1242 task_flavor_t flavor
,
1243 task_info_t task_info_out
,
1244 mach_msg_type_number_t
*task_info_count
)
1246 if (task
== TASK_NULL
)
1247 return (KERN_INVALID_ARGUMENT
);
1251 case TASK_BASIC_INFO_32
:
1253 task_basic_info_32_t basic_info
;
1256 if (*task_info_count
< TASK_BASIC_INFO_32_COUNT
)
1257 return (KERN_INVALID_ARGUMENT
);
1259 basic_info
= (task_basic_info_32_t
)task_info_out
;
1261 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1262 basic_info
->virtual_size
= CAST_DOWN(vm_offset_t
,map
->size
);
1263 basic_info
->resident_size
= pmap_resident_count(map
->pmap
)
1267 basic_info
->policy
= ((task
!= kernel_task
)?
1268 POLICY_TIMESHARE
: POLICY_RR
);
1269 basic_info
->suspend_count
= task
->user_stop_count
;
1271 absolutetime_to_microtime(
1272 task
->total_user_time
,
1273 &basic_info
->user_time
.seconds
,
1274 &basic_info
->user_time
.microseconds
);
1275 absolutetime_to_microtime(
1276 task
->total_system_time
,
1277 &basic_info
->system_time
.seconds
,
1278 &basic_info
->system_time
.microseconds
);
1281 *task_info_count
= TASK_BASIC_INFO_32_COUNT
;
1285 case TASK_BASIC_INFO_64
:
1287 task_basic_info_64_t basic_info
;
1290 if (*task_info_count
< TASK_BASIC_INFO_64_COUNT
)
1291 return (KERN_INVALID_ARGUMENT
);
1293 basic_info
= (task_basic_info_64_t
)task_info_out
;
1295 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
1296 basic_info
->virtual_size
= map
->size
;
1297 basic_info
->resident_size
= (mach_vm_size_t
)(pmap_resident_count(map
->pmap
)
1301 basic_info
->policy
= ((task
!= kernel_task
)?
1302 POLICY_TIMESHARE
: POLICY_RR
);
1303 basic_info
->suspend_count
= task
->user_stop_count
;
1305 absolutetime_to_microtime(
1306 task
->total_user_time
,
1307 &basic_info
->user_time
.seconds
,
1308 &basic_info
->user_time
.microseconds
);
1309 absolutetime_to_microtime(
1310 task
->total_system_time
,
1311 &basic_info
->system_time
.seconds
,
1312 &basic_info
->system_time
.microseconds
);
1315 *task_info_count
= TASK_BASIC_INFO_64_COUNT
;
1319 case TASK_THREAD_TIMES_INFO
:
1321 register task_thread_times_info_t times_info
;
1322 register thread_t thread
;
1324 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
)
1325 return (KERN_INVALID_ARGUMENT
);
1327 times_info
= (task_thread_times_info_t
) task_info_out
;
1328 times_info
->user_time
.seconds
= 0;
1329 times_info
->user_time
.microseconds
= 0;
1330 times_info
->system_time
.seconds
= 0;
1331 times_info
->system_time
.microseconds
= 0;
1335 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1336 time_value_t user_time
, system_time
;
1338 thread_read_times(thread
, &user_time
, &system_time
);
1340 time_value_add(×_info
->user_time
, &user_time
);
1341 time_value_add(×_info
->system_time
, &system_time
);
1346 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1350 case TASK_ABSOLUTETIME_INFO
:
1352 task_absolutetime_info_t info
;
1353 register thread_t thread
;
1355 if (*task_info_count
< TASK_ABSOLUTETIME_INFO_COUNT
)
1356 return (KERN_INVALID_ARGUMENT
);
1358 info
= (task_absolutetime_info_t
)task_info_out
;
1359 info
->threads_user
= info
->threads_system
= 0;
1363 info
->total_user
= task
->total_user_time
;
1364 info
->total_system
= task
->total_system_time
;
1366 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1369 tval
= timer_grab(&thread
->user_timer
);
1370 info
->threads_user
+= tval
;
1371 info
->total_user
+= tval
;
1373 tval
= timer_grab(&thread
->system_timer
);
1374 info
->threads_system
+= tval
;
1375 info
->total_system
+= tval
;
1380 *task_info_count
= TASK_ABSOLUTETIME_INFO_COUNT
;
1385 case TASK_SCHED_FIFO_INFO
:
1388 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
)
1389 return (KERN_INVALID_ARGUMENT
);
1391 return (KERN_INVALID_POLICY
);
1395 case TASK_SCHED_RR_INFO
:
1397 register policy_rr_base_t rr_base
;
1399 if (*task_info_count
< POLICY_RR_BASE_COUNT
)
1400 return (KERN_INVALID_ARGUMENT
);
1402 rr_base
= (policy_rr_base_t
) task_info_out
;
1405 if (task
!= kernel_task
) {
1407 return (KERN_INVALID_POLICY
);
1410 rr_base
->base_priority
= task
->priority
;
1413 rr_base
->quantum
= std_quantum_us
/ 1000;
1415 *task_info_count
= POLICY_RR_BASE_COUNT
;
1420 case TASK_SCHED_TIMESHARE_INFO
:
1422 register policy_timeshare_base_t ts_base
;
1424 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
)
1425 return (KERN_INVALID_ARGUMENT
);
1427 ts_base
= (policy_timeshare_base_t
) task_info_out
;
1430 if (task
== kernel_task
) {
1432 return (KERN_INVALID_POLICY
);
1435 ts_base
->base_priority
= task
->priority
;
1438 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
1442 case TASK_SECURITY_TOKEN
:
1444 register security_token_t
*sec_token_p
;
1446 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
)
1447 return (KERN_INVALID_ARGUMENT
);
1449 sec_token_p
= (security_token_t
*) task_info_out
;
1452 *sec_token_p
= task
->sec_token
;
1455 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
1459 case TASK_AUDIT_TOKEN
:
1461 register audit_token_t
*audit_token_p
;
1463 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
)
1464 return (KERN_INVALID_ARGUMENT
);
1466 audit_token_p
= (audit_token_t
*) task_info_out
;
1469 *audit_token_p
= task
->audit_token
;
1472 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
1476 case TASK_SCHED_INFO
:
1477 return (KERN_INVALID_ARGUMENT
);
1479 case TASK_EVENTS_INFO
:
1481 register task_events_info_t events_info
;
1483 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
)
1484 return (KERN_INVALID_ARGUMENT
);
1486 events_info
= (task_events_info_t
) task_info_out
;
1489 events_info
->faults
= task
->faults
;
1490 events_info
->pageins
= task
->pageins
;
1491 events_info
->cow_faults
= task
->cow_faults
;
1492 events_info
->messages_sent
= task
->messages_sent
;
1493 events_info
->messages_received
= task
->messages_received
;
1494 events_info
->syscalls_mach
= task
->syscalls_mach
;
1495 events_info
->syscalls_unix
= task
->syscalls_unix
;
1496 events_info
->csw
= task
->csw
;
1499 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
1504 return (KERN_INVALID_ARGUMENT
);
1507 return (KERN_SUCCESS
);
1513 * Change the assigned processor set for the task
1517 __unused task_t task
,
1518 __unused processor_set_t new_pset
,
1519 __unused boolean_t assign_threads
)
1521 return(KERN_FAILURE
);
1525 * task_assign_default:
1527 * Version of task_assign to assign to default processor set.
1530 task_assign_default(
1532 boolean_t assign_threads
)
1534 return (task_assign(task
, &default_pset
, assign_threads
));
1538 * task_get_assignment
1540 * Return name of processor set that task is assigned to.
1543 task_get_assignment(
1545 processor_set_t
*pset
)
1548 return(KERN_FAILURE
);
1550 *pset
= task
->processor_set
;
1551 pset_reference(*pset
);
1552 return(KERN_SUCCESS
);
1559 * Set scheduling policy and parameters, both base and limit, for
1560 * the given task. Policy must be a policy which is enabled for the
1561 * processor set. Change contained threads if requested.
1565 __unused task_t task
,
1566 __unused policy_t policy_id
,
1567 __unused policy_base_t base
,
1568 __unused mach_msg_type_number_t count
,
1569 __unused boolean_t set_limit
,
1570 __unused boolean_t change
)
1572 return(KERN_FAILURE
);
1578 * Set scheduling policy and parameters, both base and limit, for
1579 * the given task. Policy can be any policy implemented by the
1580 * processor set, whether enabled or not. Change contained threads
1585 __unused task_t task
,
1586 __unused processor_set_t pset
,
1587 __unused policy_t policy_id
,
1588 __unused policy_base_t base
,
1589 __unused mach_msg_type_number_t base_count
,
1590 __unused policy_limit_t limit
,
1591 __unused mach_msg_type_number_t limit_count
,
1592 __unused boolean_t change
)
1594 return(KERN_FAILURE
);
1604 extern int fast_tas_debug
;
1606 if (fast_tas_debug
) {
1607 printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
1611 task
->fast_tas_base
= pc
;
1612 task
->fast_tas_end
= endpc
;
1614 return KERN_SUCCESS
;
1616 #else /* FAST_TAS */
1619 __unused task_t task
,
1620 __unused vm_offset_t pc
,
1621 __unused vm_offset_t endpc
)
1623 return KERN_FAILURE
;
1625 #endif /* FAST_TAS */
1628 task_synchronizer_destroy_all(task_t task
)
1630 semaphore_t semaphore
;
1631 lock_set_t lock_set
;
1634 * Destroy owned semaphores
1637 while (!queue_empty(&task
->semaphore_list
)) {
1638 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
1639 (void) semaphore_destroy(task
, semaphore
);
1643 * Destroy owned lock sets
1646 while (!queue_empty(&task
->lock_set_list
)) {
1647 lock_set
= (lock_set_t
) queue_first(&task
->lock_set_list
);
1648 (void) lock_set_destroy(task
, lock_set
);
1653 * We need to export some functions to other components that
1654 * are currently implemented in macros within the osfmk
1655 * component. Just export them as functions of the same name.
1657 boolean_t
is_kerneltask(task_t t
)
1659 if (t
== kernel_task
)
1666 task_t
current_task(void);
1667 task_t
current_task(void)
1669 return (current_task_fast());
1672 #undef task_reference
1673 void task_reference(task_t task
);
1678 if (task
!= TASK_NULL
)
1679 task_reference_internal(task
);