2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * @OSF_FREE_COPYRIGHT@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
55 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub,
58 * Task management primitives implementation.
61 * Copyright (c) 1993 The University of Utah and
62 * the Computer Systems Laboratory (CSL). All rights reserved.
64 * Permission to use, copy, modify and distribute this software and its
65 * documentation is hereby granted, provided that both the copyright
66 * notice and this permission notice appear in all copies of the
67 * software, derivative works or modified versions, and any portions
68 * thereof, and that both notices appear in supporting documentation.
70 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
71 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
72 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
74 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
75 * improvements that they make and grant CSL redistribution rights.
80 #include <mach_host.h>
81 #include <mach_prof.h>
83 #include <task_swapper.h>
84 #include <platforms.h>
86 #include <mach/boolean.h>
87 #include <mach/machine/vm_types.h>
88 #include <mach/vm_param.h>
89 #include <mach/semaphore.h>
90 #include <mach/task_info.h>
91 #include <mach/task_special_ports.h>
92 #include <mach/mach_types.h>
93 #include <ipc/ipc_space.h>
94 #include <ipc/ipc_entry.h>
95 #include <kern/mach_param.h>
96 #include <kern/misc_protos.h>
97 #include <kern/task.h>
98 #include <kern/thread.h>
99 #include <kern/zalloc.h>
100 #include <kern/kalloc.h>
101 #include <kern/processor.h>
102 #include <kern/sched_prim.h> /* for thread_wakeup */
103 #include <kern/ipc_tt.h>
104 #include <kern/ledger.h>
105 #include <kern/host.h>
106 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
107 #include <kern/profile.h>
108 #include <kern/assert.h>
109 #include <kern/sync_lock.h>
111 #include <ddb/db_sym.h>
112 #endif /* MACH_KDB */
115 #include <kern/task_swap.h>
116 #endif /* TASK_SWAPPER */
119 #include <ppc/exception.h>
120 #include <ppc/hw_perfmon.h>
124 * Exported interfaces
127 #include <mach/task_server.h>
128 #include <mach/mach_host_server.h>
129 #include <mach/host_security_server.h>
130 #include <vm/task_working_set.h>
137 void task_hold_locked(
139 void task_wait_locked(
141 void task_release_locked(
143 void task_collect_scan(void);
146 void task_synchronizer_destroy_all(
149 kern_return_t
task_set_ledger(
155 task_backing_store_privileged(
159 task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
169 TASK_MAX
* sizeof(struct task
),
170 TASK_CHUNK
* sizeof(struct task
),
176 * Create the kernel task as the first task.
178 if (task_create_internal(TASK_NULL
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
179 panic("task_init\n");
181 vm_map_deallocate(kernel_task
->map
);
182 kernel_task
->map
= kernel_map
;
194 * If may_assign is false, task is already being assigned,
195 * wait for that to finish.
197 while (task
->may_assign
== FALSE
) {
200 task
->assign_active
= TRUE
;
201 res
= thread_sleep_mutex((event_t
) &task
->assign_active
,
202 &task
->lock
, THREAD_UNINT
);
203 assert(res
== THREAD_AWAKENED
);
205 task
->may_assign
= FALSE
;
210 #define thread_freeze(thread) assert(task->processor_set == &default_pset)
219 assert(task
->may_assign
== FALSE
);
220 task
->may_assign
= TRUE
;
221 if (task
->assign_active
== TRUE
) {
222 task
->assign_active
= FALSE
;
223 thread_wakeup((event_t
)&task
->assign_active
);
229 #define thread_unfreeze(thread) assert(task->processor_set == &default_pset)
232 #endif /* MACH_HOST */
235 * Create a task running in the kernel address space. It may
236 * have its own map of size mem_size and may have ipc privileges.
241 vm_offset_t map_base
,
245 return (KERN_INVALID_ARGUMENT
);
251 ledger_port_array_t ledger_ports
,
252 mach_msg_type_number_t num_ledger_ports
,
253 boolean_t inherit_memory
,
254 task_t
*child_task
) /* OUT */
256 if (parent_task
== TASK_NULL
)
257 return(KERN_INVALID_ARGUMENT
);
259 return task_create_internal(
260 parent_task
, inherit_memory
, child_task
);
264 host_security_create_task_token(
265 host_security_t host_security
,
267 security_token_t sec_token
,
268 audit_token_t audit_token
,
269 host_priv_t host_priv
,
270 ledger_port_array_t ledger_ports
,
271 mach_msg_type_number_t num_ledger_ports
,
272 boolean_t inherit_memory
,
273 task_t
*child_task
) /* OUT */
275 kern_return_t result
;
277 if (parent_task
== TASK_NULL
)
278 return(KERN_INVALID_ARGUMENT
);
280 if (host_security
== HOST_NULL
)
281 return(KERN_INVALID_SECURITY
);
283 result
= task_create_internal(
284 parent_task
, inherit_memory
, child_task
);
286 if (result
!= KERN_SUCCESS
)
289 result
= host_security_set_task_token(host_security
,
295 if (result
!= KERN_SUCCESS
)
302 task_create_internal(
304 boolean_t inherit_memory
,
305 task_t
*child_task
) /* OUT */
308 processor_set_t pset
;
310 new_task
= (task_t
) zalloc(task_zone
);
312 if (new_task
== TASK_NULL
)
313 return(KERN_RESOURCE_SHORTAGE
);
315 /* one ref for just being alive; one for our caller */
316 new_task
->ref_count
= 2;
319 new_task
->map
= vm_map_fork(parent_task
->map
);
321 new_task
->map
= vm_map_create(pmap_create(0),
322 round_page_32(VM_MIN_ADDRESS
),
323 trunc_page_32(VM_MAX_ADDRESS
), TRUE
);
325 mutex_init(&new_task
->lock
, ETAP_THREAD_TASK_NEW
);
326 queue_init(&new_task
->threads
);
327 new_task
->suspend_count
= 0;
328 new_task
->thread_count
= 0;
329 new_task
->res_thread_count
= 0;
330 new_task
->active_thread_count
= 0;
331 new_task
->user_stop_count
= 0;
332 new_task
->role
= TASK_UNSPECIFIED
;
333 new_task
->active
= TRUE
;
334 new_task
->user_data
= 0;
335 new_task
->faults
= 0;
336 new_task
->cow_faults
= 0;
337 new_task
->pageins
= 0;
338 new_task
->messages_sent
= 0;
339 new_task
->messages_received
= 0;
340 new_task
->syscalls_mach
= 0;
341 new_task
->priv_flags
= 0;
342 new_task
->syscalls_unix
=0;
344 new_task
->taskFeatures
[0] = 0; /* Init task features */
345 new_task
->taskFeatures
[1] = 0; /* Init task features */
346 new_task
->dynamic_working_set
= 0;
348 task_working_set_create(new_task
, TWS_SMALL_HASH_LINE_COUNT
,
349 0, TWS_HASH_STYLE_DEFAULT
);
352 new_task
->bsd_info
= 0;
353 #endif /* MACH_BSD */
356 if(per_proc_info
[0].pf
.Available
& pf64Bit
) new_task
->taskFeatures
[0] |= tf64BitData
; /* If 64-bit machine, show we have 64-bit registers at least */
360 new_task
->swap_state
= TASK_SW_IN
;
361 new_task
->swap_flags
= 0;
362 new_task
->swap_ast_waiting
= 0;
363 new_task
->swap_stamp
= sched_tick
;
364 new_task
->swap_rss
= 0;
365 new_task
->swap_nswap
= 0;
366 #endif /* TASK_SWAPPER */
368 queue_init(&new_task
->semaphore_list
);
369 queue_init(&new_task
->lock_set_list
);
370 new_task
->semaphores_owned
= 0;
371 new_task
->lock_sets_owned
= 0;
374 new_task
->may_assign
= TRUE
;
375 new_task
->assign_active
= FALSE
;
376 #endif /* MACH_HOST */
377 eml_task_reference(new_task
, parent_task
);
379 ipc_task_init(new_task
, parent_task
);
381 new_task
->total_user_time
.seconds
= 0;
382 new_task
->total_user_time
.microseconds
= 0;
383 new_task
->total_system_time
.seconds
= 0;
384 new_task
->total_system_time
.microseconds
= 0;
386 task_prof_init(new_task
);
388 if (parent_task
!= TASK_NULL
) {
391 * Freeze the parent, so that parent_task->processor_set
394 task_freeze(parent_task
);
395 #endif /* MACH_HOST */
396 pset
= parent_task
->processor_set
;
398 pset
= &default_pset
;
400 new_task
->sec_token
= parent_task
->sec_token
;
401 new_task
->audit_token
= parent_task
->audit_token
;
403 shared_region_mapping_ref(parent_task
->system_shared_region
);
404 new_task
->system_shared_region
= parent_task
->system_shared_region
;
406 new_task
->wired_ledger_port
= ledger_copy(
407 convert_port_to_ledger(parent_task
->wired_ledger_port
));
408 new_task
->paged_ledger_port
= ledger_copy(
409 convert_port_to_ledger(parent_task
->paged_ledger_port
));
412 pset
= &default_pset
;
414 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
415 new_task
->audit_token
= KERNEL_AUDIT_TOKEN
;
416 new_task
->wired_ledger_port
= ledger_copy(root_wired_ledger
);
417 new_task
->paged_ledger_port
= ledger_copy(root_paged_ledger
);
420 if (kernel_task
== TASK_NULL
) {
421 new_task
->priority
= BASEPRI_KERNEL
;
422 new_task
->max_priority
= MAXPRI_KERNEL
;
425 new_task
->priority
= BASEPRI_DEFAULT
;
426 new_task
->max_priority
= MAXPRI_USER
;
430 pset_add_task(pset
, new_task
);
433 if (parent_task
!= TASK_NULL
)
434 task_unfreeze(parent_task
);
435 #endif /* MACH_HOST */
437 if (vm_backing_store_low
&& parent_task
!= NULL
)
438 new_task
->priv_flags
|= (parent_task
->priv_flags
&VM_BACKING_STORE_PRIV
);
440 ipc_task_enable(new_task
);
442 *child_task
= new_task
;
443 return(KERN_SUCCESS
);
449 * Drop a reference on a task
456 processor_set_t pset
;
459 if (task
== TASK_NULL
)
463 refs
= --task
->ref_count
;
470 /* task_terminate guarantees that this task is off the list */
471 assert((task
->swap_state
& TASK_SW_ELIGIBLE
) == 0);
472 #endif /* TASK_SWAPPER */
474 if(task
->dynamic_working_set
)
475 tws_hash_destroy((tws_hash_t
)task
->dynamic_working_set
);
477 eml_task_deallocate(task
);
479 ipc_task_terminate(task
);
485 pset
= task
->processor_set
;
487 pset_remove_task(pset
,task
);
489 pset_deallocate(pset
);
495 vm_map_deallocate(task
->map
);
496 is_release(task
->itk_space
);
497 task_prof_deallocate(task
);
498 zfree(task_zone
, (vm_offset_t
) task
);
506 if (task
!= TASK_NULL
) {
517 if (task
!= TASK_NULL
) {
518 if (task_lock_try(task
)) {
530 * Terminate the specified task. See comments on thread_terminate
531 * (kern/thread.c) about problems with terminating the "current task."
538 if (task
== TASK_NULL
)
539 return(KERN_INVALID_ARGUMENT
);
541 return(KERN_FAILURE
);
542 return (task_terminate_internal(task
));
546 task_terminate_internal(
549 thread_act_t thr_act
, cur_thr_act
;
551 boolean_t interrupt_save
;
553 assert(task
!= kernel_task
);
555 cur_thr_act
= current_act();
556 cur_task
= cur_thr_act
->task
;
560 * If task is not resident (swapped out, or being swapped
561 * out), we want to bring it back in (this can block).
562 * NOTE: The only way that this can happen in the current
563 * system is if the task is swapped while it has a thread
564 * in exit(), and the thread does not hit a clean point
565 * to swap itself before getting here.
566 * Terminating other tasks is another way to this code, but
567 * it is not yet fully supported.
568 * The task_swapin is unconditional. It used to be done
569 * only if the task is not resident. Swapping in a
570 * resident task will prevent it from being swapped out
571 * while it terminates.
573 task_swapin(task
, TRUE
); /* TRUE means make it unswappable */
574 #endif /* TASK_SWAPPER */
577 * Get the task locked and make sure that we are not racing
578 * with someone else trying to terminate us.
580 if (task
== cur_task
) {
582 } else if (task
< cur_task
) {
590 if (!task
->active
|| !cur_thr_act
->active
) {
592 * Task or current act is already being terminated.
593 * Just return an error. If we are dying, this will
594 * just get us to our AST special handler and that
595 * will get us to finalize the termination of ourselves.
598 if (cur_task
!= task
)
599 task_unlock(cur_task
);
600 return(KERN_FAILURE
);
602 if (cur_task
!= task
)
603 task_unlock(cur_task
);
606 * Make sure the current thread does not get aborted out of
607 * the waits inside these operations.
609 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
612 * Indicate that we want all the threads to stop executing
613 * at user space by holding the task (we would have held
614 * each thread independently in thread_terminate_internal -
615 * but this way we may be more likely to already find it
616 * held there). Mark the task inactive, and prevent
617 * further task operations via the task port.
619 task_hold_locked(task
);
620 task
->active
= FALSE
;
621 ipc_task_disable(task
);
624 * Terminate each activation in the task.
626 * Each terminated activation will run it's special handler
627 * when its current kernel context is unwound. That will
628 * clean up most of the thread resources. Then it will be
629 * handed over to the reaper, who will finally remove the
630 * thread from the task list and free the structures.
632 queue_iterate(&task
->threads
, thr_act
, thread_act_t
, task_threads
) {
633 thread_terminate_internal(thr_act
);
637 * Give the machine dependent code a chance
638 * to perform cleanup before ripping apart
641 if (cur_thr_act
->task
== task
)
642 machine_thread_terminate_self();
647 * Destroy all synchronizers owned by the task.
649 task_synchronizer_destroy_all(task
);
652 * Destroy the IPC space, leaving just a reference for it.
654 ipc_space_destroy(task
->itk_space
);
657 * If the current thread is a member of the task
658 * being terminated, then the last reference to
659 * the task will not be dropped until the thread
660 * is finally reaped. To avoid incurring the
661 * expense of removing the address space regions
662 * at reap time, we do it explictly here.
664 (void) vm_map_remove(task
->map
,
665 task
->map
->min_offset
,
666 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
668 shared_region_mapping_dealloc(task
->system_shared_region
);
671 * Flush working set here to avoid I/O in reaper thread
673 if(task
->dynamic_working_set
)
674 tws_hash_ws_flush((tws_hash_t
)
675 task
->dynamic_working_set
);
678 * We no longer need to guard against being aborted, so restore
679 * the previous interruptible state.
681 thread_interrupt_level(interrupt_save
);
684 perfmon_release_facility(task
); // notify the perfmon facility
688 * Get rid of the task active reference on itself.
690 task_deallocate(task
);
692 return(KERN_SUCCESS
);
696 * task_halt - Shut the current task down (except for the current thread) in
697 * preparation for dramatic changes to the task (probably exec).
698 * We hold the task, terminate all other threads in the task and
699 * wait for them to terminate, clean up the portspace, and when
700 * all done, let the current thread go.
706 thread_act_t thr_act
, cur_thr_act
;
709 assert(task
!= kernel_task
);
711 cur_thr_act
= current_act();
712 cur_task
= cur_thr_act
->task
;
714 if (task
!= cur_task
) {
715 return(KERN_INVALID_ARGUMENT
);
720 * If task is not resident (swapped out, or being swapped
721 * out), we want to bring it back in and make it unswappable.
722 * This can block, so do it early.
724 task_swapin(task
, TRUE
); /* TRUE means make it unswappable */
725 #endif /* TASK_SWAPPER */
729 if (!task
->active
|| !cur_thr_act
->active
) {
731 * Task or current thread is already being terminated.
732 * Hurry up and return out of the current kernel context
733 * so that we run our AST special handler to terminate
737 return(KERN_FAILURE
);
740 if (task
->thread_count
> 1) {
742 * Mark all the threads to keep them from starting any more
743 * user-level execution. The thread_terminate_internal code
744 * would do this on a thread by thread basis anyway, but this
745 * gives us a better chance of not having to wait there.
747 task_hold_locked(task
);
750 * Terminate all the other activations in the task.
752 * Each terminated activation will run it's special handler
753 * when its current kernel context is unwound. That will
754 * clean up most of the thread resources. Then it will be
755 * handed over to the reaper, who will finally remove the
756 * thread from the task list and free the structures.
758 queue_iterate(&task
->threads
, thr_act
, thread_act_t
, task_threads
) {
759 if (thr_act
!= cur_thr_act
)
760 thread_terminate_internal(thr_act
);
762 task_release_locked(task
);
766 * Give the machine dependent code a chance
767 * to perform cleanup before ripping apart
770 machine_thread_terminate_self();
775 * Destroy all synchronizers owned by the task.
777 task_synchronizer_destroy_all(task
);
780 * Destroy the contents of the IPC space, leaving just
781 * a reference for it.
783 ipc_space_clean(task
->itk_space
);
786 * Clean out the address space, as we are going to be
789 (void) vm_map_remove(task
->map
,
790 task
->map
->min_offset
,
791 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
799 * Suspend execution of the specified task.
800 * This is a recursive-style suspension of the task, a count of
801 * suspends is maintained.
803 * CONDITIONS: the task is locked and active.
807 register task_t task
)
809 register thread_act_t thr_act
;
811 assert(task
->active
);
813 if (task
->suspend_count
++ > 0)
817 * Iterate through all the thread_act's and hold them.
819 queue_iterate(&task
->threads
, thr_act
, thread_act_t
, task_threads
) {
820 act_lock_thread(thr_act
);
821 thread_hold(thr_act
);
822 act_unlock_thread(thr_act
);
829 * Same as the internal routine above, except that is must lock
830 * and verify that the task is active. This differs from task_suspend
831 * in that it places a kernel hold on the task rather than just a
832 * user-level hold. This keeps users from over resuming and setting
833 * it running out from under the kernel.
835 * CONDITIONS: the caller holds a reference on the task
838 task_hold(task_t task
)
842 if (task
== TASK_NULL
)
843 return (KERN_INVALID_ARGUMENT
);
847 return (KERN_FAILURE
);
849 task_hold_locked(task
);
852 return(KERN_SUCCESS
);
856 * Routine: task_wait_locked
857 * Wait for all threads in task to stop.
860 * Called with task locked, active, and held.
864 register task_t task
)
866 register thread_act_t thr_act
, cur_thr_act
;
868 assert(task
->active
);
869 assert(task
->suspend_count
> 0);
871 cur_thr_act
= current_act();
873 * Iterate through all the thread's and wait for them to
874 * stop. Do not wait for the current thread if it is within
877 queue_iterate(&task
->threads
, thr_act
, thread_act_t
, task_threads
) {
878 if (thr_act
!= cur_thr_act
) {
881 thread
= act_lock_thread(thr_act
);
883 act_unlock_thread(thr_act
);
889 * task_release_locked:
891 * Release a kernel hold on a task.
893 * CONDITIONS: the task is locked and active
897 register task_t task
)
899 register thread_act_t thr_act
;
901 assert(task
->active
);
902 assert(task
->suspend_count
> 0);
904 if (--task
->suspend_count
> 0)
908 * Iterate through all the thread_act's and hold them.
909 * Do not hold the current thread_act if it is within the
912 queue_iterate(&task
->threads
, thr_act
, thread_act_t
, task_threads
) {
913 act_lock_thread(thr_act
);
914 thread_release(thr_act
);
915 act_unlock_thread(thr_act
);
922 * Same as the internal routine above, except that it must lock
923 * and verify that the task is active.
925 * CONDITIONS: The caller holds a reference to the task
928 task_release(task_t task
)
932 if (task
== TASK_NULL
)
933 return (KERN_INVALID_ARGUMENT
);
937 return (KERN_FAILURE
);
939 task_release_locked(task
);
942 return(KERN_SUCCESS
);
948 thread_act_array_t
*thr_act_list
,
949 mach_msg_type_number_t
*count
)
951 unsigned int actual
; /* this many thr_acts */
952 thread_act_t thr_act
;
953 thread_act_t
*thr_acts
;
957 vm_size_t size
, size_needed
;
960 if (task
== TASK_NULL
)
961 return KERN_INVALID_ARGUMENT
;
974 actual
= task
->thread_count
;
976 /* do we have the memory we need? */
977 size_needed
= actual
* sizeof(mach_port_t
);
978 if (size_needed
<= size
)
981 /* unlock the task and allocate more memory */
987 assert(size_needed
> 0);
992 return KERN_RESOURCE_SHORTAGE
;
995 /* OK, have memory and the task is locked & active */
996 thr_acts
= (thread_act_t
*) addr
;
998 for (i
= j
= 0, thr_act
= (thread_act_t
) queue_first(&task
->threads
);
1000 i
++, thr_act
= (thread_act_t
) queue_next(&thr_act
->task_threads
)) {
1002 if (thr_act
->act_ref_count
> 0) {
1003 act_reference_locked(thr_act
);
1004 thr_acts
[j
++] = thr_act
;
1006 act_unlock(thr_act
);
1008 assert(queue_end(&task
->threads
, (queue_entry_t
) thr_act
));
1011 size_needed
= actual
* sizeof(mach_port_t
);
1013 /* can unlock task now that we've got the thr_act refs */
1017 /* no thr_acts, so return null pointer and deallocate memory */
1025 /* if we allocated too much, must copy */
1027 if (size_needed
< size
) {
1028 vm_offset_t newaddr
;
1030 newaddr
= kalloc(size_needed
);
1032 for (i
= 0; i
< actual
; i
++)
1033 act_deallocate(thr_acts
[i
]);
1035 return KERN_RESOURCE_SHORTAGE
;
1038 bcopy((char *) addr
, (char *) newaddr
, size_needed
);
1040 thr_acts
= (thread_act_t
*) newaddr
;
1043 *thr_act_list
= thr_acts
;
1046 /* do the conversion that Mig should handle */
1048 for (i
= 0; i
< actual
; i
++)
1049 ((ipc_port_t
*) thr_acts
)[i
] =
1050 convert_act_to_port(thr_acts
[i
]);
1053 return KERN_SUCCESS
;
1057 * Routine: task_suspend
1058 * Implement a user-level suspension on a task.
1061 * The caller holds a reference to the task
1065 register task_t task
)
1067 if (task
== TASK_NULL
)
1068 return (KERN_INVALID_ARGUMENT
);
1071 if (!task
->active
) {
1073 return (KERN_FAILURE
);
1075 if ((task
->user_stop_count
)++ > 0) {
1077 * If the stop count was positive, the task is
1078 * already stopped and we can exit.
1081 return (KERN_SUCCESS
);
1085 * Put a kernel-level hold on the threads in the task (all
1086 * user-level task suspensions added together represent a
1087 * single kernel-level hold). We then wait for the threads
1088 * to stop executing user code.
1090 task_hold_locked(task
);
1091 task_wait_locked(task
);
1093 return (KERN_SUCCESS
);
1097 * Routine: task_resume
1098 * Release a kernel hold on a task.
1101 * The caller holds a reference to the task
1104 task_resume(register task_t task
)
1106 register boolean_t release
;
1108 if (task
== TASK_NULL
)
1109 return(KERN_INVALID_ARGUMENT
);
1113 if (!task
->active
) {
1115 return(KERN_FAILURE
);
1117 if (task
->user_stop_count
> 0) {
1118 if (--(task
->user_stop_count
) == 0)
1123 return(KERN_FAILURE
);
1127 * Release the task if necessary.
1130 task_release_locked(task
);
1133 return(KERN_SUCCESS
);
1137 host_security_set_task_token(
1138 host_security_t host_security
,
1140 security_token_t sec_token
,
1141 audit_token_t audit_token
,
1142 host_priv_t host_priv
)
1144 ipc_port_t host_port
;
1147 if (task
== TASK_NULL
)
1148 return(KERN_INVALID_ARGUMENT
);
1150 if (host_security
== HOST_NULL
)
1151 return(KERN_INVALID_SECURITY
);
1154 task
->sec_token
= sec_token
;
1155 task
->audit_token
= audit_token
;
1158 if (host_priv
!= HOST_PRIV_NULL
) {
1159 kr
= host_get_host_priv_port(host_priv
, &host_port
);
1161 kr
= host_get_host_port(host_priv_self(), &host_port
);
1163 assert(kr
== KERN_SUCCESS
);
1164 kr
= task_set_special_port(task
, TASK_HOST_PORT
, host_port
);
1169 * Utility routine to set a ledger
1177 if (task
== TASK_NULL
)
1178 return(KERN_INVALID_ARGUMENT
);
1182 ipc_port_release_send(task
->wired_ledger_port
);
1183 task
->wired_ledger_port
= ledger_copy(wired
);
1186 ipc_port_release_send(task
->paged_ledger_port
);
1187 task
->paged_ledger_port
= ledger_copy(paged
);
1191 return(KERN_SUCCESS
);
1195 * This routine was added, pretty much exclusively, for registering the
1196 * RPC glue vector for in-kernel short circuited tasks. Rather than
1197 * removing it completely, I have only disabled that feature (which was
1198 * the only feature at the time). It just appears that we are going to
1199 * want to add some user data to tasks in the future (i.e. bsd info,
1200 * task names, etc...), so I left it in the formal task interface.
1205 task_flavor_t flavor
,
1206 task_info_t task_info_in
, /* pointer to IN array */
1207 mach_msg_type_number_t task_info_count
)
1211 if (task
== TASK_NULL
)
1212 return(KERN_INVALID_ARGUMENT
);
1216 return (KERN_INVALID_ARGUMENT
);
1218 return (KERN_SUCCESS
);
1224 task_flavor_t flavor
,
1225 task_info_t task_info_out
,
1226 mach_msg_type_number_t
*task_info_count
)
1231 if (task
== TASK_NULL
)
1232 return(KERN_INVALID_ARGUMENT
);
1236 case TASK_BASIC_INFO
:
1238 register task_basic_info_t basic_info
;
1240 if (*task_info_count
< TASK_BASIC_INFO_COUNT
) {
1241 return(KERN_INVALID_ARGUMENT
);
1244 basic_info
= (task_basic_info_t
) task_info_out
;
1246 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
1248 basic_info
->virtual_size
= map
->size
;
1249 basic_info
->resident_size
= pmap_resident_count(map
->pmap
)
1253 basic_info
->policy
= ((task
!= kernel_task
)?
1254 POLICY_TIMESHARE
: POLICY_RR
);
1255 basic_info
->suspend_count
= task
->user_stop_count
;
1256 basic_info
->user_time
.seconds
1257 = task
->total_user_time
.seconds
;
1258 basic_info
->user_time
.microseconds
1259 = task
->total_user_time
.microseconds
;
1260 basic_info
->system_time
.seconds
1261 = task
->total_system_time
.seconds
;
1262 basic_info
->system_time
.microseconds
1263 = task
->total_system_time
.microseconds
;
1266 *task_info_count
= TASK_BASIC_INFO_COUNT
;
1270 case TASK_THREAD_TIMES_INFO
:
1272 register task_thread_times_info_t times_info
;
1273 register thread_t thread
;
1274 register thread_act_t thr_act
;
1276 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
1277 return (KERN_INVALID_ARGUMENT
);
1280 times_info
= (task_thread_times_info_t
) task_info_out
;
1281 times_info
->user_time
.seconds
= 0;
1282 times_info
->user_time
.microseconds
= 0;
1283 times_info
->system_time
.seconds
= 0;
1284 times_info
->system_time
.microseconds
= 0;
1287 queue_iterate(&task
->threads
, thr_act
,
1288 thread_act_t
, task_threads
)
1290 time_value_t user_time
, system_time
;
1293 thread
= act_lock_thread(thr_act
);
1295 /* JMM - add logic to skip threads that have migrated
1299 assert(thread
); /* Must have thread */
1301 thread_lock(thread
);
1303 thread_read_times(thread
, &user_time
, &system_time
);
1305 thread_unlock(thread
);
1307 act_unlock_thread(thr_act
);
1309 time_value_add(×_info
->user_time
, &user_time
);
1310 time_value_add(×_info
->system_time
, &system_time
);
1314 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1318 case TASK_SCHED_FIFO_INFO
:
1321 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
)
1322 return(KERN_INVALID_ARGUMENT
);
1324 return(KERN_INVALID_POLICY
);
1327 case TASK_SCHED_RR_INFO
:
1329 register policy_rr_base_t rr_base
;
1331 if (*task_info_count
< POLICY_RR_BASE_COUNT
)
1332 return(KERN_INVALID_ARGUMENT
);
1334 rr_base
= (policy_rr_base_t
) task_info_out
;
1337 if (task
!= kernel_task
) {
1339 return(KERN_INVALID_POLICY
);
1342 rr_base
->base_priority
= task
->priority
;
1345 rr_base
->quantum
= tick
/ 1000;
1347 *task_info_count
= POLICY_RR_BASE_COUNT
;
1351 case TASK_SCHED_TIMESHARE_INFO
:
1353 register policy_timeshare_base_t ts_base
;
1355 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
)
1356 return(KERN_INVALID_ARGUMENT
);
1358 ts_base
= (policy_timeshare_base_t
) task_info_out
;
1361 if (task
== kernel_task
) {
1363 return(KERN_INVALID_POLICY
);
1366 ts_base
->base_priority
= task
->priority
;
1369 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
1373 case TASK_SECURITY_TOKEN
:
1375 register security_token_t
*sec_token_p
;
1377 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
1378 return(KERN_INVALID_ARGUMENT
);
1381 sec_token_p
= (security_token_t
*) task_info_out
;
1384 *sec_token_p
= task
->sec_token
;
1387 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
1391 case TASK_AUDIT_TOKEN
:
1393 register audit_token_t
*audit_token_p
;
1395 if (*task_info_count
< TASK_AUDIT_TOKEN_COUNT
) {
1396 return(KERN_INVALID_ARGUMENT
);
1399 audit_token_p
= (audit_token_t
*) task_info_out
;
1402 *audit_token_p
= task
->audit_token
;
1405 *task_info_count
= TASK_AUDIT_TOKEN_COUNT
;
1409 case TASK_SCHED_INFO
:
1410 return(KERN_INVALID_ARGUMENT
);
1412 case TASK_EVENTS_INFO
:
1414 register task_events_info_t events_info
;
1416 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
1417 return(KERN_INVALID_ARGUMENT
);
1420 events_info
= (task_events_info_t
) task_info_out
;
1423 events_info
->faults
= task
->faults
;
1424 events_info
->pageins
= task
->pageins
;
1425 events_info
->cow_faults
= task
->cow_faults
;
1426 events_info
->messages_sent
= task
->messages_sent
;
1427 events_info
->messages_received
= task
->messages_received
;
1428 events_info
->syscalls_mach
= task
->syscalls_mach
;
1429 events_info
->syscalls_unix
= task
->syscalls_unix
;
1430 events_info
->csw
= task
->csw
;
1433 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
1438 return (KERN_INVALID_ARGUMENT
);
1441 return(KERN_SUCCESS
);
1447 * Change the assigned processor set for the task
1452 processor_set_t new_pset
,
1453 boolean_t assign_threads
)
1456 task
++; new_pset
++; assign_threads
++;
1458 return(KERN_FAILURE
);
1462 * task_assign_default:
1464 * Version of task_assign to assign to default processor set.
1467 task_assign_default(
1469 boolean_t assign_threads
)
1471 return (task_assign(task
, &default_pset
, assign_threads
));
1475 * task_get_assignment
1477 * Return name of processor set that task is assigned to.
1480 task_get_assignment(
1482 processor_set_t
*pset
)
1485 return(KERN_FAILURE
);
1487 *pset
= task
->processor_set
;
1488 pset_reference(*pset
);
1489 return(KERN_SUCCESS
);
1496 * Set scheduling policy and parameters, both base and limit, for
1497 * the given task. Policy must be a policy which is enabled for the
1498 * processor set. Change contained threads if requested.
1505 mach_msg_type_number_t count
,
1506 boolean_t set_limit
,
1509 return(KERN_FAILURE
);
1515 * Set scheduling policy and parameters, both base and limit, for
1516 * the given task. Policy can be any policy implemented by the
1517 * processor set, whether enabled or not. Change contained threads
1523 processor_set_t pset
,
1526 mach_msg_type_number_t base_count
,
1527 policy_limit_t limit
,
1528 mach_msg_type_number_t limit_count
,
1531 return(KERN_FAILURE
);
1535 * task_collect_scan:
1537 * Attempt to free resources owned by tasks.
1541 task_collect_scan(void)
1543 register task_t task
, prev_task
;
1544 processor_set_t pset
= &default_pset
;
1548 task
= (task_t
) queue_first(&pset
->tasks
);
1549 while (!queue_end(&pset
->tasks
, (queue_entry_t
) task
)) {
1551 if (task
->ref_count
> 0) {
1553 task_reference_locked(task
);
1558 * While we still have the pset locked, freeze the task in
1559 * this pset. That way, when we get back from collecting
1560 * it, we can dereference the pset_tasks chain for the task
1561 * and be assured that we are still in this chain.
1568 pmap_collect(task
->map
->pmap
);
1572 task
= (task_t
) queue_next(&task
->pset_tasks
);
1575 task_unfreeze(prev_task
);
1578 task_deallocate(prev_task
);
1581 task
= (task_t
) queue_next(&task
->pset_tasks
);
1587 pset_deallocate(pset
);
1590 /* Also disabled in vm/vm_pageout.c */
1591 boolean_t task_collect_allowed
= FALSE
;
1592 unsigned task_collect_last_tick
= 0;
1593 unsigned task_collect_max_rate
= 0; /* in ticks */
1596 * consider_task_collect:
1598 * Called by the pageout daemon when the system needs more free pages.
1602 consider_task_collect(void)
1605 * By default, don't attempt task collection more frequently
1606 * than once per second.
1609 if (task_collect_max_rate
== 0)
1610 task_collect_max_rate
= (1 << SCHED_TICK_SHIFT
) + 1;
1612 if (task_collect_allowed
&&
1613 (sched_tick
> (task_collect_last_tick
+ task_collect_max_rate
))) {
1614 task_collect_last_tick
= sched_tick
;
1615 task_collect_scan();
1626 extern int fast_tas_debug
;
1628 if (fast_tas_debug
) {
1629 printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
1633 task
->fast_tas_base
= pc
;
1634 task
->fast_tas_end
= endpc
;
1636 return KERN_SUCCESS
;
1638 #else /* FAST_TAS */
1645 return KERN_FAILURE
;
1647 #endif /* FAST_TAS */
1651 task_synchronizer_destroy_all(task_t task
)
1653 semaphore_t semaphore
;
1654 lock_set_t lock_set
;
1657 * Destroy owned semaphores
1660 while (!queue_empty(&task
->semaphore_list
)) {
1661 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
1662 (void) semaphore_destroy(task
, semaphore
);
1666 * Destroy owned lock sets
1669 while (!queue_empty(&task
->lock_set_list
)) {
1670 lock_set
= (lock_set_t
) queue_first(&task
->lock_set_list
);
1671 (void) lock_set_destroy(task
, lock_set
);
1676 * task_set_port_space:
1678 * Set port name space of task to specified size.
1682 task_set_port_space(
1688 is_write_lock(task
->itk_space
);
1689 kr
= ipc_entry_grow_table(task
->itk_space
, table_entries
);
1690 if (kr
== KERN_SUCCESS
)
1691 is_write_unlock(task
->itk_space
);
1699 * Returns true if the task is a P_CLASSIC task.
1705 boolean_t result
= FALSE
;
1708 struct proc
*p
= get_bsdtask_info(task
);
1709 result
= proc_is_classic(p
) ? TRUE
: FALSE
;
1715 * We need to export some functions to other components that
1716 * are currently implemented in macros within the osfmk
1717 * component. Just export them as functions of the same name.
1719 boolean_t
is_kerneltask(task_t t
)
1721 if (t
== kernel_task
)
1728 task_t
current_task()
1730 return (current_task_fast());