+ if (task == self_task) {
+ task_lock(task);
+ } else if (task < self_task) {
+ task_lock(task);
+ task_lock(self_task);
+ } else {
+ task_lock(self_task);
+ task_lock(task);
+ }
+
+#if CONFIG_SECLUDED_MEMORY
+ if (task->task_can_use_secluded_mem) {
+ task_set_can_use_secluded_mem_locked(task, FALSE);
+ }
+ task->task_could_use_secluded_mem = FALSE;
+ task->task_could_also_use_secluded_mem = FALSE;
+
+ if (task->task_suppressed_secluded) {
+ stop_secluded_suppression(task);
+ }
+#endif /* CONFIG_SECLUDED_MEMORY */
+
+ if (!task->active) {
+ /*
+ * Task is already being terminated.
+ * Just return an error. If we are dying, this will
+ * just get us to our AST special handler and that
+ * will get us to finalize the termination of ourselves.
+ */
+ task_unlock(task);
+ if (self_task != task) {
+ task_unlock(self_task);
+ }
+
+ return KERN_FAILURE;
+ }
+
+ if (task_corpse_pending_report(task)) {
+ /*
+ * Task is marked for reporting as corpse.
+ * Just return an error. This will
+ * just get us to our AST special handler and that
+ * will get us to finish the path to death
+ */
+ task_unlock(task);
+ if (self_task != task) {
+ task_unlock(self_task);
+ }
+
+ return KERN_FAILURE;
+ }
+
+ if (self_task != task) {
+ task_unlock(self_task);
+ }
+
+ /*
+ * Make sure the current thread does not get aborted out of
+ * the waits inside these operations.
+ */
+ interrupt_save = thread_interrupt_level(THREAD_UNINT);
+
+ /*
+ * Indicate that we want all the threads to stop executing
+ * at user space by holding the task (we would have held
+ * each thread independently in thread_terminate_internal -
+ * but this way we may be more likely to already find it
+ * held there). Mark the task inactive, and prevent
+ * further task operations via the task port.
+ */
+ task_hold_locked(task);
+ task->active = FALSE;
+ ipc_task_disable(task);
+
+#if CONFIG_TELEMETRY
+ /*
+ * Notify telemetry that this task is going away.
+ */
+ telemetry_task_ctl_locked(task, TF_TELEMETRY, 0);
+#endif
+
+ /*
+ * Terminate each thread in the task.
+ */
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ thread_terminate_internal(thread);
+ }
+
+#ifdef MACH_BSD
+ if (task->bsd_info != NULL && !task_is_exec_copy(task)) {
+ pid = proc_pid(task->bsd_info);
+ }
+#endif /* MACH_BSD */
+
+ task_unlock(task);
+
+ proc_set_task_policy(task, TASK_POLICY_ATTRIBUTE,
+ TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
+
+ /* Early object reap phase */
+
+// PR-17045188: Revisit implementation
+// task_partial_reap(task, pid);
+
+#if CONFIG_EMBEDDED
+ /*
+ * remove all task watchers
+ */
+ task_removewatchers(task);
+
+#endif /* CONFIG_EMBEDDED */
+
+ /*
+ * Destroy all synchronizers owned by the task.
+ */
+ task_synchronizer_destroy_all(task);
+
+ /*
+ * Clear the watchport boost on the task.
+ */
+ task_remove_turnstile_watchports(task);
+
+ /*
+ * Destroy the IPC space, leaving just a reference for it.
+ */
+ ipc_space_terminate(task->itk_space);
+
+#if 00
+ /* if some ledgers go negative on tear-down again... */
+ ledger_disable_panic_on_negative(task->map->pmap->ledger,
+ task_ledgers.phys_footprint);
+ ledger_disable_panic_on_negative(task->map->pmap->ledger,
+ task_ledgers.internal);
+ ledger_disable_panic_on_negative(task->map->pmap->ledger,
+ task_ledgers.internal_compressed);
+ ledger_disable_panic_on_negative(task->map->pmap->ledger,
+ task_ledgers.iokit_mapped);
+ ledger_disable_panic_on_negative(task->map->pmap->ledger,
+ task_ledgers.alternate_accounting);
+ ledger_disable_panic_on_negative(task->map->pmap->ledger,
+ task_ledgers.alternate_accounting_compressed);
+#endif
+
+ /*
+ * If the current thread is a member of the task
+ * being terminated, then the last reference to
+ * the task will not be dropped until the thread
+ * is finally reaped. To avoid incurring the
+ * expense of removing the address space regions
+ * at reap time, we do it explictly here.
+ */
+
+ vm_map_lock(task->map);
+ vm_map_disable_hole_optimization(task->map);
+ vm_map_unlock(task->map);
+
+#if MACH_ASSERT
+ /*
+ * Identify the pmap's process, in case the pmap ledgers drift
+ * and we have to report it.
+ */
+ char procname[17];
+ if (task->bsd_info && !task_is_exec_copy(task)) {
+ pid = proc_pid(task->bsd_info);
+ proc_name_kdp(task, procname, sizeof(procname));
+ } else {
+ pid = 0;
+ strlcpy(procname, "<unknown>", sizeof(procname));
+ }
+ pmap_set_process(task->map->pmap, pid, procname);
+#endif /* MACH_ASSERT */
+
+ vm_map_terminate(task->map);
+
+ /* release our shared region */
+ vm_shared_region_set(task, NULL);
+
+
+ lck_mtx_lock(&tasks_threads_lock);
+ queue_remove(&tasks, task, task_t, tasks);
+ queue_enter(&terminated_tasks, task, task_t, tasks);
+ tasks_count--;
+ terminated_tasks_count++;
+ lck_mtx_unlock(&tasks_threads_lock);
+
+ /*
+ * We no longer need to guard against being aborted, so restore
+ * the previous interruptible state.
+ */
+ thread_interrupt_level(interrupt_save);
+
+#if KPC
+ /* force the task to release all ctrs */
+ if (task->t_kpc & TASK_KPC_FORCED_ALL_CTRS) {
+ kpc_force_all_ctrs(task, 0);
+ }
+#endif /* KPC */
+
+#if CONFIG_COALITIONS
+ /*
+ * Leave our coalitions. (drop activation but not reference)
+ */
+ coalitions_remove_task(task);
+#endif
+
+ /*
+ * Get rid of the task active reference on itself.
+ */
+ task_deallocate(task);
+
+ return KERN_SUCCESS;
+}
+
+void
+tasks_system_suspend(boolean_t suspend)
+{
+ task_t task;
+
+ lck_mtx_lock(&tasks_threads_lock);
+ assert(tasks_suspend_state != suspend);
+ tasks_suspend_state = suspend;
+ queue_iterate(&tasks, task, task_t, tasks) {
+ if (task == kernel_task) {
+ continue;
+ }
+ suspend ? task_suspend_internal(task) : task_resume_internal(task);
+ }
+ lck_mtx_unlock(&tasks_threads_lock);
+}
+
+/*
+ * task_start_halt:
+ *
+ * Shut the current task down (except for the current thread) in
+ * preparation for dramatic changes to the task (probably exec).
+ * We hold the task and mark all other threads in the task for
+ * termination.
+ */
+kern_return_t
+task_start_halt(task_t task)
+{
+ kern_return_t kr = KERN_SUCCESS;
+ task_lock(task);
+ kr = task_start_halt_locked(task, FALSE);
+ task_unlock(task);
+ return kr;
+}
+
+static kern_return_t
+task_start_halt_locked(task_t task, boolean_t should_mark_corpse)
+{
+ thread_t thread, self;
+ uint64_t dispatchqueue_offset;
+
+ assert(task != kernel_task);
+
+ self = current_thread();
+
+ if (task != self->task && !task_is_a_corpse_fork(task)) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ if (task->halting || !task->active || !self->active) {
+ /*
+ * Task or current thread is already being terminated.
+ * Hurry up and return out of the current kernel context
+ * so that we run our AST special handler to terminate
+ * ourselves.
+ */
+ return KERN_FAILURE;
+ }
+
+ task->halting = TRUE;
+
+ /*
+ * Mark all the threads to keep them from starting any more
+ * user-level execution. The thread_terminate_internal code
+ * would do this on a thread by thread basis anyway, but this
+ * gives us a better chance of not having to wait there.
+ */
+ task_hold_locked(task);
+ dispatchqueue_offset = get_dispatchqueue_offset_from_proc(task->bsd_info);
+
+ /*
+ * Terminate all the other threads in the task.
+ */
+ queue_iterate(&task->threads, thread, thread_t, task_threads)
+ {
+ if (should_mark_corpse) {
+ thread_mtx_lock(thread);
+ thread->inspection = TRUE;
+ thread_mtx_unlock(thread);
+ }
+ if (thread != self) {
+ thread_terminate_internal(thread);
+ }
+ }
+ task->dispatchqueue_offset = dispatchqueue_offset;
+
+ task_release_locked(task);
+
+ return KERN_SUCCESS;
+}
+
+
+/*
+ * task_complete_halt:
+ *
+ * Complete task halt by waiting for threads to terminate, then clean
+ * up task resources (VM, port namespace, etc...) and then let the
+ * current thread go in the (practically empty) task context.
+ *
+ * Note: task->halting flag is not cleared in order to avoid creation
+ * of new thread in old exec'ed task.
+ */
+void
+task_complete_halt(task_t task)
+{
+ task_lock(task);
+ assert(task->halting);
+ assert(task == current_task());
+
+ /*
+ * Wait for the other threads to get shut down.
+ * When the last other thread is reaped, we'll be
+ * woken up.
+ */
+ if (task->thread_count > 1) {
+ assert_wait((event_t)&task->halting, THREAD_UNINT);
+ task_unlock(task);
+ thread_block(THREAD_CONTINUE_NULL);
+ } else {
+ task_unlock(task);
+ }
+
+ /*
+ * Give the machine dependent code a chance
+ * to perform cleanup of task-level resources
+ * associated with the current thread before
+ * ripping apart the task.
+ */
+ machine_task_terminate(task);
+
+ /*
+ * Destroy all synchronizers owned by the task.
+ */
+ task_synchronizer_destroy_all(task);
+
+ /*
+ * Destroy the contents of the IPC space, leaving just
+ * a reference for it.
+ */
+ ipc_space_clean(task->itk_space);
+
+ /*
+ * Clean out the address space, as we are going to be
+ * getting a new one.
+ */
+ vm_map_remove(task->map, task->map->min_offset,
+ task->map->max_offset,
+ /*
+ * Final cleanup:
+ * + no unnesting
+ * + remove immutable mappings
+ * + allow gaps in the range
+ */
+ (VM_MAP_REMOVE_NO_UNNESTING |
+ VM_MAP_REMOVE_IMMUTABLE |
+ VM_MAP_REMOVE_GAPS_OK));
+
+ /*
+ * Kick out any IOKitUser handles to the task. At best they're stale,
+ * at worst someone is racing a SUID exec.
+ */
+ iokit_task_terminate(task);
+}
+
+/*
+ * task_hold_locked:
+ *
+ * Suspend execution of the specified task.
+ * This is a recursive-style suspension of the task, a count of
+ * suspends is maintained.
+ *
+ * CONDITIONS: the task is locked and active.
+ */
+void
+task_hold_locked(
+ task_t task)
+{
+ thread_t thread;
+
+ assert(task->active);
+
+ if (task->suspend_count++ > 0) {
+ return;
+ }
+
+ if (task->bsd_info) {
+ workq_proc_suspended(task->bsd_info);
+ }
+
+ /*
+ * Iterate through all the threads and hold them.
+ */
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ thread_mtx_lock(thread);
+ thread_hold(thread);
+ thread_mtx_unlock(thread);
+ }
+}
+
+/*
+ * task_hold:
+ *
+ * Same as the internal routine above, except that is must lock
+ * and verify that the task is active. This differs from task_suspend
+ * in that it places a kernel hold on the task rather than just a
+ * user-level hold. This keeps users from over resuming and setting
+ * it running out from under the kernel.
+ *
+ * CONDITIONS: the caller holds a reference on the task
+ */
+kern_return_t
+task_hold(
+ task_t task)
+{
+ 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;
+}
+
+kern_return_t
+task_wait(
+ task_t task,
+ boolean_t until_not_runnable)
+{
+ if (task == TASK_NULL) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ task_lock(task);
+
+ if (!task->active) {
+ task_unlock(task);
+
+ return KERN_FAILURE;
+ }
+
+ task_wait_locked(task, until_not_runnable);
+ task_unlock(task);
+
+ return KERN_SUCCESS;
+}
+
+/*
+ * task_wait_locked:
+ *
+ * Wait for all threads in task to stop.
+ *
+ * Conditions:
+ * Called with task locked, active, and held.
+ */
+void
+task_wait_locked(
+ task_t task,
+ boolean_t until_not_runnable)
+{
+ thread_t thread, self;
+
+ assert(task->active);
+ assert(task->suspend_count > 0);
+
+ self = current_thread();
+
+ /*
+ * Iterate through all the threads and wait for them to
+ * stop. Do not wait for the current thread if it is within
+ * the task.
+ */
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ if (thread != self) {
+ thread_wait(thread, until_not_runnable);
+ }
+ }
+}
+
+boolean_t
+task_is_app_suspended(task_t task)
+{
+ return task->pidsuspended;
+}
+
+/*
+ * task_release_locked:
+ *
+ * Release a kernel hold on a task.
+ *
+ * CONDITIONS: the task is locked and active
+ */
+void
+task_release_locked(
+ task_t task)
+{
+ thread_t thread;
+
+ assert(task->active);
+ assert(task->suspend_count > 0);
+
+ if (--task->suspend_count > 0) {
+ return;
+ }
+
+ if (task->bsd_info) {
+ workq_proc_resumed(task->bsd_info);
+ }
+
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ thread_mtx_lock(thread);
+ thread_release(thread);
+ thread_mtx_unlock(thread);
+ }
+}
+
+/*
+ * task_release:
+ *
+ * Same as the internal routine above, except that it must lock
+ * and verify that the task is active.
+ *
+ * CONDITIONS: The caller holds a reference to the task
+ */
+kern_return_t
+task_release(
+ task_t task)
+{
+ 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 *threads_out,
+ mach_msg_type_number_t *count)
+{
+ mach_msg_type_number_t actual;
+ thread_t *thread_list;
+ thread_t thread;
+ vm_size_t size, size_needed;
+ void *addr;
+ unsigned int i, j;
+
+ if (task == TASK_NULL) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ size = 0; addr = NULL;
+
+ for (;;) {
+ task_lock(task);
+ if (!task->active) {
+ task_unlock(task);
+
+ if (size != 0) {
+ kfree(addr, size);
+ }
+
+ return KERN_FAILURE;
+ }
+
+ actual = task->thread_count;
+
+ /* do we have the memory we need? */
+ size_needed = actual * sizeof(mach_port_t);
+ if (size_needed <= size) {
+ break;
+ }
+
+ /* unlock the task and allocate more memory */
+ 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;
+ }
+ }
+
+ /* OK, have memory and the task is locked & active */
+ thread_list = (thread_t *)addr;
+
+ i = j = 0;
+
+ for (thread = (thread_t)queue_first(&task->threads); i < actual;
+ ++i, thread = (thread_t)queue_next(&thread->task_threads)) {
+ thread_reference_internal(thread);
+ thread_list[j++] = thread;
+ }
+
+ assert(queue_end(&task->threads, (queue_entry_t)thread));
+
+ actual = j;
+ size_needed = actual * sizeof(mach_port_t);
+
+ /* can unlock task now that we've got the thread refs */
+ task_unlock(task);
+
+ if (actual == 0) {
+ /* no threads, so return null pointer and deallocate memory */
+
+ *threads_out = NULL;
+ *count = 0;
+
+ if (size != 0) {
+ kfree(addr, size);
+ }
+ } else {
+ /* if we allocated too much, must copy */
+
+ if (size_needed < size) {
+ void *newaddr;
+
+ newaddr = kalloc(size_needed);
+ if (newaddr == 0) {
+ for (i = 0; i < actual; ++i) {
+ thread_deallocate(thread_list[i]);
+ }
+ kfree(addr, size);
+ return KERN_RESOURCE_SHORTAGE;
+ }
+
+ bcopy(addr, newaddr, size_needed);
+ kfree(addr, size);
+ thread_list = (thread_t *)newaddr;
+ }
+
+ *threads_out = thread_list;
+ *count = actual;
+
+ /* do the conversion that Mig should handle */
+
+ for (i = 0; i < actual; ++i) {
+ ((ipc_port_t *) thread_list)[i] = convert_thread_to_port(thread_list[i]);
+ }
+ }
+
+ return KERN_SUCCESS;
+}
+
+#define TASK_HOLD_NORMAL 0
+#define TASK_HOLD_PIDSUSPEND 1
+#define TASK_HOLD_LEGACY 2
+#define TASK_HOLD_LEGACY_ALL 3
+
+static kern_return_t
+place_task_hold(
+ task_t task,
+ int mode)
+{
+ if (!task->active && !task_is_a_corpse(task)) {
+ return KERN_FAILURE;
+ }
+
+ /* Return success for corpse task */
+ if (task_is_a_corpse(task)) {
+ return KERN_SUCCESS;
+ }
+
+ KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
+ MACHDBG_CODE(DBG_MACH_IPC, MACH_TASK_SUSPEND) | DBG_FUNC_NONE,
+ task_pid(task), ((thread_t)queue_first(&task->threads))->thread_id,
+ task->user_stop_count, task->user_stop_count + 1, 0);
+
+#if MACH_ASSERT
+ current_task()->suspends_outstanding++;
+#endif
+
+ if (mode == TASK_HOLD_LEGACY) {
+ task->legacy_stop_count++;
+ }
+
+ if (task->user_stop_count++ > 0) {
+ /*
+ * If the stop count was positive, the task is
+ * already stopped and we can exit.
+ */
+ return KERN_SUCCESS;
+ }
+
+ /*
+ * Put a kernel-level hold on the threads in the task (all
+ * user-level task suspensions added together represent a
+ * single kernel-level hold). We then wait for the threads
+ * to stop executing user code.
+ */
+ task_hold_locked(task);
+ task_wait_locked(task, FALSE);
+
+ return KERN_SUCCESS;
+}
+
+static kern_return_t
+release_task_hold(
+ task_t task,
+ int mode)
+{
+ boolean_t release = FALSE;
+
+ if (!task->active && !task_is_a_corpse(task)) {
+ return KERN_FAILURE;
+ }
+
+ /* Return success for corpse task */
+ if (task_is_a_corpse(task)) {
+ return KERN_SUCCESS;
+ }
+
+ if (mode == TASK_HOLD_PIDSUSPEND) {
+ if (task->pidsuspended == FALSE) {
+ return KERN_FAILURE;
+ }
+ task->pidsuspended = FALSE;
+ }
+
+ if (task->user_stop_count > (task->pidsuspended ? 1 : 0)) {
+ KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
+ MACHDBG_CODE(DBG_MACH_IPC, MACH_TASK_RESUME) | DBG_FUNC_NONE,
+ task_pid(task), ((thread_t)queue_first(&task->threads))->thread_id,
+ task->user_stop_count, mode, task->legacy_stop_count);
+
+#if MACH_ASSERT
+ /*
+ * This is obviously not robust; if we suspend one task and then resume a different one,
+ * we'll fly under the radar. This is only meant to catch the common case of a crashed
+ * or buggy suspender.
+ */
+ current_task()->suspends_outstanding--;
+#endif
+
+ if (mode == TASK_HOLD_LEGACY_ALL) {
+ if (task->legacy_stop_count >= task->user_stop_count) {
+ task->user_stop_count = 0;
+ release = TRUE;
+ } else {
+ task->user_stop_count -= task->legacy_stop_count;
+ }
+ task->legacy_stop_count = 0;
+ } else {
+ if (mode == TASK_HOLD_LEGACY && task->legacy_stop_count > 0) {
+ task->legacy_stop_count--;
+ }
+ if (--task->user_stop_count == 0) {
+ release = TRUE;
+ }
+ }
+ } else {
+ return KERN_FAILURE;
+ }
+
+ /*
+ * Release the task if necessary.
+ */
+ if (release) {
+ task_release_locked(task);
+ }
+
+ return KERN_SUCCESS;
+}
+
+boolean_t
+get_task_suspended(task_t task)
+{
+ return 0 != task->user_stop_count;
+}
+
+/*
+ * task_suspend:
+ *
+ * Implement an (old-fashioned) user-level suspension on a task.
+ *
+ * Because the user isn't expecting to have to manage a suspension
+ * token, we'll track it for him in the kernel in the form of a naked
+ * send right to the task's resume port. All such send rights
+ * account for a single suspension against the task (unlike task_suspend2()
+ * where each caller gets a unique suspension count represented by a
+ * unique send-once right).
+ *
+ * Conditions:
+ * The caller holds a reference to the task
+ */
+kern_return_t
+task_suspend(
+ task_t task)
+{
+ kern_return_t kr;
+ mach_port_t port;
+ mach_port_name_t name;
+
+ if (task == TASK_NULL || task == kernel_task) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ task_lock(task);
+
+ /*
+ * place a legacy hold on the task.
+ */
+ kr = place_task_hold(task, TASK_HOLD_LEGACY);
+ if (kr != KERN_SUCCESS) {
+ task_unlock(task);
+ return kr;
+ }
+
+ /*
+ * Claim a send right on the task resume port, and request a no-senders
+ * notification on that port (if none outstanding).
+ */
+ (void)ipc_kobject_make_send_lazy_alloc_port(&task->itk_resume,
+ (ipc_kobject_t)task, IKOT_TASK_RESUME);
+ port = task->itk_resume;
+
+ task_unlock(task);
+
+ /*
+ * Copyout the send right into the calling task's IPC space. It won't know it is there,
+ * but we'll look it up when calling a traditional resume. Any IPC operations that
+ * deallocate the send right will auto-release the suspension.
+ */
+ if ((kr = ipc_kmsg_copyout_object(current_task()->itk_space, ip_to_object(port),
+ MACH_MSG_TYPE_MOVE_SEND, NULL, NULL, &name)) != KERN_SUCCESS) {
+ printf("warning: %s(%d) failed to copyout suspension token for pid %d with error: %d\n",
+ proc_name_address(current_task()->bsd_info), proc_pid(current_task()->bsd_info),
+ task_pid(task), kr);
+ return kr;
+ }
+
+ return kr;
+}
+
+/*
+ * task_resume:
+ * Release a user hold on a task.
+ *
+ * Conditions:
+ * The caller holds a reference to the task
+ */
+kern_return_t
+task_resume(
+ task_t task)
+{
+ kern_return_t kr;
+ mach_port_name_t resume_port_name;
+ ipc_entry_t resume_port_entry;
+ ipc_space_t space = current_task()->itk_space;
+
+ if (task == TASK_NULL || task == kernel_task) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ /* release a legacy task hold */
+ task_lock(task);
+ kr = release_task_hold(task, TASK_HOLD_LEGACY);
+ task_unlock(task);
+
+ is_write_lock(space);
+ if (is_active(space) && IP_VALID(task->itk_resume) &&
+ ipc_hash_lookup(space, ip_to_object(task->itk_resume), &resume_port_name, &resume_port_entry) == TRUE) {
+ /*
+ * We found a suspension token in the caller's IPC space. Release a send right to indicate that
+ * we are holding one less legacy hold on the task from this caller. If the release failed,
+ * go ahead and drop all the rights, as someone either already released our holds or the task
+ * is gone.
+ */
+ if (kr == KERN_SUCCESS) {
+ ipc_right_dealloc(space, resume_port_name, resume_port_entry);
+ } else {
+ ipc_right_destroy(space, resume_port_name, resume_port_entry, FALSE, 0);
+ }
+ /* space unlocked */
+ } else {
+ is_write_unlock(space);
+ if (kr == KERN_SUCCESS) {
+ printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
+ proc_name_address(current_task()->bsd_info), proc_pid(current_task()->bsd_info),
+ task_pid(task));
+ }
+ }
+
+ return kr;
+}
+
+/*
+ * Suspend the target task.
+ * Making/holding a token/reference/port is the callers responsibility.
+ */
+kern_return_t
+task_suspend_internal(task_t task)
+{
+ kern_return_t kr;
+
+ if (task == TASK_NULL || task == kernel_task) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ task_lock(task);
+ kr = place_task_hold(task, TASK_HOLD_NORMAL);
+ task_unlock(task);
+ return kr;
+}
+
+/*
+ * Suspend the target task, and return a suspension token. The token
+ * represents a reference on the suspended task.
+ */
+kern_return_t
+task_suspend2(
+ task_t task,
+ task_suspension_token_t *suspend_token)
+{
+ kern_return_t kr;
+
+ kr = task_suspend_internal(task);
+ if (kr != KERN_SUCCESS) {
+ *suspend_token = TASK_NULL;
+ return kr;
+ }
+
+ /*
+ * Take a reference on the target task and return that to the caller
+ * as a "suspension token," which can be converted into an SO right to
+ * the now-suspended task's resume port.
+ */
+ task_reference_internal(task);
+ *suspend_token = task;
+
+ return KERN_SUCCESS;
+}
+
+/*
+ * Resume the task
+ * (reference/token/port management is caller's responsibility).
+ */
+kern_return_t
+task_resume_internal(
+ task_suspension_token_t task)
+{
+ kern_return_t kr;
+
+ if (task == TASK_NULL || task == kernel_task) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ task_lock(task);
+ kr = release_task_hold(task, TASK_HOLD_NORMAL);
+ task_unlock(task);
+ return kr;
+}
+
+/*
+ * Resume the task using a suspension token. Consumes the token's ref.
+ */
+kern_return_t
+task_resume2(
+ task_suspension_token_t task)
+{
+ kern_return_t kr;
+
+ kr = task_resume_internal(task);
+ task_suspension_token_deallocate(task);
+
+ return kr;
+}
+
+boolean_t
+task_suspension_notify(mach_msg_header_t *request_header)
+{
+ ipc_port_t port = request_header->msgh_remote_port;
+ task_t task = convert_port_to_task_suspension_token(port);
+ mach_msg_type_number_t not_count;
+
+ if (task == TASK_NULL || task == kernel_task) {
+ return TRUE; /* nothing to do */
+ }
+ switch (request_header->msgh_id) {
+ case MACH_NOTIFY_SEND_ONCE:
+ /* release the hold held by this specific send-once right */
+ task_lock(task);
+ release_task_hold(task, TASK_HOLD_NORMAL);
+ task_unlock(task);
+ break;
+
+ case MACH_NOTIFY_NO_SENDERS:
+ not_count = ((mach_no_senders_notification_t *)request_header)->not_count;
+
+ task_lock(task);
+ ip_lock(port);
+ if (port->ip_mscount == not_count) {
+ /* release all the [remaining] outstanding legacy holds */
+ assert(port->ip_nsrequest == IP_NULL);
+ ip_unlock(port);
+ release_task_hold(task, TASK_HOLD_LEGACY_ALL);
+ task_unlock(task);
+ } else if (port->ip_nsrequest == IP_NULL) {
+ ipc_port_t old_notify;
+
+ task_unlock(task);
+ /* new send rights, re-arm notification at current make-send count */
+ ipc_port_nsrequest(port, port->ip_mscount, ipc_port_make_sonce_locked(port), &old_notify);
+ assert(old_notify == IP_NULL);
+ /* port unlocked */
+ } else {
+ ip_unlock(port);
+ task_unlock(task);
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ task_suspension_token_deallocate(task); /* drop token reference */
+ return TRUE;
+}
+
+static kern_return_t
+task_pidsuspend_locked(task_t task)
+{
+ kern_return_t kr;
+
+ if (task->pidsuspended) {
+ kr = KERN_FAILURE;
+ goto out;
+ }
+
+ task->pidsuspended = TRUE;
+
+ kr = place_task_hold(task, TASK_HOLD_PIDSUSPEND);
+ if (kr != KERN_SUCCESS) {
+ task->pidsuspended = FALSE;
+ }
+out:
+ return kr;
+}
+
+
+/*
+ * task_pidsuspend:
+ *
+ * Suspends a task by placing a hold on its threads.
+ *
+ * Conditions:
+ * The caller holds a reference to the task
+ */
+kern_return_t
+task_pidsuspend(
+ task_t task)
+{
+ kern_return_t kr;
+
+ if (task == TASK_NULL || task == kernel_task) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ task_lock(task);
+
+ kr = task_pidsuspend_locked(task);
+
+ task_unlock(task);
+
+ if ((KERN_SUCCESS == kr) && task->message_app_suspended) {
+ iokit_task_app_suspended_changed(task);
+ }
+
+ return kr;
+}
+
+/*
+ * task_pidresume:
+ * Resumes a previously suspended task.
+ *
+ * Conditions:
+ * The caller holds a reference to the task
+ */
+kern_return_t
+task_pidresume(
+ task_t task)
+{
+ kern_return_t kr;
+
+ if (task == TASK_NULL || task == kernel_task) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ task_lock(task);
+
+#if CONFIG_FREEZE
+
+ while (task->changing_freeze_state) {
+ assert_wait((event_t)&task->changing_freeze_state, THREAD_UNINT);
+ task_unlock(task);
+ thread_block(THREAD_CONTINUE_NULL);
+
+ task_lock(task);
+ }
+ task->changing_freeze_state = TRUE;
+#endif
+
+ kr = release_task_hold(task, TASK_HOLD_PIDSUSPEND);
+
+ task_unlock(task);
+
+ if ((KERN_SUCCESS == kr) && task->message_app_suspended) {
+ iokit_task_app_suspended_changed(task);
+ }
+
+#if CONFIG_FREEZE
+
+ task_lock(task);
+
+ if (kr == KERN_SUCCESS) {
+ task->frozen = FALSE;
+ }
+ task->changing_freeze_state = FALSE;
+ thread_wakeup(&task->changing_freeze_state);
+
+ task_unlock(task);
+#endif
+
+ return kr;
+}
+
+os_refgrp_decl(static, task_watchports_refgrp, "task_watchports", NULL);
+
+/*
+ * task_add_turnstile_watchports:
+ * Setup watchports to boost the main thread of the task.
+ *
+ * Arguments:
+ * task: task being spawned
+ * thread: main thread of task
+ * portwatch_ports: array of watchports
+ * portwatch_count: number of watchports
+ *
+ * Conditions:
+ * Nothing locked.
+ */
+void
+task_add_turnstile_watchports(
+ task_t task,
+ thread_t thread,
+ ipc_port_t *portwatch_ports,
+ uint32_t portwatch_count)
+{
+ struct task_watchports *watchports = NULL;
+ struct task_watchport_elem *previous_elem_array[TASK_MAX_WATCHPORT_COUNT] = {};
+ os_ref_count_t refs;
+
+ /* Check if the task has terminated */
+ if (!task->active) {
+ return;
+ }
+
+ assert(portwatch_count <= TASK_MAX_WATCHPORT_COUNT);
+
+ watchports = task_watchports_alloc_init(task, thread, portwatch_count);
+
+ /* Lock the ipc space */
+ is_write_lock(task->itk_space);
+
+ /* Setup watchports to boost the main thread */
+ refs = task_add_turnstile_watchports_locked(task,
+ watchports, previous_elem_array, portwatch_ports,
+ portwatch_count);
+
+ /* Drop the space lock */
+ is_write_unlock(task->itk_space);
+
+ if (refs == 0) {
+ task_watchports_deallocate(watchports);
+ }
+
+ /* Drop the ref on previous_elem_array */
+ for (uint32_t i = 0; i < portwatch_count && previous_elem_array[i] != NULL; i++) {
+ task_watchport_elem_deallocate(previous_elem_array[i]);
+ }
+}
+
+/*
+ * task_remove_turnstile_watchports:
+ * Clear all turnstile boost on the task from watchports.
+ *
+ * Arguments:
+ * task: task being terminated
+ *
+ * Conditions:
+ * Nothing locked.
+ */
+void
+task_remove_turnstile_watchports(
+ task_t task)
+{
+ os_ref_count_t refs = TASK_MAX_WATCHPORT_COUNT;
+ struct task_watchports *watchports = NULL;
+ ipc_port_t port_freelist[TASK_MAX_WATCHPORT_COUNT] = {};
+ uint32_t portwatch_count;
+
+ /* Lock the ipc space */
+ is_write_lock(task->itk_space);
+
+ /* Check if watchport boost exist */
+ if (task->watchports == NULL) {
+ is_write_unlock(task->itk_space);
+ return;
+ }
+ watchports = task->watchports;
+ portwatch_count = watchports->tw_elem_array_count;
+
+ refs = task_remove_turnstile_watchports_locked(task, watchports,
+ port_freelist);
+
+ is_write_unlock(task->itk_space);
+
+ /* Drop all the port references */
+ for (uint32_t i = 0; i < portwatch_count && port_freelist[i] != NULL; i++) {
+ ip_release(port_freelist[i]);
+ }
+
+ /* Clear the task and thread references for task_watchport */
+ if (refs == 0) {
+ task_watchports_deallocate(watchports);
+ }
+}
+
+/*
+ * task_transfer_turnstile_watchports:
+ * Transfer all watchport turnstile boost from old task to new task.
+ *
+ * Arguments:
+ * old_task: task calling exec
+ * new_task: new exec'ed task
+ * thread: main thread of new task
+ *
+ * Conditions:
+ * Nothing locked.
+ */
+void
+task_transfer_turnstile_watchports(
+ task_t old_task,
+ task_t new_task,
+ thread_t new_thread)
+{
+ struct task_watchports *old_watchports = NULL;
+ struct task_watchports *new_watchports = NULL;
+ os_ref_count_t old_refs = TASK_MAX_WATCHPORT_COUNT;
+ os_ref_count_t new_refs = TASK_MAX_WATCHPORT_COUNT;
+ uint32_t portwatch_count;
+
+ if (old_task->watchports == NULL || !new_task->active) {
+ return;
+ }
+
+ /* Get the watch port count from the old task */
+ is_write_lock(old_task->itk_space);
+ if (old_task->watchports == NULL) {
+ is_write_unlock(old_task->itk_space);
+ return;
+ }
+
+ portwatch_count = old_task->watchports->tw_elem_array_count;
+ is_write_unlock(old_task->itk_space);
+
+ new_watchports = task_watchports_alloc_init(new_task, new_thread, portwatch_count);
+
+ /* Lock the ipc space for old task */
+ is_write_lock(old_task->itk_space);
+
+ /* Lock the ipc space for new task */
+ is_write_lock(new_task->itk_space);
+
+ /* Check if watchport boost exist */
+ if (old_task->watchports == NULL || !new_task->active) {
+ is_write_unlock(new_task->itk_space);
+ is_write_unlock(old_task->itk_space);
+ (void)task_watchports_release(new_watchports);
+ task_watchports_deallocate(new_watchports);
+ return;
+ }
+
+ old_watchports = old_task->watchports;
+ assert(portwatch_count == old_task->watchports->tw_elem_array_count);
+
+ /* Setup new task watchports */
+ new_task->watchports = new_watchports;
+
+ for (uint32_t i = 0; i < portwatch_count; i++) {
+ ipc_port_t port = old_watchports->tw_elem[i].twe_port;
+
+ if (port == NULL) {
+ task_watchport_elem_clear(&new_watchports->tw_elem[i]);
+ continue;
+ }
+
+ /* Lock the port and check if it has the entry */
+ ip_lock(port);
+ imq_lock(&port->ip_messages);
+
+ task_watchport_elem_init(&new_watchports->tw_elem[i], new_task, port);
+
+ if (ipc_port_replace_watchport_elem_conditional_locked(port,
+ &old_watchports->tw_elem[i], &new_watchports->tw_elem[i]) == KERN_SUCCESS) {
+ task_watchport_elem_clear(&old_watchports->tw_elem[i]);
+
+ task_watchports_retain(new_watchports);
+ old_refs = task_watchports_release(old_watchports);
+
+ /* Check if all ports are cleaned */
+ if (old_refs == 0) {
+ old_task->watchports = NULL;
+ }
+ } else {
+ task_watchport_elem_clear(&new_watchports->tw_elem[i]);
+ }
+ /* mqueue and port unlocked by ipc_port_replace_watchport_elem_conditional_locked */
+ }
+
+ /* Drop the reference on new task_watchports struct returned by task_watchports_alloc_init */
+ new_refs = task_watchports_release(new_watchports);
+ if (new_refs == 0) {
+ new_task->watchports = NULL;
+ }
+
+ is_write_unlock(new_task->itk_space);
+ is_write_unlock(old_task->itk_space);
+
+ /* Clear the task and thread references for old_watchport */
+ if (old_refs == 0) {
+ task_watchports_deallocate(old_watchports);
+ }
+
+ /* Clear the task and thread references for new_watchport */
+ if (new_refs == 0) {
+ task_watchports_deallocate(new_watchports);
+ }
+}
+
+/*
+ * task_add_turnstile_watchports_locked:
+ * Setup watchports to boost the main thread of the task.
+ *
+ * Arguments:
+ * task: task to boost
+ * watchports: watchport structure to be attached to the task
+ * previous_elem_array: an array of old watchport_elem to be returned to caller
+ * portwatch_ports: array of watchports
+ * portwatch_count: number of watchports
+ *
+ * Conditions:
+ * ipc space of the task locked.
+ * returns array of old watchport_elem in previous_elem_array
+ */
+static os_ref_count_t
+task_add_turnstile_watchports_locked(
+ task_t task,
+ struct task_watchports *watchports,
+ struct task_watchport_elem **previous_elem_array,
+ ipc_port_t *portwatch_ports,
+ uint32_t portwatch_count)
+{
+ os_ref_count_t refs = TASK_MAX_WATCHPORT_COUNT;
+
+ /* Check if the task is still active */
+ if (!task->active) {
+ refs = task_watchports_release(watchports);
+ return refs;
+ }
+
+ assert(task->watchports == NULL);
+ task->watchports = watchports;
+
+ for (uint32_t i = 0, j = 0; i < portwatch_count; i++) {
+ ipc_port_t port = portwatch_ports[i];
+
+ task_watchport_elem_init(&watchports->tw_elem[i], task, port);
+ if (port == NULL) {
+ task_watchport_elem_clear(&watchports->tw_elem[i]);
+ continue;
+ }
+
+ ip_lock(port);
+ imq_lock(&port->ip_messages);
+
+ /* Check if port is in valid state to be setup as watchport */
+ if (ipc_port_add_watchport_elem_locked(port, &watchports->tw_elem[i],
+ &previous_elem_array[j]) != KERN_SUCCESS) {
+ task_watchport_elem_clear(&watchports->tw_elem[i]);
+ continue;
+ }
+ /* port and mqueue unlocked on return */
+
+ ip_reference(port);
+ task_watchports_retain(watchports);
+ if (previous_elem_array[j] != NULL) {
+ j++;
+ }
+ }
+
+ /* Drop the reference on task_watchport struct returned by os_ref_init */
+ refs = task_watchports_release(watchports);
+ if (refs == 0) {
+ task->watchports = NULL;
+ }
+
+ return refs;
+}
+
+/*
+ * task_remove_turnstile_watchports_locked:
+ * Clear all turnstile boost on the task from watchports.
+ *
+ * Arguments:
+ * task: task to remove watchports from
+ * watchports: watchports structure for the task
+ * port_freelist: array of ports returned with ref to caller
+ *
+ *
+ * Conditions:
+ * ipc space of the task locked.
+ * array of ports with refs are returned in port_freelist
+ */
+static os_ref_count_t
+task_remove_turnstile_watchports_locked(
+ task_t task,
+ struct task_watchports *watchports,
+ ipc_port_t *port_freelist)
+{
+ os_ref_count_t refs = TASK_MAX_WATCHPORT_COUNT;
+
+ for (uint32_t i = 0, j = 0; i < watchports->tw_elem_array_count; i++) {
+ ipc_port_t port = watchports->tw_elem[i].twe_port;
+ if (port == NULL) {
+ continue;
+ }
+
+ /* Lock the port and check if it has the entry */
+ ip_lock(port);
+ imq_lock(&port->ip_messages);
+ if (ipc_port_clear_watchport_elem_internal_conditional_locked(port,
+ &watchports->tw_elem[i]) == KERN_SUCCESS) {
+ task_watchport_elem_clear(&watchports->tw_elem[i]);
+ port_freelist[j++] = port;
+ refs = task_watchports_release(watchports);
+
+ /* Check if all ports are cleaned */
+ if (refs == 0) {
+ task->watchports = NULL;
+ break;
+ }
+ }
+ /* mqueue and port unlocked by ipc_port_clear_watchport_elem_internal_conditional_locked */
+ }
+ return refs;
+}
+
+/*
+ * task_watchports_alloc_init:
+ * Allocate and initialize task watchport struct.
+ *
+ * Conditions:
+ * Nothing locked.
+ */
+static struct task_watchports *
+task_watchports_alloc_init(
+ task_t task,
+ thread_t thread,
+ uint32_t count)
+{
+ struct task_watchports *watchports = kalloc(sizeof(struct task_watchports) +
+ count * sizeof(struct task_watchport_elem));
+
+ task_reference(task);
+ thread_reference(thread);
+ watchports->tw_task = task;
+ watchports->tw_thread = thread;
+ watchports->tw_elem_array_count = count;
+ os_ref_init(&watchports->tw_refcount, &task_watchports_refgrp);
+
+ return watchports;
+}
+
+/*
+ * task_watchports_deallocate:
+ * Deallocate task watchport struct.
+ *
+ * Conditions:
+ * Nothing locked.
+ */
+static void
+task_watchports_deallocate(
+ struct task_watchports *watchports)
+{
+ uint32_t portwatch_count = watchports->tw_elem_array_count;
+
+ task_deallocate(watchports->tw_task);
+ thread_deallocate(watchports->tw_thread);
+ kfree(watchports, sizeof(struct task_watchports) + portwatch_count * sizeof(struct task_watchport_elem));
+}
+
+/*
+ * task_watchport_elem_deallocate:
+ * Deallocate task watchport element and release its ref on task_watchport.
+ *
+ * Conditions:
+ * Nothing locked.
+ */
+void
+task_watchport_elem_deallocate(
+ struct task_watchport_elem *watchport_elem)
+{
+ os_ref_count_t refs = TASK_MAX_WATCHPORT_COUNT;
+ task_t task = watchport_elem->twe_task;
+ struct task_watchports *watchports = NULL;
+ ipc_port_t port = NULL;
+
+ assert(task != NULL);
+
+ /* Take the space lock to modify the elememt */
+ is_write_lock(task->itk_space);
+
+ watchports = task->watchports;
+ assert(watchports != NULL);
+
+ port = watchport_elem->twe_port;
+ assert(port != NULL);
+
+ task_watchport_elem_clear(watchport_elem);
+ refs = task_watchports_release(watchports);
+
+ if (refs == 0) {
+ task->watchports = NULL;
+ }
+
+ is_write_unlock(task->itk_space);
+
+ ip_release(port);
+ if (refs == 0) {
+ task_watchports_deallocate(watchports);
+ }
+}
+
+/*
+ * task_has_watchports:
+ * Return TRUE if task has watchport boosts.
+ *
+ * Conditions:
+ * Nothing locked.
+ */
+boolean_t
+task_has_watchports(task_t task)
+{
+ return task->watchports != NULL;
+}
+
+#if DEVELOPMENT || DEBUG
+
+extern void IOSleep(int);
+
+kern_return_t
+task_disconnect_page_mappings(task_t task)
+{
+ int n;
+
+ if (task == TASK_NULL || task == kernel_task) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ /*
+ * this function is used to strip all of the mappings from
+ * the pmap for the specified task to force the task to
+ * re-fault all of the pages it is actively using... this
+ * allows us to approximate the true working set of the
+ * specified task. We only engage if at least 1 of the
+ * threads in the task is runnable, but we want to continuously
+ * sweep (at least for a while - I've arbitrarily set the limit at
+ * 100 sweeps to be re-looked at as we gain experience) to get a better
+ * view into what areas within a page are being visited (as opposed to only
+ * seeing the first fault of a page after the task becomes
+ * runnable)... in the future I may
+ * try to block until awakened by a thread in this task
+ * being made runnable, but for now we'll periodically poll from the
+ * user level debug tool driving the sysctl
+ */
+ for (n = 0; n < 100; n++) {
+ thread_t thread;
+ boolean_t runnable;
+ boolean_t do_unnest;
+ int page_count;
+
+ runnable = FALSE;
+ do_unnest = FALSE;
+
+ task_lock(task);
+
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ if (thread->state & TH_RUN) {
+ runnable = TRUE;
+ break;
+ }
+ }
+ if (n == 0) {
+ task->task_disconnected_count++;
+ }
+
+ if (task->task_unnested == FALSE) {
+ if (runnable == TRUE) {
+ task->task_unnested = TRUE;
+ do_unnest = TRUE;
+ }
+ }
+ task_unlock(task);
+
+ if (runnable == FALSE) {
+ break;
+ }
+
+ KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_DISCONNECT_TASK_PAGE_MAPPINGS)) | DBG_FUNC_START,
+ task, do_unnest, task->task_disconnected_count, 0, 0);
+
+ page_count = vm_map_disconnect_page_mappings(task->map, do_unnest);
+
+ KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_DISCONNECT_TASK_PAGE_MAPPINGS)) | DBG_FUNC_END,
+ task, page_count, 0, 0, 0);
+
+ if ((n % 5) == 4) {
+ IOSleep(1);
+ }
+ }
+ return KERN_SUCCESS;
+}
+
+#endif
+
+
+#if CONFIG_FREEZE
+
+/*
+ * task_freeze:
+ *
+ * Freeze a task.
+ *
+ * Conditions:
+ * The caller holds a reference to the task
+ */
+extern void vm_wake_compactor_swapper(void);
+extern queue_head_t c_swapout_list_head;
+
+kern_return_t
+task_freeze(
+ task_t task,
+ uint32_t *purgeable_count,
+ uint32_t *wired_count,
+ uint32_t *clean_count,
+ uint32_t *dirty_count,
+ uint32_t dirty_budget,
+ uint32_t *shared_count,
+ int *freezer_error_code,
+ boolean_t eval_only)
+{
+ kern_return_t kr = KERN_SUCCESS;
+
+ if (task == TASK_NULL || task == kernel_task) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ task_lock(task);
+
+ while (task->changing_freeze_state) {
+ assert_wait((event_t)&task->changing_freeze_state, THREAD_UNINT);
+ task_unlock(task);
+ thread_block(THREAD_CONTINUE_NULL);
+
+ task_lock(task);
+ }
+ if (task->frozen) {
+ task_unlock(task);
+ return KERN_FAILURE;
+ }
+ task->changing_freeze_state = TRUE;
+
+ task_unlock(task);
+
+ kr = vm_map_freeze(task,
+ purgeable_count,
+ wired_count,
+ clean_count,
+ dirty_count,
+ dirty_budget,
+ shared_count,
+ freezer_error_code,
+ eval_only);
+
+ task_lock(task);
+
+ if ((kr == KERN_SUCCESS) && (eval_only == FALSE)) {
+ task->frozen = TRUE;
+ }
+
+ task->changing_freeze_state = FALSE;
+ thread_wakeup(&task->changing_freeze_state);
+
+ task_unlock(task);
+
+ if (VM_CONFIG_COMPRESSOR_IS_PRESENT &&
+ (kr == KERN_SUCCESS) &&
+ (eval_only == FALSE)) {
+ vm_wake_compactor_swapper();
+ /*
+ * We do an explicit wakeup of the swapout thread here
+ * because the compact_and_swap routines don't have
+ * knowledge about these kind of "per-task packed c_segs"
+ * and so will not be evaluating whether we need to do
+ * a wakeup there.
+ */
+ thread_wakeup((event_t)&c_swapout_list_head);
+ }
+
+ return kr;
+}
+
+/*
+ * task_thaw:
+ *
+ * Thaw a currently frozen task.
+ *
+ * Conditions:
+ * The caller holds a reference to the task
+ */
+kern_return_t
+task_thaw(
+ task_t task)
+{
+ if (task == TASK_NULL || task == kernel_task) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ task_lock(task);
+
+ while (task->changing_freeze_state) {
+ assert_wait((event_t)&task->changing_freeze_state, THREAD_UNINT);
+ task_unlock(task);
+ thread_block(THREAD_CONTINUE_NULL);
+
+ task_lock(task);
+ }
+ if (!task->frozen) {
+ task_unlock(task);
+ return KERN_FAILURE;
+ }
+ task->frozen = FALSE;
+
+ task_unlock(task);
+
+ return KERN_SUCCESS;
+}
+
+#endif /* CONFIG_FREEZE */
+
+kern_return_t
+host_security_set_task_token(
+ host_security_t host_security,
+ task_t task,
+ security_token_t sec_token,
+ audit_token_t audit_token,
+ host_priv_t host_priv)
+{
+ ipc_port_t host_port;
+ 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->audit_token = audit_token;
+ task_unlock(task);
+
+ if (host_priv != HOST_PRIV_NULL) {
+ kr = host_get_host_priv_port(host_priv, &host_port);
+ } else {
+ kr = host_get_host_port(host_priv_self(), &host_port);
+ }
+ assert(kr == KERN_SUCCESS);
+
+ kr = task_set_special_port_internal(task, TASK_HOST_PORT, host_port);
+ return kr;
+}
+
+kern_return_t
+task_send_trace_memory(
+ __unused task_t target_task,
+ __unused uint32_t pid,
+ __unused uint64_t uniqueid)
+{
+ return KERN_INVALID_ARGUMENT;
+}
+
+/*
+ * This routine was added, pretty much exclusively, for registering the
+ * RPC glue vector for in-kernel short circuited tasks. Rather than
+ * removing it completely, I have only disabled that feature (which was
+ * the only feature at the time). It just appears that we are going to
+ * want to add some user data to tasks in the future (i.e. bsd info,
+ * task names, etc...), so I left it in the formal task interface.
+ */
+kern_return_t
+task_set_info(
+ task_t task,
+ task_flavor_t flavor,
+ __unused task_info_t task_info_in, /* pointer to IN array */
+ __unused mach_msg_type_number_t task_info_count)
+{
+ if (task == TASK_NULL) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ switch (flavor) {
+#if CONFIG_ATM
+ case TASK_TRACE_MEMORY_INFO:
+ {
+ if (task_info_count != TASK_TRACE_MEMORY_INFO_COUNT) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ assert(task_info_in != NULL);
+ task_trace_memory_info_t mem_info;
+ mem_info = (task_trace_memory_info_t) task_info_in;
+ kern_return_t kr = atm_register_trace_memory(task,
+ mem_info->user_memory_address,
+ mem_info->buffer_size);
+ return kr;
+ }
+
+#endif
+ default:
+ return KERN_INVALID_ARGUMENT;
+ }
+ return KERN_SUCCESS;
+}
+
+int radar_20146450 = 1;
+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)
+{
+ kern_return_t error = KERN_SUCCESS;
+ mach_msg_type_number_t original_task_info_count;
+
+ if (task == TASK_NULL) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ original_task_info_count = *task_info_count;
+ task_lock(task);
+
+ if ((task != current_task()) && (!task->active)) {
+ task_unlock(task);
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ switch (flavor) {
+ case TASK_BASIC_INFO_32:
+ case TASK_BASIC2_INFO_32:
+#if defined(__arm__) || defined(__arm64__)
+ case TASK_BASIC_INFO_64:
+#endif
+ {
+ 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;
+ }
+
+#if defined(__arm__) || defined(__arm64__)
+ case TASK_BASIC_INFO_64_2:
+ {
+ task_basic_info_64_2_t basic_info;
+ vm_map_t map;
+ clock_sec_t secs;
+ clock_usec_t usecs;
+
+ if (*task_info_count < TASK_BASIC_INFO_64_2_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ basic_info = (task_basic_info_64_2_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_2_COUNT;
+ break;
+ }
+
+#else /* defined(__arm__) || defined(__arm64__) */
+ 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;
+ }
+#endif /* defined(__arm__) || defined(__arm64__) */
+
+ 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:
+ {
+ task_thread_times_info_t times_info;
+ 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, NULL);
+
+ 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;
+ 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_64Bit_addr(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:
+ {
+ 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:
+ {
+ 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:
+ {
+ 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:
+ {
+ 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:
+ {
+ task_events_info_t events_info;
+ 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, NULL, NULL);
+ break;
+ }
+
+ case TASK_POWER_INFO_V2:
+ {
+ if (*task_info_count < TASK_POWER_INFO_V2_COUNT_OLD) {
+ 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, tpiv2, NULL);
+ break;
+ }
+
+ case TASK_VM_INFO:
+ case TASK_VM_INFO_PURGEABLE:
+ {
+ task_vm_info_t vm_info;
+ vm_map_t map;
+
+#if __arm64__
+ struct proc *p;
+ uint32_t platform, sdk;
+ p = current_proc();
+ platform = proc_platform(p);
+ sdk = proc_sdk(p);
+ if (original_task_info_count > TASK_VM_INFO_REV2_COUNT &&
+ platform == PLATFORM_IOS &&
+ sdk != 0 &&
+ (sdk >> 16) <= 12) {
+ /*
+ * Some iOS apps pass an incorrect value for
+ * task_info_count, expressed in number of bytes
+ * instead of number of "natural_t" elements.
+ * For the sake of backwards binary compatibility
+ * for apps built with an iOS12 or older SDK and using
+ * the "rev2" data structure, let's fix task_info_count
+ * for them, to avoid stomping past the actual end
+ * of their buffer.
+ */
+#if DEVELOPMENT || DEBUG
+ printf("%s:%d %d[%s] rdar://49484582 task_info_count %d -> %d platform %d sdk %d.%d.%d\n", __FUNCTION__, __LINE__, proc_pid(p), proc_name_address(p), original_task_info_count, TASK_VM_INFO_REV2_COUNT, platform, (sdk >> 16), ((sdk >> 8) & 0xff), (sdk & 0xff));
+#endif /* DEVELOPMENT || DEBUG */
+ DTRACE_VM4(workaround_task_vm_info_count,
+ mach_msg_type_number_t, original_task_info_count,
+ mach_msg_type_number_t, TASK_VM_INFO_REV2_COUNT,
+ uint32_t, platform,
+ uint32_t, sdk);
+ original_task_info_count = TASK_VM_INFO_REV2_COUNT;
+ *task_info_count = original_task_info_count;
+ }
+#endif /* __arm64__ */
+
+ if (*task_info_count < TASK_VM_INFO_REV0_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_compressed_size;
+ mach_vm_size_t volatile_pmap_size;
+ mach_vm_size_t volatile_compressed_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_compressed_size,
+ &volatile_pmap_size,
+ &volatile_compressed_pmap_size);
+ if (kr == KERN_SUCCESS) {
+ vm_info->purgeable_volatile_pmap =
+ volatile_pmap_size;
+ if (radar_20146450) {
+ vm_info->compressed -=
+ volatile_compressed_pmap_size;
+ }
+ vm_info->purgeable_volatile_resident =
+ volatile_resident_size;
+ vm_info->purgeable_volatile_virtual =
+ volatile_virtual_size;
+ }
+ }
+ }
+ *task_info_count = TASK_VM_INFO_REV0_COUNT;
+
+ if (original_task_info_count >= TASK_VM_INFO_REV1_COUNT) {
+ vm_info->phys_footprint =
+ (mach_vm_size_t) get_task_phys_footprint(task);
+ *task_info_count = TASK_VM_INFO_REV1_COUNT;
+ }
+ if (original_task_info_count >= TASK_VM_INFO_REV2_COUNT) {
+ vm_info->min_address = map->min_offset;
+ vm_info->max_address = map->max_offset;
+ *task_info_count = TASK_VM_INFO_REV2_COUNT;
+ }
+ if (original_task_info_count >= TASK_VM_INFO_REV3_COUNT) {
+ ledger_get_lifetime_max(task->ledger,
+ task_ledgers.phys_footprint,
+ &vm_info->ledger_phys_footprint_peak);
+ ledger_get_balance(task->ledger,
+ task_ledgers.purgeable_nonvolatile,
+ &vm_info->ledger_purgeable_nonvolatile);
+ ledger_get_balance(task->ledger,
+ task_ledgers.purgeable_nonvolatile_compressed,
+ &vm_info->ledger_purgeable_novolatile_compressed);
+ ledger_get_balance(task->ledger,
+ task_ledgers.purgeable_volatile,
+ &vm_info->ledger_purgeable_volatile);
+ ledger_get_balance(task->ledger,
+ task_ledgers.purgeable_volatile_compressed,
+ &vm_info->ledger_purgeable_volatile_compressed);
+ ledger_get_balance(task->ledger,
+ task_ledgers.network_nonvolatile,
+ &vm_info->ledger_tag_network_nonvolatile);
+ ledger_get_balance(task->ledger,
+ task_ledgers.network_nonvolatile_compressed,
+ &vm_info->ledger_tag_network_nonvolatile_compressed);
+ ledger_get_balance(task->ledger,
+ task_ledgers.network_volatile,
+ &vm_info->ledger_tag_network_volatile);
+ ledger_get_balance(task->ledger,
+ task_ledgers.network_volatile_compressed,
+ &vm_info->ledger_tag_network_volatile_compressed);
+ ledger_get_balance(task->ledger,
+ task_ledgers.media_footprint,
+ &vm_info->ledger_tag_media_footprint);
+ ledger_get_balance(task->ledger,
+ task_ledgers.media_footprint_compressed,
+ &vm_info->ledger_tag_media_footprint_compressed);
+ ledger_get_balance(task->ledger,
+ task_ledgers.media_nofootprint,
+ &vm_info->ledger_tag_media_nofootprint);
+ ledger_get_balance(task->ledger,
+ task_ledgers.media_nofootprint_compressed,
+ &vm_info->ledger_tag_media_nofootprint_compressed);
+ ledger_get_balance(task->ledger,
+ task_ledgers.graphics_footprint,
+ &vm_info->ledger_tag_graphics_footprint);
+ ledger_get_balance(task->ledger,
+ task_ledgers.graphics_footprint_compressed,
+ &vm_info->ledger_tag_graphics_footprint_compressed);
+ ledger_get_balance(task->ledger,
+ task_ledgers.graphics_nofootprint,
+ &vm_info->ledger_tag_graphics_nofootprint);
+ ledger_get_balance(task->ledger,
+ task_ledgers.graphics_nofootprint_compressed,
+ &vm_info->ledger_tag_graphics_nofootprint_compressed);
+ ledger_get_balance(task->ledger,
+ task_ledgers.neural_footprint,
+ &vm_info->ledger_tag_neural_footprint);
+ ledger_get_balance(task->ledger,
+ task_ledgers.neural_footprint_compressed,
+ &vm_info->ledger_tag_neural_footprint_compressed);
+ ledger_get_balance(task->ledger,
+ task_ledgers.neural_nofootprint,
+ &vm_info->ledger_tag_neural_nofootprint);
+ ledger_get_balance(task->ledger,
+ task_ledgers.neural_nofootprint_compressed,
+ &vm_info->ledger_tag_neural_nofootprint_compressed);
+ *task_info_count = TASK_VM_INFO_REV3_COUNT;
+ }
+ if (original_task_info_count >= TASK_VM_INFO_REV4_COUNT) {
+ if (task->bsd_info) {
+ vm_info->limit_bytes_remaining =
+ memorystatus_available_memory_internal(task->bsd_info);
+ } else {
+ vm_info->limit_bytes_remaining = 0;
+ }
+ *task_info_count = TASK_VM_INFO_REV4_COUNT;
+ }
+ if (original_task_info_count >= TASK_VM_INFO_REV5_COUNT) {
+ thread_t thread;
+ integer_t total = task->decompressions;
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ total += thread->decompressions;
+ }
+ vm_info->decompressions = total;
+ *task_info_count = TASK_VM_INFO_REV5_COUNT;
+ }
+
+ if (task != kernel_task) {
+ vm_map_unlock_read(map);
+ }
+
+ 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));
+
+#if CONFIG_SCHED_SFI
+ 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];
+ }
+
+#endif /* CONFIG_SCHED_SFI */
+ wait_state_info->total_wait_sfi_state_time = total_sfi_ledger_val;
+ *task_info_count = TASK_WAIT_STATE_INFO_COUNT;
+
+ break;
+ }
+ case TASK_VM_INFO_PURGEABLE_ACCOUNT:
+ {
+#if DEVELOPMENT || DEBUG
+ pvm_account_info_t acnt_info;
+
+ if (*task_info_count < PVM_ACCOUNT_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ if (task_info_out == NULL) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ acnt_info = (pvm_account_info_t) task_info_out;
+
+ error = vm_purgeable_account(task, acnt_info);
+
+ *task_info_count = PVM_ACCOUNT_INFO_COUNT;
+
+ break;
+#else /* DEVELOPMENT || DEBUG */
+ error = KERN_NOT_SUPPORTED;
+ break;
+#endif /* DEVELOPMENT || DEBUG */
+ }
+ case TASK_FLAGS_INFO:
+ {
+ task_flags_info_t flags_info;
+
+ if (*task_info_count < TASK_FLAGS_INFO_COUNT) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+
+ flags_info = (task_flags_info_t)task_info_out;
+
+ /* only publish the 64-bit flag of the task */
+ flags_info->flags = task->t_flags & (TF_64B_ADDR | TF_64B_DATA);
+
+ *task_info_count = TASK_FLAGS_INFO_COUNT;
+ break;
+ }
+
+ case TASK_DEBUG_INFO_INTERNAL:
+ {
+#if DEVELOPMENT || DEBUG
+ task_debug_info_internal_t dbg_info;
+ ipc_space_t space = task->itk_space;
+ if (*task_info_count < TASK_DEBUG_INFO_INTERNAL_COUNT) {
+ error = KERN_NOT_SUPPORTED;
+ break;
+ }
+
+ if (task_info_out == NULL) {
+ error = KERN_INVALID_ARGUMENT;
+ break;
+ }
+ dbg_info = (task_debug_info_internal_t) task_info_out;
+ dbg_info->ipc_space_size = 0;
+
+ if (space) {
+ is_read_lock(space);
+ dbg_info->ipc_space_size = space->is_table_size;
+ is_read_unlock(space);
+ }
+
+ dbg_info->suspend_count = task->suspend_count;
+
+ error = KERN_SUCCESS;
+ *task_info_count = TASK_DEBUG_INFO_INTERNAL_COUNT;
+ break;
+#else /* DEVELOPMENT || DEBUG */
+ error = KERN_NOT_SUPPORTED;
+ break;
+#endif /* DEVELOPMENT || DEBUG */
+ }
+ default:
+ error = KERN_INVALID_ARGUMENT;
+ }
+
+ task_unlock(task);
+ return error;
+}
+
+/*
+ * task_info_from_user
+ *
+ * When calling task_info from user space,
+ * this function will be executed as mig server side
+ * instead of calling directly into task_info.
+ * This gives the possibility to perform more security
+ * checks on task_port.
+ *
+ * In the case of TASK_DYLD_INFO, we require the more
+ * privileged task_port not the less-privileged task_name_port.
+ *
+ */
+kern_return_t
+task_info_from_user(
+ mach_port_t task_port,
+ task_flavor_t flavor,
+ task_info_t task_info_out,
+ mach_msg_type_number_t *task_info_count)
+{
+ task_t task;
+ kern_return_t ret;
+
+ if (flavor == TASK_DYLD_INFO) {
+ task = convert_port_to_task(task_port);
+ } else {
+ task = convert_port_to_task_name(task_port);
+ }
+
+ ret = task_info(task, flavor, task_info_out, task_info_count);
+
+ task_deallocate(task);
+
+ return ret;
+}
+
+/*
+ * 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,
+ task_power_info_v2_t infov2,
+ uint64_t *runnable_time)
+{
+ thread_t thread;
+ ledger_amount_t tmp;
+
+ uint64_t runnable_time_sum = 0;
+
+ 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;
+ runnable_time_sum = task->total_runnable_time;
+
+#if CONFIG_EMBEDDED
+ if (infov2) {
+ infov2->task_energy = task->task_energy;
+ }
+#endif
+
+ if (ginfo) {
+ ginfo->task_gpu_utilisation = task->task_gpu_ns;
+ }
+
+ if (infov2) {
+ infov2->task_ptime = task->total_ptime;
+ infov2->task_pset_switches = task->ps_switch;
+ }
+
+ 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;
+
+#if CONFIG_EMBEDDED
+ if (infov2) {
+ infov2->task_energy += ml_energy_stat(thread);
+ }
+#endif
+
+ tval = timer_grab(&thread->user_timer);
+ info->total_user += tval;
+
+ if (infov2) {
+ tval = timer_grab(&thread->ptime);
+ infov2->task_ptime += tval;
+ infov2->task_pset_switches += thread->ps_switch;
+ }
+
+ 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;
+ }
+
+ tval = timer_grab(&thread->runnable_timer);
+
+ runnable_time_sum += tval;
+
+ if (ginfo) {
+ ginfo->task_gpu_utilisation += ml_gpu_stat(thread);
+ }
+ thread_unlock(thread);
+ splx(x);
+ }
+
+ if (runnable_time) {
+ *runnable_time = runnable_time_sum;
+ }
+}
+
+/*
+ * 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;
+#if !CONFIG_EMBEDDED
+ 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);
+#else /* CONFIG_EMBEDDED */
+ /* silence compiler warning */
+ (void)task;
+#endif /* !CONFIG_EMBEDDED */
+ return gpu_time;
+}
+
+/*
+ * task_energy
+ *
+ * Returns the total energy used by the all the threads of the task
+ * (both dead and alive)
+ */
+uint64_t
+task_energy(
+ task_t task)
+{
+ uint64_t energy = 0;
+ thread_t thread;
+
+ task_lock(task);
+ energy += task->task_energy;
+
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ spl_t x;
+ x = splsched();
+ thread_lock(thread);
+ energy += ml_energy_stat(thread);
+ thread_unlock(thread);
+ splx(x);
+ }
+
+ task_unlock(task);
+ return energy;
+}
+
+#if __AMP__
+
+uint64_t
+task_cpu_ptime(
+ task_t task)
+{
+ uint64_t cpu_ptime = 0;
+ thread_t thread;
+
+ task_lock(task);
+ cpu_ptime += task->total_ptime;
+
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ cpu_ptime += timer_grab(&thread->ptime);
+ }
+
+ task_unlock(task);
+ return cpu_ptime;
+}
+
+#else /* __AMP__ */
+
+uint64_t
+task_cpu_ptime(
+ __unused task_t task)
+{
+ return 0;
+}
+
+#endif /* __AMP__ */
+
+/* This function updates the cpu time in the arrays for each
+ * effective and requested QoS class
+ */
+void
+task_update_cpu_time_qos_stats(
+ task_t task,
+ uint64_t *eqos_stats,
+ uint64_t *rqos_stats)
+{
+ if (!eqos_stats && !rqos_stats) {
+ return;
+ }
+
+ task_lock(task);
+ thread_t thread;
+ queue_iterate(&task->threads, thread, thread_t, task_threads) {
+ if (thread->options & TH_OPT_IDLE_THREAD) {
+ continue;
+ }
+
+ thread_update_qos_cpu_time(thread);
+ }
+
+ if (eqos_stats) {
+ eqos_stats[THREAD_QOS_DEFAULT] += task->cpu_time_eqos_stats.cpu_time_qos_default;
+ eqos_stats[THREAD_QOS_MAINTENANCE] += task->cpu_time_eqos_stats.cpu_time_qos_maintenance;
+ eqos_stats[THREAD_QOS_BACKGROUND] += task->cpu_time_eqos_stats.cpu_time_qos_background;
+ eqos_stats[THREAD_QOS_UTILITY] += task->cpu_time_eqos_stats.cpu_time_qos_utility;
+ eqos_stats[THREAD_QOS_LEGACY] += task->cpu_time_eqos_stats.cpu_time_qos_legacy;
+ eqos_stats[THREAD_QOS_USER_INITIATED] += task->cpu_time_eqos_stats.cpu_time_qos_user_initiated;
+ eqos_stats[THREAD_QOS_USER_INTERACTIVE] += task->cpu_time_eqos_stats.cpu_time_qos_user_interactive;
+ }
+
+ if (rqos_stats) {
+ rqos_stats[THREAD_QOS_DEFAULT] += task->cpu_time_rqos_stats.cpu_time_qos_default;
+ rqos_stats[THREAD_QOS_MAINTENANCE] += task->cpu_time_rqos_stats.cpu_time_qos_maintenance;
+ rqos_stats[THREAD_QOS_BACKGROUND] += task->cpu_time_rqos_stats.cpu_time_qos_background;
+ rqos_stats[THREAD_QOS_UTILITY] += task->cpu_time_rqos_stats.cpu_time_qos_utility;
+ rqos_stats[THREAD_QOS_LEGACY] += task->cpu_time_rqos_stats.cpu_time_qos_legacy;
+ rqos_stats[THREAD_QOS_USER_INITIATED] += task->cpu_time_rqos_stats.cpu_time_qos_user_initiated;
+ rqos_stats[THREAD_QOS_USER_INTERACTIVE] += task->cpu_time_rqos_stats.cpu_time_qos_user_interactive;
+ }
+
+ task_unlock(task);
+}
+
+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;
+
+ 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 = 0;
+ clock_sec_t secs = 0;
+ uint64_t tsum;
+
+ assert(task == current_task());
+
+ spl_t s = splsched();
+ thread_lock(thread);
+
+ if ((task->vtimers & which) != (uint32_t)which) {
+ thread_unlock(thread);
+ splx(s);
+ return;
+ }
+
+ 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;
+ }
+
+ thread_unlock(thread);
+ splx(s);
+}
+
+/*
+ * 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 || !task->active) {
+ return KERN_FAILURE;
+ }
+
+ *pset = &pset0;
+
+ return KERN_SUCCESS;
+}
+
+uint64_t
+get_task_dispatchqueue_offset(
+ task_t task)
+{
+ return task->dispatchqueue_offset;
+}
+
+/*
+ * 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)
+{
+ /*
+ * Destroy owned semaphores
+ */
+ semaphore_destroy_all(task);
+}
+
+/*
+ * 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;
+}
+
+
+static kern_return_t __attribute__((noinline, not_tail_called))
+PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(
+ mach_exception_code_t code,
+ mach_exception_subcode_t subcode,
+ void *reason)
+{
+#ifdef MACH_BSD
+ if (1 == proc_selfpid()) {
+ return KERN_NOT_SUPPORTED; // initproc is immune
+ }
+#endif
+ mach_exception_data_type_t codes[EXCEPTION_CODE_MAX] = {
+ [0] = code,
+ [1] = subcode,
+ };
+ task_t task = current_task();
+ kern_return_t kr;
+
+ /* (See jetsam-related comments below) */
+
+ proc_memstat_terminated(task->bsd_info, TRUE);
+ kr = task_enqueue_exception_with_corpse(task, EXC_GUARD, codes, 2, reason);
+ proc_memstat_terminated(task->bsd_info, FALSE);
+ return kr;
+}
+
+kern_return_t
+task_violated_guard(
+ mach_exception_code_t code,
+ mach_exception_subcode_t subcode,
+ void *reason)
+{
+ return PROC_VIOLATED_GUARD__SEND_EXC_GUARD_AND_SUSPEND(code, subcode, reason);
+}
+
+
+#if CONFIG_MEMORYSTATUS
+
+boolean_t
+task_get_memlimit_is_active(task_t task)
+{
+ assert(task != NULL);
+
+ if (task->memlimit_is_active == 1) {
+ return TRUE;