+ 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.
+ */
+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,
+ /* no unnesting on final cleanup: */
+ VM_MAP_REMOVE_NO_UNNESTING);
+
+ /*
+ * 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->halting = FALSE;
+}
+
+/*
+ * 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;
+
+ /*
+ * 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);
+ }
+}
+
+/*
+ * 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;
+
+ 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);
+}
+
+
+/*
+ * 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, send, old_notify;
+ mach_port_name_t name;
+
+ if (task == TASK_NULL || task == kernel_task)
+ return (KERN_INVALID_ARGUMENT);
+
+ task_lock(task);
+
+ /*
+ * Claim a send right on the task resume port, and request a no-senders
+ * notification on that port (if none outstanding).
+ */
+ if (task->itk_resume == IP_NULL) {
+ task->itk_resume = ipc_port_alloc_kernel();
+ if (!IP_VALID(task->itk_resume))
+ panic("failed to create resume port");
+ ipc_kobject_set(task->itk_resume, (ipc_kobject_t)task, IKOT_TASK_RESUME);
+ }
+
+ port = task->itk_resume;
+ ip_lock(port);
+ assert(ip_active(port));
+
+ send = ipc_port_make_send_locked(port);
+ assert(IP_VALID(send));
+
+ if (port->ip_nsrequest == IP_NULL) {
+ 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);
+ }
+
+ /*
+ * place a legacy hold on the task.
+ */
+ kr = place_task_hold(task, TASK_HOLD_LEGACY);
+ if (kr != KERN_SUCCESS) {
+ task_unlock(task);
+ ipc_port_release_send(send);
+ return kr;
+ }
+
+ 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, (ipc_object_t)send,
+ MACH_MSG_TYPE_MOVE_SEND, &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, (ipc_object_t)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 = (ipc_port_t) 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;
+}
+
+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);
+
+ 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 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);
+}
+
+
+#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();
+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,
+ boolean_t *shared,
+ boolean_t walk_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);
+
+ if (walk_only) {
+ panic("task_freeze - walk_only == TRUE");
+ } else {
+ kr = vm_map_freeze(task->map, purgeable_count, wired_count, clean_count, dirty_count, dirty_budget, shared);
+ }
+
+ task_lock(task);
+
+ if (walk_only == FALSE && kr == KERN_SUCCESS)
+ task->frozen = TRUE;
+ task->changing_freeze_state = FALSE;
+ thread_wakeup(&task->changing_freeze_state);
+
+ task_unlock(task);
+
+ if (VM_CONFIG_COMPRESSOR_IS_PRESENT) {
+ 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(task, TASK_HOST_PORT, host_port);
+ return(kr);
+}
+
+kern_return_t
+task_send_trace_memory(
+ task_t target_task,
+ __unused uint32_t pid,
+ __unused uint64_t uniqueid)
+{
+ kern_return_t kr = KERN_INVALID_ARGUMENT;
+ if (target_task == TASK_NULL)
+ return (KERN_INVALID_ARGUMENT);
+
+#if CONFIG_ATM
+ kr = atm_send_proc_inspect_notification(target_task,
+ pid,
+ uniqueid);
+
+#endif
+ return (kr);
+}
+/*
+ * 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:
+ {
+ 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:
+ {
+ 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);
+
+ 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_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:
+ {
+ 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);
+ 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;
+
+ uint64_t *task_energy = NULL;
+ task_power_info_locked(task, &tpiv2->cpu_energy, &tpiv2->gpu_energy, task_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_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 (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;
+
+ *task_info_count = TASK_FLAGS_INFO_COUNT;
+ break;
+ }
+
+ case TASK_DEBUG_INFO_INTERNAL:
+ {
+#if DEVELOPMENT || DEBUG
+ task_debug_info_internal_t dbg_info;
+ 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 (task->itk_space){
+ dbg_info->ipc_space_size = task->itk_space->is_table_size;
+ }
+
+ 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_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,
+ uint64_t *task_energy)
+{
+ 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 (task_energy) {
+ *task_energy = task->task_energy;
+ }
+
+ 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;
+
+ if (task_energy) {
+ *task_energy += ml_energy_stat(thread);
+ }
+
+ 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;
+}
+
+/*
+ * 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;
+}
+
+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;
+}
+
+#if CONFIG_MEMORYSTATUS
+#define HWM_USERCORE_MINSPACE 250 // free space (in MB) required *after* core file creation
+
+void __attribute__((noinline))
+PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb, boolean_t is_fatal)
+{
+ task_t task = current_task();
+ int pid = 0;
+ const char *procname = "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 CONFIG_COREDUMP
+ 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, COREDUMP_IGNORE_ULIMIT)) != 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);
+ }
+#endif /* CONFIG_COREDUMP */
+
+ 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;
+ }
+
+ /*
+ * A task that has triggered an EXC_RESOURCE, should not be
+ * jetsammed when the device is under memory pressure. Here
+ * we set the P_MEMSTAT_TERMINATED flag so that the process
+ * will be skipped if the memorystatus_thread wakes up.
+ */
+ proc_memstat_terminated(current_task()->bsd_info, TRUE);
+
+ 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);
+
+ /* Do not generate a corpse fork if the violation is a fatal one */
+ if (is_fatal || exc_via_corpse_forking == 0) {
+ /* Do not send a EXC_RESOURCE is corpse_for_fatal_memkill is set */
+ if (corpse_for_fatal_memkill == 0) {
+ /*
+ * 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);
+ }
+ } else {
+ task_enqueue_exception_with_corpse(task, code, EXCEPTION_CODE_MAX);
+ }
+
+ /*
+ * After the EXC_RESOURCE has been handled, we must clear the
+ * P_MEMSTAT_TERMINATED flag so that the process can again be
+ * considered for jetsam if the memorystatus_thread wakes up.
+ */
+ proc_memstat_terminated(current_task()->bsd_info, FALSE); /* clear the flag */
+}
+
+/*
+ * 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;
+ task_t task;
+ boolean_t is_fatal;
+ boolean_t trigger_exception;
+
+ if (warning == LEDGER_WARNING_DIPPED_BELOW) {
+ /*
+ * Task memory limits only provide a warning on the way up.
+ */
+ return;
+ }