+ return (KERN_INVALID_ARGUMENT);
+ }
+
+ switch (flavor) {
+
+ case TASK_BASIC_INFO_32:
+ case TASK_BASIC2_INFO_32:
+ {
+ task_basic_info_32_t basic_info;
+ vm_map_t map;
+ clock_sec_t secs;
+ clock_usec_t usecs;
+
+ if (*task_info_count < TASK_BASIC_INFO_32_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ basic_info = (task_basic_info_32_t)task_info_out;
+
+ map = (task == kernel_task)? kernel_map: task->map;
+ basic_info->virtual_size = (typeof(basic_info->virtual_size))map->size;
+ if (flavor == TASK_BASIC2_INFO_32) {
+ /*
+ * The "BASIC2" flavor gets the maximum resident
+ * size instead of the current resident size...
+ */
+ basic_info->resident_size = pmap_resident_max(map->pmap);
+ } else {
+ basic_info->resident_size = pmap_resident_count(map->pmap);
+ }
+ basic_info->resident_size *= PAGE_SIZE;
+
+ basic_info->policy = ((task != kernel_task)?
+ POLICY_TIMESHARE: POLICY_RR);
+ basic_info->suspend_count = task->user_stop_count;
+
+ absolutetime_to_microtime(task->total_user_time, &secs, &usecs);
+ basic_info->user_time.seconds =
+ (typeof(basic_info->user_time.seconds))secs;
+ basic_info->user_time.microseconds = usecs;
+
+ absolutetime_to_microtime(task->total_system_time, &secs, &usecs);
+ basic_info->system_time.seconds =
+ (typeof(basic_info->system_time.seconds))secs;
+ basic_info->system_time.microseconds = usecs;
+
+ *task_info_count = TASK_BASIC_INFO_32_COUNT;
+ break;
+ }
+
+ case TASK_BASIC_INFO_64:
+ {
+ task_basic_info_64_t basic_info;
+ vm_map_t map;
+ clock_sec_t secs;
+ clock_usec_t usecs;
+
+ if (*task_info_count < TASK_BASIC_INFO_64_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ basic_info = (task_basic_info_64_t)task_info_out;
+
+ map = (task == kernel_task)? kernel_map: task->map;
+ basic_info->virtual_size = map->size;
+ basic_info->resident_size =
+ (mach_vm_size_t)(pmap_resident_count(map->pmap))
+ * PAGE_SIZE_64;
+
+ basic_info->policy = ((task != kernel_task)?
+ POLICY_TIMESHARE: POLICY_RR);
+ basic_info->suspend_count = task->user_stop_count;
+
+ absolutetime_to_microtime(task->total_user_time, &secs, &usecs);
+ basic_info->user_time.seconds =
+ (typeof(basic_info->user_time.seconds))secs;
+ basic_info->user_time.microseconds = usecs;
+
+ absolutetime_to_microtime(task->total_system_time, &secs, &usecs);
+ basic_info->system_time.seconds =
+ (typeof(basic_info->system_time.seconds))secs;
+ basic_info->system_time.microseconds = usecs;
+
+ *task_info_count = TASK_BASIC_INFO_64_COUNT;
+ break;
+ }
+
+ case MACH_TASK_BASIC_INFO:
+ {
+ mach_task_basic_info_t basic_info;
+ vm_map_t map;
+ clock_sec_t secs;
+ clock_usec_t usecs;
+
+ if (*task_info_count < MACH_TASK_BASIC_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ basic_info = (mach_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 =
+ (mach_vm_size_t)(pmap_resident_count(map->pmap));
+ basic_info->resident_size *= PAGE_SIZE_64;
+
+ basic_info->resident_size_max =
+ (mach_vm_size_t)(pmap_resident_max(map->pmap));
+ basic_info->resident_size_max *= PAGE_SIZE_64;
+
+ basic_info->policy = ((task != kernel_task) ?
+ POLICY_TIMESHARE : POLICY_RR);
+
+ basic_info->suspend_count = task->user_stop_count;
+
+ absolutetime_to_microtime(task->total_user_time, &secs, &usecs);
+ basic_info->user_time.seconds =
+ (typeof(basic_info->user_time.seconds))secs;
+ basic_info->user_time.microseconds = usecs;
+
+ absolutetime_to_microtime(task->total_system_time, &secs, &usecs);
+ basic_info->system_time.seconds =
+ (typeof(basic_info->system_time.seconds))secs;
+ basic_info->system_time.microseconds = usecs;
+
+ *task_info_count = MACH_TASK_BASIC_INFO_COUNT;
+ break;
+ }
+
+ case TASK_THREAD_TIMES_INFO:
+ {
+ register task_thread_times_info_t times_info;
+ register thread_t thread;
+
+ if (*task_info_count < TASK_THREAD_TIMES_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ 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;
+
+
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ time_value_t user_time, system_time;
+
+ if (thread->options & TH_OPT_IDLE_THREAD)
+ continue;
+
+ thread_read_times(thread, &user_time, &system_time);
+
+ time_value_add(×_info->user_time, &user_time);
+ time_value_add(×_info->system_time, &system_time);
+ }
+
+ *task_info_count = TASK_THREAD_TIMES_INFO_COUNT;
+ break;
+ }
+
+ case TASK_ABSOLUTETIME_INFO:
+ {
+ task_absolutetime_info_t info;
+ register thread_t thread;
+
+ if (*task_info_count < TASK_ABSOLUTETIME_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ info = (task_absolutetime_info_t)task_info_out;
+ info->threads_user = info->threads_system = 0;
+
+
+ info->total_user = task->total_user_time;
+ info->total_system = task->total_system_time;
+
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ uint64_t tval;
+ spl_t x;
+
+ if (thread->options & TH_OPT_IDLE_THREAD)
+ continue;
+
+ x = splsched();
+ thread_lock(thread);
+
+ tval = timer_grab(&thread->user_timer);
+ info->threads_user += tval;
+ info->total_user += tval;
+
+ tval = timer_grab(&thread->system_timer);
+ if (thread->precise_user_kernel_time) {
+ info->threads_system += tval;
+ info->total_system += tval;
+ } else {
+ /* system_timer may represent either sys or user */
+ info->threads_user += tval;
+ info->total_user += tval;
+ }
+
+ thread_unlock(thread);
+ splx(x);
+ }
+
+
+ *task_info_count = TASK_ABSOLUTETIME_INFO_COUNT;
+ break;
+ }
+
+ case TASK_DYLD_INFO:
+ {
+ task_dyld_info_t info;
+
+ /*
+ * We added the format field to TASK_DYLD_INFO output. For
+ * temporary backward compatibility, accept the fact that
+ * clients may ask for the old version - distinquished by the
+ * size of the expected result structure.
+ */
+#define TASK_LEGACY_DYLD_INFO_COUNT \
+ offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
+
+ if (*task_info_count < TASK_LEGACY_DYLD_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ info = (task_dyld_info_t)task_info_out;
+ info->all_image_info_addr = task->all_image_info_addr;
+ info->all_image_info_size = task->all_image_info_size;
+
+ /* only set format on output for those expecting it */
+ if (*task_info_count >= TASK_DYLD_INFO_COUNT) {
+ info->all_image_info_format = task_has_64BitAddr(task) ?
+ TASK_DYLD_ALL_IMAGE_INFO_64 :
+ TASK_DYLD_ALL_IMAGE_INFO_32 ;
+ *task_info_count = TASK_DYLD_INFO_COUNT;
+ } else {
+ *task_info_count = TASK_LEGACY_DYLD_INFO_COUNT;
+ }
+ break;
+ }
+
+ case TASK_EXTMOD_INFO:
+ {
+ task_extmod_info_t info;
+ void *p;
+
+ if (*task_info_count < TASK_EXTMOD_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ info = (task_extmod_info_t)task_info_out;
+
+ p = get_bsdtask_info(task);
+ if (p) {
+ proc_getexecutableuuid(p, info->task_uuid, sizeof(info->task_uuid));
+ } else {
+ bzero(info->task_uuid, sizeof(info->task_uuid));
+ }
+ info->extmod_statistics = task->extmod_statistics;
+ *task_info_count = TASK_EXTMOD_INFO_COUNT;
+
+ break;
+ }
+
+ case TASK_KERNELMEMORY_INFO:
+ {
+ task_kernelmemory_info_t tkm_info;
+ ledger_amount_t credit, debit;
+
+ if (*task_info_count < TASK_KERNELMEMORY_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ tkm_info = (task_kernelmemory_info_t) task_info_out;
+ tkm_info->total_palloc = 0;
+ tkm_info->total_pfree = 0;
+ tkm_info->total_salloc = 0;
+ tkm_info->total_sfree = 0;
+
+ if (task == kernel_task) {
+ /*
+ * All shared allocs/frees from other tasks count against
+ * the kernel private memory usage. If we are looking up
+ * info for the kernel task, gather from everywhere.
+ */
+ task_unlock(task);
+
+ /* start by accounting for all the terminated tasks against the kernel */
+ tkm_info->total_palloc = tasks_tkm_private.alloc + tasks_tkm_shared.alloc;
+ tkm_info->total_pfree = tasks_tkm_private.free + tasks_tkm_shared.free;
+
+ /* count all other task/thread shared alloc/free against the kernel */
+ lck_mtx_lock(&tasks_threads_lock);
+
+ /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
+ queue_iterate(&tasks, task, task_t, tasks) {
+ if (task == kernel_task) {
+ if (ledger_get_entries(task->ledger,
+ task_ledgers.tkm_private, &credit,
+ &debit) == KERN_SUCCESS) {
+ tkm_info->total_palloc += credit;
+ tkm_info->total_pfree += debit;
+ }
+ }
+ if (!ledger_get_entries(task->ledger,
+ task_ledgers.tkm_shared, &credit, &debit)) {
+ tkm_info->total_palloc += credit;
+ tkm_info->total_pfree += debit;
+ }
+ }
+ lck_mtx_unlock(&tasks_threads_lock);
+ } else {
+ if (!ledger_get_entries(task->ledger,
+ task_ledgers.tkm_private, &credit, &debit)) {
+ tkm_info->total_palloc = credit;
+ tkm_info->total_pfree = debit;
+ }
+ if (!ledger_get_entries(task->ledger,
+ task_ledgers.tkm_shared, &credit, &debit)) {
+ tkm_info->total_salloc = credit;
+ tkm_info->total_sfree = debit;
+ }
+ task_unlock(task);
+ }
+
+ *task_info_count = TASK_KERNELMEMORY_INFO_COUNT;
+ return KERN_SUCCESS;
+ }
+
+ /* OBSOLETE */
+ case TASK_SCHED_FIFO_INFO:
+ {
+
+ if (*task_info_count < POLICY_FIFO_BASE_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ error = KERN_INVALID_POLICY;
+ break;
+ }
+
+ /* OBSOLETE */
+ case TASK_SCHED_RR_INFO:
+ {
+ register policy_rr_base_t rr_base;
+ uint32_t quantum_time;
+ uint64_t quantum_ns;
+
+ if (*task_info_count < POLICY_RR_BASE_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ rr_base = (policy_rr_base_t) task_info_out;
+
+ if (task != kernel_task) {
+ error = KERN_INVALID_POLICY;
+ break;
+ }
+
+ rr_base->base_priority = task->priority;
+
+ quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
+ absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
+
+ rr_base->quantum = (uint32_t)(quantum_ns / 1000 / 1000);
+
+ *task_info_count = POLICY_RR_BASE_COUNT;
+ break;
+ }
+
+ /* OBSOLETE */
+ case TASK_SCHED_TIMESHARE_INFO:
+ {
+ register policy_timeshare_base_t ts_base;
+
+ if (*task_info_count < POLICY_TIMESHARE_BASE_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ ts_base = (policy_timeshare_base_t) task_info_out;
+
+ if (task == kernel_task) {
+ error = KERN_INVALID_POLICY;
+ break;
+ }
+
+ ts_base->base_priority = task->priority;
+
+ *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) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ sec_token_p = (security_token_t *) task_info_out;
+
+ *sec_token_p = task->sec_token;
+
+ *task_info_count = TASK_SECURITY_TOKEN_COUNT;
+ break;
+ }
+
+ case TASK_AUDIT_TOKEN:
+ {
+ register audit_token_t *audit_token_p;
+
+ if (*task_info_count < TASK_AUDIT_TOKEN_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ audit_token_p = (audit_token_t *) task_info_out;
+
+ *audit_token_p = task->audit_token;
+
+ *task_info_count = TASK_AUDIT_TOKEN_COUNT;
+ break;
+ }
+
+ case TASK_SCHED_INFO:
+ error = KERN_INVALID_ARGUMENT;
+ break;
+
+ case TASK_EVENTS_INFO:
+ {
+ register task_events_info_t events_info;
+ register thread_t thread;
+
+ if (*task_info_count < TASK_EVENTS_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ events_info = (task_events_info_t) task_info_out;
+
+
+ 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->c_switch;
+
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ events_info->csw += thread->c_switch;
+ events_info->syscalls_mach += thread->syscalls_mach;
+ events_info->syscalls_unix += thread->syscalls_unix;
+ }
+
+
+ *task_info_count = TASK_EVENTS_INFO_COUNT;
+ break;
+ }
+ case TASK_AFFINITY_TAG_INFO:
+ {
+ if (*task_info_count < TASK_AFFINITY_TAG_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ error = task_affinity_info(task, task_info_out, task_info_count);
+ break;
+ }
+ case TASK_POWER_INFO:
+ {
+ if (*task_info_count < TASK_POWER_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ task_power_info_locked(task, (task_power_info_t)task_info_out, NULL);
+ break;
+ }
+
+ case TASK_POWER_INFO_V2:
+ {
+ if (*task_info_count < TASK_POWER_INFO_V2_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+ task_power_info_v2_t tpiv2 = (task_power_info_v2_t) task_info_out;
+ task_power_info_locked(task, &tpiv2->cpu_energy, &tpiv2->gpu_energy);
+ break;
+ }
+
+ case TASK_VM_INFO:
+ case TASK_VM_INFO_PURGEABLE:
+ {
+ task_vm_info_t vm_info;
+ vm_map_t map;
+
+ if (*task_info_count < TASK_VM_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ vm_info = (task_vm_info_t)task_info_out;
+
+ if (task == kernel_task) {
+ map = kernel_map;
+ /* no lock */
+ } else {
+ map = task->map;
+ vm_map_lock_read(map);
+ }
+
+ vm_info->virtual_size = (typeof(vm_info->virtual_size))map->size;
+ vm_info->region_count = map->hdr.nentries;
+ vm_info->page_size = vm_map_page_size(map);
+
+ vm_info->resident_size = pmap_resident_count(map->pmap);
+ vm_info->resident_size *= PAGE_SIZE;
+ vm_info->resident_size_peak = pmap_resident_max(map->pmap);
+ vm_info->resident_size_peak *= PAGE_SIZE;
+
+#define _VM_INFO(_name) \
+ vm_info->_name = ((mach_vm_size_t) map->pmap->stats._name) * PAGE_SIZE
+
+ _VM_INFO(device);
+ _VM_INFO(device_peak);
+ _VM_INFO(external);
+ _VM_INFO(external_peak);
+ _VM_INFO(internal);
+ _VM_INFO(internal_peak);
+ _VM_INFO(reusable);
+ _VM_INFO(reusable_peak);
+ _VM_INFO(compressed);
+ _VM_INFO(compressed_peak);
+ _VM_INFO(compressed_lifetime);
+
+ vm_info->purgeable_volatile_pmap = 0;
+ vm_info->purgeable_volatile_resident = 0;
+ vm_info->purgeable_volatile_virtual = 0;
+ if (task == kernel_task) {
+ /*
+ * We do not maintain the detailed stats for the
+ * kernel_pmap, so just count everything as
+ * "internal"...
+ */
+ vm_info->internal = vm_info->resident_size;
+ /*
+ * ... but since the memory held by the VM compressor
+ * in the kernel address space ought to be attributed
+ * to user-space tasks, we subtract it from "internal"
+ * to give memory reporting tools a more accurate idea
+ * of what the kernel itself is actually using, instead
+ * of making it look like the kernel is leaking memory
+ * when the system is under memory pressure.
+ */
+ vm_info->internal -= (VM_PAGE_COMPRESSOR_COUNT *
+ PAGE_SIZE);
+ } else {
+ mach_vm_size_t volatile_virtual_size;
+ mach_vm_size_t volatile_resident_size;
+ mach_vm_size_t volatile_pmap_size;
+ kern_return_t kr;
+
+ if (flavor == TASK_VM_INFO_PURGEABLE) {
+ kr = vm_map_query_volatile(
+ map,
+ &volatile_virtual_size,
+ &volatile_resident_size,
+ &volatile_pmap_size);
+ if (kr == KERN_SUCCESS) {
+ vm_info->purgeable_volatile_pmap =
+ volatile_pmap_size;
+ vm_info->purgeable_volatile_resident =
+ volatile_resident_size;
+ vm_info->purgeable_volatile_virtual =
+ volatile_virtual_size;
+ }
+ }
+ vm_map_unlock_read(map);
+ }
+
+ *task_info_count = TASK_VM_INFO_COUNT;
+ break;
+ }
+
+ case TASK_WAIT_STATE_INFO:
+ {
+ /*
+ * Deprecated flavor. Currently allowing some results until all users
+ * stop calling it. The results may not be accurate.
+ */
+ task_wait_state_info_t wait_state_info;
+ uint64_t total_sfi_ledger_val = 0;
+
+ if (*task_info_count < TASK_WAIT_STATE_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ wait_state_info = (task_wait_state_info_t) task_info_out;
+
+ wait_state_info->total_wait_state_time = 0;
+ bzero(wait_state_info->_reserved, sizeof(wait_state_info->_reserved));
+
+ int i, prev_lentry = -1;
+ int64_t val_credit, val_debit;
+
+ for (i = 0; i < MAX_SFI_CLASS_ID; i++){
+ val_credit =0;
+ /*
+ * checking with prev_lentry != entry ensures adjacent classes
+ * which share the same ledger do not add wait times twice.
+ * Note: Use ledger() call to get data for each individual sfi class.
+ */
+ if (prev_lentry != task_ledgers.sfi_wait_times[i] &&
+ KERN_SUCCESS == ledger_get_entries(task->ledger,
+ task_ledgers.sfi_wait_times[i], &val_credit, &val_debit)) {
+ total_sfi_ledger_val += val_credit;
+ }
+ prev_lentry = task_ledgers.sfi_wait_times[i];
+ }
+
+ wait_state_info->total_wait_sfi_state_time = total_sfi_ledger_val;
+ *task_info_count = TASK_WAIT_STATE_INFO_COUNT;
+
+ break;
+ }
+
+ default:
+ error = KERN_INVALID_ARGUMENT;
+ }
+
+ task_unlock(task);
+ return (error);
+}
+
+/*
+ * task_power_info
+ *
+ * Returns power stats for the task.
+ * Note: Called with task locked.
+ */
+void
+task_power_info_locked(
+ task_t task,
+ task_power_info_t info,
+ gpu_energy_data_t ginfo)
+{
+ thread_t thread;
+ ledger_amount_t tmp;
+
+ task_lock_assert_owned(task);
+
+ ledger_get_entries(task->ledger, task_ledgers.interrupt_wakeups,
+ (ledger_amount_t *)&info->task_interrupt_wakeups, &tmp);
+ ledger_get_entries(task->ledger, task_ledgers.platform_idle_wakeups,
+ (ledger_amount_t *)&info->task_platform_idle_wakeups, &tmp);
+
+ info->task_timer_wakeups_bin_1 = task->task_timer_wakeups_bin_1;
+ info->task_timer_wakeups_bin_2 = task->task_timer_wakeups_bin_2;
+
+ info->total_user = task->total_user_time;
+ info->total_system = task->total_system_time;
+
+ if (ginfo) {
+ ginfo->task_gpu_utilisation = task->task_gpu_ns;
+ }
+
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ uint64_t tval;
+ spl_t x;
+
+ if (thread->options & TH_OPT_IDLE_THREAD)
+ continue;
+
+ x = splsched();
+ thread_lock(thread);
+
+ info->task_timer_wakeups_bin_1 += thread->thread_timer_wakeups_bin_1;
+ info->task_timer_wakeups_bin_2 += thread->thread_timer_wakeups_bin_2;
+
+ tval = timer_grab(&thread->user_timer);
+ info->total_user += tval;
+
+ tval = timer_grab(&thread->system_timer);
+ if (thread->precise_user_kernel_time) {
+ info->total_system += tval;
+ } else {
+ /* system_timer may represent either sys or user */
+ info->total_user += tval;
+ }
+
+ if (ginfo) {
+ ginfo->task_gpu_utilisation += ml_gpu_stat(thread);
+ }
+ thread_unlock(thread);
+ splx(x);
+ }
+}
+
+/*
+ * task_gpu_utilisation
+ *
+ * Returns the total gpu time used by the all the threads of the task
+ * (both dead and alive)
+ */
+uint64_t
+task_gpu_utilisation(
+ task_t task)
+{
+ uint64_t gpu_time = 0;
+ thread_t thread;
+
+ task_lock(task);
+ gpu_time += task->task_gpu_ns;
+
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ spl_t x;
+ x = splsched();
+ thread_lock(thread);
+ gpu_time += ml_gpu_stat(thread);
+ thread_unlock(thread);
+ splx(x);
+ }
+
+ task_unlock(task);
+ return gpu_time;
+}
+
+kern_return_t
+task_purgable_info(
+ task_t task,
+ task_purgable_info_t *stats)
+{
+ if (task == TASK_NULL || stats == NULL)
+ return KERN_INVALID_ARGUMENT;
+ /* Take task reference */
+ task_reference(task);
+ vm_purgeable_stats((vm_purgeable_info_t)stats, task);
+ /* Drop task reference */
+ task_deallocate(task);
+ return KERN_SUCCESS;
+}
+
+void
+task_vtimer_set(
+ task_t task,
+ integer_t which)
+{
+ thread_t thread;
+ spl_t x;
+
+ /* assert(task == current_task()); */ /* bogus assert 4803227 4807483 */
+
+ task_lock(task);
+
+ task->vtimers |= which;
+
+ switch (which) {
+
+ case TASK_VTIMER_USER:
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ x = splsched();
+ thread_lock(thread);
+ if (thread->precise_user_kernel_time)
+ thread->vtimer_user_save = timer_grab(&thread->user_timer);
+ else
+ thread->vtimer_user_save = timer_grab(&thread->system_timer);
+ thread_unlock(thread);
+ splx(x);
+ }
+ break;
+
+ case TASK_VTIMER_PROF:
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ x = splsched();
+ thread_lock(thread);
+ thread->vtimer_prof_save = timer_grab(&thread->user_timer);
+ thread->vtimer_prof_save += timer_grab(&thread->system_timer);
+ thread_unlock(thread);
+ splx(x);
+ }
+ break;
+
+ case TASK_VTIMER_RLIM:
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ x = splsched();
+ thread_lock(thread);
+ thread->vtimer_rlim_save = timer_grab(&thread->user_timer);
+ thread->vtimer_rlim_save += timer_grab(&thread->system_timer);
+ thread_unlock(thread);
+ splx(x);
+ }
+ break;
+ }
+
+ task_unlock(task);
+}
+
+void
+task_vtimer_clear(
+ task_t task,
+ integer_t which)
+{
+ assert(task == current_task());
+
+ task_lock(task);
+
+ task->vtimers &= ~which;
+
+ task_unlock(task);
+}
+
+void
+task_vtimer_update(
+__unused
+ task_t task,
+ integer_t which,
+ uint32_t *microsecs)
+{
+ thread_t thread = current_thread();
+ uint32_t tdelt;
+ clock_sec_t secs;
+ uint64_t tsum;
+
+ assert(task == current_task());
+
+ assert(task->vtimers & which);
+
+ secs = tdelt = 0;
+
+ switch (which) {
+
+ case TASK_VTIMER_USER:
+ if (thread->precise_user_kernel_time) {
+ tdelt = (uint32_t)timer_delta(&thread->user_timer,
+ &thread->vtimer_user_save);
+ } else {
+ tdelt = (uint32_t)timer_delta(&thread->system_timer,
+ &thread->vtimer_user_save);
+ }
+ absolutetime_to_microtime(tdelt, &secs, microsecs);
+ break;
+
+ case TASK_VTIMER_PROF:
+ tsum = timer_grab(&thread->user_timer);
+ tsum += timer_grab(&thread->system_timer);
+ tdelt = (uint32_t)(tsum - thread->vtimer_prof_save);
+ absolutetime_to_microtime(tdelt, &secs, microsecs);
+ /* if the time delta is smaller than a usec, ignore */
+ if (*microsecs != 0)
+ thread->vtimer_prof_save = tsum;
+ break;
+
+ case TASK_VTIMER_RLIM:
+ tsum = timer_grab(&thread->user_timer);
+ tsum += timer_grab(&thread->system_timer);
+ tdelt = (uint32_t)(tsum - thread->vtimer_rlim_save);
+ thread->vtimer_rlim_save = tsum;
+ absolutetime_to_microtime(tdelt, &secs, microsecs);
+ break;
+ }
+
+}
+
+/*
+ * task_assign:
+ *
+ * Change the assigned processor set for the task
+ */
+kern_return_t
+task_assign(
+ __unused task_t task,
+ __unused processor_set_t new_pset,
+ __unused boolean_t assign_threads)
+{
+ return(KERN_FAILURE);
+}
+
+/*
+ * task_assign_default:
+ *
+ * Version of task_assign to assign to default processor set.
+ */
+kern_return_t
+task_assign_default(
+ task_t task,
+ boolean_t assign_threads)
+{
+ return (task_assign(task, &pset0, assign_threads));
+}
+
+/*
+ * task_get_assignment
+ *
+ * Return name of processor set that task is assigned to.
+ */
+kern_return_t
+task_get_assignment(
+ task_t task,
+ processor_set_t *pset)
+{
+ if (!task->active)
+ return(KERN_FAILURE);
+
+ *pset = &pset0;
+
+ return (KERN_SUCCESS);
+}
+
+
+/*
+ * task_policy
+ *
+ * Set scheduling policy and parameters, both base and limit, for
+ * the given task. Policy must be a policy which is enabled for the
+ * processor set. Change contained threads if requested.
+ */
+kern_return_t
+task_policy(
+ __unused task_t task,
+ __unused policy_t policy_id,
+ __unused policy_base_t base,
+ __unused mach_msg_type_number_t count,
+ __unused boolean_t set_limit,
+ __unused boolean_t change)
+{
+ return(KERN_FAILURE);
+}
+
+/*
+ * task_set_policy
+ *
+ * Set scheduling policy and parameters, both base and limit, for
+ * the given task. Policy can be any policy implemented by the
+ * processor set, whether enabled or not. Change contained threads
+ * if requested.
+ */
+kern_return_t
+task_set_policy(
+ __unused task_t task,
+ __unused processor_set_t pset,
+ __unused policy_t policy_id,
+ __unused policy_base_t base,
+ __unused mach_msg_type_number_t base_count,
+ __unused policy_limit_t limit,
+ __unused mach_msg_type_number_t limit_count,
+ __unused boolean_t change)
+{
+ return(KERN_FAILURE);
+}
+
+kern_return_t
+task_set_ras_pc(
+ __unused task_t task,
+ __unused vm_offset_t pc,
+ __unused vm_offset_t endpc)
+{
+ return KERN_FAILURE;
+}
+
+void
+task_synchronizer_destroy_all(task_t task)
+{
+ semaphore_t semaphore;
+
+ /*
+ * Destroy owned semaphores
+ */
+
+ while (!queue_empty(&task->semaphore_list)) {
+ semaphore = (semaphore_t) queue_first(&task->semaphore_list);
+ (void) semaphore_destroy(task, semaphore);
+ }
+}
+
+/*
+ * Install default (machine-dependent) initial thread state
+ * on the task. Subsequent thread creation will have this initial
+ * state set on the thread by machine_thread_inherit_taskwide().
+ * Flavors and structures are exactly the same as those to thread_set_state()
+ */
+kern_return_t
+task_set_state(
+ task_t task,
+ int flavor,
+ thread_state_t state,
+ mach_msg_type_number_t state_count)
+{
+ kern_return_t ret;
+
+ if (task == TASK_NULL) {
+ return (KERN_INVALID_ARGUMENT);
+ }
+
+ task_lock(task);
+
+ if (!task->active) {
+ task_unlock(task);
+ return (KERN_FAILURE);
+ }
+
+ ret = machine_task_set_state(task, flavor, state, state_count);
+
+ task_unlock(task);
+ return ret;
+}
+
+/*
+ * Examine the default (machine-dependent) initial thread state
+ * on the task, as set by task_set_state(). Flavors and structures
+ * are exactly the same as those passed to thread_get_state().
+ */
+kern_return_t
+task_get_state(
+ task_t task,
+ int flavor,
+ thread_state_t state,
+ mach_msg_type_number_t *state_count)
+{
+ kern_return_t ret;
+
+ if (task == TASK_NULL) {
+ return (KERN_INVALID_ARGUMENT);
+ }
+
+ task_lock(task);
+
+ if (!task->active) {
+ task_unlock(task);
+ return (KERN_FAILURE);
+ }
+
+ ret = machine_task_get_state(task, flavor, state, state_count);
+
+ task_unlock(task);
+ return ret;
+}
+
+#if CONFIG_JETSAM
+#define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
+
+void __attribute__((noinline))
+THIS_PROCESS_CROSSED_HIGH_WATERMARK__SENDING_EXC_RESOURCE(int max_footprint_mb)
+{
+ task_t task = current_task();
+ int pid = 0;
+ char *procname = (char *) "unknown";
+ mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
+
+#ifdef MACH_BSD
+ pid = proc_selfpid();
+
+ if (pid == 1) {
+ /*
+ * Cannot have ReportCrash analyzing
+ * a suspended initproc.
+ */
+ return;
+ }
+
+ if (task->bsd_info != NULL)
+ procname = proc_name_address(current_task()->bsd_info);
+#endif
+
+ if (hwm_user_cores) {
+ int error;
+ uint64_t starttime, end;
+ clock_sec_t secs = 0;
+ uint32_t microsecs = 0;
+
+ starttime = mach_absolute_time();
+ /*
+ * Trigger a coredump of this process. Don't proceed unless we know we won't
+ * be filling up the disk; and ignore the core size resource limit for this
+ * core file.
+ */
+ if ((error = coredump(current_task()->bsd_info, HWM_USERCORE_MINSPACE, 1)) != 0) {
+ printf("couldn't take coredump of %s[%d]: %d\n", procname, pid, error);
+ }
+ /*
+ * coredump() leaves the task suspended.
+ */
+ task_resume_internal(current_task());
+
+ end = mach_absolute_time();
+ absolutetime_to_microtime(end - starttime, &secs, µsecs);
+ printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
+ proc_name_address(current_task()->bsd_info), pid, (int)secs, microsecs);
+ }
+
+ if (disable_exc_resource) {
+ printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
+ "supressed by a boot-arg.\n", procname, pid, max_footprint_mb);
+ return;
+ }
+
+ printf("process %s[%d] crossed memory high watermark (%d MB); sending "
+ "EXC_RESOURCE.\n", procname, pid, max_footprint_mb);
+
+ code[0] = code[1] = 0;
+ EXC_RESOURCE_ENCODE_TYPE(code[0], RESOURCE_TYPE_MEMORY);
+ EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_HIGH_WATERMARK);
+ EXC_RESOURCE_HWM_ENCODE_LIMIT(code[0], max_footprint_mb);
+
+ /*
+ * Use the _internal_ variant so that no user-space
+ * process can resume our task from under us.
+ */
+ task_suspend_internal(task);
+ exception_triage(EXC_RESOURCE, code, EXCEPTION_CODE_MAX);
+ task_resume_internal(task);
+}
+
+/*
+ * Callback invoked when a task exceeds its physical footprint limit.
+ */
+void
+task_footprint_exceeded(int warning, __unused const void *param0, __unused const void *param1)
+{
+ ledger_amount_t max_footprint, max_footprint_mb;
+ ledger_amount_t footprint_after_purge;
+ task_t task;
+
+ if (warning == LEDGER_WARNING_DIPPED_BELOW) {
+ /*
+ * Task memory limits only provide a warning on the way up.
+ */
+ return;
+ }
+
+ task = current_task();
+
+ ledger_get_limit(task->ledger, task_ledgers.phys_footprint, &max_footprint);
+ max_footprint_mb = max_footprint >> 20;
+
+ /*
+ * Try and purge all "volatile" memory in that task first.
+ */
+ (void) task_purge_volatile_memory(task);
+ /* are we still over the limit ? */
+ ledger_get_balance(task->ledger,
+ task_ledgers.phys_footprint,
+ &footprint_after_purge);
+ if ((!warning &&
+ footprint_after_purge <= max_footprint) ||
+ (warning &&
+ footprint_after_purge <= ((max_footprint *
+ PHYS_FOOTPRINT_WARNING_LEVEL) / 100))) {
+ /* all better now */
+ ledger_reset_callback_state(task->ledger,
+ task_ledgers.phys_footprint);
+ return;
+ }
+ /* still over the limit after purging... */