#include <mach_kdb.h>
#include <mach_host.h>
#include <mach_prof.h>
#include <fast_tas.h>
#include <task_swapper.h>
#include <platforms.h>
#include <mach/boolean.h>
#include <mach/machine/vm_types.h>
#include <mach/vm_param.h>
#include <mach/semaphore.h>
#include <mach/task_info.h>
#include <mach/task_special_ports.h>
#include <mach/mach_types.h>
#include <ipc/ipc_space.h>
#include <ipc/ipc_entry.h>
#include <kern/mach_param.h>
#include <kern/misc_protos.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/zalloc.h>
#include <kern/kalloc.h>
#include <kern/processor.h>
#include <kern/sched_prim.h>
#include <kern/ipc_tt.h>
#include <kern/ledger.h>
#include <kern/host.h>
#include <vm/vm_kern.h>
#include <kern/profile.h>
#include <kern/assert.h>
#include <kern/sync_lock.h>
#if MACH_KDB
#include <ddb/db_sym.h>
#endif
#if TASK_SWAPPER
#include <kern/task_swap.h>
#endif
#include <mach/task_server.h>
#include <mach/mach_host_server.h>
#include <mach/host_security_server.h>
#include <vm/task_working_set.h>
task_t kernel_task;
zone_t task_zone;
void task_hold_locked(
task_t task);
void task_wait_locked(
task_t task);
void task_release_locked(
task_t task);
void task_collect_scan(void);
void task_free(
task_t task );
void task_synchronizer_destroy_all(
task_t task);
kern_return_t task_set_ledger(
task_t task,
ledger_t wired,
ledger_t paged);
void
task_init(void)
{
task_zone = zinit(
sizeof(struct task),
TASK_MAX * sizeof(struct task),
TASK_CHUNK * sizeof(struct task),
"tasks");
eml_init();
if (task_create_local(
TASK_NULL, FALSE, FALSE, &kernel_task) != KERN_SUCCESS)
panic("task_init\n");
vm_map_deallocate(kernel_task->map);
kernel_task->map = kernel_map;
#if MACH_ASSERT
if (watchacts & WA_TASK)
printf("task_init: kernel_task = %x map=%x\n",
kernel_task, kernel_map);
#endif
}
#if MACH_HOST
#if 0
static void
task_freeze(
task_t task)
{
task_lock(task);
while (task->may_assign == FALSE) {
wait_result_t res;
task->assign_active = TRUE;
res = thread_sleep_mutex((event_t) &task->assign_active,
&task->lock, THREAD_UNINT);
assert(res == THREAD_AWAKENED);
}
task->may_assign = FALSE;
task_unlock(task);
return;
}
#else
#define thread_freeze(thread) assert(task->processor_set == &default_pset)
#endif
#if 0
static void
task_unfreeze(
task_t task)
{
task_lock(task);
assert(task->may_assign == FALSE);
task->may_assign = TRUE;
if (task->assign_active == TRUE) {
task->assign_active = FALSE;
thread_wakeup((event_t)&task->assign_active);
}
task_unlock(task);
return;
}
#else
#define thread_unfreeze(thread) assert(task->processor_set == &default_pset)
#endif
#endif
kern_return_t
kernel_task_create(
task_t parent_task,
vm_offset_t map_base,
vm_size_t map_size,
task_t *child_task)
{
kern_return_t result;
task_t new_task;
vm_map_t old_map;
result = task_create_local(parent_task, FALSE, TRUE, &new_task);
if (result != KERN_SUCCESS)
return (result);
if (map_size == 0) {
vm_map_deallocate(new_task->map);
new_task->map = kernel_map;
*child_task = new_task;
} else {
old_map = new_task->map;
if ((result = kmem_suballoc(kernel_map, &map_base,
map_size, TRUE, FALSE,
&new_task->map)) != KERN_SUCCESS) {
printf("kmem_suballoc(%x,%x,%x,1,0,&new) Fails\n",
kernel_map, map_base, map_size);
--new_task->ref_count;
task_deallocate(new_task);
return (result);
}
vm_map_deallocate(old_map);
*child_task = new_task;
}
return (KERN_SUCCESS);
}
kern_return_t
task_create(
task_t parent_task,
ledger_port_array_t ledger_ports,
mach_msg_type_number_t num_ledger_ports,
boolean_t inherit_memory,
task_t *child_task)
{
if (parent_task == TASK_NULL)
return(KERN_INVALID_ARGUMENT);
return task_create_local(
parent_task, inherit_memory, FALSE, child_task);
}
kern_return_t
host_security_create_task_token(
host_security_t host_security,
task_t parent_task,
security_token_t sec_token,
host_priv_t host_priv,
ledger_port_array_t ledger_ports,
mach_msg_type_number_t num_ledger_ports,
boolean_t inherit_memory,
task_t *child_task)
{
kern_return_t result;
if (parent_task == TASK_NULL)
return(KERN_INVALID_ARGUMENT);
if (host_security == HOST_NULL)
return(KERN_INVALID_SECURITY);
result = task_create_local(
parent_task, inherit_memory, FALSE, child_task);
if (result != KERN_SUCCESS)
return(result);
result = host_security_set_task_token(host_security,
*child_task,
sec_token,
host_priv);
if (result != KERN_SUCCESS)
return(result);
return(result);
}
kern_return_t
task_create_local(
task_t parent_task,
boolean_t inherit_memory,
boolean_t kernel_loaded,
task_t *child_task)
{
task_t new_task;
processor_set_t pset;
new_task = (task_t) zalloc(task_zone);
if (new_task == TASK_NULL)
return(KERN_RESOURCE_SHORTAGE);
new_task->ref_count = 2;
if (inherit_memory)
new_task->map = vm_map_fork(parent_task->map);
else
new_task->map = vm_map_create(pmap_create(0),
round_page(VM_MIN_ADDRESS),
trunc_page(VM_MAX_ADDRESS), TRUE);
mutex_init(&new_task->lock, ETAP_THREAD_TASK_NEW);
queue_init(&new_task->thr_acts);
new_task->suspend_count = 0;
new_task->thr_act_count = 0;
new_task->res_act_count = 0;
new_task->active_act_count = 0;
new_task->user_stop_count = 0;
new_task->role = TASK_UNSPECIFIED;
new_task->active = TRUE;
new_task->kernel_loaded = kernel_loaded;
new_task->user_data = 0;
new_task->faults = 0;
new_task->cow_faults = 0;
new_task->pageins = 0;
new_task->messages_sent = 0;
new_task->messages_received = 0;
new_task->syscalls_mach = 0;
new_task->syscalls_unix=0;
new_task->csw=0;
new_task->dynamic_working_set = 0;
task_working_set_create(new_task, TWS_SMALL_HASH_LINE_COUNT,
0, TWS_HASH_STYLE_DEFAULT);
#ifdef MACH_BSD
new_task->bsd_info = 0;
#endif
#if TASK_SWAPPER
new_task->swap_state = TASK_SW_IN;
new_task->swap_flags = 0;
new_task->swap_ast_waiting = 0;
new_task->swap_stamp = sched_tick;
new_task->swap_rss = 0;
new_task->swap_nswap = 0;
#endif
queue_init(&new_task->semaphore_list);
queue_init(&new_task->lock_set_list);
new_task->semaphores_owned = 0;
new_task->lock_sets_owned = 0;
#if MACH_HOST
new_task->may_assign = TRUE;
new_task->assign_active = FALSE;
#endif
eml_task_reference(new_task, parent_task);
ipc_task_init(new_task, parent_task);
new_task->total_user_time.seconds = 0;
new_task->total_user_time.microseconds = 0;
new_task->total_system_time.seconds = 0;
new_task->total_system_time.microseconds = 0;
task_prof_init(new_task);
if (parent_task != TASK_NULL) {
#if MACH_HOST
task_freeze(parent_task);
#endif
pset = parent_task->processor_set;
if (!pset->active)
pset = &default_pset;
new_task->sec_token = parent_task->sec_token;
shared_region_mapping_ref(parent_task->system_shared_region);
new_task->system_shared_region = parent_task->system_shared_region;
new_task->wired_ledger_port = ledger_copy(
convert_port_to_ledger(parent_task->wired_ledger_port));
new_task->paged_ledger_port = ledger_copy(
convert_port_to_ledger(parent_task->paged_ledger_port));
}
else {
pset = &default_pset;
new_task->sec_token = KERNEL_SECURITY_TOKEN;
new_task->wired_ledger_port = ledger_copy(root_wired_ledger);
new_task->paged_ledger_port = ledger_copy(root_paged_ledger);
}
if (kernel_task == TASK_NULL) {
new_task->priority = MINPRI_KERNEL;
new_task->max_priority = MAXPRI_KERNEL;
}
else {
new_task->priority = BASEPRI_DEFAULT;
new_task->max_priority = MAXPRI_USER;
}
pset_lock(pset);
pset_add_task(pset, new_task);
pset_unlock(pset);
#if MACH_HOST
if (parent_task != TASK_NULL)
task_unfreeze(parent_task);
#endif
#if FAST_TAS
if (inherit_memory) {
new_task->fast_tas_base = parent_task->fast_tas_base;
new_task->fast_tas_end = parent_task->fast_tas_end;
} else {
new_task->fast_tas_base = (vm_offset_t)0;
new_task->fast_tas_end = (vm_offset_t)0;
}
#endif
ipc_task_enable(new_task);
#if TASK_SWAPPER
task_swapout_eligible(new_task);
#endif
#if MACH_ASSERT
if (watchacts & WA_TASK)
printf("*** task_create_local(par=%x inh=%x) == 0x%x\n",
parent_task, inherit_memory, new_task);
#endif
*child_task = new_task;
return(KERN_SUCCESS);
}
void
task_deallocate(
task_t task)
{
processor_set_t pset;
int refs;
if (task == TASK_NULL)
return;
task_lock(task);
refs = --task->ref_count;
task_unlock(task);
if (refs > 0)
return;
#if TASK_SWAPPER
assert((task->swap_state & TASK_SW_ELIGIBLE) == 0);
#endif
if(task->dynamic_working_set)
tws_hash_destroy((tws_hash_t)task->dynamic_working_set);
eml_task_deallocate(task);
ipc_task_terminate(task);
#if MACH_HOST
task_freeze(task);
#endif
pset = task->processor_set;
pset_lock(pset);
pset_remove_task(pset,task);
pset_unlock(pset);
pset_deallocate(pset);
#if MACH_HOST
task_unfreeze(task);
#endif
if (task->kernel_loaded)
vm_map_remove(kernel_map, task->map->min_offset,
task->map->max_offset, VM_MAP_NO_FLAGS);
vm_map_deallocate(task->map);
is_release(task->itk_space);
task_prof_deallocate(task);
zfree(task_zone, (vm_offset_t) task);
}
void
task_reference(
task_t task)
{
if (task != TASK_NULL) {
task_lock(task);
task->ref_count++;
task_unlock(task);
}
}
boolean_t
task_reference_try(
task_t task)
{
if (task != TASK_NULL) {
if (task_lock_try(task)) {
task->ref_count++;
task_unlock(task);
return TRUE;
}
}
return FALSE;
}
kern_return_t
task_terminate(
task_t task)
{
if (task == TASK_NULL)
return(KERN_INVALID_ARGUMENT);
if (task->bsd_info)
return(KERN_FAILURE);
return (task_terminate_internal(task));
}
kern_return_t
task_terminate_internal(
task_t task)
{
thread_act_t thr_act, cur_thr_act;
task_t cur_task;
boolean_t interrupt_save;
assert(task != kernel_task);
cur_thr_act = current_act();
cur_task = cur_thr_act->task;
#if TASK_SWAPPER
task_swapin(task, TRUE);
#endif
if (task == cur_task) {
task_lock(task);
} else if (task < cur_task) {
task_lock(task);
task_lock(cur_task);
} else {
task_lock(cur_task);
task_lock(task);
}
if (!task->active || !cur_thr_act->active) {
task_unlock(task);
if (cur_task != task)
task_unlock(cur_task);
return(KERN_FAILURE);
}
if (cur_task != task)
task_unlock(cur_task);
interrupt_save = thread_interrupt_level(THREAD_UNINT);
task_hold_locked(task);
task->active = FALSE;
ipc_task_disable(task);
queue_iterate(&task->thr_acts, thr_act, thread_act_t, thr_acts) {
thread_terminate_internal(thr_act);
}
if (cur_thr_act->task == task)
act_virtual_machine_destroy(cur_thr_act);
task_unlock(task);
task_synchronizer_destroy_all(task);
if (!task->kernel_loaded)
ipc_space_destroy(task->itk_space);
(void) vm_map_remove(task->map,
task->map->min_offset,
task->map->max_offset, VM_MAP_NO_FLAGS);
shared_region_mapping_dealloc(task->system_shared_region);
if(task->dynamic_working_set)
tws_hash_ws_flush((tws_hash_t)
task->dynamic_working_set);
thread_interrupt_level(interrupt_save);
task_deallocate(task);
return(KERN_SUCCESS);
}
kern_return_t
task_halt(
task_t task)
{
thread_act_t thr_act, cur_thr_act;
task_t cur_task;
assert(task != kernel_task);
cur_thr_act = current_act();
cur_task = cur_thr_act->task;
if (task != cur_task) {
return(KERN_INVALID_ARGUMENT);
}
#if TASK_SWAPPER
task_swapin(task, TRUE);
#endif
task_lock(task);
if (!task->active || !cur_thr_act->active) {
task_unlock(task);
return(KERN_FAILURE);
}
if (task->thr_act_count > 1) {
task_hold_locked(task);
queue_iterate(&task->thr_acts, thr_act, thread_act_t,thr_acts) {
if (thr_act != cur_thr_act)
thread_terminate_internal(thr_act);
}
task_release_locked(task);
}
act_virtual_machine_destroy(cur_thr_act);
task_unlock(task);
task_synchronizer_destroy_all(task);
if (!task->kernel_loaded)
ipc_space_clean(task->itk_space);
(void) vm_map_remove(task->map,
task->map->min_offset,
task->map->max_offset, VM_MAP_NO_FLAGS);
return KERN_SUCCESS;
}
void
task_hold_locked(
register task_t task)
{
register thread_act_t thr_act;
assert(task->active);
if (task->suspend_count++ > 0)
return;
queue_iterate(&task->thr_acts, thr_act, thread_act_t, thr_acts) {
act_lock_thread(thr_act);
thread_hold(thr_act);
act_unlock_thread(thr_act);
}
}
kern_return_t
task_hold(task_t task)
{
kern_return_t kret;
if (task == TASK_NULL)
return (KERN_INVALID_ARGUMENT);
task_lock(task);
if (!task->active) {
task_unlock(task);
return (KERN_FAILURE);
}
task_hold_locked(task);
task_unlock(task);
return(KERN_SUCCESS);
}
void
task_wait_locked(
register task_t task)
{
register thread_act_t thr_act, cur_thr_act;
assert(task->active);
assert(task->suspend_count > 0);
cur_thr_act = current_act();
queue_iterate(&task->thr_acts, thr_act, thread_act_t, thr_acts) {
if (thr_act != cur_thr_act) {
thread_shuttle_t thr_shuttle;
thr_shuttle = act_lock_thread(thr_act);
thread_wait(thr_shuttle);
act_unlock_thread(thr_act);
}
}
}
void
task_release_locked(
register task_t task)
{
register thread_act_t thr_act;
assert(task->active);
assert(task->suspend_count > 0);
if (--task->suspend_count > 0)
return;
queue_iterate(&task->thr_acts, thr_act, thread_act_t, thr_acts) {
act_lock_thread(thr_act);
thread_release(thr_act);
act_unlock_thread(thr_act);
}
}
kern_return_t
task_release(task_t task)
{
kern_return_t kret;
if (task == TASK_NULL)
return (KERN_INVALID_ARGUMENT);
task_lock(task);
if (!task->active) {
task_unlock(task);
return (KERN_FAILURE);
}
task_release_locked(task);
task_unlock(task);
return(KERN_SUCCESS);
}
kern_return_t
task_threads(
task_t task,
thread_act_array_t *thr_act_list,
mach_msg_type_number_t *count)
{
unsigned int actual;
thread_act_t thr_act;
thread_act_t *thr_acts;
thread_t thread;
int i, j;
vm_size_t size, size_needed;
vm_offset_t addr;
if (task == TASK_NULL)
return KERN_INVALID_ARGUMENT;
size = 0; addr = 0;
for (;;) {
task_lock(task);
if (!task->active) {
task_unlock(task);
if (size != 0)
kfree(addr, size);
return KERN_FAILURE;
}
actual = task->thr_act_count;
size_needed = actual * sizeof(mach_port_t);
if (size_needed <= size)
break;
task_unlock(task);
if (size != 0)
kfree(addr, size);
assert(size_needed > 0);
size = size_needed;
addr = kalloc(size);
if (addr == 0)
return KERN_RESOURCE_SHORTAGE;
}
thr_acts = (thread_act_t *) addr;
for (i = j = 0, thr_act = (thread_act_t) queue_first(&task->thr_acts);
i < actual;
i++, thr_act = (thread_act_t) queue_next(&thr_act->thr_acts)) {
act_lock(thr_act);
if (thr_act->ref_count > 0) {
act_locked_act_reference(thr_act);
thr_acts[j++] = thr_act;
}
act_unlock(thr_act);
}
assert(queue_end(&task->thr_acts, (queue_entry_t) thr_act));
actual = j;
size_needed = actual * sizeof(mach_port_t);
task_unlock(task);
if (actual == 0) {
*thr_act_list = 0;
*count = 0;
if (size != 0)
kfree(addr, size);
} else {
if (size_needed < size) {
vm_offset_t newaddr;
newaddr = kalloc(size_needed);
if (newaddr == 0) {
for (i = 0; i < actual; i++)
act_deallocate(thr_acts[i]);
kfree(addr, size);
return KERN_RESOURCE_SHORTAGE;
}
bcopy((char *) addr, (char *) newaddr, size_needed);
kfree(addr, size);
thr_acts = (thread_act_t *) newaddr;
}
*thr_act_list = thr_acts;
*count = actual;
for (i = 0; i < actual; i++)
((ipc_port_t *) thr_acts)[i] =
convert_act_to_port(thr_acts[i]);
}
return KERN_SUCCESS;
}
kern_return_t
task_suspend(
register task_t task)
{
if (task == TASK_NULL)
return (KERN_INVALID_ARGUMENT);
task_lock(task);
if (!task->active) {
task_unlock(task);
return (KERN_FAILURE);
}
if ((task->user_stop_count)++ > 0) {
task_unlock(task);
return (KERN_SUCCESS);
}
task_hold_locked(task);
task_wait_locked(task);
task_unlock(task);
return (KERN_SUCCESS);
}
kern_return_t
task_resume(register task_t task)
{
register boolean_t release;
if (task == TASK_NULL)
return(KERN_INVALID_ARGUMENT);
release = FALSE;
task_lock(task);
if (!task->active) {
task_unlock(task);
return(KERN_FAILURE);
}
if (task->user_stop_count > 0) {
if (--(task->user_stop_count) == 0)
release = TRUE;
}
else {
task_unlock(task);
return(KERN_FAILURE);
}
if (release)
task_release_locked(task);
task_unlock(task);
return(KERN_SUCCESS);
}
kern_return_t
host_security_set_task_token(
host_security_t host_security,
task_t task,
security_token_t sec_token,
host_priv_t host_priv)
{
kern_return_t kr;
if (task == TASK_NULL)
return(KERN_INVALID_ARGUMENT);
if (host_security == HOST_NULL)
return(KERN_INVALID_SECURITY);
task_lock(task);
task->sec_token = sec_token;
task_unlock(task);
if (host_priv != HOST_PRIV_NULL) {
kr = task_set_special_port(task,
TASK_HOST_PORT,
ipc_port_make_send(realhost.host_priv_self));
} else {
kr = task_set_special_port(task,
TASK_HOST_PORT,
ipc_port_make_send(realhost.host_self));
}
return(kr);
}
kern_return_t
task_set_ledger(
task_t task,
ledger_t wired,
ledger_t paged)
{
if (task == TASK_NULL)
return(KERN_INVALID_ARGUMENT);
task_lock(task);
if (wired) {
ipc_port_release_send(task->wired_ledger_port);
task->wired_ledger_port = ledger_copy(wired);
}
if (paged) {
ipc_port_release_send(task->paged_ledger_port);
task->paged_ledger_port = ledger_copy(paged);
}
task_unlock(task);
return(KERN_SUCCESS);
}
kern_return_t
task_set_info(
task_t task,
task_flavor_t flavor,
task_info_t task_info_in,
mach_msg_type_number_t task_info_count)
{
vm_map_t map;
if (task == TASK_NULL)
return(KERN_INVALID_ARGUMENT);
switch (flavor) {
default:
return (KERN_INVALID_ARGUMENT);
}
return (KERN_SUCCESS);
}
kern_return_t
task_info(
task_t task,
task_flavor_t flavor,
task_info_t task_info_out,
mach_msg_type_number_t *task_info_count)
{
thread_t thread;
vm_map_t map;
if (task == TASK_NULL)
return(KERN_INVALID_ARGUMENT);
switch (flavor) {
case TASK_BASIC_INFO:
{
register task_basic_info_t basic_info;
if (*task_info_count < TASK_BASIC_INFO_COUNT) {
return(KERN_INVALID_ARGUMENT);
}
basic_info = (task_basic_info_t) task_info_out;
map = (task == kernel_task) ? kernel_map : task->map;
basic_info->virtual_size = map->size;
basic_info->resident_size = pmap_resident_count(map->pmap)
* PAGE_SIZE;
task_lock(task);
basic_info->policy = ((task != kernel_task)?
POLICY_TIMESHARE: POLICY_RR);
basic_info->suspend_count = task->user_stop_count;
basic_info->user_time.seconds
= task->total_user_time.seconds;
basic_info->user_time.microseconds
= task->total_user_time.microseconds;
basic_info->system_time.seconds
= task->total_system_time.seconds;
basic_info->system_time.microseconds
= task->total_system_time.microseconds;
task_unlock(task);
*task_info_count = TASK_BASIC_INFO_COUNT;
break;
}
case TASK_THREAD_TIMES_INFO:
{
register task_thread_times_info_t times_info;
register thread_t thread;
register thread_act_t thr_act;
if (*task_info_count < TASK_THREAD_TIMES_INFO_COUNT) {
return (KERN_INVALID_ARGUMENT);
}
times_info = (task_thread_times_info_t) task_info_out;
times_info->user_time.seconds = 0;
times_info->user_time.microseconds = 0;
times_info->system_time.seconds = 0;
times_info->system_time.microseconds = 0;
task_lock(task);
queue_iterate(&task->thr_acts, thr_act,
thread_act_t, thr_acts)
{
time_value_t user_time, system_time;
spl_t s;
thread = act_lock_thread(thr_act);
assert(thread);
s = splsched();
thread_lock(thread);
thread_read_times(thread, &user_time, &system_time);
thread_unlock(thread);
splx(s);
act_unlock_thread(thr_act);
time_value_add(×_info->user_time, &user_time);
time_value_add(×_info->system_time, &system_time);
}
task_unlock(task);
*task_info_count = TASK_THREAD_TIMES_INFO_COUNT;
break;
}
case TASK_SCHED_FIFO_INFO:
{
if (*task_info_count < POLICY_FIFO_BASE_COUNT)
return(KERN_INVALID_ARGUMENT);
return(KERN_INVALID_POLICY);
}
case TASK_SCHED_RR_INFO:
{
register policy_rr_base_t rr_base;
if (*task_info_count < POLICY_RR_BASE_COUNT)
return(KERN_INVALID_ARGUMENT);
rr_base = (policy_rr_base_t) task_info_out;
task_lock(task);
if (task != kernel_task) {
task_unlock(task);
return(KERN_INVALID_POLICY);
}
rr_base->base_priority = task->priority;
task_unlock(task);
rr_base->quantum = tick / 1000;
*task_info_count = POLICY_RR_BASE_COUNT;
break;
}
case TASK_SCHED_TIMESHARE_INFO:
{
register policy_timeshare_base_t ts_base;
if (*task_info_count < POLICY_TIMESHARE_BASE_COUNT)
return(KERN_INVALID_ARGUMENT);
ts_base = (policy_timeshare_base_t) task_info_out;
task_lock(task);
if (task == kernel_task) {
task_unlock(task);
return(KERN_INVALID_POLICY);
}
ts_base->base_priority = task->priority;
task_unlock(task);
*task_info_count = POLICY_TIMESHARE_BASE_COUNT;
break;
}
case TASK_SECURITY_TOKEN:
{
register security_token_t *sec_token_p;
if (*task_info_count < TASK_SECURITY_TOKEN_COUNT) {
return(KERN_INVALID_ARGUMENT);
}
sec_token_p = (security_token_t *) task_info_out;
task_lock(task);
*sec_token_p = task->sec_token;
task_unlock(task);
*task_info_count = TASK_SECURITY_TOKEN_COUNT;
break;
}
case TASK_SCHED_INFO:
return(KERN_INVALID_ARGUMENT);
case TASK_EVENTS_INFO:
{
register task_events_info_t events_info;
if (*task_info_count < TASK_EVENTS_INFO_COUNT) {
return(KERN_INVALID_ARGUMENT);
}
events_info = (task_events_info_t) task_info_out;
task_lock(task);
events_info->faults = task->faults;
events_info->pageins = task->pageins;
events_info->cow_faults = task->cow_faults;
events_info->messages_sent = task->messages_sent;
events_info->messages_received = task->messages_received;
events_info->syscalls_mach = task->syscalls_mach;
events_info->syscalls_unix = task->syscalls_unix;
events_info->csw = task->csw;
task_unlock(task);
*task_info_count = TASK_EVENTS_INFO_COUNT;
break;
}
default:
return (KERN_INVALID_ARGUMENT);
}
return(KERN_SUCCESS);
}
kern_return_t
task_assign(
task_t task,
processor_set_t new_pset,
boolean_t assign_threads)
{
#ifdef lint
task++; new_pset++; assign_threads++;
#endif
return(KERN_FAILURE);
}
kern_return_t
task_assign_default(
task_t task,
boolean_t assign_threads)
{
return (task_assign(task, &default_pset, assign_threads));
}
kern_return_t
task_get_assignment(
task_t task,
processor_set_t *pset)
{
if (!task->active)
return(KERN_FAILURE);
*pset = task->processor_set;
pset_reference(*pset);
return(KERN_SUCCESS);
}
kern_return_t
task_policy(
task_t task,
policy_t policy_id,
policy_base_t base,
mach_msg_type_number_t count,
boolean_t set_limit,
boolean_t change)
{
return(KERN_FAILURE);
}
kern_return_t
task_set_policy(
task_t task,
processor_set_t pset,
policy_t policy_id,
policy_base_t base,
mach_msg_type_number_t base_count,
policy_limit_t limit,
mach_msg_type_number_t limit_count,
boolean_t change)
{
return(KERN_FAILURE);
}
void
task_collect_scan(void)
{
register task_t task, prev_task;
processor_set_t pset = &default_pset;
pset_lock(pset);
pset->ref_count++;
task = (task_t) queue_first(&pset->tasks);
while (!queue_end(&pset->tasks, (queue_entry_t) task)) {
task_lock(task);
if (task->ref_count > 0) {
task_reference_locked(task);
task_unlock(task);
#if MACH_HOST
task_freeze(task);
#endif
pset_unlock(pset);
pmap_collect(task->map->pmap);
pset_lock(pset);
prev_task = task;
task = (task_t) queue_next(&task->pset_tasks);
#if MACH_HOST
task_unfreeze(prev_task);
#endif
task_deallocate(prev_task);
} else {
task_unlock(task);
task = (task_t) queue_next(&task->pset_tasks);
}
}
pset_unlock(pset);
pset_deallocate(pset);
}
boolean_t task_collect_allowed = FALSE;
unsigned task_collect_last_tick = 0;
unsigned task_collect_max_rate = 0;
void
consider_task_collect(void)
{
if (task_collect_max_rate == 0)
task_collect_max_rate = (1 << SCHED_TICK_SHIFT) + 1;
if (task_collect_allowed &&
(sched_tick > (task_collect_last_tick + task_collect_max_rate))) {
task_collect_last_tick = sched_tick;
task_collect_scan();
}
}
kern_return_t
task_set_ras_pc(
task_t task,
vm_offset_t pc,
vm_offset_t endpc)
{
#if FAST_TAS
extern int fast_tas_debug;
if (fast_tas_debug) {
printf("task 0x%x: setting fast_tas to [0x%x, 0x%x]\n",
task, pc, endpc);
}
task_lock(task);
task->fast_tas_base = pc;
task->fast_tas_end = endpc;
task_unlock(task);
return KERN_SUCCESS;
#else
#ifdef lint
task++;
pc++;
endpc++;
#endif
return KERN_FAILURE;
#endif
}
void
task_synchronizer_destroy_all(task_t task)
{
semaphore_t semaphore;
lock_set_t lock_set;
while (!queue_empty(&task->semaphore_list)) {
semaphore = (semaphore_t) queue_first(&task->semaphore_list);
(void) semaphore_destroy(task, semaphore);
}
while (!queue_empty(&task->lock_set_list)) {
lock_set = (lock_set_t) queue_first(&task->lock_set_list);
(void) lock_set_destroy(task, lock_set);
}
}
kern_return_t
task_set_port_space(
task_t task,
int table_entries)
{
kern_return_t kr;
is_write_lock(task->itk_space);
kr = ipc_entry_grow_table(task->itk_space, table_entries);
if (kr == KERN_SUCCESS)
is_write_unlock(task->itk_space);
return kr;
}
boolean_t is_kerneltask(task_t t)
{
if (t == kernel_task)
return(TRUE);
else
return((t->kernel_loaded));
}
#undef current_task
task_t current_task()
{
return (current_task_fast());
}