2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * @OSF_FREE_COPYRIGHT@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
52 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub,
55 * Task management primitives implementation.
58 * Copyright (c) 1993 The University of Utah and
59 * the Computer Systems Laboratory (CSL). All rights reserved.
61 * Permission to use, copy, modify and distribute this software and its
62 * documentation is hereby granted, provided that both the copyright
63 * notice and this permission notice appear in all copies of the
64 * software, derivative works or modified versions, and any portions
65 * thereof, and that both notices appear in supporting documentation.
67 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
68 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
69 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
71 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
72 * improvements that they make and grant CSL redistribution rights.
77 #include <mach_host.h>
78 #include <mach_prof.h>
80 #include <task_swapper.h>
81 #include <platforms.h>
83 #include <mach/boolean.h>
84 #include <mach/machine/vm_types.h>
85 #include <mach/vm_param.h>
86 #include <mach/semaphore.h>
87 #include <mach/task_info.h>
88 #include <mach/task_special_ports.h>
89 #include <mach/mach_types.h>
90 #include <ipc/ipc_space.h>
91 #include <ipc/ipc_entry.h>
92 #include <kern/mach_param.h>
93 #include <kern/misc_protos.h>
94 #include <kern/task.h>
95 #include <kern/thread.h>
96 #include <kern/zalloc.h>
97 #include <kern/kalloc.h>
98 #include <kern/processor.h>
99 #include <kern/sched_prim.h> /* for thread_wakeup */
100 #include <kern/ipc_tt.h>
101 #include <kern/ledger.h>
102 #include <kern/host.h>
103 #include <vm/vm_kern.h> /* for kernel_map, ipc_kernel_map */
104 #include <kern/profile.h>
105 #include <kern/assert.h>
106 #include <kern/sync_lock.h>
108 #include <ddb/db_sym.h>
109 #endif /* MACH_KDB */
112 #include <kern/task_swap.h>
113 #endif /* TASK_SWAPPER */
116 * Exported interfaces
119 #include <mach/task_server.h>
120 #include <mach/mach_host_server.h>
121 #include <mach/host_security_server.h>
122 #include <vm/task_working_set.h>
129 void task_hold_locked(
131 void task_wait_locked(
133 void task_release_locked(
135 void task_collect_scan(void);
138 void task_synchronizer_destroy_all(
141 kern_return_t
task_set_ledger(
151 TASK_MAX
* sizeof(struct task
),
152 TASK_CHUNK
* sizeof(struct task
),
158 * Create the kernel task as the first task.
159 * Task_create_local must assign to kernel_task as a side effect,
160 * for other initialization. (:-()
162 if (task_create_local(
163 TASK_NULL
, FALSE
, FALSE
, &kernel_task
) != KERN_SUCCESS
)
164 panic("task_init\n");
165 vm_map_deallocate(kernel_task
->map
);
166 kernel_task
->map
= kernel_map
;
169 if (watchacts
& WA_TASK
)
170 printf("task_init: kernel_task = %x map=%x\n",
171 kernel_task
, kernel_map
);
172 #endif /* MACH_ASSERT */
184 * If may_assign is false, task is already being assigned,
185 * wait for that to finish.
187 while (task
->may_assign
== FALSE
) {
190 task
->assign_active
= TRUE
;
191 res
= thread_sleep_mutex((event_t
) &task
->assign_active
,
192 &task
->lock
, THREAD_UNINT
);
193 assert(res
== THREAD_AWAKENED
);
195 task
->may_assign
= FALSE
;
200 #define thread_freeze(thread) assert(task->processor_set == &default_pset)
209 assert(task
->may_assign
== FALSE
);
210 task
->may_assign
= TRUE
;
211 if (task
->assign_active
== TRUE
) {
212 task
->assign_active
= FALSE
;
213 thread_wakeup((event_t
)&task
->assign_active
);
219 #define thread_unfreeze(thread) assert(task->processor_set == &default_pset)
222 #endif /* MACH_HOST */
225 * Create a task running in the kernel address space. It may
226 * have its own map of size mem_size and may have ipc privileges.
231 vm_offset_t map_base
,
235 kern_return_t result
;
242 result
= task_create_local(parent_task
, FALSE
, TRUE
, &new_task
);
243 if (result
!= KERN_SUCCESS
)
247 * Task_create_local creates the task with a user-space map.
248 * We attempt to replace the map and free it afterwards; else
249 * task_deallocate will free it (can NOT set map to null before
250 * task_deallocate, this impersonates a norma placeholder task).
251 * _Mark the memory as pageable_ -- this is what we
252 * want for images (like servers) loaded into the kernel.
255 vm_map_deallocate(new_task
->map
);
256 new_task
->map
= kernel_map
;
257 *child_task
= new_task
;
259 old_map
= new_task
->map
;
260 if ((result
= kmem_suballoc(kernel_map
, &map_base
,
261 map_size
, TRUE
, FALSE
,
262 &new_task
->map
)) != KERN_SUCCESS
) {
264 * New task created with ref count of 2 -- decrement by
265 * one to force task deletion.
267 printf("kmem_suballoc(%x,%x,%x,1,0,&new) Fails\n",
268 kernel_map
, map_base
, map_size
);
269 --new_task
->ref_count
;
270 task_deallocate(new_task
);
273 vm_map_deallocate(old_map
);
274 *child_task
= new_task
;
276 return (KERN_SUCCESS
);
282 ledger_port_array_t ledger_ports
,
283 mach_msg_type_number_t num_ledger_ports
,
284 boolean_t inherit_memory
,
285 task_t
*child_task
) /* OUT */
287 if (parent_task
== TASK_NULL
)
288 return(KERN_INVALID_ARGUMENT
);
290 return task_create_local(
291 parent_task
, inherit_memory
, FALSE
, child_task
);
295 host_security_create_task_token(
296 host_security_t host_security
,
298 security_token_t sec_token
,
299 host_priv_t host_priv
,
300 ledger_port_array_t ledger_ports
,
301 mach_msg_type_number_t num_ledger_ports
,
302 boolean_t inherit_memory
,
303 task_t
*child_task
) /* OUT */
305 kern_return_t result
;
307 if (parent_task
== TASK_NULL
)
308 return(KERN_INVALID_ARGUMENT
);
310 if (host_security
== HOST_NULL
)
311 return(KERN_INVALID_SECURITY
);
313 result
= task_create_local(
314 parent_task
, inherit_memory
, FALSE
, child_task
);
316 if (result
!= KERN_SUCCESS
)
319 result
= host_security_set_task_token(host_security
,
324 if (result
!= KERN_SUCCESS
)
333 boolean_t inherit_memory
,
334 boolean_t kernel_loaded
,
335 task_t
*child_task
) /* OUT */
338 processor_set_t pset
;
340 new_task
= (task_t
) zalloc(task_zone
);
342 if (new_task
== TASK_NULL
)
343 return(KERN_RESOURCE_SHORTAGE
);
345 /* one ref for just being alive; one for our caller */
346 new_task
->ref_count
= 2;
349 new_task
->map
= vm_map_fork(parent_task
->map
);
351 new_task
->map
= vm_map_create(pmap_create(0),
352 round_page(VM_MIN_ADDRESS
),
353 trunc_page(VM_MAX_ADDRESS
), TRUE
);
355 mutex_init(&new_task
->lock
, ETAP_THREAD_TASK_NEW
);
356 queue_init(&new_task
->thr_acts
);
357 new_task
->suspend_count
= 0;
358 new_task
->thr_act_count
= 0;
359 new_task
->res_act_count
= 0;
360 new_task
->active_act_count
= 0;
361 new_task
->user_stop_count
= 0;
362 new_task
->role
= TASK_UNSPECIFIED
;
363 new_task
->active
= TRUE
;
364 new_task
->kernel_loaded
= kernel_loaded
;
365 new_task
->user_data
= 0;
366 new_task
->faults
= 0;
367 new_task
->cow_faults
= 0;
368 new_task
->pageins
= 0;
369 new_task
->messages_sent
= 0;
370 new_task
->messages_received
= 0;
371 new_task
->syscalls_mach
= 0;
372 new_task
->syscalls_unix
=0;
374 new_task
->dynamic_working_set
= 0;
376 task_working_set_create(new_task
, TWS_SMALL_HASH_LINE_COUNT
,
377 0, TWS_HASH_STYLE_DEFAULT
);
380 new_task
->bsd_info
= 0;
381 #endif /* MACH_BSD */
384 new_task
->swap_state
= TASK_SW_IN
;
385 new_task
->swap_flags
= 0;
386 new_task
->swap_ast_waiting
= 0;
387 new_task
->swap_stamp
= sched_tick
;
388 new_task
->swap_rss
= 0;
389 new_task
->swap_nswap
= 0;
390 #endif /* TASK_SWAPPER */
392 queue_init(&new_task
->semaphore_list
);
393 queue_init(&new_task
->lock_set_list
);
394 new_task
->semaphores_owned
= 0;
395 new_task
->lock_sets_owned
= 0;
398 new_task
->may_assign
= TRUE
;
399 new_task
->assign_active
= FALSE
;
400 #endif /* MACH_HOST */
401 eml_task_reference(new_task
, parent_task
);
403 ipc_task_init(new_task
, parent_task
);
405 new_task
->total_user_time
.seconds
= 0;
406 new_task
->total_user_time
.microseconds
= 0;
407 new_task
->total_system_time
.seconds
= 0;
408 new_task
->total_system_time
.microseconds
= 0;
410 task_prof_init(new_task
);
412 if (parent_task
!= TASK_NULL
) {
415 * Freeze the parent, so that parent_task->processor_set
418 task_freeze(parent_task
);
419 #endif /* MACH_HOST */
420 pset
= parent_task
->processor_set
;
422 pset
= &default_pset
;
424 new_task
->sec_token
= parent_task
->sec_token
;
426 shared_region_mapping_ref(parent_task
->system_shared_region
);
427 new_task
->system_shared_region
= parent_task
->system_shared_region
;
429 new_task
->wired_ledger_port
= ledger_copy(
430 convert_port_to_ledger(parent_task
->wired_ledger_port
));
431 new_task
->paged_ledger_port
= ledger_copy(
432 convert_port_to_ledger(parent_task
->paged_ledger_port
));
435 pset
= &default_pset
;
437 new_task
->sec_token
= KERNEL_SECURITY_TOKEN
;
438 new_task
->wired_ledger_port
= ledger_copy(root_wired_ledger
);
439 new_task
->paged_ledger_port
= ledger_copy(root_paged_ledger
);
442 if (kernel_task
== TASK_NULL
) {
443 new_task
->priority
= MINPRI_KERNEL
;
444 new_task
->max_priority
= MAXPRI_KERNEL
;
447 new_task
->priority
= BASEPRI_DEFAULT
;
448 new_task
->max_priority
= MAXPRI_USER
;
452 pset_add_task(pset
, new_task
);
455 if (parent_task
!= TASK_NULL
)
456 task_unfreeze(parent_task
);
457 #endif /* MACH_HOST */
460 if (inherit_memory
) {
461 new_task
->fast_tas_base
= parent_task
->fast_tas_base
;
462 new_task
->fast_tas_end
= parent_task
->fast_tas_end
;
464 new_task
->fast_tas_base
= (vm_offset_t
)0;
465 new_task
->fast_tas_end
= (vm_offset_t
)0;
467 #endif /* FAST_TAS */
469 ipc_task_enable(new_task
);
472 task_swapout_eligible(new_task
);
473 #endif /* TASK_SWAPPER */
476 if (watchacts
& WA_TASK
)
477 printf("*** task_create_local(par=%x inh=%x) == 0x%x\n",
478 parent_task
, inherit_memory
, new_task
);
479 #endif /* MACH_ASSERT */
481 *child_task
= new_task
;
482 return(KERN_SUCCESS
);
488 * Drop a reference on a task
495 processor_set_t pset
;
498 if (task
== TASK_NULL
)
502 refs
= --task
->ref_count
;
509 /* task_terminate guarantees that this task is off the list */
510 assert((task
->swap_state
& TASK_SW_ELIGIBLE
) == 0);
511 #endif /* TASK_SWAPPER */
513 eml_task_deallocate(task
);
515 ipc_task_terminate(task
);
521 pset
= task
->processor_set
;
523 pset_remove_task(pset
,task
);
525 pset_deallocate(pset
);
531 if (task
->kernel_loaded
)
532 vm_map_remove(kernel_map
, task
->map
->min_offset
,
533 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
534 vm_map_deallocate(task
->map
);
535 is_release(task
->itk_space
);
536 task_prof_deallocate(task
);
537 zfree(task_zone
, (vm_offset_t
) task
);
545 if (task
!= TASK_NULL
) {
556 if (task
!= TASK_NULL
) {
557 if (task_lock_try(task
)) {
569 * Terminate the specified task. See comments on thread_terminate
570 * (kern/thread.c) about problems with terminating the "current task."
577 if (task
== TASK_NULL
)
578 return(KERN_INVALID_ARGUMENT
);
580 return(KERN_FAILURE
);
581 return (task_terminate_internal(task
));
585 task_terminate_internal(
588 thread_act_t thr_act
, cur_thr_act
;
590 boolean_t interrupt_save
;
592 assert(task
!= kernel_task
);
594 cur_thr_act
= current_act();
595 cur_task
= cur_thr_act
->task
;
599 * If task is not resident (swapped out, or being swapped
600 * out), we want to bring it back in (this can block).
601 * NOTE: The only way that this can happen in the current
602 * system is if the task is swapped while it has a thread
603 * in exit(), and the thread does not hit a clean point
604 * to swap itself before getting here.
605 * Terminating other tasks is another way to this code, but
606 * it is not yet fully supported.
607 * The task_swapin is unconditional. It used to be done
608 * only if the task is not resident. Swapping in a
609 * resident task will prevent it from being swapped out
610 * while it terminates.
612 task_swapin(task
, TRUE
); /* TRUE means make it unswappable */
613 #endif /* TASK_SWAPPER */
616 * Get the task locked and make sure that we are not racing
617 * with someone else trying to terminate us.
619 if (task
== cur_task
) {
621 } else if (task
< cur_task
) {
629 if (!task
->active
|| !cur_thr_act
->active
) {
631 * Task or current act is already being terminated.
632 * Just return an error. If we are dying, this will
633 * just get us to our AST special handler and that
634 * will get us to finalize the termination of ourselves.
637 if (cur_task
!= task
)
638 task_unlock(cur_task
);
639 return(KERN_FAILURE
);
641 if (cur_task
!= task
)
642 task_unlock(cur_task
);
645 * Make sure the current thread does not get aborted out of
646 * the waits inside these operations.
648 interrupt_save
= thread_interrupt_level(THREAD_UNINT
);
651 * Indicate that we want all the threads to stop executing
652 * at user space by holding the task (we would have held
653 * each thread independently in thread_terminate_internal -
654 * but this way we may be more likely to already find it
655 * held there). Mark the task inactive, and prevent
656 * further task operations via the task port.
658 task_hold_locked(task
);
659 task
->active
= FALSE
;
660 ipc_task_disable(task
);
663 * Terminate each activation in the task.
665 * Each terminated activation will run it's special handler
666 * when its current kernel context is unwound. That will
667 * clean up most of the thread resources. Then it will be
668 * handed over to the reaper, who will finally remove the
669 * thread from the task list and free the structures.
671 queue_iterate(&task
->thr_acts
, thr_act
, thread_act_t
, thr_acts
) {
672 thread_terminate_internal(thr_act
);
676 * Clean up any virtual machine state/resources associated
677 * with the current activation because it may hold wiring
678 * and other references on resources we will be trying to
681 if (cur_thr_act
->task
== task
)
682 act_virtual_machine_destroy(cur_thr_act
);
687 * Destroy all synchronizers owned by the task.
689 task_synchronizer_destroy_all(task
);
692 * Destroy the IPC space, leaving just a reference for it.
694 if (!task
->kernel_loaded
)
695 ipc_space_destroy(task
->itk_space
);
698 * If the current thread is a member of the task
699 * being terminated, then the last reference to
700 * the task will not be dropped until the thread
701 * is finally reaped. To avoid incurring the
702 * expense of removing the address space regions
703 * at reap time, we do it explictly here.
705 (void) vm_map_remove(task
->map
,
706 task
->map
->min_offset
,
707 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
709 shared_region_mapping_dealloc(task
->system_shared_region
);
711 if(task
->dynamic_working_set
)
712 tws_hash_destroy((tws_hash_t
)task
->dynamic_working_set
);
715 * We no longer need to guard against being aborted, so restore
716 * the previous interruptible state.
718 thread_interrupt_level(interrupt_save
);
721 * Get rid of the task active reference on itself.
723 task_deallocate(task
);
725 return(KERN_SUCCESS
);
729 * task_halt - Shut the current task down (except for the current thread) in
730 * preparation for dramatic changes to the task (probably exec).
731 * We hold the task, terminate all other threads in the task and
732 * wait for them to terminate, clean up the portspace, and when
733 * all done, let the current thread go.
739 thread_act_t thr_act
, cur_thr_act
;
742 assert(task
!= kernel_task
);
744 cur_thr_act
= current_act();
745 cur_task
= cur_thr_act
->task
;
747 if (task
!= cur_task
) {
748 return(KERN_INVALID_ARGUMENT
);
753 * If task is not resident (swapped out, or being swapped
754 * out), we want to bring it back in and make it unswappable.
755 * This can block, so do it early.
757 task_swapin(task
, TRUE
); /* TRUE means make it unswappable */
758 #endif /* TASK_SWAPPER */
762 if (!task
->active
|| !cur_thr_act
->active
) {
764 * Task or current thread is already being terminated.
765 * Hurry up and return out of the current kernel context
766 * so that we run our AST special handler to terminate
770 return(KERN_FAILURE
);
773 if (task
->thr_act_count
> 1) {
775 * Mark all the threads to keep them from starting any more
776 * user-level execution. The thread_terminate_internal code
777 * would do this on a thread by thread basis anyway, but this
778 * gives us a better chance of not having to wait there.
780 task_hold_locked(task
);
783 * Terminate all the other activations in the task.
785 * Each terminated activation will run it's special handler
786 * when its current kernel context is unwound. That will
787 * clean up most of the thread resources. Then it will be
788 * handed over to the reaper, who will finally remove the
789 * thread from the task list and free the structures.
791 queue_iterate(&task
->thr_acts
, thr_act
, thread_act_t
,thr_acts
) {
792 if (thr_act
!= cur_thr_act
)
793 thread_terminate_internal(thr_act
);
795 task_release_locked(task
);
799 * If the current thread has any virtual machine state
800 * associated with it, we need to explicitly clean that
801 * up now (because we did not terminate the current act)
802 * before we try to clean up the task VM and port spaces.
804 act_virtual_machine_destroy(cur_thr_act
);
809 * Destroy all synchronizers owned by the task.
811 task_synchronizer_destroy_all(task
);
814 * Destroy the contents of the IPC space, leaving just
815 * a reference for it.
817 if (!task
->kernel_loaded
)
818 ipc_space_clean(task
->itk_space
);
821 * Clean out the address space, as we are going to be
824 (void) vm_map_remove(task
->map
,
825 task
->map
->min_offset
,
826 task
->map
->max_offset
, VM_MAP_NO_FLAGS
);
834 * Suspend execution of the specified task.
835 * This is a recursive-style suspension of the task, a count of
836 * suspends is maintained.
838 * CONDITIONS: the task is locked and active.
842 register task_t task
)
844 register thread_act_t thr_act
;
846 assert(task
->active
);
848 if (task
->suspend_count
++ > 0)
852 * Iterate through all the thread_act's and hold them.
854 queue_iterate(&task
->thr_acts
, thr_act
, thread_act_t
, thr_acts
) {
855 act_lock_thread(thr_act
);
856 thread_hold(thr_act
);
857 act_unlock_thread(thr_act
);
864 * Same as the internal routine above, except that is must lock
865 * and verify that the task is active. This differs from task_suspend
866 * in that it places a kernel hold on the task rather than just a
867 * user-level hold. This keeps users from over resuming and setting
868 * it running out from under the kernel.
870 * CONDITIONS: the caller holds a reference on the task
873 task_hold(task_t task
)
877 if (task
== TASK_NULL
)
878 return (KERN_INVALID_ARGUMENT
);
882 return (KERN_FAILURE
);
884 task_hold_locked(task
);
887 return(KERN_SUCCESS
);
891 * Routine: task_wait_locked
892 * Wait for all threads in task to stop.
895 * Called with task locked, active, and held.
899 register task_t task
)
901 register thread_act_t thr_act
, cur_thr_act
;
903 assert(task
->active
);
904 assert(task
->suspend_count
> 0);
906 cur_thr_act
= current_act();
908 * Iterate through all the thread's and wait for them to
909 * stop. Do not wait for the current thread if it is within
912 queue_iterate(&task
->thr_acts
, thr_act
, thread_act_t
, thr_acts
) {
913 if (thr_act
!= cur_thr_act
) {
914 thread_shuttle_t thr_shuttle
;
916 thr_shuttle
= act_lock_thread(thr_act
);
917 thread_wait(thr_shuttle
);
918 act_unlock_thread(thr_act
);
924 * task_release_locked:
926 * Release a kernel hold on a task.
928 * CONDITIONS: the task is locked and active
932 register task_t task
)
934 register thread_act_t thr_act
;
936 assert(task
->active
);
937 assert(task
->suspend_count
> 0);
939 if (--task
->suspend_count
> 0)
943 * Iterate through all the thread_act's and hold them.
944 * Do not hold the current thread_act if it is within the
947 queue_iterate(&task
->thr_acts
, thr_act
, thread_act_t
, thr_acts
) {
948 act_lock_thread(thr_act
);
949 thread_release(thr_act
);
950 act_unlock_thread(thr_act
);
957 * Same as the internal routine above, except that it must lock
958 * and verify that the task is active.
960 * CONDITIONS: The caller holds a reference to the task
963 task_release(task_t task
)
967 if (task
== TASK_NULL
)
968 return (KERN_INVALID_ARGUMENT
);
972 return (KERN_FAILURE
);
974 task_release_locked(task
);
977 return(KERN_SUCCESS
);
983 thread_act_array_t
*thr_act_list
,
984 mach_msg_type_number_t
*count
)
986 unsigned int actual
; /* this many thr_acts */
987 thread_act_t thr_act
;
988 thread_act_t
*thr_acts
;
992 vm_size_t size
, size_needed
;
995 if (task
== TASK_NULL
)
996 return KERN_INVALID_ARGUMENT
;
1002 if (!task
->active
) {
1006 return KERN_FAILURE
;
1009 actual
= task
->thr_act_count
;
1011 /* do we have the memory we need? */
1012 size_needed
= actual
* sizeof(mach_port_t
);
1013 if (size_needed
<= size
)
1016 /* unlock the task and allocate more memory */
1022 assert(size_needed
> 0);
1025 addr
= kalloc(size
);
1027 return KERN_RESOURCE_SHORTAGE
;
1030 /* OK, have memory and the task is locked & active */
1031 thr_acts
= (thread_act_t
*) addr
;
1033 for (i
= j
= 0, thr_act
= (thread_act_t
) queue_first(&task
->thr_acts
);
1035 i
++, thr_act
= (thread_act_t
) queue_next(&thr_act
->thr_acts
)) {
1037 if (thr_act
->ref_count
> 0) {
1038 act_locked_act_reference(thr_act
);
1039 thr_acts
[j
++] = thr_act
;
1041 act_unlock(thr_act
);
1043 assert(queue_end(&task
->thr_acts
, (queue_entry_t
) thr_act
));
1046 size_needed
= actual
* sizeof(mach_port_t
);
1048 /* can unlock task now that we've got the thr_act refs */
1052 /* no thr_acts, so return null pointer and deallocate memory */
1060 /* if we allocated too much, must copy */
1062 if (size_needed
< size
) {
1063 vm_offset_t newaddr
;
1065 newaddr
= kalloc(size_needed
);
1067 for (i
= 0; i
< actual
; i
++)
1068 act_deallocate(thr_acts
[i
]);
1070 return KERN_RESOURCE_SHORTAGE
;
1073 bcopy((char *) addr
, (char *) newaddr
, size_needed
);
1075 thr_acts
= (thread_act_t
*) newaddr
;
1078 *thr_act_list
= thr_acts
;
1081 /* do the conversion that Mig should handle */
1083 for (i
= 0; i
< actual
; i
++)
1084 ((ipc_port_t
*) thr_acts
)[i
] =
1085 convert_act_to_port(thr_acts
[i
]);
1088 return KERN_SUCCESS
;
1092 * Routine: task_suspend
1093 * Implement a user-level suspension on a task.
1096 * The caller holds a reference to the task
1100 register task_t task
)
1102 if (task
== TASK_NULL
)
1103 return (KERN_INVALID_ARGUMENT
);
1106 if (!task
->active
) {
1108 return (KERN_FAILURE
);
1110 if ((task
->user_stop_count
)++ > 0) {
1112 * If the stop count was positive, the task is
1113 * already stopped and we can exit.
1116 return (KERN_SUCCESS
);
1120 * Put a kernel-level hold on the threads in the task (all
1121 * user-level task suspensions added together represent a
1122 * single kernel-level hold). We then wait for the threads
1123 * to stop executing user code.
1125 task_hold_locked(task
);
1126 task_wait_locked(task
);
1128 return (KERN_SUCCESS
);
1132 * Routine: task_resume
1133 * Release a kernel hold on a task.
1136 * The caller holds a reference to the task
1139 task_resume(register task_t task
)
1141 register boolean_t release
;
1143 if (task
== TASK_NULL
)
1144 return(KERN_INVALID_ARGUMENT
);
1148 if (!task
->active
) {
1150 return(KERN_FAILURE
);
1152 if (task
->user_stop_count
> 0) {
1153 if (--(task
->user_stop_count
) == 0)
1158 return(KERN_FAILURE
);
1162 * Release the task if necessary.
1165 task_release_locked(task
);
1168 return(KERN_SUCCESS
);
1172 host_security_set_task_token(
1173 host_security_t host_security
,
1175 security_token_t sec_token
,
1176 host_priv_t host_priv
)
1180 if (task
== TASK_NULL
)
1181 return(KERN_INVALID_ARGUMENT
);
1183 if (host_security
== HOST_NULL
)
1184 return(KERN_INVALID_SECURITY
);
1187 task
->sec_token
= sec_token
;
1190 if (host_priv
!= HOST_PRIV_NULL
) {
1191 kr
= task_set_special_port(task
,
1193 ipc_port_make_send(realhost
.host_priv_self
));
1195 kr
= task_set_special_port(task
,
1197 ipc_port_make_send(realhost
.host_self
));
1203 * Utility routine to set a ledger
1211 if (task
== TASK_NULL
)
1212 return(KERN_INVALID_ARGUMENT
);
1216 ipc_port_release_send(task
->wired_ledger_port
);
1217 task
->wired_ledger_port
= ledger_copy(wired
);
1220 ipc_port_release_send(task
->paged_ledger_port
);
1221 task
->paged_ledger_port
= ledger_copy(paged
);
1225 return(KERN_SUCCESS
);
1229 * This routine was added, pretty much exclusively, for registering the
1230 * RPC glue vector for in-kernel short circuited tasks. Rather than
1231 * removing it completely, I have only disabled that feature (which was
1232 * the only feature at the time). It just appears that we are going to
1233 * want to add some user data to tasks in the future (i.e. bsd info,
1234 * task names, etc...), so I left it in the formal task interface.
1239 task_flavor_t flavor
,
1240 task_info_t task_info_in
, /* pointer to IN array */
1241 mach_msg_type_number_t task_info_count
)
1245 if (task
== TASK_NULL
)
1246 return(KERN_INVALID_ARGUMENT
);
1250 return (KERN_INVALID_ARGUMENT
);
1252 return (KERN_SUCCESS
);
1258 task_flavor_t flavor
,
1259 task_info_t task_info_out
,
1260 mach_msg_type_number_t
*task_info_count
)
1265 if (task
== TASK_NULL
)
1266 return(KERN_INVALID_ARGUMENT
);
1270 case TASK_BASIC_INFO
:
1272 register task_basic_info_t basic_info
;
1274 if (*task_info_count
< TASK_BASIC_INFO_COUNT
) {
1275 return(KERN_INVALID_ARGUMENT
);
1278 basic_info
= (task_basic_info_t
) task_info_out
;
1280 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
1282 basic_info
->virtual_size
= map
->size
;
1283 basic_info
->resident_size
= pmap_resident_count(map
->pmap
)
1287 basic_info
->policy
= ((task
!= kernel_task
)?
1288 POLICY_TIMESHARE
: POLICY_RR
);
1289 basic_info
->suspend_count
= task
->user_stop_count
;
1290 basic_info
->user_time
.seconds
1291 = task
->total_user_time
.seconds
;
1292 basic_info
->user_time
.microseconds
1293 = task
->total_user_time
.microseconds
;
1294 basic_info
->system_time
.seconds
1295 = task
->total_system_time
.seconds
;
1296 basic_info
->system_time
.microseconds
1297 = task
->total_system_time
.microseconds
;
1300 *task_info_count
= TASK_BASIC_INFO_COUNT
;
1304 case TASK_THREAD_TIMES_INFO
:
1306 register task_thread_times_info_t times_info
;
1307 register thread_t thread
;
1308 register thread_act_t thr_act
;
1310 if (*task_info_count
< TASK_THREAD_TIMES_INFO_COUNT
) {
1311 return (KERN_INVALID_ARGUMENT
);
1314 times_info
= (task_thread_times_info_t
) task_info_out
;
1315 times_info
->user_time
.seconds
= 0;
1316 times_info
->user_time
.microseconds
= 0;
1317 times_info
->system_time
.seconds
= 0;
1318 times_info
->system_time
.microseconds
= 0;
1321 queue_iterate(&task
->thr_acts
, thr_act
,
1322 thread_act_t
, thr_acts
)
1324 time_value_t user_time
, system_time
;
1327 thread
= act_lock_thread(thr_act
);
1329 /* JMM - add logic to skip threads that have migrated
1333 assert(thread
); /* Must have thread */
1335 thread_lock(thread
);
1337 thread_read_times(thread
, &user_time
, &system_time
);
1339 thread_unlock(thread
);
1341 act_unlock_thread(thr_act
);
1343 time_value_add(×_info
->user_time
, &user_time
);
1344 time_value_add(×_info
->system_time
, &system_time
);
1348 *task_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1352 case TASK_SCHED_FIFO_INFO
:
1355 if (*task_info_count
< POLICY_FIFO_BASE_COUNT
)
1356 return(KERN_INVALID_ARGUMENT
);
1358 return(KERN_INVALID_POLICY
);
1361 case TASK_SCHED_RR_INFO
:
1363 register policy_rr_base_t rr_base
;
1365 if (*task_info_count
< POLICY_RR_BASE_COUNT
)
1366 return(KERN_INVALID_ARGUMENT
);
1368 rr_base
= (policy_rr_base_t
) task_info_out
;
1371 if (task
!= kernel_task
) {
1373 return(KERN_INVALID_POLICY
);
1376 rr_base
->base_priority
= task
->priority
;
1379 rr_base
->quantum
= tick
/ 1000;
1381 *task_info_count
= POLICY_RR_BASE_COUNT
;
1385 case TASK_SCHED_TIMESHARE_INFO
:
1387 register policy_timeshare_base_t ts_base
;
1389 if (*task_info_count
< POLICY_TIMESHARE_BASE_COUNT
)
1390 return(KERN_INVALID_ARGUMENT
);
1392 ts_base
= (policy_timeshare_base_t
) task_info_out
;
1395 if (task
== kernel_task
) {
1397 return(KERN_INVALID_POLICY
);
1400 ts_base
->base_priority
= task
->priority
;
1403 *task_info_count
= POLICY_TIMESHARE_BASE_COUNT
;
1407 case TASK_SECURITY_TOKEN
:
1409 register security_token_t
*sec_token_p
;
1411 if (*task_info_count
< TASK_SECURITY_TOKEN_COUNT
) {
1412 return(KERN_INVALID_ARGUMENT
);
1415 sec_token_p
= (security_token_t
*) task_info_out
;
1418 *sec_token_p
= task
->sec_token
;
1421 *task_info_count
= TASK_SECURITY_TOKEN_COUNT
;
1425 case TASK_SCHED_INFO
:
1426 return(KERN_INVALID_ARGUMENT
);
1428 case TASK_EVENTS_INFO
:
1430 register task_events_info_t events_info
;
1432 if (*task_info_count
< TASK_EVENTS_INFO_COUNT
) {
1433 return(KERN_INVALID_ARGUMENT
);
1436 events_info
= (task_events_info_t
) task_info_out
;
1439 events_info
->faults
= task
->faults
;
1440 events_info
->pageins
= task
->pageins
;
1441 events_info
->cow_faults
= task
->cow_faults
;
1442 events_info
->messages_sent
= task
->messages_sent
;
1443 events_info
->messages_received
= task
->messages_received
;
1444 events_info
->syscalls_mach
= task
->syscalls_mach
;
1445 events_info
->syscalls_unix
= task
->syscalls_unix
;
1446 events_info
->csw
= task
->csw
;
1449 *task_info_count
= TASK_EVENTS_INFO_COUNT
;
1454 return (KERN_INVALID_ARGUMENT
);
1457 return(KERN_SUCCESS
);
1463 * Change the assigned processor set for the task
1468 processor_set_t new_pset
,
1469 boolean_t assign_threads
)
1472 task
++; new_pset
++; assign_threads
++;
1474 return(KERN_FAILURE
);
1478 * task_assign_default:
1480 * Version of task_assign to assign to default processor set.
1483 task_assign_default(
1485 boolean_t assign_threads
)
1487 return (task_assign(task
, &default_pset
, assign_threads
));
1491 * task_get_assignment
1493 * Return name of processor set that task is assigned to.
1496 task_get_assignment(
1498 processor_set_t
*pset
)
1501 return(KERN_FAILURE
);
1503 *pset
= task
->processor_set
;
1504 pset_reference(*pset
);
1505 return(KERN_SUCCESS
);
1512 * Set scheduling policy and parameters, both base and limit, for
1513 * the given task. Policy must be a policy which is enabled for the
1514 * processor set. Change contained threads if requested.
1521 mach_msg_type_number_t count
,
1522 boolean_t set_limit
,
1525 return(KERN_FAILURE
);
1531 * Set scheduling policy and parameters, both base and limit, for
1532 * the given task. Policy can be any policy implemented by the
1533 * processor set, whether enabled or not. Change contained threads
1539 processor_set_t pset
,
1542 mach_msg_type_number_t base_count
,
1543 policy_limit_t limit
,
1544 mach_msg_type_number_t limit_count
,
1547 return(KERN_FAILURE
);
1551 * task_collect_scan:
1553 * Attempt to free resources owned by tasks.
1557 task_collect_scan(void)
1559 register task_t task
, prev_task
;
1560 processor_set_t pset
= &default_pset
;
1564 task
= (task_t
) queue_first(&pset
->tasks
);
1565 while (!queue_end(&pset
->tasks
, (queue_entry_t
) task
)) {
1567 if (task
->ref_count
> 0) {
1569 task_reference_locked(task
);
1574 * While we still have the pset locked, freeze the task in
1575 * this pset. That way, when we get back from collecting
1576 * it, we can dereference the pset_tasks chain for the task
1577 * and be assured that we are still in this chain.
1584 pmap_collect(task
->map
->pmap
);
1588 task
= (task_t
) queue_next(&task
->pset_tasks
);
1591 task_unfreeze(prev_task
);
1594 task_deallocate(prev_task
);
1597 task
= (task_t
) queue_next(&task
->pset_tasks
);
1603 pset_deallocate(pset
);
1606 /* Also disabled in vm/vm_pageout.c */
1607 boolean_t task_collect_allowed
= FALSE
;
1608 unsigned task_collect_last_tick
= 0;
1609 unsigned task_collect_max_rate
= 0; /* in ticks */
1612 * consider_task_collect:
1614 * Called by the pageout daemon when the system needs more free pages.
1618 consider_task_collect(void)
1621 * By default, don't attempt task collection more frequently
1622 * than once per second.
1625 if (task_collect_max_rate
== 0)
1626 task_collect_max_rate
= (1 << SCHED_TICK_SHIFT
) + 1;
1628 if (task_collect_allowed
&&
1629 (sched_tick
> (task_collect_last_tick
+ task_collect_max_rate
))) {
1630 task_collect_last_tick
= sched_tick
;
1631 task_collect_scan();
1642 extern int fast_tas_debug
;
1644 if (fast_tas_debug
) {
1645 printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
1649 task
->fast_tas_base
= pc
;
1650 task
->fast_tas_end
= endpc
;
1652 return KERN_SUCCESS
;
1654 #else /* FAST_TAS */
1661 return KERN_FAILURE
;
1663 #endif /* FAST_TAS */
1667 task_synchronizer_destroy_all(task_t task
)
1669 semaphore_t semaphore
;
1670 lock_set_t lock_set
;
1673 * Destroy owned semaphores
1676 while (!queue_empty(&task
->semaphore_list
)) {
1677 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
1678 (void) semaphore_destroy(task
, semaphore
);
1682 * Destroy owned lock sets
1685 while (!queue_empty(&task
->lock_set_list
)) {
1686 lock_set
= (lock_set_t
) queue_first(&task
->lock_set_list
);
1687 (void) lock_set_destroy(task
, lock_set
);
1692 * task_set_port_space:
1694 * Set port name space of task to specified size.
1698 task_set_port_space(
1704 is_write_lock(task
->itk_space
);
1705 kr
= ipc_entry_grow_table(task
->itk_space
, table_entries
);
1706 if (kr
== KERN_SUCCESS
)
1707 is_write_unlock(task
->itk_space
);
1712 * We need to export some functions to other components that
1713 * are currently implemented in macros within the osfmk
1714 * component. Just export them as functions of the same name.
1716 boolean_t
is_kerneltask(task_t t
)
1718 if (t
== kernel_task
)
1721 return((t
->kernel_loaded
));
1725 task_t
current_task()
1727 return (current_task_fast());