]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/task.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / kern / task.c
index 20eef5136946dd1f58dac660c95f0950230b27bf..1266acd83e8b3d820f7af5c96b0b1dc38e1790df 100644 (file)
@@ -1,8 +1,8 @@
 /*
 /*
- * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * unlawful or unlicensed copies of an Apple operating system, or to
  * circumvent, violate, or enable the circumvention or violation of, any
  * terms of an Apple operating system software license agreement.
  * unlawful or unlicensed copies of an Apple operating system, or to
  * circumvent, violate, or enable the circumvention or violation of, any
  * terms of an Apple operating system software license agreement.
- * 
+ *
  * Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this file.
  * Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * @OSF_FREE_COPYRIGHT@
  */
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * @OSF_FREE_COPYRIGHT@
  */
-/* 
+/*
  * Mach Operating System
  * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
  * All Rights Reserved.
  * Mach Operating System
  * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
  * All Rights Reserved.
- * 
+ *
  * Permission to use, copy, modify and distribute this software and its
  * documentation is hereby granted, provided that both the copyright
  * notice and this permission notice appear in all copies of the
  * software, derivative works or modified versions, and any portions
  * thereof, and that both notices appear in supporting documentation.
  * Permission to use, copy, modify and distribute this software and its
  * documentation is hereby granted, provided that both the copyright
  * notice and this permission notice appear in all copies of the
  * software, derivative works or modified versions, and any portions
  * thereof, and that both notices appear in supporting documentation.
- * 
+ *
  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- * 
+ *
  * Carnegie Mellon requests users of this software to return to
  * Carnegie Mellon requests users of this software to return to
- * 
+ *
  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  *  School of Computer Science
  *  Carnegie Mellon University
  *  Pittsburgh PA 15213-3890
  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  *  School of Computer Science
  *  Carnegie Mellon University
  *  Pittsburgh PA 15213-3890
- * 
+ *
  * any improvements or extensions that they make and grant Carnegie Mellon
  * the rights to redistribute these changes.
  */
  * any improvements or extensions that they make and grant Carnegie Mellon
  * the rights to redistribute these changes.
  */
 #include <kern/kalloc.h>
 #include <kern/kern_cdata.h>
 #include <kern/processor.h>
 #include <kern/kalloc.h>
 #include <kern/kern_cdata.h>
 #include <kern/processor.h>
-#include <kern/sched_prim.h>   /* for thread_wakeup */
+#include <kern/sched_prim.h>    /* for thread_wakeup */
 #include <kern/ipc_tt.h>
 #include <kern/host.h>
 #include <kern/clock.h>
 #include <kern/ipc_tt.h>
 #include <kern/host.h>
 #include <kern/clock.h>
 #include <kern/exc_resource.h>
 #include <kern/machine.h>
 #include <kern/policy_internal.h>
 #include <kern/exc_resource.h>
 #include <kern/machine.h>
 #include <kern/policy_internal.h>
+#include <kern/restartable.h>
 
 #include <corpses/task_corpse.h>
 #if CONFIG_TELEMETRY
 
 #include <corpses/task_corpse.h>
 #if CONFIG_TELEMETRY
 
 #include <vm/pmap.h>
 #include <vm/vm_map.h>
 
 #include <vm/pmap.h>
 #include <vm/vm_map.h>
-#include <vm/vm_kern.h>                /* for kernel_map, ipc_kernel_map */
+#include <vm/vm_kern.h>         /* for kernel_map, ipc_kernel_map */
 #include <vm/vm_pageout.h>
 #include <vm/vm_protos.h>
 #include <vm/vm_purgeable_internal.h>
 #include <vm/vm_pageout.h>
 #include <vm/vm_protos.h>
 #include <vm/vm_purgeable_internal.h>
+#include <vm/vm_compressor_pager.h>
 
 #include <sys/resource.h>
 #include <sys/signalvar.h> /* for coredump */
 
 #include <sys/resource.h>
 #include <sys/signalvar.h> /* for coredump */
-
+#include <sys/bsdtask_info.h>
 /*
  * Exported interfaces
  */
 /*
  * Exported interfaces
  */
 #include <libkern/OSAtomic.h>
 #include <libkern/section_keywords.h>
 
 #include <libkern/OSAtomic.h>
 #include <libkern/section_keywords.h>
 
-#if CONFIG_ATM
-#include <atm/atm_internal.h>
-#endif
+#include <mach-o/loader.h>
+#include <kdp/kdp_dyld.h>
 
 
-#include <kern/sfi.h>          /* picks up ledger.h */
+#include <kern/sfi.h>           /* picks up ledger.h */
 
 #if CONFIG_MACF
 #include <security/mac_mach_internal.h>
 #endif
 
 
 #if CONFIG_MACF
 #include <security/mac_mach_internal.h>
 #endif
 
+#include <IOKit/IOBSD.h>
+
 #if KPERF
 extern int kpc_force_all_ctrs(task_t, int);
 #endif
 
 #if KPERF
 extern int kpc_force_all_ctrs(task_t, int);
 #endif
 
-task_t                 kernel_task;
-zone_t                 task_zone;
-lck_attr_t      task_lck_attr;
-lck_grp_t       task_lck_grp;
-lck_grp_attr_t  task_lck_grp_attr;
+SECURITY_READ_ONLY_LATE(task_t) kernel_task;
+
+static SECURITY_READ_ONLY_LATE(zone_t) task_zone;
+ZONE_INIT(&task_zone, "tasks", sizeof(struct task),
+    ZC_NOENCRYPT | ZC_ZFREE_CLEARMEM,
+    ZONE_ID_TASK, NULL);
 
 extern int exc_via_corpse_forking;
 extern int corpse_for_fatal_memkill;
 extern boolean_t proc_send_synchronous_EXC_RESOURCE(void *p);
 
 extern int exc_via_corpse_forking;
 extern int corpse_for_fatal_memkill;
 extern boolean_t proc_send_synchronous_EXC_RESOURCE(void *p);
+extern void task_disown_frozen_csegs(task_t owner_task);
 
 /* Flag set by core audio when audio is playing. Used to stifle EXC_RESOURCE generation when active. */
 int audio_active = 0;
 
 
 /* Flag set by core audio when audio is playing. Used to stifle EXC_RESOURCE generation when active. */
 int audio_active = 0;
 
+/*
+ *     structure for tracking zone usage
+ *     Used either one per task/thread for all zones or <per-task,per-zone>.
+ */
+typedef struct zinfo_usage_store_t {
+       /* These fields may be updated atomically, and so must be 8 byte aligned */
+       uint64_t        alloc __attribute__((aligned(8)));              /* allocation counter */
+       uint64_t        free __attribute__((aligned(8)));               /* free counter */
+} zinfo_usage_store_t;
+
 zinfo_usage_store_t tasks_tkm_private;
 zinfo_usage_store_t tasks_tkm_shared;
 
 /* A container to accumulate statistics for expired tasks */
 zinfo_usage_store_t tasks_tkm_private;
 zinfo_usage_store_t tasks_tkm_shared;
 
 /* A container to accumulate statistics for expired tasks */
-expired_task_statistics_t              dead_task_statistics;
-lck_spin_t             dead_task_statistics_lock;
+expired_task_statistics_t               dead_task_statistics;
+LCK_SPIN_DECLARE_ATTR(dead_task_statistics_lock, &task_lck_grp, &task_lck_attr);
 
 ledger_template_t task_ledger_template = NULL;
 
 
 ledger_template_t task_ledger_template = NULL;
 
+/* global lock for task_dyld_process_info_notify_{register, deregister, get_trap} */
+LCK_GRP_DECLARE(g_dyldinfo_mtx_grp, "g_dyldinfo");
+LCK_MTX_DECLARE(g_dyldinfo_mtx, &g_dyldinfo_mtx_grp);
+
 SECURITY_READ_ONLY_LATE(struct _task_ledger_indices) task_ledgers __attribute__((used)) =
 {.cpu_time = -1,
  .tkm_private = -1,
 SECURITY_READ_ONLY_LATE(struct _task_ledger_indices) task_ledgers __attribute__((used)) =
 {.cpu_time = -1,
  .tkm_private = -1,
@@ -216,21 +235,50 @@ SECURITY_READ_ONLY_LATE(struct _task_ledger_indices) task_ledgers __attribute__(
  .purgeable_nonvolatile = -1,
  .purgeable_volatile_compressed = -1,
  .purgeable_nonvolatile_compressed = -1,
  .purgeable_nonvolatile = -1,
  .purgeable_volatile_compressed = -1,
  .purgeable_nonvolatile_compressed = -1,
+ .tagged_nofootprint = -1,
+ .tagged_footprint = -1,
+ .tagged_nofootprint_compressed = -1,
+ .tagged_footprint_compressed = -1,
  .network_volatile = -1,
  .network_nonvolatile = -1,
  .network_volatile_compressed = -1,
  .network_nonvolatile_compressed = -1,
  .network_volatile = -1,
  .network_nonvolatile = -1,
  .network_volatile_compressed = -1,
  .network_nonvolatile_compressed = -1,
+ .media_nofootprint = -1,
+ .media_footprint = -1,
+ .media_nofootprint_compressed = -1,
+ .media_footprint_compressed = -1,
+ .graphics_nofootprint = -1,
+ .graphics_footprint = -1,
+ .graphics_nofootprint_compressed = -1,
+ .graphics_footprint_compressed = -1,
+ .neural_nofootprint = -1,
+ .neural_footprint = -1,
+ .neural_nofootprint_compressed = -1,
+ .neural_footprint_compressed = -1,
  .platform_idle_wakeups = -1,
  .interrupt_wakeups = -1,
  .platform_idle_wakeups = -1,
  .interrupt_wakeups = -1,
-#if !CONFIG_EMBEDDED
+#if CONFIG_SCHED_SFI
  .sfi_wait_times = { 0 /* initialized at runtime */},
  .sfi_wait_times = { 0 /* initialized at runtime */},
-#endif /* !CONFIG_EMBEDDED */   
+#endif /* CONFIG_SCHED_SFI */
  .cpu_time_billed_to_me = -1,
  .cpu_time_billed_to_others = -1,
  .physical_writes = -1,
  .logical_writes = -1,
  .cpu_time_billed_to_me = -1,
  .cpu_time_billed_to_others = -1,
  .physical_writes = -1,
  .logical_writes = -1,
+ .logical_writes_to_external = -1,
+#if DEBUG || DEVELOPMENT
+ .pages_grabbed = -1,
+ .pages_grabbed_kern = -1,
+ .pages_grabbed_iopl = -1,
+ .pages_grabbed_upl = -1,
+#endif
+#if CONFIG_FREEZE
+ .frozen_to_swap = -1,
+#endif /* CONFIG_FREEZE */
  .energy_billed_to_me = -1,
  .energy_billed_to_me = -1,
- .energy_billed_to_others = -1
+ .energy_billed_to_others = -1,
+#if CONFIG_PHYS_WRITE_ACCT
+ .fs_metadata_writes = -1,
+#endif /* CONFIG_PHYS_WRITE_ACCT */
 };
 
 /* System sleep state */
 };
 
 /* System sleep state */
@@ -250,16 +298,17 @@ kern_return_t task_resume_internal(task_t);
 static kern_return_t task_start_halt_locked(task_t task, boolean_t should_mark_corpse);
 
 extern kern_return_t iokit_task_terminate(task_t task);
 static kern_return_t task_start_halt_locked(task_t task, boolean_t should_mark_corpse);
 
 extern kern_return_t iokit_task_terminate(task_t task);
+extern void          iokit_task_app_suspended_changed(task_t task);
 
 extern kern_return_t exception_deliver(thread_t, exception_type_t, mach_exception_data_t, mach_msg_type_number_t, struct exception_action *, lck_mtx_t *);
 extern void bsd_copythreadname(void *dst_uth, void *src_uth);
 extern kern_return_t thread_resume(thread_t thread);
 
 // Warn tasks when they hit 80% of their memory limit.
 
 extern kern_return_t exception_deliver(thread_t, exception_type_t, mach_exception_data_t, mach_msg_type_number_t, struct exception_action *, lck_mtx_t *);
 extern void bsd_copythreadname(void *dst_uth, void *src_uth);
 extern kern_return_t thread_resume(thread_t thread);
 
 // Warn tasks when they hit 80% of their memory limit.
-#define        PHYS_FOOTPRINT_WARNING_LEVEL 80
+#define PHYS_FOOTPRINT_WARNING_LEVEL 80
 
 
-#define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT             150 /* wakeups per second */
-#define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL  300 /* in seconds. */
+#define TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT              150 /* wakeups per second */
+#define TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL   300 /* in seconds. */
 
 /*
  * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
 
 /*
  * Level (in terms of percentage of the limit) at which the wakeups monitor triggers telemetry.
@@ -267,30 +316,31 @@ extern kern_return_t thread_resume(thread_t thread);
  * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
  *  stacktraces, aka micro-stackshots)
  */
  * (ie when the task's wakeups rate exceeds 70% of the limit, start taking user
  *  stacktraces, aka micro-stackshots)
  */
-#define        TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER        70
+#define TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER        70
 
 int task_wakeups_monitor_interval; /* In seconds. Time period over which wakeups rate is observed */
 int task_wakeups_monitor_rate;     /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
 
 
 int task_wakeups_monitor_interval; /* In seconds. Time period over which wakeups rate is observed */
 int task_wakeups_monitor_rate;     /* In hz. Maximum allowable wakeups per task before EXC_RESOURCE is sent */
 
-int task_wakeups_monitor_ustackshots_trigger_pct; /* Percentage. Level at which we start gathering telemetry. */
+unsigned int task_wakeups_monitor_ustackshots_trigger_pct; /* Percentage. Level at which we start gathering telemetry. */
 
 int disable_exc_resource; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
 
 ledger_amount_t max_task_footprint = 0;  /* Per-task limit on physical memory consumption in bytes     */
 
 int disable_exc_resource; /* Global override to supress EXC_RESOURCE for resource monitor violations. */
 
 ledger_amount_t max_task_footprint = 0;  /* Per-task limit on physical memory consumption in bytes     */
-int max_task_footprint_warning_level = 0;  /* Per-task limit warning percentage */
+unsigned int max_task_footprint_warning_level = 0;  /* Per-task limit warning percentage */
 int max_task_footprint_mb = 0;  /* Per-task limit on physical memory consumption in megabytes */
 
 /* I/O Monitor Limits */
 int max_task_footprint_mb = 0;  /* Per-task limit on physical memory consumption in megabytes */
 
 /* I/O Monitor Limits */
-#define IOMON_DEFAULT_LIMIT                    (20480ull)      /* MB of logical/physical I/O */
-#define IOMON_DEFAULT_INTERVAL                 (86400ull)      /* in seconds */
+#define IOMON_DEFAULT_LIMIT                     (20480ull)      /* MB of logical/physical I/O */
+#define IOMON_DEFAULT_INTERVAL                  (86400ull)      /* in seconds */
 
 
-uint64_t task_iomon_limit_mb;          /* Per-task I/O monitor limit in MBs */
-uint64_t task_iomon_interval_secs;     /* Per-task I/O monitor interval in secs */
+uint64_t task_iomon_limit_mb;           /* Per-task I/O monitor limit in MBs */
+uint64_t task_iomon_interval_secs;      /* Per-task I/O monitor interval in secs */
 
 
-#define IO_TELEMETRY_DEFAULT_LIMIT             (10ll * 1024ll * 1024ll)
-int64_t io_telemetry_limit;                    /* Threshold to take a microstackshot (0 indicated I/O telemetry is turned off) */
-int64_t global_logical_writes_count = 0;       /* Global count for logical writes */
-static boolean_t global_update_logical_writes(int64_t);
+#define IO_TELEMETRY_DEFAULT_LIMIT              (10ll * 1024ll * 1024ll)
+int64_t io_telemetry_limit;                     /* Threshold to take a microstackshot (0 indicated I/O telemetry is turned off) */
+int64_t global_logical_writes_count = 0;        /* Global count for logical writes */
+int64_t global_logical_writes_to_external_count = 0;        /* Global count for logical writes to external storage*/
+static boolean_t global_update_logical_writes(int64_t, int64_t*);
 
 #define TASK_MAX_THREAD_LIMIT 256
 
 
 #define TASK_MAX_THREAD_LIMIT 256
 
@@ -306,21 +356,24 @@ int hwm_user_cores = 0; /* high watermark violations generate user core files */
 #endif
 
 #ifdef MACH_BSD
 #endif
 
 #ifdef MACH_BSD
-extern void    proc_getexecutableuuid(void *, unsigned char *, unsigned long);
-extern int     proc_pid(struct proc *p);
-extern int     proc_selfpid(void);
+extern uint32_t proc_platform(const struct proc *);
+extern uint32_t proc_min_sdk(struct proc *);
+extern void     proc_getexecutableuuid(void *, unsigned char *, unsigned long);
+extern int      proc_pid(struct proc *p);
+extern int      proc_selfpid(void);
 extern struct proc *current_proc(void);
 extern struct proc *current_proc(void);
-extern char    *proc_name_address(struct proc *p);
+extern char     *proc_name_address(struct proc *p);
 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
-extern int kevent_proc_copy_uptrs(void *proc, uint64_t *buf, int bufsize);
+extern int kevent_proc_copy_uptrs(void *proc, uint64_t *buf, uint32_t bufsize);
 extern void workq_proc_suspended(struct proc *p);
 extern void workq_proc_resumed(struct proc *p);
 
 #if CONFIG_MEMORYSTATUS
 extern void workq_proc_suspended(struct proc *p);
 extern void workq_proc_resumed(struct proc *p);
 
 #if CONFIG_MEMORYSTATUS
-extern void    proc_memstat_terminated(struct proc* p, boolean_t set);
-extern void    memorystatus_on_ledger_footprint_exceeded(int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
-extern void    memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
+extern void     proc_memstat_terminated(struct proc* p, boolean_t set);
+extern void     memorystatus_on_ledger_footprint_exceeded(int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
+extern void     memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
 extern boolean_t memorystatus_allowed_vm_map_fork(task_t task);
 extern boolean_t memorystatus_allowed_vm_map_fork(task_t task);
+extern uint64_t  memorystatus_available_memory_internal(struct proc *p);
 
 #if DEVELOPMENT || DEBUG
 extern void memorystatus_abort_vm_map_fork(task_t);
 
 #if DEVELOPMENT || DEBUG
 extern void memorystatus_abort_vm_map_fork(task_t);
@@ -334,9 +387,9 @@ extern void memorystatus_abort_vm_map_fork(task_t);
 int exc_resource_threads_enabled;
 #endif /* DEVELOPMENT || DEBUG */
 
 int exc_resource_threads_enabled;
 #endif /* DEVELOPMENT || DEBUG */
 
-#if (DEVELOPMENT || DEBUG) && TASK_EXC_GUARD_DELIVER_CORPSE
-uint32_t task_exc_guard_default = TASK_EXC_GUARD_MP_DELIVER | TASK_EXC_GUARD_MP_CORPSE |
-                                  TASK_EXC_GUARD_VM_DELIVER | TASK_EXC_GUARD_VM_CORPSE;
+#if (DEVELOPMENT || DEBUG)
+uint32_t task_exc_guard_default = TASK_EXC_GUARD_MP_DELIVER | TASK_EXC_GUARD_MP_ONCE | TASK_EXC_GUARD_MP_CORPSE |
+    TASK_EXC_GUARD_VM_DELIVER | TASK_EXC_GUARD_VM_ONCE | TASK_EXC_GUARD_VM_CORPSE;
 #else
 uint32_t task_exc_guard_default = 0;
 #endif
 #else
 uint32_t task_exc_guard_default = 0;
 #endif
@@ -348,13 +401,35 @@ static void task_wait_locked(task_t task, boolean_t until_not_runnable);
 static void task_release_locked(task_t task);
 
 static void task_synchronizer_destroy_all(task_t task);
 static void task_release_locked(task_t task);
 
 static void task_synchronizer_destroy_all(task_t task);
+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);
+
+static os_ref_count_t
+task_remove_turnstile_watchports_locked(
+       task_t                 task,
+       struct task_watchports *watchports,
+       ipc_port_t             *port_freelist);
+
+static struct task_watchports *
+task_watchports_alloc_init(
+       task_t        task,
+       thread_t      thread,
+       uint32_t      count);
 
 
+static void
+task_watchports_deallocate(
+       struct task_watchports *watchports);
 
 void
 task_set_64bit(
 
 void
 task_set_64bit(
-               task_t task,
-               boolean_t is_64bit,
-               boolean_t is_64bit_data)
+       task_t task,
+       boolean_t is_64bit,
+       boolean_t is_64bit_data)
 {
 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
        thread_t thread;
 {
 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
        thread_t thread;
@@ -379,13 +454,15 @@ task_set_64bit(
         * Switching to/from 64-bit register state.
         */
        if (is_64bit_data) {
         * Switching to/from 64-bit register state.
         */
        if (is_64bit_data) {
-               if (task_has_64Bit_data(task))
+               if (task_has_64Bit_data(task)) {
                        goto out;
                        goto out;
+               }
 
                task_set_64Bit_data(task);
        } else {
 
                task_set_64Bit_data(task);
        } else {
-               if ( !task_has_64Bit_data(task))
+               if (!task_has_64Bit_data(task)) {
                        goto out;
                        goto out;
+               }
 
                task_clear_64Bit_data(task);
        }
 
                task_clear_64Bit_data(task);
        }
@@ -402,31 +479,6 @@ task_set_64bit(
                thread_mtx_lock(thread);
                machine_thread_switch_addrmode(thread);
                thread_mtx_unlock(thread);
                thread_mtx_lock(thread);
                machine_thread_switch_addrmode(thread);
                thread_mtx_unlock(thread);
-
-#if defined(__arm64__)
-               /* specifically, if running on H9 */
-               if (thread == current_thread()) {
-                       uint64_t arg1, arg2;
-                       int urgency;
-                       spl_t spl = splsched();
-                       /*
-                        * This call tell that the current thread changed it's 32bitness.
-                        * Other thread were no more on core when 32bitness was changed,
-                        * but current_thread() is on core and the previous call to
-                        * machine_thread_going_on_core() gave 32bitness which is now wrong.
-                        *
-                        * This is needed for bring-up, a different callback should be used
-                        * in the future.
-                        *
-                        * TODO: Remove this callout when we no longer support 32-bit code on H9
-                        */
-                       thread_lock(thread);
-                       urgency = thread_get_urgency(thread, &arg1, &arg2);
-                       machine_thread_going_on_core(thread, urgency, 0, 0, mach_approximate_time());
-                       thread_unlock(thread);
-                       splx(spl);
-               }
-#endif /* defined(__arm64__) */
        }
 #endif /* defined(__x86_64__) || defined(__arm64__) */
 
        }
 #endif /* defined(__x86_64__) || defined(__arm64__) */
 
@@ -442,14 +494,18 @@ task_get_64bit_data(task_t task)
 
 void
 task_set_platform_binary(
 
 void
 task_set_platform_binary(
-               task_t task,
-               boolean_t is_platform)
+       task_t task,
+       boolean_t is_platform)
 {
        task_lock(task);
        if (is_platform) {
                task->t_flags |= TF_PLATFORM;
 {
        task_lock(task);
        if (is_platform) {
                task->t_flags |= TF_PLATFORM;
+               /* set exc guard default behavior for first-party code */
+               task->task_exc_guard = (task_exc_guard_default & TASK_EXC_GUARD_ALL);
        } else {
                task->t_flags &= ~(TF_PLATFORM);
        } else {
                task->t_flags &= ~(TF_PLATFORM);
+               /* set exc guard default behavior for third-party code */
+               task->task_exc_guard = ((task_exc_guard_default >> TASK_EXC_GUARD_THIRD_PARTY_DEFAULT_SHIFT) & TASK_EXC_GUARD_ALL);
        }
        task_unlock(task);
 }
        }
        task_unlock(task);
 }
@@ -460,17 +516,18 @@ task_set_platform_binary(
  */
 bool
 task_set_ca_client_wi(
  */
 bool
 task_set_ca_client_wi(
-               task_t task,
-               boolean_t set_or_clear)
+       task_t task,
+       boolean_t set_or_clear)
 {
        bool ret = true;
        task_lock(task);
        if (set_or_clear) {
                /* Tasks can have only one CA_CLIENT work interval */
 {
        bool ret = true;
        task_lock(task);
        if (set_or_clear) {
                /* Tasks can have only one CA_CLIENT work interval */
-               if (task->t_flags & TF_CA_CLIENT_WI)
+               if (task->t_flags & TF_CA_CLIENT_WI) {
                        ret = false;
                        ret = false;
-               else
+               } else {
                        task->t_flags |= TF_CA_CLIENT_WI;
                        task->t_flags |= TF_CA_CLIENT_WI;
+               }
        } else {
                task->t_flags &= ~TF_CA_CLIENT_WI;
        }
        } else {
                task->t_flags &= ~TF_CA_CLIENT_WI;
        }
@@ -480,33 +537,31 @@ task_set_ca_client_wi(
 
 void
 task_set_dyld_info(
 
 void
 task_set_dyld_info(
-    task_t task, 
-    mach_vm_address_t addr, 
-    mach_vm_size_t size)
+       task_t task,
+       mach_vm_address_t addr,
+       mach_vm_size_t size)
 {
        task_lock(task);
        task->all_image_info_addr = addr;
 {
        task_lock(task);
        task->all_image_info_addr = addr;
-       task->all_image_info_size = size;    
-    task_unlock(task);
+       task->all_image_info_size = size;
+       task_unlock(task);
 }
 
 void
 }
 
 void
-task_atm_reset(__unused task_t task) {
-
-#if CONFIG_ATM
-       if (task->atm_context != NULL) {
-                atm_task_descriptor_destroy(task->atm_context);
-                task->atm_context = NULL;
-       }
-#endif
-
+task_set_mach_header_address(
+       task_t task,
+       mach_vm_address_t addr)
+{
+       task_lock(task);
+       task->mach_header_vm_address = addr;
+       task_unlock(task);
 }
 
 void
 }
 
 void
-task_bank_reset(__unused task_t task) {
-
+task_bank_reset(__unused task_t task)
+{
        if (task->bank_context != NULL) {
        if (task->bank_context != NULL) {
-                bank_task_destroy(task);
+               bank_task_destroy(task);
        }
 }
 
        }
 }
 
@@ -516,8 +571,8 @@ task_bank_reset(__unused task_t task) {
  *      proc associated with the task.
  */
 void
  *      proc associated with the task.
  */
 void
-task_bank_init(__unused task_t task) {
-
+task_bank_init(__unused task_t task)
+{
        if (task->bank_context != NULL) {
                panic("Task bank init called with non null bank context for task: %p and bank_context: %p", task, task->bank_context);
        }
        if (task->bank_context != NULL) {
                panic("Task bank init called with non null bank context for task: %p and bank_context: %p", task, task->bank_context);
        }
@@ -536,54 +591,80 @@ task_clear_exec_copy_flag(task_t task)
        task->t_procflags &= ~TPF_EXEC_COPY;
 }
 
        task->t_procflags &= ~TPF_EXEC_COPY;
 }
 
-/*
- * This wait event is t_procflags instead of t_flags because t_flags is volatile
- *
- * TODO: store the flags in the same place as the event
- * rdar://problem/28501994
- */
 event_t
 task_get_return_wait_event(task_t task)
 {
 event_t
 task_get_return_wait_event(task_t task)
 {
-       return (event_t)&task->t_procflags;
+       return (event_t)&task->returnwait_inheritor;
 }
 
 void
 }
 
 void
-task_clear_return_wait(task_t task)
+task_clear_return_wait(task_t task, uint32_t flags)
 {
 {
-       task_lock(task);
-
-       task->t_flags &= ~TF_LRETURNWAIT;
-
-       if (task->t_flags & TF_LRETURNWAITER) {
+       if (flags & TCRW_CLEAR_INITIAL_WAIT) {
                thread_wakeup(task_get_return_wait_event(task));
                thread_wakeup(task_get_return_wait_event(task));
-               task->t_flags &= ~TF_LRETURNWAITER;
        }
 
        }
 
-       task_unlock(task);
+       if (flags & TCRW_CLEAR_FINAL_WAIT) {
+               is_write_lock(task->itk_space);
+
+               task->t_returnwaitflags &= ~TRW_LRETURNWAIT;
+               task->returnwait_inheritor = NULL;
+
+               if (task->t_returnwaitflags & TRW_LRETURNWAITER) {
+                       struct turnstile *turnstile = turnstile_prepare((uintptr_t) task_get_return_wait_event(task),
+                           NULL, TURNSTILE_NULL, TURNSTILE_ULOCK);
+
+                       waitq_wakeup64_all(&turnstile->ts_waitq,
+                           CAST_EVENT64_T(task_get_return_wait_event(task)),
+                           THREAD_AWAKENED, 0);
+
+                       turnstile_update_inheritor(turnstile, NULL,
+                           TURNSTILE_IMMEDIATE_UPDATE | TURNSTILE_INHERITOR_THREAD);
+                       turnstile_update_inheritor_complete(turnstile, TURNSTILE_INTERLOCK_HELD);
+
+                       turnstile_complete((uintptr_t) task_get_return_wait_event(task), NULL, NULL, TURNSTILE_ULOCK);
+                       turnstile_cleanup();
+                       task->t_returnwaitflags &= ~TRW_LRETURNWAITER;
+               }
+               is_write_unlock(task->itk_space);
+       }
 }
 
 void __attribute__((noreturn))
 task_wait_to_return(void)
 {
 }
 
 void __attribute__((noreturn))
 task_wait_to_return(void)
 {
-       task_t task;
+       task_t task = current_task();
 
 
-       task = current_task();
-       task_lock(task);
+       is_write_lock(task->itk_space);
+
+       if (task->t_returnwaitflags & TRW_LRETURNWAIT) {
+               struct turnstile *turnstile = turnstile_prepare((uintptr_t) task_get_return_wait_event(task),
+                   NULL, TURNSTILE_NULL, TURNSTILE_ULOCK);
 
 
-       if (task->t_flags & TF_LRETURNWAIT) {
                do {
                do {
-                       task->t_flags |= TF_LRETURNWAITER;
-                       assert_wait(task_get_return_wait_event(task), THREAD_UNINT);
-                       task_unlock(task);
+                       task->t_returnwaitflags |= TRW_LRETURNWAITER;
+                       turnstile_update_inheritor(turnstile, task->returnwait_inheritor,
+                           (TURNSTILE_DELAYED_UPDATE | TURNSTILE_INHERITOR_THREAD));
+
+                       waitq_assert_wait64(&turnstile->ts_waitq,
+                           CAST_EVENT64_T(task_get_return_wait_event(task)),
+                           THREAD_UNINT, TIMEOUT_WAIT_FOREVER);
+
+                       is_write_unlock(task->itk_space);
+
+                       turnstile_update_inheritor_complete(turnstile, TURNSTILE_INTERLOCK_NOT_HELD);
 
                        thread_block(THREAD_CONTINUE_NULL);
 
 
                        thread_block(THREAD_CONTINUE_NULL);
 
-                       task_lock(task);
-               } while (task->t_flags & TF_LRETURNWAIT);
+                       is_write_lock(task->itk_space);
+               } while (task->t_returnwaitflags & TRW_LRETURNWAIT);
+
+               turnstile_complete((uintptr_t) task_get_return_wait_event(task), NULL, NULL, TURNSTILE_ULOCK);
        }
 
        }
 
-       task_unlock(task);
+       is_write_unlock(task->itk_space);
+       turnstile_cleanup();
+
 
 #if CONFIG_MACF
        /*
 
 #if CONFIG_MACF
        /*
@@ -643,11 +724,11 @@ task_is_halting(task_t task)
 #include <kern/btlog.h>
 
 static btlog_t *task_ref_btlog;
 #include <kern/btlog.h>
 
 static btlog_t *task_ref_btlog;
-#define TASK_REF_OP_INCR       0x1
-#define TASK_REF_OP_DECR       0x2
+#define TASK_REF_OP_INCR        0x1
+#define TASK_REF_OP_DECR        0x2
 
 
-#define TASK_REF_NUM_RECORDS   100000
-#define TASK_REF_BTDEPTH       7
+#define TASK_REF_NUM_RECORDS    100000
+#define TASK_REF_BTDEPTH        7
 
 void
 task_reference_internal(task_t task)
 
 void
 task_reference_internal(task_t task)
@@ -655,24 +736,25 @@ task_reference_internal(task_t task)
        void *       bt[TASK_REF_BTDEPTH];
        int             numsaved = 0;
 
        void *       bt[TASK_REF_BTDEPTH];
        int             numsaved = 0;
 
+       task_require(task);
+       os_ref_retain(&task->ref_count);
+
        numsaved = OSBacktrace(bt, TASK_REF_BTDEPTH);
        numsaved = OSBacktrace(bt, TASK_REF_BTDEPTH);
-       
-       (void)hw_atomic_add(&(task)->ref_count, 1);
        btlog_add_entry(task_ref_btlog, task, TASK_REF_OP_INCR,
        btlog_add_entry(task_ref_btlog, task, TASK_REF_OP_INCR,
-                                       bt, numsaved);
+           bt, numsaved);
 }
 
 }
 
-uint32_t
+os_ref_count_t
 task_deallocate_internal(task_t task)
 {
        void *       bt[TASK_REF_BTDEPTH];
        int             numsaved = 0;
 
        numsaved = OSBacktrace(bt, TASK_REF_BTDEPTH);
 task_deallocate_internal(task_t task)
 {
        void *       bt[TASK_REF_BTDEPTH];
        int             numsaved = 0;
 
        numsaved = OSBacktrace(bt, TASK_REF_BTDEPTH);
-
        btlog_add_entry(task_ref_btlog, task, TASK_REF_OP_DECR,
        btlog_add_entry(task_ref_btlog, task, TASK_REF_OP_DECR,
-                                       bt, numsaved);
-       return hw_atomic_sub(&(task)->ref_count, 1);
+           bt, numsaved);
+
+       return os_ref_release(&task->ref_count);
 }
 
 #endif /* TASK_REFERENCE_LEAK_DEBUG */
 }
 
 #endif /* TASK_REFERENCE_LEAK_DEBUG */
@@ -680,25 +762,6 @@ task_deallocate_internal(task_t task)
 void
 task_init(void)
 {
 void
 task_init(void)
 {
-
-       lck_grp_attr_setdefault(&task_lck_grp_attr);
-       lck_grp_init(&task_lck_grp, "task", &task_lck_grp_attr);
-       lck_attr_setdefault(&task_lck_attr);
-       lck_mtx_init(&tasks_threads_lock, &task_lck_grp, &task_lck_attr);
-       lck_mtx_init(&tasks_corpse_lock, &task_lck_grp, &task_lck_attr);
-
-       task_zone = zinit(
-                       sizeof(struct task),
-                       task_max * sizeof(struct task),
-                       TASK_CHUNK * sizeof(struct task),
-                       "tasks");
-
-       zone_change(task_zone, Z_NOENCRYPT, TRUE);
-
-#if CONFIG_EMBEDDED
-       task_watch_init();
-#endif /* CONFIG_EMBEDDED */
-
        /*
         * Configure per-task memory limit.
         * The boot-arg is interpreted as Megabytes,
        /*
         * Configure per-task memory limit.
         * The boot-arg is interpreted as Megabytes,
@@ -706,12 +769,12 @@ task_init(void)
         * Setting the boot-arg to 0 disables task limits.
         */
        if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb,
         * Setting the boot-arg to 0 disables task limits.
         */
        if (!PE_parse_boot_argn("max_task_pmem", &max_task_footprint_mb,
-                       sizeof (max_task_footprint_mb))) {
+           sizeof(max_task_footprint_mb))) {
                /*
                 * No limit was found in boot-args, so go look in the device tree.
                 */
                if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb,
                /*
                 * No limit was found in boot-args, so go look in the device tree.
                 */
                if (!PE_get_default("kern.max_task_pmem", &max_task_footprint_mb,
-                               sizeof(max_task_footprint_mb))) {
+                   sizeof(max_task_footprint_mb))) {
                        /*
                         * No limit was found in device tree.
                         */
                        /*
                         * No limit was found in device tree.
                         */
@@ -722,12 +785,12 @@ task_init(void)
        if (max_task_footprint_mb != 0) {
 #if CONFIG_MEMORYSTATUS
                if (max_task_footprint_mb < 50) {
        if (max_task_footprint_mb != 0) {
 #if CONFIG_MEMORYSTATUS
                if (max_task_footprint_mb < 50) {
-                               printf("Warning: max_task_pmem %d below minimum.\n",
-                               max_task_footprint_mb);
-                               max_task_footprint_mb = 50;
+                       printf("Warning: max_task_pmem %d below minimum.\n",
+                           max_task_footprint_mb);
+                       max_task_footprint_mb = 50;
                }
                printf("Limiting task physical memory footprint to %d MB\n",
                }
                printf("Limiting task physical memory footprint to %d MB\n",
-                       max_task_footprint_mb);
+                   max_task_footprint_mb);
 
                max_task_footprint = (ledger_amount_t)max_task_footprint_mb * 1024 * 1024; // Convert MB to bytes
 
 
                max_task_footprint = (ledger_amount_t)max_task_footprint_mb * 1024 * 1024; // Convert MB to bytes
 
@@ -771,8 +834,8 @@ task_init(void)
 
 #if DEVELOPMENT || DEBUG
        if (!PE_parse_boot_argn("exc_resource_threads",
 
 #if DEVELOPMENT || DEBUG
        if (!PE_parse_boot_argn("exc_resource_threads",
-               &exc_resource_threads_enabled,
-               sizeof(exc_resource_threads_enabled))) {
+           &exc_resource_threads_enabled,
+           sizeof(exc_resource_threads_enabled))) {
                exc_resource_threads_enabled = 1;
        }
        PE_parse_boot_argn("task_exc_guard_default",
                exc_resource_threads_enabled = 1;
        }
        PE_parse_boot_argn("task_exc_guard_default",
@@ -782,40 +845,40 @@ task_init(void)
 
 #if CONFIG_COREDUMP
        if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores,
 
 #if CONFIG_COREDUMP
        if (!PE_parse_boot_argn("hwm_user_cores", &hwm_user_cores,
-                       sizeof (hwm_user_cores))) {
+           sizeof(hwm_user_cores))) {
                hwm_user_cores = 0;
        }
 #endif
 
        proc_init_cpumon_params();
 
                hwm_user_cores = 0;
        }
 #endif
 
        proc_init_cpumon_params();
 
-       if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate, sizeof (task_wakeups_monitor_rate))) {
+       if (!PE_parse_boot_argn("task_wakeups_monitor_rate", &task_wakeups_monitor_rate, sizeof(task_wakeups_monitor_rate))) {
                task_wakeups_monitor_rate = TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT;
        }
 
                task_wakeups_monitor_rate = TASK_WAKEUPS_MONITOR_DEFAULT_LIMIT;
        }
 
-       if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval, sizeof (task_wakeups_monitor_interval))) {
+       if (!PE_parse_boot_argn("task_wakeups_monitor_interval", &task_wakeups_monitor_interval, sizeof(task_wakeups_monitor_interval))) {
                task_wakeups_monitor_interval = TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL;
        }
 
        if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct,
                task_wakeups_monitor_interval = TASK_WAKEUPS_MONITOR_DEFAULT_INTERVAL;
        }
 
        if (!PE_parse_boot_argn("task_wakeups_monitor_ustackshots_trigger_pct", &task_wakeups_monitor_ustackshots_trigger_pct,
-               sizeof (task_wakeups_monitor_ustackshots_trigger_pct))) {
+           sizeof(task_wakeups_monitor_ustackshots_trigger_pct))) {
                task_wakeups_monitor_ustackshots_trigger_pct = TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER;
        }
 
        if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource,
                task_wakeups_monitor_ustackshots_trigger_pct = TASK_WAKEUPS_MONITOR_DEFAULT_USTACKSHOTS_TRIGGER;
        }
 
        if (!PE_parse_boot_argn("disable_exc_resource", &disable_exc_resource,
-               sizeof (disable_exc_resource))) {
+           sizeof(disable_exc_resource))) {
                disable_exc_resource = 0;
        }
 
                disable_exc_resource = 0;
        }
 
-       if (!PE_parse_boot_argn("task_iomon_limit_mb", &task_iomon_limit_mb, sizeof (task_iomon_limit_mb))) {
+       if (!PE_parse_boot_argn("task_iomon_limit_mb", &task_iomon_limit_mb, sizeof(task_iomon_limit_mb))) {
                task_iomon_limit_mb = IOMON_DEFAULT_LIMIT;
        }
 
                task_iomon_limit_mb = IOMON_DEFAULT_LIMIT;
        }
 
-       if (!PE_parse_boot_argn("task_iomon_interval_secs", &task_iomon_interval_secs, sizeof (task_iomon_interval_secs))) {
+       if (!PE_parse_boot_argn("task_iomon_interval_secs", &task_iomon_interval_secs, sizeof(task_iomon_interval_secs))) {
                task_iomon_interval_secs = IOMON_DEFAULT_INTERVAL;
        }
 
                task_iomon_interval_secs = IOMON_DEFAULT_INTERVAL;
        }
 
-       if (!PE_parse_boot_argn("io_telemetry_limit", &io_telemetry_limit, sizeof (io_telemetry_limit))) {
+       if (!PE_parse_boot_argn("io_telemetry_limit", &io_telemetry_limit, sizeof(io_telemetry_limit))) {
                io_telemetry_limit = IO_TELEMETRY_DEFAULT_LIMIT;
        }
 
                io_telemetry_limit = IO_TELEMETRY_DEFAULT_LIMIT;
        }
 
@@ -839,16 +902,22 @@ task_init(void)
         * Create the kernel task as the first task.
         */
 #ifdef __LP64__
         * Create the kernel task as the first task.
         */
 #ifdef __LP64__
-       if (task_create_internal(TASK_NULL, NULL, FALSE, TRUE, TRUE, TF_NONE, TPF_NONE, &kernel_task) != KERN_SUCCESS)
+       if (task_create_internal(TASK_NULL, NULL, FALSE, TRUE, TRUE, TF_NONE, TPF_NONE, TWF_NONE, &kernel_task) != KERN_SUCCESS)
 #else
 #else
-       if (task_create_internal(TASK_NULL, NULL, FALSE, FALSE, FALSE, TF_NONE, TPF_NONE, &kernel_task) != KERN_SUCCESS)
+       if (task_create_internal(TASK_NULL, NULL, FALSE, FALSE, FALSE, TF_NONE, TPF_NONE, TWF_NONE, &kernel_task) != KERN_SUCCESS)
+#endif
+       { panic("task_init\n");}
+
+#if defined(HAS_APPLE_PAC)
+       kernel_task->rop_pid = ml_default_rop_pid();
+       kernel_task->jop_pid = ml_default_jop_pid();
+       // kernel_task never runs at EL0, but machine_thread_state_convert_from/to_user() relies on
+       // disable_user_jop to be false for kernel threads (e.g. in exception delivery on thread_exception_daemon)
+       ml_task_set_disable_user_jop(kernel_task, FALSE);
 #endif
 #endif
-               panic("task_init\n");
-
 
        vm_map_deallocate(kernel_task->map);
        kernel_task->map = kernel_map;
 
        vm_map_deallocate(kernel_task->map);
        kernel_task->map = kernel_map;
-       lck_spin_init(&dead_task_statistics_lock, &task_lck_grp, &task_lck_attr);
 }
 
 /*
 }
 
 /*
@@ -857,54 +926,57 @@ task_init(void)
  */
 kern_return_t
 kernel_task_create(
  */
 kern_return_t
 kernel_task_create(
-       __unused task_t         parent_task,
-       __unused vm_offset_t            map_base,
-       __unused vm_size_t              map_size,
-       __unused task_t         *child_task)
+       __unused task_t         parent_task,
+       __unused vm_offset_t            map_base,
+       __unused vm_size_t              map_size,
+       __unused task_t         *child_task)
 {
 {
-       return (KERN_INVALID_ARGUMENT);
+       return KERN_INVALID_ARGUMENT;
 }
 
 kern_return_t
 task_create(
 }
 
 kern_return_t
 task_create(
-       task_t                          parent_task,
-       __unused ledger_port_array_t    ledger_ports,
-       __unused mach_msg_type_number_t num_ledger_ports,
-       __unused boolean_t              inherit_memory,
-       __unused task_t                 *child_task)    /* OUT */
+       task_t                          parent_task,
+       __unused ledger_port_array_t    ledger_ports,
+       __unused mach_msg_type_number_t num_ledger_ports,
+       __unused boolean_t              inherit_memory,
+       __unused task_t                 *child_task)    /* OUT */
 {
 {
-       if (parent_task == TASK_NULL)
-               return(KERN_INVALID_ARGUMENT);
+       if (parent_task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        /*
         * No longer supported: too many calls assume that a task has a valid
         * process attached.
         */
 
        /*
         * No longer supported: too many calls assume that a task has a valid
         * process attached.
         */
-       return(KERN_FAILURE);
+       return KERN_FAILURE;
 }
 
 kern_return_t
 host_security_create_task_token(
 }
 
 kern_return_t
 host_security_create_task_token(
-       host_security_t                 host_security,
-       task_t                          parent_task,
-       __unused security_token_t       sec_token,
-       __unused audit_token_t          audit_token,
-       __unused host_priv_t            host_priv,
-       __unused ledger_port_array_t    ledger_ports,
-       __unused mach_msg_type_number_t num_ledger_ports,
-       __unused boolean_t              inherit_memory,
-       __unused task_t                 *child_task)    /* OUT */
-{
-       if (parent_task == TASK_NULL)
-               return(KERN_INVALID_ARGUMENT);
-
-       if (host_security == HOST_NULL)
-               return(KERN_INVALID_SECURITY);
+       host_security_t                 host_security,
+       task_t                          parent_task,
+       __unused security_token_t       sec_token,
+       __unused audit_token_t          audit_token,
+       __unused host_priv_t            host_priv,
+       __unused ledger_port_array_t    ledger_ports,
+       __unused mach_msg_type_number_t num_ledger_ports,
+       __unused boolean_t              inherit_memory,
+       __unused task_t                 *child_task)    /* OUT */
+{
+       if (parent_task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
+       }
+
+       if (host_security == HOST_NULL) {
+               return KERN_INVALID_SECURITY;
+       }
 
        /*
         * No longer supported.
         */
 
        /*
         * No longer supported.
         */
-       return(KERN_FAILURE);
+       return KERN_FAILURE;
 }
 
 /*
 }
 
 /*
@@ -931,32 +1003,37 @@ host_security_create_task_token(
  *
  * iokit_mapped
  *   IOKit mappings: The total size of all IOKit mappings in this task, regardless of
  *
  * iokit_mapped
  *   IOKit mappings: The total size of all IOKit mappings in this task, regardless of
-     clean/dirty or internal/external state].
*    clean/dirty or internal/external state].
  *
  * alternate_accounting
  *   The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
  *   are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
  *   double counting.
  *
  * alternate_accounting
  *   The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
  *   are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
  *   double counting.
+ *
+ * pages_grabbed
+ *   pages_grabbed counts all page grabs in a task.  It is also broken out into three subtypes
+ *   which track UPL, IOPL and Kernel page grabs.
  */
 void
 init_task_ledgers(void)
 {
        ledger_template_t t;
  */
 void
 init_task_ledgers(void)
 {
        ledger_template_t t;
-       
+
        assert(task_ledger_template == NULL);
        assert(kernel_task == TASK_NULL);
 
 #if MACH_ASSERT
        PE_parse_boot_argn("pmap_ledgers_panic",
        assert(task_ledger_template == NULL);
        assert(kernel_task == TASK_NULL);
 
 #if MACH_ASSERT
        PE_parse_boot_argn("pmap_ledgers_panic",
-                          &pmap_ledgers_panic,
-                         sizeof (pmap_ledgers_panic));
+           &pmap_ledgers_panic,
+           sizeof(pmap_ledgers_panic));
        PE_parse_boot_argn("pmap_ledgers_panic_leeway",
        PE_parse_boot_argn("pmap_ledgers_panic_leeway",
-                          &pmap_ledgers_panic_leeway,
-                         sizeof (pmap_ledgers_panic_leeway));
+           &pmap_ledgers_panic_leeway,
+           sizeof(pmap_ledgers_panic_leeway));
 #endif /* MACH_ASSERT */
 
 #endif /* MACH_ASSERT */
 
-       if ((t = ledger_template_create("Per-task ledger")) == NULL)
+       if ((t = ledger_template_create("Per-task ledger")) == NULL) {
                panic("couldn't create task ledger template");
                panic("couldn't create task ledger template");
+       }
 
        task_ledgers.cpu_time = ledger_entry_add(t, "cpu_time", "sched", "ns");
        task_ledgers.tkm_private = ledger_entry_add(t, "tkm_private",
 
        task_ledgers.cpu_time = ledger_entry_add(t, "cpu_time", "sched", "ns");
        task_ledgers.tkm_private = ledger_entry_add(t, "tkm_private",
@@ -970,32 +1047,57 @@ init_task_ledgers(void)
        task_ledgers.internal = ledger_entry_add(t, "internal", "physmem",
            "bytes");
        task_ledgers.iokit_mapped = ledger_entry_add(t, "iokit_mapped", "mappings",
        task_ledgers.internal = ledger_entry_add(t, "internal", "physmem",
            "bytes");
        task_ledgers.iokit_mapped = ledger_entry_add(t, "iokit_mapped", "mappings",
-           "bytes");
+           "bytes");
        task_ledgers.alternate_accounting = ledger_entry_add(t, "alternate_accounting", "physmem",
        task_ledgers.alternate_accounting = ledger_entry_add(t, "alternate_accounting", "physmem",
-           "bytes");
+           "bytes");
        task_ledgers.alternate_accounting_compressed = ledger_entry_add(t, "alternate_accounting_compressed", "physmem",
        task_ledgers.alternate_accounting_compressed = ledger_entry_add(t, "alternate_accounting_compressed", "physmem",
-           "bytes");
+           "bytes");
        task_ledgers.page_table = ledger_entry_add(t, "page_table", "physmem",
            "bytes");
        task_ledgers.phys_footprint = ledger_entry_add(t, "phys_footprint", "physmem",
        task_ledgers.page_table = ledger_entry_add(t, "page_table", "physmem",
            "bytes");
        task_ledgers.phys_footprint = ledger_entry_add(t, "phys_footprint", "physmem",
-           "bytes");
+           "bytes");
        task_ledgers.internal_compressed = ledger_entry_add(t, "internal_compressed", "physmem",
        task_ledgers.internal_compressed = ledger_entry_add(t, "internal_compressed", "physmem",
-           "bytes");
+           "bytes");
        task_ledgers.purgeable_volatile = ledger_entry_add(t, "purgeable_volatile", "physmem", "bytes");
        task_ledgers.purgeable_nonvolatile = ledger_entry_add(t, "purgeable_nonvolatile", "physmem", "bytes");
        task_ledgers.purgeable_volatile_compressed = ledger_entry_add(t, "purgeable_volatile_compress", "physmem", "bytes");
        task_ledgers.purgeable_nonvolatile_compressed = ledger_entry_add(t, "purgeable_nonvolatile_compress", "physmem", "bytes");
        task_ledgers.purgeable_volatile = ledger_entry_add(t, "purgeable_volatile", "physmem", "bytes");
        task_ledgers.purgeable_nonvolatile = ledger_entry_add(t, "purgeable_nonvolatile", "physmem", "bytes");
        task_ledgers.purgeable_volatile_compressed = ledger_entry_add(t, "purgeable_volatile_compress", "physmem", "bytes");
        task_ledgers.purgeable_nonvolatile_compressed = ledger_entry_add(t, "purgeable_nonvolatile_compress", "physmem", "bytes");
-
+#if DEBUG || DEVELOPMENT
+       task_ledgers.pages_grabbed = ledger_entry_add(t, "pages_grabbed", "physmem", "count");
+       task_ledgers.pages_grabbed_kern = ledger_entry_add(t, "pages_grabbed_kern", "physmem", "count");
+       task_ledgers.pages_grabbed_iopl = ledger_entry_add(t, "pages_grabbed_iopl", "physmem", "count");
+       task_ledgers.pages_grabbed_upl = ledger_entry_add(t, "pages_grabbed_upl", "physmem", "count");
+#endif
+       task_ledgers.tagged_nofootprint = ledger_entry_add(t, "tagged_nofootprint", "physmem", "bytes");
+       task_ledgers.tagged_footprint = ledger_entry_add(t, "tagged_footprint", "physmem", "bytes");
+       task_ledgers.tagged_nofootprint_compressed = ledger_entry_add(t, "tagged_nofootprint_compressed", "physmem", "bytes");
+       task_ledgers.tagged_footprint_compressed = ledger_entry_add(t, "tagged_footprint_compressed", "physmem", "bytes");
        task_ledgers.network_volatile = ledger_entry_add(t, "network_volatile", "physmem", "bytes");
        task_ledgers.network_nonvolatile = ledger_entry_add(t, "network_nonvolatile", "physmem", "bytes");
        task_ledgers.network_volatile_compressed = ledger_entry_add(t, "network_volatile_compressed", "physmem", "bytes");
        task_ledgers.network_nonvolatile_compressed = ledger_entry_add(t, "network_nonvolatile_compressed", "physmem", "bytes");
        task_ledgers.network_volatile = ledger_entry_add(t, "network_volatile", "physmem", "bytes");
        task_ledgers.network_nonvolatile = ledger_entry_add(t, "network_nonvolatile", "physmem", "bytes");
        task_ledgers.network_volatile_compressed = ledger_entry_add(t, "network_volatile_compressed", "physmem", "bytes");
        task_ledgers.network_nonvolatile_compressed = ledger_entry_add(t, "network_nonvolatile_compressed", "physmem", "bytes");
+       task_ledgers.media_nofootprint = ledger_entry_add(t, "media_nofootprint", "physmem", "bytes");
+       task_ledgers.media_footprint = ledger_entry_add(t, "media_footprint", "physmem", "bytes");
+       task_ledgers.media_nofootprint_compressed = ledger_entry_add(t, "media_nofootprint_compressed", "physmem", "bytes");
+       task_ledgers.media_footprint_compressed = ledger_entry_add(t, "media_footprint_compressed", "physmem", "bytes");
+       task_ledgers.graphics_nofootprint = ledger_entry_add(t, "graphics_nofootprint", "physmem", "bytes");
+       task_ledgers.graphics_footprint = ledger_entry_add(t, "graphics_footprint", "physmem", "bytes");
+       task_ledgers.graphics_nofootprint_compressed = ledger_entry_add(t, "graphics_nofootprint_compressed", "physmem", "bytes");
+       task_ledgers.graphics_footprint_compressed = ledger_entry_add(t, "graphics_footprint_compressed", "physmem", "bytes");
+       task_ledgers.neural_nofootprint = ledger_entry_add(t, "neural_nofootprint", "physmem", "bytes");
+       task_ledgers.neural_footprint = ledger_entry_add(t, "neural_footprint", "physmem", "bytes");
+       task_ledgers.neural_nofootprint_compressed = ledger_entry_add(t, "neural_nofootprint_compressed", "physmem", "bytes");
+       task_ledgers.neural_footprint_compressed = ledger_entry_add(t, "neural_footprint_compressed", "physmem", "bytes");
+
+#if CONFIG_FREEZE
+       task_ledgers.frozen_to_swap = ledger_entry_add(t, "frozen_to_swap", "physmem", "bytes");
+#endif /* CONFIG_FREEZE */
 
        task_ledgers.platform_idle_wakeups = ledger_entry_add(t, "platform_idle_wakeups", "power",
 
        task_ledgers.platform_idle_wakeups = ledger_entry_add(t, "platform_idle_wakeups", "power",
-           "count");
+           "count");
        task_ledgers.interrupt_wakeups = ledger_entry_add(t, "interrupt_wakeups", "power",
        task_ledgers.interrupt_wakeups = ledger_entry_add(t, "interrupt_wakeups", "power",
-           "count");
-       
+           "count");
+
 #if CONFIG_SCHED_SFI
        sfi_class_id_t class_id, ledger_alias;
        for (class_id = SFI_CLASS_UNSPECIFIED; class_id < MAX_SFI_CLASS_ID; class_id++) {
 #if CONFIG_SCHED_SFI
        sfi_class_id_t class_id, ledger_alias;
        for (class_id = SFI_CLASS_UNSPECIFIED; class_id < MAX_SFI_CLASS_ID; class_id++) {
@@ -1022,13 +1124,17 @@ init_task_ledgers(void)
                }
        }
 
                }
        }
 
-       assert(task_ledgers.sfi_wait_times[MAX_SFI_CLASS_ID -1] != -1);
+       assert(task_ledgers.sfi_wait_times[MAX_SFI_CLASS_ID - 1] != -1);
 #endif /* CONFIG_SCHED_SFI */
 
        task_ledgers.cpu_time_billed_to_me = ledger_entry_add(t, "cpu_time_billed_to_me", "sched", "ns");
        task_ledgers.cpu_time_billed_to_others = ledger_entry_add(t, "cpu_time_billed_to_others", "sched", "ns");
        task_ledgers.physical_writes = ledger_entry_add(t, "physical_writes", "res", "bytes");
        task_ledgers.logical_writes = ledger_entry_add(t, "logical_writes", "res", "bytes");
 #endif /* CONFIG_SCHED_SFI */
 
        task_ledgers.cpu_time_billed_to_me = ledger_entry_add(t, "cpu_time_billed_to_me", "sched", "ns");
        task_ledgers.cpu_time_billed_to_others = ledger_entry_add(t, "cpu_time_billed_to_others", "sched", "ns");
        task_ledgers.physical_writes = ledger_entry_add(t, "physical_writes", "res", "bytes");
        task_ledgers.logical_writes = ledger_entry_add(t, "logical_writes", "res", "bytes");
+       task_ledgers.logical_writes_to_external = ledger_entry_add(t, "logical_writes_to_external", "res", "bytes");
+#if CONFIG_PHYS_WRITE_ACCT
+       task_ledgers.fs_metadata_writes = ledger_entry_add(t, "fs_metadata_writes", "res", "bytes");
+#endif /* CONFIG_PHYS_WRITE_ACCT */
        task_ledgers.energy_billed_to_me = ledger_entry_add(t, "energy_billed_to_me", "power", "nj");
        task_ledgers.energy_billed_to_others = ledger_entry_add(t, "energy_billed_to_others", "power", "nj");
 
        task_ledgers.energy_billed_to_me = ledger_entry_add(t, "energy_billed_to_me", "power", "nj");
        task_ledgers.energy_billed_to_others = ledger_entry_add(t, "energy_billed_to_others", "power", "nj");
 
@@ -1048,15 +1154,38 @@ init_task_ledgers(void)
            (task_ledgers.purgeable_nonvolatile < 0) ||
            (task_ledgers.purgeable_volatile_compressed < 0) ||
            (task_ledgers.purgeable_nonvolatile_compressed < 0) ||
            (task_ledgers.purgeable_nonvolatile < 0) ||
            (task_ledgers.purgeable_volatile_compressed < 0) ||
            (task_ledgers.purgeable_nonvolatile_compressed < 0) ||
+           (task_ledgers.tagged_nofootprint < 0) ||
+           (task_ledgers.tagged_footprint < 0) ||
+           (task_ledgers.tagged_nofootprint_compressed < 0) ||
+           (task_ledgers.tagged_footprint_compressed < 0) ||
+#if CONFIG_FREEZE
+           (task_ledgers.frozen_to_swap < 0) ||
+#endif /* CONFIG_FREEZE */
            (task_ledgers.network_volatile < 0) ||
            (task_ledgers.network_nonvolatile < 0) ||
            (task_ledgers.network_volatile_compressed < 0) ||
            (task_ledgers.network_nonvolatile_compressed < 0) ||
            (task_ledgers.network_volatile < 0) ||
            (task_ledgers.network_nonvolatile < 0) ||
            (task_ledgers.network_volatile_compressed < 0) ||
            (task_ledgers.network_nonvolatile_compressed < 0) ||
+           (task_ledgers.media_nofootprint < 0) ||
+           (task_ledgers.media_footprint < 0) ||
+           (task_ledgers.media_nofootprint_compressed < 0) ||
+           (task_ledgers.media_footprint_compressed < 0) ||
+           (task_ledgers.graphics_nofootprint < 0) ||
+           (task_ledgers.graphics_footprint < 0) ||
+           (task_ledgers.graphics_nofootprint_compressed < 0) ||
+           (task_ledgers.graphics_footprint_compressed < 0) ||
+           (task_ledgers.neural_nofootprint < 0) ||
+           (task_ledgers.neural_footprint < 0) ||
+           (task_ledgers.neural_nofootprint_compressed < 0) ||
+           (task_ledgers.neural_footprint_compressed < 0) ||
            (task_ledgers.platform_idle_wakeups < 0) ||
            (task_ledgers.interrupt_wakeups < 0) ||
            (task_ledgers.cpu_time_billed_to_me < 0) || (task_ledgers.cpu_time_billed_to_others < 0) ||
            (task_ledgers.physical_writes < 0) ||
            (task_ledgers.logical_writes < 0) ||
            (task_ledgers.platform_idle_wakeups < 0) ||
            (task_ledgers.interrupt_wakeups < 0) ||
            (task_ledgers.cpu_time_billed_to_me < 0) || (task_ledgers.cpu_time_billed_to_others < 0) ||
            (task_ledgers.physical_writes < 0) ||
            (task_ledgers.logical_writes < 0) ||
+           (task_ledgers.logical_writes_to_external < 0) ||
+#if CONFIG_PHYS_WRITE_ACCT
+           (task_ledgers.fs_metadata_writes < 0) ||
+#endif /* CONFIG_PHYS_WRITE_ACCT */
            (task_ledgers.energy_billed_to_me < 0) ||
            (task_ledgers.energy_billed_to_others < 0)
            ) {
            (task_ledgers.energy_billed_to_me < 0) ||
            (task_ledgers.energy_billed_to_others < 0)
            ) {
@@ -1074,11 +1203,33 @@ init_task_ledgers(void)
        ledger_track_credit_only(t, task_ledgers.purgeable_nonvolatile);
        ledger_track_credit_only(t, task_ledgers.purgeable_volatile_compressed);
        ledger_track_credit_only(t, task_ledgers.purgeable_nonvolatile_compressed);
        ledger_track_credit_only(t, task_ledgers.purgeable_nonvolatile);
        ledger_track_credit_only(t, task_ledgers.purgeable_volatile_compressed);
        ledger_track_credit_only(t, task_ledgers.purgeable_nonvolatile_compressed);
+#if DEBUG || DEVELOPMENT
+       ledger_track_credit_only(t, task_ledgers.pages_grabbed);
+       ledger_track_credit_only(t, task_ledgers.pages_grabbed_kern);
+       ledger_track_credit_only(t, task_ledgers.pages_grabbed_iopl);
+       ledger_track_credit_only(t, task_ledgers.pages_grabbed_upl);
+#endif
 
 
+       ledger_track_credit_only(t, task_ledgers.tagged_nofootprint);
+       ledger_track_credit_only(t, task_ledgers.tagged_footprint);
+       ledger_track_credit_only(t, task_ledgers.tagged_nofootprint_compressed);
+       ledger_track_credit_only(t, task_ledgers.tagged_footprint_compressed);
        ledger_track_credit_only(t, task_ledgers.network_volatile);
        ledger_track_credit_only(t, task_ledgers.network_nonvolatile);
        ledger_track_credit_only(t, task_ledgers.network_volatile_compressed);
        ledger_track_credit_only(t, task_ledgers.network_nonvolatile_compressed);
        ledger_track_credit_only(t, task_ledgers.network_volatile);
        ledger_track_credit_only(t, task_ledgers.network_nonvolatile);
        ledger_track_credit_only(t, task_ledgers.network_volatile_compressed);
        ledger_track_credit_only(t, task_ledgers.network_nonvolatile_compressed);
+       ledger_track_credit_only(t, task_ledgers.media_nofootprint);
+       ledger_track_credit_only(t, task_ledgers.media_footprint);
+       ledger_track_credit_only(t, task_ledgers.media_nofootprint_compressed);
+       ledger_track_credit_only(t, task_ledgers.media_footprint_compressed);
+       ledger_track_credit_only(t, task_ledgers.graphics_nofootprint);
+       ledger_track_credit_only(t, task_ledgers.graphics_footprint);
+       ledger_track_credit_only(t, task_ledgers.graphics_nofootprint_compressed);
+       ledger_track_credit_only(t, task_ledgers.graphics_footprint_compressed);
+       ledger_track_credit_only(t, task_ledgers.neural_nofootprint);
+       ledger_track_credit_only(t, task_ledgers.neural_footprint);
+       ledger_track_credit_only(t, task_ledgers.neural_nofootprint_compressed);
+       ledger_track_credit_only(t, task_ledgers.neural_footprint_compressed);
 
        ledger_track_maximum(t, task_ledgers.phys_footprint, 60);
 #if MACH_ASSERT
 
        ledger_track_maximum(t, task_ledgers.phys_footprint, 60);
 #if MACH_ASSERT
@@ -1094,11 +1245,30 @@ init_task_ledgers(void)
                ledger_panic_on_negative(t, task_ledgers.purgeable_nonvolatile);
                ledger_panic_on_negative(t, task_ledgers.purgeable_volatile_compressed);
                ledger_panic_on_negative(t, task_ledgers.purgeable_nonvolatile_compressed);
                ledger_panic_on_negative(t, task_ledgers.purgeable_nonvolatile);
                ledger_panic_on_negative(t, task_ledgers.purgeable_volatile_compressed);
                ledger_panic_on_negative(t, task_ledgers.purgeable_nonvolatile_compressed);
-
+#if CONFIG_PHYS_WRITE_ACCT
+               ledger_panic_on_negative(t, task_ledgers.fs_metadata_writes);
+#endif /* CONFIG_PHYS_WRITE_ACCT */
+
+               ledger_panic_on_negative(t, task_ledgers.tagged_nofootprint);
+               ledger_panic_on_negative(t, task_ledgers.tagged_footprint);
+               ledger_panic_on_negative(t, task_ledgers.tagged_nofootprint_compressed);
+               ledger_panic_on_negative(t, task_ledgers.tagged_footprint_compressed);
                ledger_panic_on_negative(t, task_ledgers.network_volatile);
                ledger_panic_on_negative(t, task_ledgers.network_nonvolatile);
                ledger_panic_on_negative(t, task_ledgers.network_volatile_compressed);
                ledger_panic_on_negative(t, task_ledgers.network_nonvolatile_compressed);
                ledger_panic_on_negative(t, task_ledgers.network_volatile);
                ledger_panic_on_negative(t, task_ledgers.network_nonvolatile);
                ledger_panic_on_negative(t, task_ledgers.network_volatile_compressed);
                ledger_panic_on_negative(t, task_ledgers.network_nonvolatile_compressed);
+               ledger_panic_on_negative(t, task_ledgers.media_nofootprint);
+               ledger_panic_on_negative(t, task_ledgers.media_footprint);
+               ledger_panic_on_negative(t, task_ledgers.media_nofootprint_compressed);
+               ledger_panic_on_negative(t, task_ledgers.media_footprint_compressed);
+               ledger_panic_on_negative(t, task_ledgers.graphics_nofootprint);
+               ledger_panic_on_negative(t, task_ledgers.graphics_footprint);
+               ledger_panic_on_negative(t, task_ledgers.graphics_nofootprint_compressed);
+               ledger_panic_on_negative(t, task_ledgers.graphics_footprint_compressed);
+               ledger_panic_on_negative(t, task_ledgers.neural_nofootprint);
+               ledger_panic_on_negative(t, task_ledgers.neural_footprint);
+               ledger_panic_on_negative(t, task_ledgers.neural_nofootprint_compressed);
+               ledger_panic_on_negative(t, task_ledgers.neural_footprint_compressed);
        }
 #endif /* MACH_ASSERT */
 
        }
 #endif /* MACH_ASSERT */
 
@@ -1107,45 +1277,60 @@ init_task_ledgers(void)
 #endif /* CONFIG_MEMORYSTATUS */
 
        ledger_set_callback(t, task_ledgers.interrupt_wakeups,
 #endif /* CONFIG_MEMORYSTATUS */
 
        ledger_set_callback(t, task_ledgers.interrupt_wakeups,
-               task_wakeups_rate_exceeded, NULL, NULL);
+           task_wakeups_rate_exceeded, NULL, NULL);
        ledger_set_callback(t, task_ledgers.physical_writes, task_io_rate_exceeded, (void *)FLAVOR_IO_PHYSICAL_WRITES, NULL);
        ledger_set_callback(t, task_ledgers.physical_writes, task_io_rate_exceeded, (void *)FLAVOR_IO_PHYSICAL_WRITES, NULL);
-       ledger_set_callback(t, task_ledgers.logical_writes, task_io_rate_exceeded, (void *)FLAVOR_IO_LOGICAL_WRITES, NULL);
 
 
+#if XNU_MONITOR
+       ledger_template_complete_secure_alloc(t);
+#else /* XNU_MONITOR */
        ledger_template_complete(t);
        ledger_template_complete(t);
+#endif /* XNU_MONITOR */
        task_ledger_template = t;
 }
 
        task_ledger_template = t;
 }
 
+os_refgrp_decl(static, task_refgrp, "task", NULL);
+
 kern_return_t
 task_create_internal(
 kern_return_t
 task_create_internal(
-       task_t          parent_task,
-       coalition_t     *parent_coalitions __unused,
-       boolean_t       inherit_memory,
-       __unused boolean_t      is_64bit,
+       task_t          parent_task,
+       coalition_t     *parent_coalitions __unused,
+       boolean_t       inherit_memory,
+       __unused boolean_t      is_64bit,
        boolean_t is_64bit_data,
        boolean_t is_64bit_data,
-       uint32_t        t_flags,
-       uint32_t        t_procflags,
-       task_t          *child_task)            /* OUT */
+       uint32_t        t_flags,
+       uint32_t        t_procflags,
+       uint8_t         t_returnwaitflags,
+       task_t          *child_task)            /* OUT */
 {
 {
-       task_t                  new_task;
-       vm_shared_region_t      shared_region;
-       ledger_t                ledger = NULL;
+       task_t                  new_task;
+       vm_shared_region_t      shared_region;
+       ledger_t                ledger = NULL;
 
        new_task = (task_t) zalloc(task_zone);
 
 
        new_task = (task_t) zalloc(task_zone);
 
-       if (new_task == TASK_NULL)
-               return(KERN_RESOURCE_SHORTAGE);
+       if (new_task == TASK_NULL) {
+               return KERN_RESOURCE_SHORTAGE;
+       }
 
        /* one ref for just being alive; one for our caller */
 
        /* one ref for just being alive; one for our caller */
-       new_task->ref_count = 2;
+       os_ref_init_count(&new_task->ref_count, &task_refgrp, 2);
 
        /* allocate with active entries */
        assert(task_ledger_template != NULL);
        if ((ledger = ledger_instantiate(task_ledger_template,
 
        /* allocate with active entries */
        assert(task_ledger_template != NULL);
        if ((ledger = ledger_instantiate(task_ledger_template,
-                       LEDGER_CREATE_ACTIVE_ENTRIES)) == NULL) {
+           LEDGER_CREATE_ACTIVE_ENTRIES)) == NULL) {
                zfree(task_zone, new_task);
                zfree(task_zone, new_task);
-               return(KERN_RESOURCE_SHORTAGE);
+               return KERN_RESOURCE_SHORTAGE;
        }
 
        }
 
+       counter_alloc(&(new_task->faults));
+
+#if defined(HAS_APPLE_PAC)
+       ml_task_set_rop_pid(new_task, parent_task, inherit_memory);
+       ml_task_set_jop_pid(new_task, parent_task, inherit_memory);
+       ml_task_set_disable_user_jop(new_task, inherit_memory ? parent_task->disable_user_jop : FALSE);
+#endif
+
 
        new_task->ledger = ledger;
 
 
        new_task->ledger = ledger;
 
@@ -1154,16 +1339,19 @@ task_create_internal(
 #endif
 
        /* if inherit_memory is true, parent_task MUST not be NULL */
 #endif
 
        /* if inherit_memory is true, parent_task MUST not be NULL */
-       if (!(t_flags & TF_CORPSE_FORK) && inherit_memory)
+       if (!(t_flags & TF_CORPSE_FORK) && inherit_memory) {
                new_task->map = vm_map_fork(ledger, parent_task->map, 0);
                new_task->map = vm_map_fork(ledger, parent_task->map, 0);
-       else
-               new_task->map = vm_map_create(pmap_create(ledger, 0, is_64bit),
-                               (vm_map_offset_t)(VM_MIN_ADDRESS),
-                               (vm_map_offset_t)(VM_MAX_ADDRESS), TRUE);
+       } else {
+               unsigned int pmap_flags = is_64bit ? PMAP_CREATE_64BIT : 0;
+               new_task->map = vm_map_create(pmap_create_options(ledger, 0, pmap_flags),
+                   (vm_map_offset_t)(VM_MIN_ADDRESS),
+                   (vm_map_offset_t)(VM_MAX_ADDRESS), TRUE);
+       }
 
        /* Inherit memlock limit from parent */
 
        /* Inherit memlock limit from parent */
-       if (parent_task)
+       if (parent_task) {
                vm_map_set_user_wire_limit(new_task->map, (vm_size_t)parent_task->map->user_wire_limit);
                vm_map_set_user_wire_limit(new_task->map, (vm_size_t)parent_task->map->user_wire_limit);
+       }
 
        lck_mtx_init(&new_task->lock, &task_lck_grp, &task_lck_attr);
        queue_init(&new_task->threads);
 
        lck_mtx_init(&new_task->lock, &task_lck_grp, &task_lck_attr);
        queue_init(&new_task->threads);
@@ -1177,15 +1365,15 @@ task_create_internal(
        new_task->priv_flags = 0;
        new_task->t_flags = t_flags;
        new_task->t_procflags = t_procflags;
        new_task->priv_flags = 0;
        new_task->t_flags = t_flags;
        new_task->t_procflags = t_procflags;
+       new_task->t_returnwaitflags = t_returnwaitflags;
+       new_task->returnwait_inheritor = current_thread();
        new_task->importance = 0;
        new_task->crashed_thread_id = 0;
        new_task->exec_token = 0;
        new_task->importance = 0;
        new_task->crashed_thread_id = 0;
        new_task->exec_token = 0;
+       new_task->watchports = NULL;
+       new_task->restartable_ranges = NULL;
+       new_task->task_exc_guard = 0;
 
 
-       new_task->task_exc_guard = task_exc_guard_default;
-       
-#if CONFIG_ATM
-       new_task->atm_context = NULL;
-#endif
        new_task->bank_context = NULL;
 
 #ifdef MACH_BSD
        new_task->bank_context = NULL;
 
 #ifdef MACH_BSD
@@ -1195,6 +1383,9 @@ task_create_internal(
 
 #if CONFIG_MACF
        new_task->crash_label = NULL;
 
 #if CONFIG_MACF
        new_task->crash_label = NULL;
+
+       new_task->mach_trap_filter_mask = NULL;
+       new_task->mach_kobj_filter_mask = NULL;
 #endif
 
 #if CONFIG_MEMORYSTATUS
 #endif
 
 #if CONFIG_MEMORYSTATUS
@@ -1251,11 +1442,11 @@ task_create_internal(
        new_task->hv_task_target = NULL;
 #endif /* HYPERVISOR */
 
        new_task->hv_task_target = NULL;
 #endif /* HYPERVISOR */
 
-#if CONFIG_EMBEDDED
+#if CONFIG_TASKWATCH
        queue_init(&new_task->task_watchers);
        new_task->num_taskwatchers  = 0;
        new_task->watchapplying  = 0;
        queue_init(&new_task->task_watchers);
        new_task->num_taskwatchers  = 0;
        new_task->watchapplying  = 0;
-#endif /* CONFIG_EMBEDDED */
+#endif /* CONFIG_TASKWATCH */
 
        new_task->mem_notify_reserved = 0;
        new_task->memlimit_attrs_reserved = 0;
 
        new_task->mem_notify_reserved = 0;
        new_task->memlimit_attrs_reserved = 0;
@@ -1263,6 +1454,8 @@ task_create_internal(
        new_task->requested_policy = default_task_requested_policy;
        new_task->effective_policy = default_task_effective_policy;
 
        new_task->requested_policy = default_task_requested_policy;
        new_task->effective_policy = default_task_effective_policy;
 
+       new_task->task_shared_region_slide = -1;
+
        task_importance_init_from_parent(new_task, parent_task);
 
        if (parent_task != TASK_NULL) {
        task_importance_init_from_parent(new_task, parent_task);
 
        if (parent_task != TASK_NULL) {
@@ -1273,22 +1466,45 @@ task_create_internal(
                shared_region = vm_shared_region_get(parent_task);
                vm_shared_region_set(new_task, shared_region);
 
                shared_region = vm_shared_region_get(parent_task);
                vm_shared_region_set(new_task, shared_region);
 
-               if(task_has_64Bit_addr(parent_task)) {
+#if __has_feature(ptrauth_calls)
+               /* use parent's shared_region_id */
+               char *shared_region_id = task_get_vm_shared_region_id_and_jop_pid(parent_task, NULL);
+               if (shared_region_id != NULL) {
+                       shared_region_key_alloc(shared_region_id, FALSE, 0);   /* get a reference */
+               }
+               task_set_shared_region_id(new_task, shared_region_id);
+#endif /* __has_feature(ptrauth_calls) */
+
+               if (task_has_64Bit_addr(parent_task)) {
                        task_set_64Bit_addr(new_task);
                }
 
                        task_set_64Bit_addr(new_task);
                }
 
-               if(task_has_64Bit_data(parent_task)) {
+               if (task_has_64Bit_data(parent_task)) {
                        task_set_64Bit_data(new_task);
                }
 
                new_task->all_image_info_addr = parent_task->all_image_info_addr;
                new_task->all_image_info_size = parent_task->all_image_info_size;
                        task_set_64Bit_data(new_task);
                }
 
                new_task->all_image_info_addr = parent_task->all_image_info_addr;
                new_task->all_image_info_size = parent_task->all_image_info_size;
+               new_task->mach_header_vm_address = 0;
 
 
-               if (inherit_memory && parent_task->affinity_space)
+               if (inherit_memory && parent_task->affinity_space) {
                        task_affinity_create(parent_task, new_task);
                        task_affinity_create(parent_task, new_task);
+               }
 
                new_task->pset_hint = parent_task->pset_hint = task_choose_pset(parent_task);
 
 
                new_task->pset_hint = parent_task->pset_hint = task_choose_pset(parent_task);
 
+               if (parent_task->t_flags & TF_NO_SMT) {
+                       new_task->t_flags |= TF_NO_SMT;
+               }
+
+               if (parent_task->t_flags & TF_TECS) {
+                       new_task->t_flags |= TF_TECS;
+               }
+
+               if (parent_task->t_flags & TF_FILTER_MSG) {
+                       new_task->t_flags |= TF_FILTER_MSG;
+               }
+
                new_task->priority = BASEPRI_DEFAULT;
                new_task->max_priority = MAXPRI_USER;
 
                new_task->priority = BASEPRI_DEFAULT;
                new_task->max_priority = MAXPRI_USER;
 
@@ -1297,12 +1513,12 @@ task_create_internal(
                new_task->sec_token = KERNEL_SECURITY_TOKEN;
                new_task->audit_token = KERNEL_AUDIT_TOKEN;
 #ifdef __LP64__
                new_task->sec_token = KERNEL_SECURITY_TOKEN;
                new_task->audit_token = KERNEL_AUDIT_TOKEN;
 #ifdef __LP64__
-               if(is_64bit) {
+               if (is_64bit) {
                        task_set_64Bit_addr(new_task);
                }
 #endif
 
                        task_set_64Bit_addr(new_task);
                }
 #endif
 
-               if(is_64bit_data) {
+               if (is_64bit_data) {
                        task_set_64Bit_data(new_task);
                }
 
                        task_set_64Bit_data(new_task);
                }
 
@@ -1321,13 +1537,14 @@ task_create_internal(
        }
 
        bzero(new_task->coalition, sizeof(new_task->coalition));
        }
 
        bzero(new_task->coalition, sizeof(new_task->coalition));
-       for (int i = 0; i < COALITION_NUM_TYPES; i++)
+       for (int i = 0; i < COALITION_NUM_TYPES; i++) {
                queue_chain_init(new_task->task_coalition[i]);
                queue_chain_init(new_task->task_coalition[i]);
+       }
 
        /* Allocate I/O Statistics */
 
        /* Allocate I/O Statistics */
-       new_task->task_io_stats = (io_stat_info_t)kalloc(sizeof(struct io_stat_info));
+       new_task->task_io_stats = kheap_alloc(KHEAP_DATA_BUFFERS,
+           sizeof(struct io_stat_info), Z_WAITOK | Z_ZERO);
        assert(new_task->task_io_stats != NULL);
        assert(new_task->task_io_stats != NULL);
-       bzero(new_task->task_io_stats, sizeof(struct io_stat_info));
 
        bzero(&(new_task->cpu_time_eqos_stats), sizeof(new_task->cpu_time_eqos_stats));
        bzero(&(new_task->cpu_time_rqos_stats), sizeof(new_task->cpu_time_rqos_stats));
 
        bzero(&(new_task->cpu_time_eqos_stats), sizeof(new_task->cpu_time_eqos_stats));
        bzero(&(new_task->cpu_time_rqos_stats), sizeof(new_task->cpu_time_rqos_stats));
@@ -1343,7 +1560,6 @@ task_create_internal(
                new_task->total_system_time = 0;
                new_task->total_ptime = 0;
                new_task->total_runnable_time = 0;
                new_task->total_system_time = 0;
                new_task->total_ptime = 0;
                new_task->total_runnable_time = 0;
-               new_task->faults = 0;
                new_task->pageins = 0;
                new_task->cow_faults = 0;
                new_task->messages_sent = 0;
                new_task->pageins = 0;
                new_task->cow_faults = 0;
                new_task->messages_sent = 0;
@@ -1353,6 +1569,7 @@ task_create_internal(
                new_task->c_switch = 0;
                new_task->p_switch = 0;
                new_task->ps_switch = 0;
                new_task->c_switch = 0;
                new_task->p_switch = 0;
                new_task->ps_switch = 0;
+               new_task->decompressions = 0;
                new_task->low_mem_notified_warn = 0;
                new_task->low_mem_notified_critical = 0;
                new_task->purged_memory_warn = 0;
                new_task->low_mem_notified_warn = 0;
                new_task->low_mem_notified_critical = 0;
                new_task->purged_memory_warn = 0;
@@ -1365,10 +1582,18 @@ task_create_internal(
                new_task->task_timer_wakeups_bin_1 = 0;
                new_task->task_timer_wakeups_bin_2 = 0;
                new_task->task_gpu_ns = 0;
                new_task->task_timer_wakeups_bin_1 = 0;
                new_task->task_timer_wakeups_bin_2 = 0;
                new_task->task_gpu_ns = 0;
-               new_task->task_immediate_writes = 0;
-               new_task->task_deferred_writes = 0;
-               new_task->task_invalidated_writes = 0;
-               new_task->task_metadata_writes = 0;
+               new_task->task_writes_counters_internal.task_immediate_writes = 0;
+               new_task->task_writes_counters_internal.task_deferred_writes = 0;
+               new_task->task_writes_counters_internal.task_invalidated_writes = 0;
+               new_task->task_writes_counters_internal.task_metadata_writes = 0;
+               new_task->task_writes_counters_external.task_immediate_writes = 0;
+               new_task->task_writes_counters_external.task_deferred_writes = 0;
+               new_task->task_writes_counters_external.task_invalidated_writes = 0;
+               new_task->task_writes_counters_external.task_metadata_writes = 0;
+#if CONFIG_PHYS_WRITE_ACCT
+               new_task->task_fs_metadata_writes = 0;
+#endif /* CONFIG_PHYS_WRITE_ACCT */
+
                new_task->task_energy = 0;
 #if MONOTONIC
                memset(&new_task->task_monotonic, 0, sizeof(new_task->task_monotonic));
                new_task->task_energy = 0;
 #if MONOTONIC
                memset(&new_task->task_monotonic, 0, sizeof(new_task->task_monotonic));
@@ -1415,15 +1640,24 @@ task_create_internal(
                new_task->dispatchqueue_offset = parent_task->dispatchqueue_offset;
        }
 
                new_task->dispatchqueue_offset = parent_task->dispatchqueue_offset;
        }
 
+       new_task->task_can_transfer_memory_ownership = FALSE;
        new_task->task_volatile_objects = 0;
        new_task->task_nonvolatile_objects = 0;
        new_task->task_volatile_objects = 0;
        new_task->task_nonvolatile_objects = 0;
-       new_task->task_purgeable_disowning = FALSE;
-       new_task->task_purgeable_disowned = FALSE;
+       new_task->task_objects_disowning = FALSE;
+       new_task->task_objects_disowned = FALSE;
+       new_task->task_owned_objects = 0;
        queue_init(&new_task->task_objq);
        queue_init(&new_task->task_objq);
+
+#if CONFIG_FREEZE
+       queue_init(&new_task->task_frozen_cseg_q);
+#endif /* CONFIG_FREEZE */
+
        task_objq_lock_init(new_task);
 
 #if __arm64__
        new_task->task_legacy_footprint = FALSE;
        task_objq_lock_init(new_task);
 
 #if __arm64__
        new_task->task_legacy_footprint = FALSE;
+       new_task->task_extra_footprint_limit = FALSE;
+       new_task->task_ios13extended_footprint_limit = FALSE;
 #endif /* __arm64__ */
        new_task->task_region_footprint = FALSE;
        new_task->task_has_crossed_thread_limit = FALSE;
 #endif /* __arm64__ */
        new_task->task_region_footprint = FALSE;
        new_task->task_has_crossed_thread_limit = FALSE;
@@ -1442,20 +1676,21 @@ task_create_internal(
         */
        new_task->t_flags &= ~(TF_DARKWAKE_MODE);
 
         */
        new_task->t_flags &= ~(TF_DARKWAKE_MODE);
 
-        queue_init(&new_task->io_user_clients);
+       queue_init(&new_task->io_user_clients);
+       new_task->loadTag = 0;
 
        ipc_task_enable(new_task);
 
        lck_mtx_lock(&tasks_threads_lock);
        queue_enter(&tasks, new_task, task_t, tasks);
        tasks_count++;
 
        ipc_task_enable(new_task);
 
        lck_mtx_lock(&tasks_threads_lock);
        queue_enter(&tasks, new_task, task_t, tasks);
        tasks_count++;
-        if (tasks_suspend_state) {
-            task_suspend_internal(new_task);
-        }
+       if (tasks_suspend_state) {
+               task_suspend_internal(new_task);
+       }
        lck_mtx_unlock(&tasks_threads_lock);
 
        *child_task = new_task;
        lck_mtx_unlock(&tasks_threads_lock);
 
        *child_task = new_task;
-       return(KERN_SUCCESS);
+       return KERN_SUCCESS;
 }
 
 /*
 }
 
 /*
@@ -1473,9 +1708,10 @@ task_rollup_accounting_info(task_t to_task, task_t from_task)
        to_task->total_system_time = from_task->total_system_time;
        to_task->total_ptime = from_task->total_ptime;
        to_task->total_runnable_time = from_task->total_runnable_time;
        to_task->total_system_time = from_task->total_system_time;
        to_task->total_ptime = from_task->total_ptime;
        to_task->total_runnable_time = from_task->total_runnable_time;
-       to_task->faults = from_task->faults;
+       counter_add(&to_task->faults, counter_load(&from_task->faults));
        to_task->pageins = from_task->pageins;
        to_task->cow_faults = from_task->cow_faults;
        to_task->pageins = from_task->pageins;
        to_task->cow_faults = from_task->cow_faults;
+       to_task->decompressions = from_task->decompressions;
        to_task->messages_sent = from_task->messages_sent;
        to_task->messages_received = from_task->messages_received;
        to_task->syscalls_mach = from_task->syscalls_mach;
        to_task->messages_sent = from_task->messages_sent;
        to_task->messages_received = from_task->messages_received;
        to_task->syscalls_mach = from_task->syscalls_mach;
@@ -1495,10 +1731,17 @@ task_rollup_accounting_info(task_t to_task, task_t from_task)
        to_task->task_timer_wakeups_bin_1 = from_task->task_timer_wakeups_bin_1;
        to_task->task_timer_wakeups_bin_2 = from_task->task_timer_wakeups_bin_2;
        to_task->task_gpu_ns = from_task->task_gpu_ns;
        to_task->task_timer_wakeups_bin_1 = from_task->task_timer_wakeups_bin_1;
        to_task->task_timer_wakeups_bin_2 = from_task->task_timer_wakeups_bin_2;
        to_task->task_gpu_ns = from_task->task_gpu_ns;
-       to_task->task_immediate_writes = from_task->task_immediate_writes;
-       to_task->task_deferred_writes = from_task->task_deferred_writes;
-       to_task->task_invalidated_writes = from_task->task_invalidated_writes;
-       to_task->task_metadata_writes = from_task->task_metadata_writes;
+       to_task->task_writes_counters_internal.task_immediate_writes = from_task->task_writes_counters_internal.task_immediate_writes;
+       to_task->task_writes_counters_internal.task_deferred_writes = from_task->task_writes_counters_internal.task_deferred_writes;
+       to_task->task_writes_counters_internal.task_invalidated_writes = from_task->task_writes_counters_internal.task_invalidated_writes;
+       to_task->task_writes_counters_internal.task_metadata_writes = from_task->task_writes_counters_internal.task_metadata_writes;
+       to_task->task_writes_counters_external.task_immediate_writes = from_task->task_writes_counters_external.task_immediate_writes;
+       to_task->task_writes_counters_external.task_deferred_writes = from_task->task_writes_counters_external.task_deferred_writes;
+       to_task->task_writes_counters_external.task_invalidated_writes = from_task->task_writes_counters_external.task_invalidated_writes;
+       to_task->task_writes_counters_external.task_metadata_writes = from_task->task_writes_counters_external.task_metadata_writes;
+#if CONFIG_PHYS_WRITE_ACCT
+       to_task->task_fs_metadata_writes = from_task->task_fs_metadata_writes;
+#endif /* CONFIG_PHYS_WRITE_ACCT */
        to_task->task_energy = from_task->task_energy;
 
        /* Skip ledger roll up for memory accounting entries */
        to_task->task_energy = from_task->task_energy;
 
        /* Skip ledger roll up for memory accounting entries */
@@ -1527,59 +1770,62 @@ int task_dropped_imp_count = 0;
  */
 void
 task_deallocate(
  */
 void
 task_deallocate(
-       task_t          task)
+       task_t          task)
 {
        ledger_amount_t credit, debit, interrupt_wakeups, platform_idle_wakeups;
 {
        ledger_amount_t credit, debit, interrupt_wakeups, platform_idle_wakeups;
-       uint32_t refs;
+       os_ref_count_t refs;
 
 
-       if (task == TASK_NULL)
-           return;
+       if (task == TASK_NULL) {
+               return;
+       }
 
        refs = task_deallocate_internal(task);
 
 #if IMPORTANCE_INHERITANCE
 
        refs = task_deallocate_internal(task);
 
 #if IMPORTANCE_INHERITANCE
-       if (refs > 1)
-               return;
-
-       atomic_load_explicit(&task->ref_count, memory_order_acquire);
-       
        if (refs == 1) {
                /*
                 * If last ref potentially comes from the task's importance,
                 * disconnect it.  But more task refs may be added before
                 * that completes, so wait for the reference to go to zero
        if (refs == 1) {
                /*
                 * If last ref potentially comes from the task's importance,
                 * disconnect it.  But more task refs may be added before
                 * that completes, so wait for the reference to go to zero
-                * naturually (it may happen on a recursive task_deallocate()
+                * naturally (it may happen on a recursive task_deallocate()
                 * from the ipc_importance_disconnect_task() call).
                 */
                 * from the ipc_importance_disconnect_task() call).
                 */
-               if (IIT_NULL != task->task_imp_base)
+               if (IIT_NULL != task->task_imp_base) {
                        ipc_importance_disconnect_task(task);
                        ipc_importance_disconnect_task(task);
+               }
                return;
        }
                return;
        }
-#else
-       if (refs > 0)
-               return;
+#endif /* IMPORTANCE_INHERITANCE */
 
 
-       atomic_load_explicit(&task->ref_count, memory_order_acquire);
+       if (refs > 0) {
+               return;
+       }
 
 
-#endif /* IMPORTANCE_INHERITANCE */
+       /*
+        * The task should be dead at this point. Ensure other resources
+        * like threads, are gone before we trash the world.
+        */
+       assert(queue_empty(&task->threads));
+       assert(task->bsd_info == NULL);
+       assert(!is_active(task->itk_space));
+       assert(!task->active);
+       assert(task->active_thread_count == 0);
 
        lck_mtx_lock(&tasks_threads_lock);
 
        lck_mtx_lock(&tasks_threads_lock);
+       assert(terminated_tasks_count > 0);
        queue_remove(&terminated_tasks, task, task_t, tasks);
        terminated_tasks_count--;
        lck_mtx_unlock(&tasks_threads_lock);
 
        queue_remove(&terminated_tasks, task, task_t, tasks);
        terminated_tasks_count--;
        lck_mtx_unlock(&tasks_threads_lock);
 
-       /*
-        * remove the reference on atm descriptor
-        */
-       task_atm_reset(task);
-
        /*
         * remove the reference on bank context
         */
        task_bank_reset(task);
 
        /*
         * remove the reference on bank context
         */
        task_bank_reset(task);
 
-       if (task->task_io_stats)
-               kfree(task->task_io_stats, sizeof(struct io_stat_info));
+       if (task->task_io_stats) {
+               kheap_free(KHEAP_DATA_BUFFERS, task->task_io_stats,
+                   sizeof(struct io_stat_info));
+       }
 
        /*
         *      Give the machine dependent code a chance
 
        /*
         *      Give the machine dependent code a chance
@@ -1593,8 +1839,9 @@ task_deallocate(
        /* let iokit know */
        iokit_task_terminate(task);
 
        /* let iokit know */
        iokit_task_terminate(task);
 
-       if (task->affinity_space)
+       if (task->affinity_space) {
                task_affinity_deallocate(task);
                task_affinity_deallocate(task);
+       }
 
 #if MACH_ASSERT
        if (task->ledger != NULL &&
 
 #if MACH_ASSERT
        if (task->ledger != NULL &&
@@ -1605,24 +1852,29 @@ task_deallocate(
        }
 #endif /* MACH_ASSERT */
 
        }
 #endif /* MACH_ASSERT */
 
-       vm_purgeable_disown(task);
-       assert(task->task_purgeable_disowned);
+       vm_owned_objects_disown(task);
+       assert(task->task_objects_disowned);
        if (task->task_volatile_objects != 0 ||
        if (task->task_volatile_objects != 0 ||
-           task->task_nonvolatile_objects != 0) {
+           task->task_nonvolatile_objects != 0 ||
+           task->task_owned_objects != 0) {
                panic("task_deallocate(%p): "
                panic("task_deallocate(%p): "
-                     "volatile_objects=%d nonvolatile_objects=%d\n",
-                     task,
-                     task->task_volatile_objects,
-                     task->task_nonvolatile_objects);
+                   "volatile_objects=%d nonvolatile_objects=%d owned=%d\n",
+                   task,
+                   task->task_volatile_objects,
+                   task->task_nonvolatile_objects,
+                   task->task_owned_objects);
        }
 
        vm_map_deallocate(task->map);
        is_release(task->itk_space);
        }
 
        vm_map_deallocate(task->map);
        is_release(task->itk_space);
+       if (task->restartable_ranges) {
+               restartable_ranges_release(task->restartable_ranges);
+       }
 
        ledger_get_entries(task->ledger, task_ledgers.interrupt_wakeups,
 
        ledger_get_entries(task->ledger, task_ledgers.interrupt_wakeups,
-                          &interrupt_wakeups, &debit);
+           &interrupt_wakeups, &debit);
        ledger_get_entries(task->ledger, task_ledgers.platform_idle_wakeups,
        ledger_get_entries(task->ledger, task_ledgers.platform_idle_wakeups,
-                          &platform_idle_wakeups, &debit);
+           &platform_idle_wakeups, &debit);
 
 #if defined(CONFIG_SCHED_MULTIQ)
        sched_group_destroy(task->sched_group);
 
 #if defined(CONFIG_SCHED_MULTIQ)
        sched_group_destroy(task->sched_group);
@@ -1662,6 +1914,8 @@ task_deallocate(
        btlog_remove_entries_for_element(task_ref_btlog, task);
 #endif
 
        btlog_remove_entries_for_element(task_ref_btlog, task);
 #endif
 
+       counter_free(&task->faults);
+
 #if CONFIG_COALITIONS
        task_release_coalitions(task);
 #endif /* CONFIG_COALITIONS */
 #if CONFIG_COALITIONS
        task_release_coalitions(task);
 #endif /* CONFIG_COALITIONS */
@@ -1675,7 +1929,8 @@ task_deallocate(
                task_crashinfo_destroy(task->corpse_info);
                task->corpse_info = NULL;
                if (corpse_info_kernel) {
                task_crashinfo_destroy(task->corpse_info);
                task->corpse_info = NULL;
                if (corpse_info_kernel) {
-                       kfree(corpse_info_kernel, CORPSEINFO_ALLOCATION_SIZE);
+                       kheap_free(KHEAP_DATA_BUFFERS, corpse_info_kernel,
+                           CORPSEINFO_ALLOCATION_SIZE);
                }
        }
 #endif
                }
        }
 #endif
@@ -1688,6 +1943,7 @@ task_deallocate(
 #endif
 
        assert(queue_empty(&task->task_objq));
 #endif
 
        assert(queue_empty(&task->task_objq));
+       task_objq_lock_destroy(task);
 
        zfree(task_zone, task);
 }
 
        zfree(task_zone, task);
 }
@@ -1699,9 +1955,31 @@ task_deallocate(
  */
 void
 task_name_deallocate(
  */
 void
 task_name_deallocate(
-       task_name_t             task_name)
+       task_name_t             task_name)
+{
+       return task_deallocate((task_t)task_name);
+}
+
+/*
+ *     task_policy_set_deallocate:
+ *
+ *     Drop a reference on a task type.
+ */
+void
+task_policy_set_deallocate(task_policy_set_t task_policy_set)
+{
+       return task_deallocate((task_t)task_policy_set);
+}
+
+/*
+ *     task_policy_get_deallocate:
+ *
+ *     Drop a reference on a task type.
+ */
+void
+task_policy_get_deallocate(task_policy_get_t task_policy_get)
 {
 {
-       return(task_deallocate((task_t)task_name));
+       return task_deallocate((task_t)task_policy_get);
 }
 
 /*
 }
 
 /*
@@ -1711,9 +1989,21 @@ task_name_deallocate(
  */
 void
 task_inspect_deallocate(
  */
 void
 task_inspect_deallocate(
-       task_inspect_t          task_inspect)
+       task_inspect_t          task_inspect)
+{
+       return task_deallocate((task_t)task_inspect);
+}
+
+/*
+ *     task_read_deallocate:
+ *
+ *     Drop a reference on task read port.
+ */
+void
+task_read_deallocate(
+       task_read_t          task_read)
 {
 {
-       return(task_deallocate((task_t)task_inspect));
+       return task_deallocate((task_t)task_read);
 }
 
 /*
 }
 
 /*
@@ -1723,9 +2013,9 @@ task_inspect_deallocate(
  */
 void
 task_suspension_token_deallocate(
  */
 void
 task_suspension_token_deallocate(
-       task_suspension_token_t         token)
+       task_suspension_token_t         token)
 {
 {
-       return(task_deallocate((task_t)token));
+       return task_deallocate((task_t)token);
 }
 
 
 }
 
 
@@ -1761,7 +2051,7 @@ task_collect_crash_info(
 #if CONFIG_MACF
        free_label = label = mac_exc_create_label();
 #endif
 #if CONFIG_MACF
        free_label = label = mac_exc_create_label();
 #endif
-       
+
        task_lock(task);
 
        assert(is_corpse_fork || task->bsd_info != NULL);
        task_lock(task);
 
        assert(is_corpse_fork || task->bsd_info != NULL);
@@ -1774,18 +2064,18 @@ task_collect_crash_info(
 #endif
                task_unlock(task);
 
 #endif
                task_unlock(task);
 
-               crash_data_kernel = (void *) kalloc(CORPSEINFO_ALLOCATION_SIZE);
+               crash_data_kernel = kheap_alloc(KHEAP_DATA_BUFFERS,
+                   CORPSEINFO_ALLOCATION_SIZE, Z_WAITOK | Z_ZERO);
                if (crash_data_kernel == NULL) {
                        kr = KERN_RESOURCE_SHORTAGE;
                        goto out_no_lock;
                }
                if (crash_data_kernel == NULL) {
                        kr = KERN_RESOURCE_SHORTAGE;
                        goto out_no_lock;
                }
-               bzero(crash_data_kernel, CORPSEINFO_ALLOCATION_SIZE);
                crash_data_ptr = (mach_vm_offset_t) crash_data_kernel;
 
                /* Do not get a corpse ref for corpse fork */
                crash_data = task_crashinfo_alloc_init((mach_vm_address_t)crash_data_ptr, size,
                crash_data_ptr = (mach_vm_offset_t) crash_data_kernel;
 
                /* Do not get a corpse ref for corpse fork */
                crash_data = task_crashinfo_alloc_init((mach_vm_address_t)crash_data_ptr, size,
-                               is_corpse_fork ? 0 : CORPSE_CRASHINFO_HAS_REF,
-                               KCFLAG_USE_MEMCOPY);
+                   is_corpse_fork ? 0 : CORPSE_CRASHINFO_HAS_REF,
+                   KCFLAG_USE_MEMCOPY);
                if (crash_data) {
                        task_lock(task);
                        crash_data_release = task->corpse_info;
                if (crash_data) {
                        task_lock(task);
                        crash_data_release = task->corpse_info;
@@ -1795,7 +2085,8 @@ task_collect_crash_info(
                        task_unlock(task);
                        kr = KERN_SUCCESS;
                } else {
                        task_unlock(task);
                        kr = KERN_SUCCESS;
                } else {
-                       kfree(crash_data_kernel, CORPSEINFO_ALLOCATION_SIZE);
+                       kheap_free(KHEAP_DATA_BUFFERS, crash_data_kernel,
+                           CORPSEINFO_ALLOCATION_SIZE);
                        kr = KERN_FAILURE;
                }
 
                        kr = KERN_FAILURE;
                }
 
@@ -1803,7 +2094,8 @@ task_collect_crash_info(
                        task_crashinfo_destroy(crash_data_release);
                }
                if (crash_data_kernel_release != NULL) {
                        task_crashinfo_destroy(crash_data_release);
                }
                if (crash_data_kernel_release != NULL) {
-                       kfree(crash_data_kernel_release, CORPSEINFO_ALLOCATION_SIZE);
+                       kheap_free(KHEAP_DATA_BUFFERS, crash_data_kernel_release,
+                           CORPSEINFO_ALLOCATION_SIZE);
                }
        } else {
                task_unlock(task);
                }
        } else {
                task_unlock(task);
@@ -1837,8 +2129,9 @@ task_deliver_crash_notification(
        mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
        ipc_port_t task_port, old_notify;
 
        mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
        ipc_port_t task_port, old_notify;
 
-       if (crash_info == NULL)
+       if (crash_info == NULL) {
                return KERN_FAILURE;
                return KERN_FAILURE;
+       }
 
        task_lock(task);
        if (task_is_a_corpse_fork(task)) {
 
        task_lock(task);
        if (task_is_a_corpse_fork(task)) {
@@ -1867,7 +2160,7 @@ task_deliver_crash_notification(
        task_reference(task);
        task_port = convert_task_to_port(task);
        ip_lock(task_port);
        task_reference(task);
        task_port = convert_task_to_port(task);
        ip_lock(task_port);
-       assert(ip_active(task_port));
+       require_ip_active(task_port);
        ipc_port_nsrequest(task_port, task_port->ip_mscount, ipc_port_make_sonce_locked(task_port), &old_notify);
        /* port unlocked */
        assert(IP_NULL == old_notify);
        ipc_port_nsrequest(task_port, task_port->ip_mscount, ipc_port_make_sonce_locked(task_port), &old_notify);
        /* port unlocked */
        assert(IP_NULL == old_notify);
@@ -1897,15 +2190,17 @@ task_deliver_crash_notification(
 
 kern_return_t
 task_terminate(
 
 kern_return_t
 task_terminate(
-       task_t          task)
+       task_t          task)
 {
 {
-       if (task == TASK_NULL)
-               return (KERN_INVALID_ARGUMENT);
+       if (task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
 
-       if (task->bsd_info)
-               return (KERN_FAILURE);
+       if (task->bsd_info) {
+               return KERN_FAILURE;
+       }
 
 
-       return (task_terminate_internal(task));
+       return task_terminate_internal(task);
 }
 
 #if MACH_ASSERT
 }
 
 #if MACH_ASSERT
@@ -1917,19 +2212,19 @@ extern void proc_name_kdp(task_t t, char *buf, int size);
 static void
 __unused task_partial_reap(task_t task, __unused int pid)
 {
 static void
 __unused task_partial_reap(task_t task, __unused int pid)
 {
-        unsigned int    reclaimed_resident = 0;
-        unsigned int    reclaimed_compressed = 0;
+       unsigned int    reclaimed_resident = 0;
+       unsigned int    reclaimed_compressed = 0;
        uint64_t        task_page_count;
 
        task_page_count = (get_task_phys_footprint(task) / PAGE_SIZE_64);
 
        KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, VM_MAP_PARTIAL_REAP) | DBG_FUNC_START),
        uint64_t        task_page_count;
 
        task_page_count = (get_task_phys_footprint(task) / PAGE_SIZE_64);
 
        KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, VM_MAP_PARTIAL_REAP) | DBG_FUNC_START),
-                              pid, task_page_count, 0, 0, 0);
+           pid, task_page_count, 0, 0, 0);
 
        vm_map_partial_reap(task->map, &reclaimed_resident, &reclaimed_compressed);
 
 
        vm_map_partial_reap(task->map, &reclaimed_resident, &reclaimed_compressed);
 
-        KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, VM_MAP_PARTIAL_REAP) | DBG_FUNC_END),
-                              pid, reclaimed_resident, reclaimed_compressed, 0, 0);
+       KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, VM_MAP_PARTIAL_REAP) | DBG_FUNC_END),
+           pid, reclaimed_resident, reclaimed_compressed, 0, 0);
 }
 
 kern_return_t
 }
 
 kern_return_t
@@ -1950,12 +2245,12 @@ task_mark_corpse(task_t task)
 #if CONFIG_MACF
        crash_label = mac_exc_create_label_for_proc((struct proc*)task->bsd_info);
 #endif
 #if CONFIG_MACF
        crash_label = mac_exc_create_label_for_proc((struct proc*)task->bsd_info);
 #endif
-       
+
        kr = task_collect_crash_info(task,
 #if CONFIG_MACF
        kr = task_collect_crash_info(task,
 #if CONFIG_MACF
-                                                                crash_label,
+           crash_label,
 #endif
 #endif
-                                                                FALSE);
+           FALSE);
        if (kr != KERN_SUCCESS) {
                goto out;
        }
        if (kr != KERN_SUCCESS) {
                goto out;
        }
@@ -1974,7 +2269,7 @@ task_mark_corpse(task_t task)
 
        ipc_task_reset(task);
        /* Remove the naked send right for task port, needed to arm no sender notification */
 
        ipc_task_reset(task);
        /* Remove the naked send right for task port, needed to arm no sender notification */
-       task_set_special_port(task, TASK_KERNEL_PORT, IPC_PORT_NULL);
+       task_set_special_port_internal(task, TASK_KERNEL_PORT, IPC_PORT_NULL);
        ipc_task_enable(task);
 
        task_unlock(task);
        ipc_task_enable(task);
 
        task_unlock(task);
@@ -1983,9 +2278,9 @@ task_mark_corpse(task_t task)
 
        /* Add it to global corpse task list */
        task_add_to_corpse_task_list(task);
 
        /* Add it to global corpse task list */
        task_add_to_corpse_task_list(task);
-       
+
        task_start_halt(task);
        task_start_halt(task);
-       thread_terminate_internal(self_thread);
+       thread_terminate_internal(self_thread, TH_TERMINATE_OPTION_NONE);
 
        (void) thread_interrupt_level(wsave);
        assert(task->halting == TRUE);
 
        (void) thread_interrupt_level(wsave);
        assert(task->halting == TRUE);
@@ -2013,6 +2308,7 @@ task_clear_corpse(task_t task)
        {
                thread_mtx_lock(th_iter);
                th_iter->inspection = FALSE;
        {
                thread_mtx_lock(th_iter);
                th_iter->inspection = FALSE;
+               ipc_thread_disable(th_iter);
                thread_mtx_unlock(th_iter);
        }
 
                thread_mtx_unlock(th_iter);
        }
 
@@ -2037,9 +2333,9 @@ task_port_notify(mach_msg_header_t *msg)
        ipc_port_t port = notification->not_header.msgh_remote_port;
        task_t task;
 
        ipc_port_t port = notification->not_header.msgh_remote_port;
        task_t task;
 
-       assert(ip_active(port));
-       assert(IKOT_TASK == ip_kotype(port));
-       task = (task_t) port->ip_kobject;
+       require_ip_active(port);
+       assert(IKOT_TASK_CONTROL == ip_kotype(port));
+       task = (task_t) ip_get_kobject(port);
 
        assert(task_is_a_corpse(task));
 
 
        assert(task_is_a_corpse(task));
 
@@ -2050,6 +2346,81 @@ task_port_notify(mach_msg_header_t *msg)
        task_terminate_internal(task);
 }
 
        task_terminate_internal(task);
 }
 
+/*
+ *     task_port_with_flavor_notify
+ *
+ *     Called whenever the Mach port system detects no-senders on
+ *     the task inspect or read port. These ports are allocated lazily and
+ *     should be deallocated here when there are no senders remaining.
+ */
+void
+task_port_with_flavor_notify(mach_msg_header_t *msg)
+{
+       mach_no_senders_notification_t *notification = (void *)msg;
+       ipc_port_t port = notification->not_header.msgh_remote_port;
+       task_t task;
+       mach_task_flavor_t flavor;
+       ipc_kobject_type_t kotype;
+
+       ip_lock(port);
+       if (port->ip_srights > 0) {
+               ip_unlock(port);
+               return;
+       }
+       task = (task_t)ipc_kobject_get(port);
+       kotype = ip_kotype(port);
+       if (task != TASK_NULL) {
+               assert((IKOT_TASK_READ == kotype) || (IKOT_TASK_INSPECT == kotype));
+               task_reference_internal(task);
+       }
+       ip_unlock(port);
+
+       if (task == TASK_NULL) {
+               /* The task is exiting or disabled; it will eventually deallocate the port */
+               return;
+       }
+
+       if (kotype == IKOT_TASK_READ) {
+               flavor = TASK_FLAVOR_READ;
+       } else {
+               flavor = TASK_FLAVOR_INSPECT;
+       }
+
+       itk_lock(task);
+       ip_lock(port);
+       /*
+        * If the port is no longer active, then ipc_task_terminate() ran
+        * and destroyed the kobject already. Just deallocate the task
+        * ref we took and go away.
+        *
+        * It is also possible that several nsrequests are in flight,
+        * only one shall NULL-out the port entry, and this is the one
+        * that gets to dealloc the port.
+        *
+        * Check for a stale no-senders notification. A call to any function
+        * that vends out send rights to this port could resurrect it between
+        * this notification being generated and actually being handled here.
+        */
+       if (!ip_active(port) ||
+           task->itk_task_ports[flavor] != port ||
+           port->ip_srights > 0) {
+               ip_unlock(port);
+               itk_unlock(task);
+               task_deallocate(task);
+               return;
+       }
+
+       assert(task->itk_task_ports[flavor] == port);
+       task->itk_task_ports[flavor] = IP_NULL;
+
+       ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
+       ip_unlock(port);
+       itk_unlock(task);
+       task_deallocate(task);
+
+       ipc_port_dealloc_kernel(port);
+}
+
 /*
  *     task_wait_till_threads_terminate_locked
  *
 /*
  *     task_wait_till_threads_terminate_locked
  *
@@ -2078,13 +2449,13 @@ task_wait_till_threads_terminate_locked(task_t task)
  */
 kern_return_t
 task_duplicate_map_and_threads(
  */
 kern_return_t
 task_duplicate_map_and_threads(
-               task_t task,
-               void *p,
-               task_t new_task,
-               thread_t *thread_ret,
-               uint64_t **udata_buffer,
-               int *size,
-               int *num_udata)
+       task_t task,
+       void *p,
+       task_t new_task,
+       thread_t *thread_ret,
+       uint64_t **udata_buffer,
+       int *size,
+       int *num_udata)
 {
        kern_return_t kr = KERN_SUCCESS;
        int active;
 {
        kern_return_t kr = KERN_SUCCESS;
        int active;
@@ -2127,14 +2498,13 @@ task_duplicate_map_and_threads(
 
        /* Check with VM if vm_map_fork is allowed for this task */
        if (memorystatus_allowed_vm_map_fork(task)) {
 
        /* Check with VM if vm_map_fork is allowed for this task */
        if (memorystatus_allowed_vm_map_fork(task)) {
-
                /* Setup new task's vmmap, switch from parent task's map to it COW map */
                oldmap = new_task->map;
                new_task->map = vm_map_fork(new_task->ledger,
                /* Setup new task's vmmap, switch from parent task's map to it COW map */
                oldmap = new_task->map;
                new_task->map = vm_map_fork(new_task->ledger,
-                                           task->map,
-                                           (VM_MAP_FORK_SHARE_IF_INHERIT_NONE |
-                                            VM_MAP_FORK_PRESERVE_PURGEABLE |
-                                            VM_MAP_FORK_CORPSE_FOOTPRINT));
+                   task->map,
+                   (VM_MAP_FORK_SHARE_IF_INHERIT_NONE |
+                   VM_MAP_FORK_PRESERVE_PURGEABLE |
+                   VM_MAP_FORK_CORPSE_FOOTPRINT));
                vm_map_deallocate(oldmap);
 
                /* copy ledgers that impact the memory footprint */
                vm_map_deallocate(oldmap);
 
                /* copy ledgers that impact the memory footprint */
@@ -2144,7 +2514,7 @@ task_duplicate_map_and_threads(
                est_knotes = kevent_proc_copy_uptrs(p, NULL, 0);
                if (est_knotes > 0) {
                        buf_size = (est_knotes + 32) * sizeof(uint64_t);
                est_knotes = kevent_proc_copy_uptrs(p, NULL, 0);
                if (est_knotes > 0) {
                        buf_size = (est_knotes + 32) * sizeof(uint64_t);
-                       buffer = (uint64_t *) kalloc(buf_size);
+                       buffer = kheap_alloc(KHEAP_DATA_BUFFERS, buf_size, Z_WAITOK);
                        num_knotes = kevent_proc_copy_uptrs(p, buffer, buf_size);
                        if (num_knotes > est_knotes + 32) {
                                num_knotes = est_knotes + 32;
                        num_knotes = kevent_proc_copy_uptrs(p, buffer, buf_size);
                        if (num_knotes > est_knotes + 32) {
                                num_knotes = est_knotes + 32;
@@ -2155,13 +2525,14 @@ task_duplicate_map_and_threads(
        active_thread_count = task->active_thread_count;
        if (active_thread_count == 0) {
                if (buffer != NULL) {
        active_thread_count = task->active_thread_count;
        if (active_thread_count == 0) {
                if (buffer != NULL) {
-                       kfree(buffer, buf_size);
+                       kheap_free(KHEAP_DATA_BUFFERS, buffer, buf_size);
                }
                task_resume_internal(task);
                return KERN_FAILURE;
        }
 
                }
                task_resume_internal(task);
                return KERN_FAILURE;
        }
 
-       thread_array = (thread_t *) kalloc(sizeof(thread_t) * active_thread_count);
+       thread_array = kheap_alloc(KHEAP_TEMP,
+           sizeof(thread_t) * active_thread_count, Z_WAITOK);
 
        /* Iterate all the threads and drop the task lock before calling thread_create_with_continuation */
        task_lock(task);
 
        /* Iterate all the threads and drop the task lock before calling thread_create_with_continuation */
        task_lock(task);
@@ -2182,7 +2553,6 @@ task_duplicate_map_and_threads(
        task_unlock(task);
 
        for (i = 0; i < array_count; i++) {
        task_unlock(task);
 
        for (i = 0; i < array_count; i++) {
-
                kr = thread_create_with_continuation(new_task, &new_thread, (thread_continue_t)thread_corpse_continue);
                if (kr != KERN_SUCCESS) {
                        break;
                kr = thread_create_with_continuation(new_task, &new_thread, (thread_continue_t)thread_corpse_continue);
                if (kr != KERN_SUCCESS) {
                        break;
@@ -2216,8 +2586,7 @@ task_duplicate_map_and_threads(
        /* return the first thread if we couldn't find the equivalent of current */
        if (thread_return == THREAD_NULL) {
                thread_return = first_thread;
        /* return the first thread if we couldn't find the equivalent of current */
        if (thread_return == THREAD_NULL) {
                thread_return = first_thread;
-       }
-       else if (first_thread != THREAD_NULL) {
+       } else if (first_thread != THREAD_NULL) {
                /* drop the extra ref returned by thread_create_with_continuation */
                thread_deallocate(first_thread);
        }
                /* drop the extra ref returned by thread_create_with_continuation */
                thread_deallocate(first_thread);
        }
@@ -2227,7 +2596,7 @@ task_duplicate_map_and_threads(
        for (i = 0; i < array_count; i++) {
                thread_deallocate(thread_array[i]);
        }
        for (i = 0; i < array_count; i++) {
                thread_deallocate(thread_array[i]);
        }
-       kfree(thread_array, sizeof(thread_t) * active_thread_count);
+       kheap_free(KHEAP_TEMP, thread_array, sizeof(thread_t) * active_thread_count);
 
        if (kr == KERN_SUCCESS) {
                *thread_ret = thread_return;
 
        if (kr == KERN_SUCCESS) {
                *thread_ret = thread_return;
@@ -2239,7 +2608,7 @@ task_duplicate_map_and_threads(
                        thread_deallocate(thread_return);
                }
                if (buffer != NULL) {
                        thread_deallocate(thread_return);
                }
                if (buffer != NULL) {
-                       kfree(buffer, buf_size);
+                       kheap_free(KHEAP_DATA_BUFFERS, buffer, buf_size);
                }
        }
 
                }
        }
 
@@ -2248,18 +2617,21 @@ task_duplicate_map_and_threads(
 
 #if CONFIG_SECLUDED_MEMORY
 extern void task_set_can_use_secluded_mem_locked(
 
 #if CONFIG_SECLUDED_MEMORY
 extern void task_set_can_use_secluded_mem_locked(
-       task_t          task,
-       boolean_t       can_use_secluded_mem);
+       task_t          task,
+       boolean_t       can_use_secluded_mem);
 #endif /* CONFIG_SECLUDED_MEMORY */
 
 #endif /* CONFIG_SECLUDED_MEMORY */
 
+#if MACH_ASSERT
+int debug4k_panic_on_terminate = 0;
+#endif /* MACH_ASSERT */
 kern_return_t
 task_terminate_internal(
 kern_return_t
 task_terminate_internal(
-       task_t                  task)
+       task_t                  task)
 {
 {
-       thread_t                        thread, self;
-       task_t                          self_task;
-       boolean_t                       interrupt_save;
-       int                             pid = 0;
+       thread_t                        thread, self;
+       task_t                          self_task;
+       boolean_t                       interrupt_save;
+       int                             pid = 0;
 
        assert(task != kernel_task);
 
 
        assert(task != kernel_task);
 
@@ -2270,14 +2642,12 @@ task_terminate_internal(
         *      Get the task locked and make sure that we are not racing
         *      with someone else trying to terminate us.
         */
         *      Get the task locked and make sure that we are not racing
         *      with someone else trying to terminate us.
         */
-       if (task == self_task)
+       if (task == self_task) {
                task_lock(task);
                task_lock(task);
-       else
-       if (task < self_task) {
+       } else if (task < self_task) {
                task_lock(task);
                task_lock(self_task);
                task_lock(task);
                task_lock(self_task);
-       }
-       else {
+       } else {
                task_lock(self_task);
                task_lock(task);
        }
                task_lock(self_task);
                task_lock(task);
        }
@@ -2302,10 +2672,11 @@ task_terminate_internal(
                 *      will get us to finalize the termination of ourselves.
                 */
                task_unlock(task);
                 *      will get us to finalize the termination of ourselves.
                 */
                task_unlock(task);
-               if (self_task != task)
+               if (self_task != task) {
                        task_unlock(self_task);
                        task_unlock(self_task);
+               }
 
 
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        if (task_corpse_pending_report(task)) {
        }
 
        if (task_corpse_pending_report(task)) {
@@ -2316,14 +2687,16 @@ task_terminate_internal(
                 *      will get us to finish the path to death
                 */
                task_unlock(task);
                 *      will get us to finish the path to death
                 */
                task_unlock(task);
-               if (self_task != task)
+               if (self_task != task) {
                        task_unlock(self_task);
                        task_unlock(self_task);
+               }
 
 
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        }
 
-       if (self_task != task)
+       if (self_task != task) {
                task_unlock(self_task);
                task_unlock(self_task);
+       }
 
        /*
         * Make sure the current thread does not get aborted out of
 
        /*
         * Make sure the current thread does not get aborted out of
@@ -2354,7 +2727,7 @@ task_terminate_internal(
         *      Terminate each thread in the task.
         */
        queue_iterate(&task->threads, thread, thread_t, task_threads) {
         *      Terminate each thread in the task.
         */
        queue_iterate(&task->threads, thread, thread_t, task_threads) {
-                       thread_terminate_internal(thread);
+               thread_terminate_internal(thread, TH_TERMINATE_OPTION_NONE);
        }
 
 #ifdef MACH_BSD
        }
 
 #ifdef MACH_BSD
@@ -2366,26 +2739,31 @@ task_terminate_internal(
        task_unlock(task);
 
        proc_set_task_policy(task, TASK_POLICY_ATTRIBUTE,
        task_unlock(task);
 
        proc_set_task_policy(task, TASK_POLICY_ATTRIBUTE,
-                            TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
+           TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
 
 
-        /* Early object reap phase */
+       /* Early object reap phase */
 
 // PR-17045188: Revisit implementation
 //        task_partial_reap(task, pid);
 
 
 // PR-17045188: Revisit implementation
 //        task_partial_reap(task, pid);
 
-#if CONFIG_EMBEDDED
+#if CONFIG_TASKWATCH
        /*
        /*
-        * remove all task watchers 
+        * remove all task watchers
         */
        task_removewatchers(task);
 
         */
        task_removewatchers(task);
 
-#endif /* CONFIG_EMBEDDED */
+#endif /* CONFIG_TASKWATCH */
 
        /*
         *      Destroy all synchronizers owned by the task.
         */
        task_synchronizer_destroy_all(task);
 
 
        /*
         *      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.
         */
        /*
         *      Destroy the IPC space, leaving just a reference for it.
         */
@@ -2394,17 +2772,17 @@ task_terminate_internal(
 #if 00
        /* if some ledgers go negative on tear-down again... */
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
 #if 00
        /* if some ledgers go negative on tear-down again... */
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
-                                        task_ledgers.phys_footprint);
+           task_ledgers.phys_footprint);
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
-                                        task_ledgers.internal);
+           task_ledgers.internal);
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
-                                        task_ledgers.internal_compressed);
+           task_ledgers.internal_compressed);
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
-                                        task_ledgers.iokit_mapped);
+           task_ledgers.iokit_mapped);
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
-                                        task_ledgers.alternate_accounting);
+           task_ledgers.alternate_accounting);
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
        ledger_disable_panic_on_negative(task->map->pmap->ledger,
-                                        task_ledgers.alternate_accounting_compressed);
+           task_ledgers.alternate_accounting_compressed);
 #endif
 
        /*
 #endif
 
        /*
@@ -2428,30 +2806,28 @@ task_terminate_internal(
        char procname[17];
        if (task->bsd_info && !task_is_exec_copy(task)) {
                pid = proc_pid(task->bsd_info);
        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));
+               proc_name_kdp(task, procname, sizeof(procname));
        } else {
                pid = 0;
        } else {
                pid = 0;
-               strlcpy(procname, "<unknown>", sizeof (procname));
+               strlcpy(procname, "<unknown>", sizeof(procname));
        }
        pmap_set_process(task->map->pmap, pid, procname);
        }
        pmap_set_process(task->map->pmap, pid, procname);
+       if (vm_map_page_shift(task->map) < (int)PAGE_SHIFT) {
+               DEBUG4K_LIFE("map %p procname: %s\n", task->map, procname);
+               if (debug4k_panic_on_terminate) {
+                       panic("DEBUG4K: %s:%d %d[%s] map %p\n", __FUNCTION__, __LINE__, pid, procname, task->map);
+               }
+       }
 #endif /* MACH_ASSERT */
 
 #endif /* MACH_ASSERT */
 
-       vm_map_remove(task->map,
-                     task->map->min_offset,
-                     task->map->max_offset,
-                     /*
-                      * Final cleanup:
-                      * + no unnesting
-                      * + remove immutable mappings
-                      * + allow gaps in range
-                      */
-                     (VM_MAP_REMOVE_NO_UNNESTING |
-                      VM_MAP_REMOVE_IMMUTABLE |
-                      VM_MAP_REMOVE_GAPS_OK));
+       vm_map_terminate(task->map);
 
        /* release our shared region */
        vm_shared_region_set(task, NULL);
 
 
        /* release our shared region */
        vm_shared_region_set(task, NULL);
 
+#if __has_feature(ptrauth_calls)
+       task_set_shared_region_id(task, NULL);
+#endif /* __has_feature(ptrauth_calls) */
 
        lck_mtx_lock(&tasks_threads_lock);
        queue_remove(&tasks, task, task_t, tasks);
 
        lck_mtx_lock(&tasks_threads_lock);
        queue_remove(&tasks, task, task_t, tasks);
@@ -2468,8 +2844,9 @@ task_terminate_internal(
 
 #if KPC
        /* force the task to release all ctrs */
 
 #if KPC
        /* force the task to release all ctrs */
-       if (task->t_kpc & TASK_KPC_FORCED_ALL_CTRS)
+       if (task->t_kpc & TASK_KPC_FORCED_ALL_CTRS) {
                kpc_force_all_ctrs(task, 0);
                kpc_force_all_ctrs(task, 0);
+       }
 #endif /* KPC */
 
 #if CONFIG_COALITIONS
 #endif /* KPC */
 
 #if CONFIG_COALITIONS
@@ -2479,12 +2856,20 @@ task_terminate_internal(
        coalitions_remove_task(task);
 #endif
 
        coalitions_remove_task(task);
 #endif
 
-       /*
-        * Get rid of the task active reference on itself.
-        */
+#if CONFIG_FREEZE
+       extern int      vm_compressor_available;
+       if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE && vm_compressor_available) {
+               task_disown_frozen_csegs(task);
+               assert(queue_empty(&task->task_frozen_cseg_q));
+       }
+#endif /* CONFIG_FREEZE */
+
+       /*
+        * Get rid of the task active reference on itself.
+        */
        task_deallocate(task);
 
        task_deallocate(task);
 
-       return (KERN_SUCCESS);
+       return KERN_SUCCESS;
 }
 
 void
 }
 
 void
@@ -2507,7 +2892,7 @@ tasks_system_suspend(boolean_t suspend)
 /*
  * task_start_halt:
  *
 /*
  * task_start_halt:
  *
- *     Shut the current task down (except for the current thread) in
+ *      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.
  *     preparation for dramatic changes to the task (probably exec).
  *     We hold the task and mark all other threads in the task for
  *     termination.
@@ -2532,8 +2917,9 @@ task_start_halt_locked(task_t task, boolean_t should_mark_corpse)
 
        self = current_thread();
 
 
        self = current_thread();
 
-       if (task != self->task && !task_is_a_corpse_fork(task))
-               return (KERN_INVALID_ARGUMENT);
+       if (task != self->task && !task_is_a_corpse_fork(task)) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        if (task->halting || !task->active || !self->active) {
                /*
 
        if (task->halting || !task->active || !self->active) {
                /*
@@ -2542,7 +2928,7 @@ task_start_halt_locked(task_t task, boolean_t should_mark_corpse)
                 * so that we run our AST special handler to terminate
                 * ourselves.
                 */
                 * so that we run our AST special handler to terminate
                 * ourselves.
                 */
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        task->halting = TRUE;
        }
 
        task->halting = TRUE;
@@ -2566,8 +2952,9 @@ task_start_halt_locked(task_t task, boolean_t should_mark_corpse)
                        thread->inspection = TRUE;
                        thread_mtx_unlock(thread);
                }
                        thread->inspection = TRUE;
                        thread_mtx_unlock(thread);
                }
-               if (thread != self)
-                       thread_terminate_internal(thread);
+               if (thread != self) {
+                       thread_terminate_internal(thread, TH_TERMINATE_OPTION_NONE);
+               }
        }
        task->dispatchqueue_offset = dispatchqueue_offset;
 
        }
        task->dispatchqueue_offset = dispatchqueue_offset;
 
@@ -2631,16 +3018,16 @@ task_complete_halt(task_t task)
         * getting a new one.
         */
        vm_map_remove(task->map, task->map->min_offset,
         * 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));
+           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,
 
        /*
         * Kick out any IOKitUser handles to the task. At best they're stale,
@@ -2660,14 +3047,15 @@ task_complete_halt(task_t task)
  */
 void
 task_hold_locked(
  */
 void
 task_hold_locked(
-       task_t          task)
+       task_t          task)
 {
 {
-       thread_t        thread;
+       thread_t        thread;
 
        assert(task->active);
 
 
        assert(task->active);
 
-       if (task->suspend_count++ > 0)
+       if (task->suspend_count++ > 0) {
                return;
                return;
+       }
 
        if (task->bsd_info) {
                workq_proc_suspended(task->bsd_info);
 
        if (task->bsd_info) {
                workq_proc_suspended(task->bsd_info);
@@ -2688,53 +3076,55 @@ task_hold_locked(
  *
  *     Same as the internal routine above, except that is must lock
  *     and verify that the task is active.  This differs from task_suspend
  *
  *     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 
+ *     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.
  *
  *     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
+ *      CONDITIONS: the caller holds a reference on the task
  */
 kern_return_t
 task_hold(
  */
 kern_return_t
 task_hold(
-       task_t          task)
+       task_t          task)
 {
 {
-       if (task == TASK_NULL)
-               return (KERN_INVALID_ARGUMENT);
+       if (task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
 
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
 
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        task_hold_locked(task);
        task_unlock(task);
 
        }
 
        task_hold_locked(task);
        task_unlock(task);
 
-       return (KERN_SUCCESS);
+       return KERN_SUCCESS;
 }
 
 kern_return_t
 task_wait(
 }
 
 kern_return_t
 task_wait(
-               task_t          task,
-               boolean_t       until_not_runnable)
+       task_t          task,
+       boolean_t       until_not_runnable)
 {
 {
-       if (task == TASK_NULL)
-               return (KERN_INVALID_ARGUMENT);
+       if (task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
 
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
 
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        task_wait_locked(task, until_not_runnable);
        task_unlock(task);
 
        }
 
        task_wait_locked(task, until_not_runnable);
        task_unlock(task);
 
-       return (KERN_SUCCESS);
+       return KERN_SUCCESS;
 }
 
 /*
 }
 
 /*
@@ -2747,10 +3137,10 @@ task_wait(
  */
 void
 task_wait_locked(
  */
 void
 task_wait_locked(
-       task_t          task,
-       boolean_t               until_not_runnable)
+       task_t          task,
+       boolean_t               until_not_runnable)
 {
 {
-       thread_t        thread, self;
+       thread_t        thread, self;
 
        assert(task->active);
        assert(task->suspend_count > 0);
 
        assert(task->active);
        assert(task->suspend_count > 0);
@@ -2763,29 +3153,37 @@ task_wait_locked(
         *      the task.
         */
        queue_iterate(&task->threads, thread, thread_t, task_threads) {
         *      the task.
         */
        queue_iterate(&task->threads, thread, thread_t, task_threads) {
-               if (thread != self)
+               if (thread != self) {
                        thread_wait(thread, until_not_runnable);
                        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.
  *
 /*
  *     task_release_locked:
  *
  *     Release a kernel hold on a task.
  *
- *     CONDITIONS: the task is locked and active
+ *      CONDITIONS: the task is locked and active
  */
 void
 task_release_locked(
  */
 void
 task_release_locked(
-       task_t          task)
+       task_t          task)
 {
 {
-       thread_t        thread;
+       thread_t        thread;
 
        assert(task->active);
        assert(task->suspend_count > 0);
 
 
        assert(task->active);
        assert(task->suspend_count > 0);
 
-       if (--task->suspend_count > 0)
+       if (--task->suspend_count > 0) {
                return;
                return;
+       }
 
        if (task->bsd_info) {
                workq_proc_resumed(task->bsd_info);
 
        if (task->bsd_info) {
                workq_proc_resumed(task->bsd_info);
@@ -2804,77 +3202,86 @@ task_release_locked(
  *     Same as the internal routine above, except that it must lock
  *     and verify that the task is active.
  *
  *     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
+ *      CONDITIONS: The caller holds a reference to the task
  */
 kern_return_t
 task_release(
  */
 kern_return_t
 task_release(
-       task_t          task)
+       task_t          task)
 {
 {
-       if (task == TASK_NULL)
-               return (KERN_INVALID_ARGUMENT);
+       if (task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
 
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
 
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        task_release_locked(task);
        task_unlock(task);
 
        }
 
        task_release_locked(task);
        task_unlock(task);
 
-       return (KERN_SUCCESS);
+       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);
+static kern_return_t
+task_threads_internal(
+       task_t                      task,
+       thread_act_array_t         *threads_out,
+       mach_msg_type_number_t     *count,
+       mach_thread_flavor_t        flavor)
+{
+       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;
 
        size = 0; addr = NULL;
 
 
        size = 0; addr = NULL;
 
+       if (task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
+       }
+
+       assert(flavor <= THREAD_FLAVOR_INSPECT);
+
        for (;;) {
                task_lock(task);
                if (!task->active) {
                        task_unlock(task);
 
        for (;;) {
                task_lock(task);
                if (!task->active) {
                        task_unlock(task);
 
-                       if (size != 0)
+                       if (size != 0) {
                                kfree(addr, size);
                                kfree(addr, size);
+                       }
 
 
-                       return (KERN_FAILURE);
+                       return KERN_FAILURE;
                }
 
                actual = task->thread_count;
 
                /* do we have the memory we need? */
                }
 
                actual = task->thread_count;
 
                /* do we have the memory we need? */
-               size_needed = actual * sizeof (mach_port_t);
-               if (size_needed <= size)
+               size_needed = actual * sizeof(mach_port_t);
+               if (size_needed <= size) {
                        break;
                        break;
+               }
 
                /* unlock the task and allocate more memory */
                task_unlock(task);
 
 
                /* unlock the task and allocate more memory */
                task_unlock(task);
 
-               if (size != 0)
+               if (size != 0) {
                        kfree(addr, size);
                        kfree(addr, size);
+               }
 
                assert(size_needed > 0);
                size = size_needed;
 
                addr = kalloc(size);
 
                assert(size_needed > 0);
                size = size_needed;
 
                addr = kalloc(size);
-               if (addr == 0)
-                       return (KERN_RESOURCE_SHORTAGE);
+               if (addr == 0) {
+                       return KERN_RESOURCE_SHORTAGE;
+               }
        }
 
        /* OK, have memory and the task is locked & active */
        }
 
        /* OK, have memory and the task is locked & active */
@@ -2883,7 +3290,7 @@ task_threads(
        i = j = 0;
 
        for (thread = (thread_t)queue_first(&task->threads); i < actual;
        i = j = 0;
 
        for (thread = (thread_t)queue_first(&task->threads); i < actual;
-                               ++i, thread = (thread_t)queue_next(&thread->task_threads)) {
+           ++i, thread = (thread_t)queue_next(&thread->task_threads)) {
                thread_reference_internal(thread);
                thread_list[j++] = thread;
        }
                thread_reference_internal(thread);
                thread_list[j++] = thread;
        }
@@ -2891,7 +3298,7 @@ task_threads(
        assert(queue_end(&task->threads, (queue_entry_t)thread));
 
        actual = j;
        assert(queue_end(&task->threads, (queue_entry_t)thread));
 
        actual = j;
-       size_needed = actual * sizeof (mach_port_t);
+       size_needed = actual * sizeof(mach_port_t);
 
        /* can unlock task now that we've got the thread refs */
        task_unlock(task);
 
        /* can unlock task now that we've got the thread refs */
        task_unlock(task);
@@ -2902,10 +3309,10 @@ task_threads(
                *threads_out = NULL;
                *count = 0;
 
                *threads_out = NULL;
                *count = 0;
 
-               if (size != 0)
+               if (size != 0) {
                        kfree(addr, size);
                        kfree(addr, size);
-       }
-       else {
+               }
+       else {
                /* if we allocated too much, must copy */
 
                if (size_needed < size) {
                /* if we allocated too much, must copy */
 
                if (size_needed < size) {
@@ -2913,10 +3320,11 @@ task_threads(
 
                        newaddr = kalloc(size_needed);
                        if (newaddr == 0) {
 
                        newaddr = kalloc(size_needed);
                        if (newaddr == 0) {
-                               for (i = 0; i < actual; ++i)
+                               for (i = 0; i < actual; ++i) {
                                        thread_deallocate(thread_list[i]);
                                        thread_deallocate(thread_list[i]);
+                               }
                                kfree(addr, size);
                                kfree(addr, size);
-                               return (KERN_RESOURCE_SHORTAGE);
+                               return KERN_RESOURCE_SHORTAGE;
                        }
 
                        bcopy(addr, newaddr, size_needed);
                        }
 
                        bcopy(addr, newaddr, size_needed);
@@ -2929,25 +3337,90 @@ task_threads(
 
                /* do the conversion that Mig should handle */
 
 
                /* 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]);
+               switch (flavor) {
+               case THREAD_FLAVOR_CONTROL:
+                       if (task == current_task()) {
+                               for (i = 0; i < actual; ++i) {
+                                       ((ipc_port_t *) thread_list)[i] = convert_thread_to_port_pinned(thread_list[i]);
+                               }
+                       } else {
+                               for (i = 0; i < actual; ++i) {
+                                       ((ipc_port_t *) thread_list)[i] = convert_thread_to_port(thread_list[i]);
+                               }
+                       }
+                       break;
+               case THREAD_FLAVOR_READ:
+                       for (i = 0; i < actual; ++i) {
+                               ((ipc_port_t *) thread_list)[i] = convert_thread_read_to_port(thread_list[i]);
+                       }
+                       break;
+               case THREAD_FLAVOR_INSPECT:
+                       for (i = 0; i < actual; ++i) {
+                               ((ipc_port_t *) thread_list)[i] = convert_thread_inspect_to_port(thread_list[i]);
+                       }
+                       break;
+               }
+       }
+
+       return KERN_SUCCESS;
+}
+
+kern_return_t
+task_threads(
+       task_t                      task,
+       thread_act_array_t         *threads_out,
+       mach_msg_type_number_t     *count)
+{
+       return task_threads_internal(task, threads_out, count, THREAD_FLAVOR_CONTROL);
+}
+
+
+kern_return_t
+task_threads_from_user(
+       mach_port_t                 port,
+       thread_act_array_t         *threads_out,
+       mach_msg_type_number_t     *count)
+{
+       ipc_kobject_type_t kotype;
+       kern_return_t kr;
+
+       task_t task = convert_port_to_task_check_type(port, &kotype, TASK_FLAVOR_INSPECT, FALSE);
+
+       if (task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
        }
 
        }
 
-       return (KERN_SUCCESS);
+       switch (kotype) {
+       case IKOT_TASK_CONTROL:
+               kr = task_threads_internal(task, threads_out, count, THREAD_FLAVOR_CONTROL);
+               break;
+       case IKOT_TASK_READ:
+               kr = task_threads_internal(task, threads_out, count, THREAD_FLAVOR_READ);
+               break;
+       case IKOT_TASK_INSPECT:
+               kr = task_threads_internal(task, threads_out, count, THREAD_FLAVOR_INSPECT);
+               break;
+       default:
+               panic("strange kobject type");
+               break;
+       }
+
+       task_deallocate(task);
+       return kr;
 }
 
 }
 
-#define TASK_HOLD_NORMAL       0
-#define TASK_HOLD_PIDSUSPEND   1
-#define TASK_HOLD_LEGACY       2
-#define TASK_HOLD_LEGACY_ALL   3
+#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
 
 static kern_return_t
-place_task_hold    (
+place_task_hold(
        task_t task,
        int mode)
        task_t task,
        int mode)
-{    
+{
        if (!task->active && !task_is_a_corpse(task)) {
        if (!task->active && !task_is_a_corpse(task)) {
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        /* Return success for corpse task */
        }
 
        /* Return success for corpse task */
@@ -2955,24 +3428,25 @@ place_task_hold    (
                return KERN_SUCCESS;
        }
 
                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);
+       KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_IPC, MACH_TASK_SUSPEND),
+           task_pid(task),
+           task->thread_count > 0 ?((thread_t)queue_first(&task->threads))->thread_id : 0,
+           task->user_stop_count, task->user_stop_count + 1);
 
 #if MACH_ASSERT
        current_task()->suspends_outstanding++;
 #endif
 
 
 #if MACH_ASSERT
        current_task()->suspends_outstanding++;
 #endif
 
-       if (mode == TASK_HOLD_LEGACY)
+       if (mode == TASK_HOLD_LEGACY) {
                task->legacy_stop_count++;
                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.
                 */
 
        if (task->user_stop_count++ > 0) {
                /*
                 *      If the stop count was positive, the task is
                 *      already stopped and we can exit.
                 */
-               return (KERN_SUCCESS);
+               return KERN_SUCCESS;
        }
 
        /*
        }
 
        /*
@@ -2983,37 +3457,36 @@ place_task_hold    (
         */
        task_hold_locked(task);
        task_wait_locked(task, FALSE);
         */
        task_hold_locked(task);
        task_wait_locked(task, FALSE);
-       
-       return (KERN_SUCCESS);
+
+       return KERN_SUCCESS;
 }
 
 static kern_return_t
 }
 
 static kern_return_t
-release_task_hold    (
-       task_t          task,
-       int                     mode)
+release_task_hold(
+       task_t          task,
+       int                     mode)
 {
        boolean_t release = FALSE;
 {
        boolean_t release = FALSE;
-    
+
        if (!task->active && !task_is_a_corpse(task)) {
        if (!task->active && !task_is_a_corpse(task)) {
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        /* Return success for corpse task */
        if (task_is_a_corpse(task)) {
                return KERN_SUCCESS;
        }
        }
 
        /* Return success for corpse task */
        if (task_is_a_corpse(task)) {
                return KERN_SUCCESS;
        }
-       
+
        if (mode == TASK_HOLD_PIDSUSPEND) {
        if (mode == TASK_HOLD_PIDSUSPEND) {
-           if (task->pidsuspended == FALSE) {
-                   return (KERN_FAILURE);
-           }
-           task->pidsuspended = FALSE;
+               if (task->pidsuspended == FALSE) {
+                       return KERN_FAILURE;
+               }
+               task->pidsuspended = FALSE;
        }
 
        if (task->user_stop_count > (task->pidsuspended ? 1 : 0)) {
        }
 
        if (task->user_stop_count > (task->pidsuspended ? 1 : 0)) {
-
                KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
                KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
-                   MACHDBG_CODE(DBG_MACH_IPC,MACH_TASK_RESUME) | DBG_FUNC_NONE,
+                   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);
 
                    task_pid(task), ((thread_t)queue_first(&task->threads))->thread_id,
                    task->user_stop_count, mode, task->legacy_stop_count);
 
@@ -3035,25 +3508,32 @@ release_task_hold    (
                        }
                        task->legacy_stop_count = 0;
                } else {
                        }
                        task->legacy_stop_count = 0;
                } else {
-                       if (mode == TASK_HOLD_LEGACY && task->legacy_stop_count > 0)
+                       if (mode == TASK_HOLD_LEGACY && task->legacy_stop_count > 0) {
                                task->legacy_stop_count--;
                                task->legacy_stop_count--;
-                       if (--task->user_stop_count == 0)
+                       }
+                       if (--task->user_stop_count == 0) {
                                release = TRUE;
                                release = TRUE;
+                       }
                }
                }
-       }
-       else {
-               return (KERN_FAILURE);
+       } else {
+               return KERN_FAILURE;
        }
 
        /*
         *      Release the task if necessary.
         */
        }
 
        /*
         *      Release the task if necessary.
         */
-       if (release)
+       if (release) {
                task_release_locked(task);
                task_release_locked(task);
-               
-    return (KERN_SUCCESS);
+       }
+
+       return KERN_SUCCESS;
 }
 
 }
 
+boolean_t
+get_task_suspended(task_t task)
+{
+       return 0 != task->user_stop_count;
+}
 
 /*
  *     task_suspend:
 
 /*
  *     task_suspend:
@@ -3068,46 +3548,21 @@ release_task_hold    (
  *     unique send-once right).
  *
  * Conditions:
  *     unique send-once right).
  *
  * Conditions:
- *     The caller holds a reference to the task
+ *      The caller holds a reference to the task
  */
 kern_return_t
 task_suspend(
  */
 kern_return_t
 task_suspend(
-       task_t          task)
+       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);
+       kern_return_t                   kr;
+       mach_port_t                     port;
+       mach_port_name_t                name;
 
 
-       /* 
-        * 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);
+       if (task == TASK_NULL || task == kernel_task) {
+               return KERN_INVALID_ARGUMENT;
        }
 
        }
 
-       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);
-       }
+       task_lock(task);
 
        /*
         * place a legacy hold on the task.
 
        /*
         * place a legacy hold on the task.
@@ -3115,10 +3570,17 @@ task_suspend(
        kr = place_task_hold(task, TASK_HOLD_LEGACY);
        if (kr != KERN_SUCCESS) {
                task_unlock(task);
        kr = place_task_hold(task, TASK_HOLD_LEGACY);
        if (kr != KERN_SUCCESS) {
                task_unlock(task);
-               ipc_port_release_send(send);
                return kr;
        }
 
                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((ipc_port_t *) &task->itk_resume,
+           (ipc_kobject_t)task, IKOT_TASK_RESUME, IPC_KOBJECT_ALLOC_NONE, true,
+           OS_PTRAUTH_DISCRIMINATOR("task.itk_resume"));
+       port = task->itk_resume;
        task_unlock(task);
 
        /*
        task_unlock(task);
 
        /*
@@ -3126,35 +3588,43 @@ task_suspend(
         * but we'll look it up when calling a traditional resume.  Any IPC operations that
         * deallocate the send right will auto-release the suspension.
         */
         * 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);
+       if (IP_VALID(port)) {
+               kr = ipc_object_copyout(current_space(), ip_to_object(port),
+                   MACH_MSG_TYPE_MOVE_SEND, IPC_OBJECT_COPYOUT_FLAGS_NONE,
+                   NULL, NULL, &name);
+       } else {
+               kr = KERN_SUCCESS;
+       }
+       if (kr != 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.
 }
 
 /*
  *     task_resume:
  *             Release a user hold on a task.
- *             
+ *
  * Conditions:
  *             The caller holds a reference to the task
  */
  * Conditions:
  *             The caller holds a reference to the task
  */
-kern_return_t 
+kern_return_t
 task_resume(
 task_resume(
-       task_t  task)
+       task_t  task)
 {
 {
-       kern_return_t    kr;
+       kern_return_t    kr;
        mach_port_name_t resume_port_name;
        mach_port_name_t resume_port_name;
-       ipc_entry_t              resume_port_entry;
-       ipc_space_t              space = current_task()->itk_space;
+       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);
+       if (task == TASK_NULL || task == kernel_task) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        /* release a legacy task hold */
        task_lock(task);
 
        /* release a legacy task hold */
        task_lock(task);
@@ -3163,24 +3633,26 @@ task_resume(
 
        is_write_lock(space);
        if (is_active(space) && IP_VALID(task->itk_resume) &&
 
        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) {
+           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.
                 */
                /*
                 * 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)
+               if (kr == KERN_SUCCESS) {
                        ipc_right_dealloc(space, resume_port_name, resume_port_entry);
                        ipc_right_dealloc(space, resume_port_name, resume_port_entry);
-               else
+               } else {
                        ipc_right_destroy(space, resume_port_name, resume_port_entry, FALSE, 0);
                        ipc_right_destroy(space, resume_port_name, resume_port_entry, FALSE, 0);
+               }
                /* space unlocked */
        } else {
                is_write_unlock(space);
                /* space unlocked */
        } else {
                is_write_unlock(space);
-               if (kr == KERN_SUCCESS)
+               if (kr == KERN_SUCCESS) {
                        printf("warning: %s(%d) performed out-of-band resume on pid %d\n",
                        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));
+                           proc_name_address(current_task()->bsd_info), proc_pid(current_task()->bsd_info),
+                           task_pid(task));
+               }
        }
 
        return kr;
        }
 
        return kr;
@@ -3193,15 +3665,16 @@ task_resume(
 kern_return_t
 task_suspend_internal(task_t task)
 {
 kern_return_t
 task_suspend_internal(task_t task)
 {
-       kern_return_t    kr;
-       
-       if (task == TASK_NULL || task == kernel_task)
-               return (KERN_INVALID_ARGUMENT);
+       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);
 
        task_lock(task);
        kr = place_task_hold(task, TASK_HOLD_NORMAL);
        task_unlock(task);
-       return (kr);
+       return kr;
 }
 
 /*
 }
 
 /*
@@ -3210,15 +3683,15 @@ task_suspend_internal(task_t task)
  */
 kern_return_t
 task_suspend2(
  */
 kern_return_t
 task_suspend2(
-       task_t                  task,
+       task_t                  task,
        task_suspension_token_t *suspend_token)
 {
        task_suspension_token_t *suspend_token)
 {
-       kern_return_t    kr;
+       kern_return_t    kr;
+
        kr = task_suspend_internal(task);
        if (kr != KERN_SUCCESS) {
                *suspend_token = TASK_NULL;
        kr = task_suspend_internal(task);
        if (kr != KERN_SUCCESS) {
                *suspend_token = TASK_NULL;
-               return (kr);
+               return kr;
        }
 
        /*
        }
 
        /*
@@ -3229,7 +3702,7 @@ task_suspend2(
        task_reference_internal(task);
        *suspend_token = task;
 
        task_reference_internal(task);
        *suspend_token = task;
 
-       return (KERN_SUCCESS);
+       return KERN_SUCCESS;
 }
 
 /*
 }
 
 /*
@@ -3238,17 +3711,18 @@ task_suspend2(
  */
 kern_return_t
 task_resume_internal(
  */
 kern_return_t
 task_resume_internal(
-       task_suspension_token_t         task)
+       task_suspension_token_t         task)
 {
        kern_return_t kr;
 
 {
        kern_return_t kr;
 
-       if (task == TASK_NULL || task == kernel_task)
-               return (KERN_INVALID_ARGUMENT);
+       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);
 
        task_lock(task);
        kr = release_task_hold(task, TASK_HOLD_NORMAL);
        task_unlock(task);
-       return (kr);
+       return kr;
 }
 
 /*
 }
 
 /*
@@ -3256,28 +3730,27 @@ task_resume_internal(
  */
 kern_return_t
 task_resume2(
  */
 kern_return_t
 task_resume2(
-       task_suspension_token_t         task)
+       task_suspension_token_t         task)
 {
        kern_return_t kr;
 
        kr = task_resume_internal(task);
        task_suspension_token_deallocate(task);
 
 {
        kern_return_t kr;
 
        kr = task_resume_internal(task);
        task_suspension_token_deallocate(task);
 
-       return (kr);
+       return kr;
 }
 
 boolean_t
 task_suspension_notify(mach_msg_header_t *request_header)
 {
 }
 
 boolean_t
 task_suspension_notify(mach_msg_header_t *request_header)
 {
-       ipc_port_t port = (ipc_port_t) request_header->msgh_remote_port;
+       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;
 
        task_t task = convert_port_to_task_suspension_token(port);
        mach_msg_type_number_t not_count;
 
-       if (task == TASK_NULL || task == kernel_task)
+       if (task == TASK_NULL || task == kernel_task) {
                return TRUE;  /* nothing to do */
                return TRUE;  /* nothing to do */
-
+       }
        switch (request_header->msgh_id) {
        switch (request_header->msgh_id) {
-
        case MACH_NOTIFY_SEND_ONCE:
                /* release the hold held by this specific send-once right */
                task_lock(task);
        case MACH_NOTIFY_SEND_ONCE:
                /* release the hold held by this specific send-once right */
                task_lock(task);
@@ -3291,13 +3764,11 @@ task_suspension_notify(mach_msg_header_t *request_header)
                task_lock(task);
                ip_lock(port);
                if (port->ip_mscount == 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);
                        /* 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;
 
                } else if (port->ip_nsrequest == IP_NULL) {
                        ipc_port_t old_notify;
 
@@ -3320,7 +3791,7 @@ task_suspension_notify(mach_msg_header_t *request_header)
        return TRUE;
 }
 
        return TRUE;
 }
 
-kern_return_t
+static kern_return_t
 task_pidsuspend_locked(task_t task)
 {
        kern_return_t kr;
 task_pidsuspend_locked(task_t task)
 {
        kern_return_t kr;
@@ -3337,7 +3808,7 @@ task_pidsuspend_locked(task_t task)
                task->pidsuspended = FALSE;
        }
 out:
                task->pidsuspended = FALSE;
        }
 out:
-       return(kr);
+       return kr;
 }
 
 
 }
 
 
@@ -3347,16 +3818,17 @@ out:
  *     Suspends a task by placing a hold on its threads.
  *
  * Conditions:
  *     Suspends a task by placing a hold on its threads.
  *
  * Conditions:
- *     The caller holds a reference to the task
+ *      The caller holds a reference to the task
  */
 kern_return_t
 task_pidsuspend(
  */
 kern_return_t
 task_pidsuspend(
-       task_t          task)
+       task_t          task)
 {
 {
-       kern_return_t    kr;
-    
-       if (task == TASK_NULL || task == kernel_task)
-               return (KERN_INVALID_ARGUMENT);
+       kern_return_t    kr;
+
+       if (task == TASK_NULL || task == kernel_task) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        task_lock(task);
 
 
        task_lock(task);
 
@@ -3364,31 +3836,35 @@ task_pidsuspend(
 
        task_unlock(task);
 
 
        task_unlock(task);
 
-       return (kr);
+       if ((KERN_SUCCESS == kr) && task->message_app_suspended) {
+               iokit_task_app_suspended_changed(task);
+       }
+
+       return kr;
 }
 
 /*
  *     task_pidresume:
  *             Resumes a previously suspended task.
 }
 
 /*
  *     task_pidresume:
  *             Resumes a previously suspended task.
- *             
+ *
  * Conditions:
  *             The caller holds a reference to the task
  */
  * Conditions:
  *             The caller holds a reference to the task
  */
-kern_return_t 
+kern_return_t
 task_pidresume(
 task_pidresume(
-       task_t  task)
+       task_t  task)
 {
 {
-       kern_return_t    kr;
+       kern_return_t    kr;
 
 
-       if (task == TASK_NULL || task == kernel_task)
-               return (KERN_INVALID_ARGUMENT);
+       if (task == TASK_NULL || task == kernel_task) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        task_lock(task);
 
        task_lock(task);
-       
+
 #if CONFIG_FREEZE
 
        while (task->changing_freeze_state) {
 #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);
                assert_wait((event_t)&task->changing_freeze_state, THREAD_UNINT);
                task_unlock(task);
                thread_block(THREAD_CONTINUE_NULL);
@@ -3402,113 +3878,552 @@ task_pidresume(
 
        task_unlock(task);
 
 
        task_unlock(task);
 
+       if ((KERN_SUCCESS == kr) && task->message_app_suspended) {
+               iokit_task_app_suspended_changed(task);
+       }
+
 #if CONFIG_FREEZE
 
        task_lock(task);
 
 #if CONFIG_FREEZE
 
        task_lock(task);
 
-       if (kr == KERN_SUCCESS)
+       if (kr == KERN_SUCCESS) {
                task->frozen = FALSE;
                task->frozen = FALSE;
+       }
        task->changing_freeze_state = FALSE;
        thread_wakeup(&task->changing_freeze_state);
        task->changing_freeze_state = FALSE;
        thread_wakeup(&task->changing_freeze_state);
-       
+
        task_unlock(task);
 #endif
 
        task_unlock(task);
 #endif
 
-       return (kr);
+       return kr;
 }
 
 }
 
+os_refgrp_decl(static, task_watchports_refgrp, "task_watchports", NULL);
 
 
-#if DEVELOPMENT || DEBUG
-
-extern void IOSleep(int);
-
-kern_return_t
-task_disconnect_page_mappings(task_t task)
+/*
+ *     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)
 {
 {
-       int     n;
+       struct task_watchports *watchports = NULL;
+       struct task_watchport_elem *previous_elem_array[TASK_MAX_WATCHPORT_COUNT] = {};
+       os_ref_count_t refs;
 
 
-       if (task == TASK_NULL || task == kernel_task)
-               return (KERN_INVALID_ARGUMENT);
+       /* Check if the task has terminated */
+       if (!task->active) {
+               return;
+       }
 
 
-       /*
-        * 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;
+       assert(portwatch_count <= TASK_MAX_WATCHPORT_COUNT);
 
 
-               runnable = FALSE;
-               do_unnest = FALSE;
+       watchports = task_watchports_alloc_init(task, thread, portwatch_count);
 
 
-               task_lock(task);
+       /* Lock the ipc space */
+       is_write_lock(task->itk_space);
 
 
-               queue_iterate(&task->threads, thread, thread_t, task_threads) {
-       
-                       if (thread->state & TH_RUN) {
-                               runnable = TRUE;
-                               break;
-                       }
-               }
-               if (n == 0)
-                       task->task_disconnected_count++;
+       /* Setup watchports to boost the main thread */
+       refs = task_add_turnstile_watchports_locked(task,
+           watchports, previous_elem_array, portwatch_ports,
+           portwatch_count);
 
 
-               if (task->task_unnested == FALSE) {
-                       if (runnable == TRUE) {
-                               task->task_unnested = TRUE;
-                               do_unnest = TRUE;
-                       }
-               }
-               task_unlock(task);
+       /* Drop the space lock */
+       is_write_unlock(task->itk_space);
 
 
-               if (runnable == FALSE)
-                       break;
+       if (refs == 0) {
+               task_watchports_deallocate(watchports);
+       }
 
 
-               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);
+       /* 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]);
+       }
+}
 
 
-               page_count = vm_map_disconnect_page_mappings(task->map, do_unnest);
+/*
+ *     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;
 
 
-               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);
+       /* Lock the ipc space */
+       is_write_lock(task->itk_space);
 
 
-               if ((n % 5) == 4)
-                       IOSleep(1);
+       /* Check if watchport boost exist */
+       if (task->watchports == NULL) {
+               is_write_unlock(task->itk_space);
+               return;
        }
        }
-       return (KERN_SUCCESS);
-}
+       watchports = task->watchports;
+       portwatch_count = watchports->tw_elem_array_count;
 
 
-#endif
+       refs = task_remove_turnstile_watchports_locked(task, watchports,
+           port_freelist);
 
 
+       is_write_unlock(task->itk_space);
 
 
-#if CONFIG_FREEZE
+       /* 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_freeze:
+ *     task_transfer_turnstile_watchports:
+ *             Transfer all watchport turnstile boost from old task to new task.
  *
  *
- *     Freeze a task.
+ *     Arguments:
+ *             old_task: task calling exec
+ *             new_task: new exec'ed task
+ *             thread: main thread of new task
  *
  *
- * Conditions:
- *     The caller holds a reference to the task
+ *     Conditions:
+ *             Nothing locked.
  */
  */
-extern void            vm_wake_compactor_swapper(void);
-extern queue_head_t    c_swapout_list_head;
+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;
+       }
 
 
-kern_return_t
+       /* 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;
+extern struct freezer_context freezer_context_global;
+
+kern_return_t
 task_freeze(
        task_t    task,
        uint32_t           *purgeable_count,
 task_freeze(
        task_t    task,
        uint32_t           *purgeable_count,
@@ -3516,19 +4431,19 @@ task_freeze(
        uint32_t           *clean_count,
        uint32_t           *dirty_count,
        uint32_t           dirty_budget,
        uint32_t           *clean_count,
        uint32_t           *dirty_count,
        uint32_t           dirty_budget,
-       uint32_t           *shared_count,
-       int                *freezer_error_code,
-       boolean_t          eval_only)
+       uint32_t           *shared_count,
+       int                *freezer_error_code,
+       boolean_t          eval_only)
 {
        kern_return_t kr = KERN_SUCCESS;
 {
        kern_return_t kr = KERN_SUCCESS;
-    
-       if (task == TASK_NULL || task == kernel_task)
-               return (KERN_INVALID_ARGUMENT);
+
+       if (task == TASK_NULL || task == kernel_task) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        task_lock(task);
 
        while (task->changing_freeze_state) {
 
        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);
                assert_wait((event_t)&task->changing_freeze_state, THREAD_UNINT);
                task_unlock(task);
                thread_block(THREAD_CONTINUE_NULL);
@@ -3537,34 +4452,51 @@ task_freeze(
        }
        if (task->frozen) {
                task_unlock(task);
        }
        if (task->frozen) {
                task_unlock(task);
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
        task->changing_freeze_state = TRUE;
 
        }
        task->changing_freeze_state = TRUE;
 
+       freezer_context_global.freezer_ctx_task = task;
+
        task_unlock(task);
 
        task_unlock(task);
 
-       kr = vm_map_freeze(task->map,
-                          purgeable_count,
-                          wired_count,
-                          clean_count,
-                          dirty_count,
-                          dirty_budget,
-                          shared_count,
-                          freezer_error_code,
-                          eval_only);
+       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_lock(task);
 
        if ((kr == KERN_SUCCESS) && (eval_only == FALSE)) {
                task->frozen = TRUE;
+
+               freezer_context_global.freezer_ctx_task = NULL;
+               freezer_context_global.freezer_ctx_uncompressed_pages = 0;
+
+               if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
+                       /*
+                        * reset the counter tracking the # of swapped compressed pages
+                        * because we are now done with this freeze session and task.
+                        */
+
+                       *dirty_count = (uint32_t) (freezer_context_global.freezer_ctx_swapped_bytes / PAGE_SIZE_64); /*used to track pageouts*/
+               }
+
+               freezer_context_global.freezer_ctx_swapped_bytes = 0;
        }
 
        task->changing_freeze_state = FALSE;
        thread_wakeup(&task->changing_freeze_state);
        }
 
        task->changing_freeze_state = FALSE;
        thread_wakeup(&task->changing_freeze_state);
-       
+
        task_unlock(task);
 
        if (VM_CONFIG_COMPRESSOR_IS_PRESENT &&
        task_unlock(task);
 
        if (VM_CONFIG_COMPRESSOR_IS_PRESENT &&
+           (kr == KERN_SUCCESS) &&
            (eval_only == FALSE)) {
                vm_wake_compactor_swapper();
                /*
            (eval_only == FALSE)) {
                vm_wake_compactor_swapper();
                /*
@@ -3577,7 +4509,7 @@ task_freeze(
                thread_wakeup((event_t)&c_swapout_list_head);
        }
 
                thread_wakeup((event_t)&c_swapout_list_head);
        }
 
-       return (kr);
+       return kr;
 }
 
 /*
 }
 
 /*
@@ -3586,19 +4518,19 @@ task_freeze(
  *     Thaw a currently frozen task.
  *
  * Conditions:
  *     Thaw a currently frozen task.
  *
  * Conditions:
- *     The caller holds a reference to the task
+ *      The caller holds a reference to the task
  */
 kern_return_t
 task_thaw(
  */
 kern_return_t
 task_thaw(
-       task_t          task)
+       task_t          task)
 {
 {
-       if (task == TASK_NULL || task == kernel_task)
-               return (KERN_INVALID_ARGUMENT);
+       if (task == TASK_NULL || task == kernel_task) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
        task_lock(task);
 
        task_lock(task);
-       
-       while (task->changing_freeze_state) {
 
 
+       while (task->changing_freeze_state) {
                assert_wait((event_t)&task->changing_freeze_state, THREAD_UNINT);
                task_unlock(task);
                thread_block(THREAD_CONTINUE_NULL);
                assert_wait((event_t)&task->changing_freeze_state, THREAD_UNINT);
                task_unlock(task);
                thread_block(THREAD_CONTINUE_NULL);
@@ -3607,38 +4539,65 @@ task_thaw(
        }
        if (!task->frozen) {
                task_unlock(task);
        }
        if (!task->frozen) {
                task_unlock(task);
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
        task->frozen = FALSE;
        }
        task->frozen = FALSE;
-       
+
        task_unlock(task);
 
        task_unlock(task);
 
-       return (KERN_SUCCESS);
+       return KERN_SUCCESS;
 }
 
 }
 
+void
+task_update_frozen_to_swap_acct(task_t task, int64_t amount, freezer_acct_op_t op)
+{
+       /*
+        * We don't assert that the task lock is held because we call this
+        * routine from the decompression path and we won't be holding the
+        * task lock. However, since we are in the context of the task we are
+        * safe.
+        * In the case of the task_freeze path, we call it from behind the task
+        * lock but we don't need to because we have a reference on the proc
+        * being frozen.
+        */
+
+       assert(task);
+       if (amount == 0) {
+               return;
+       }
+
+       if (op == CREDIT_TO_SWAP) {
+               ledger_credit_nocheck(task->ledger, task_ledgers.frozen_to_swap, amount);
+       } else if (op == DEBIT_FROM_SWAP) {
+               ledger_debit_nocheck(task->ledger, task_ledgers.frozen_to_swap, amount);
+       } else {
+               panic("task_update_frozen_to_swap_acct: Invalid ledger op\n");
+       }
+}
 #endif /* CONFIG_FREEZE */
 
 kern_return_t
 host_security_set_task_token(
 #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)
+       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;
+       ipc_port_t       host_port;
+       kern_return_t    kr;
 
 
-       if (task == TASK_NULL)
-               return(KERN_INVALID_ARGUMENT);
+       if (task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
+       }
 
 
-       if (host_security == HOST_NULL)
-               return(KERN_INVALID_SECURITY);
+       if (host_security == HOST_NULL) {
+               return KERN_INVALID_SECURITY;
+       }
 
 
-        task_lock(task);
-        task->sec_token = sec_token;
+       task_lock(task);
+       task->sec_token = sec_token;
        task->audit_token = audit_token;
        task->audit_token = audit_token;
-
        task_unlock(task);
 
        if (host_priv != HOST_PRIV_NULL) {
        task_unlock(task);
 
        if (host_priv != HOST_PRIV_NULL) {
@@ -3647,28 +4606,20 @@ host_security_set_task_token(
                kr = host_get_host_port(host_priv_self(), &host_port);
        }
        assert(kr == KERN_SUCCESS);
                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);
+
+       kr = task_set_special_port_internal(task, TASK_HOST_PORT, host_port);
+       return kr;
 }
 
 kern_return_t
 task_send_trace_memory(
 }
 
 kern_return_t
 task_send_trace_memory(
-       task_t        target_task,
+       __unused task_t   target_task,
        __unused uint32_t pid,
        __unused uint64_t uniqueid)
 {
        __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);
+       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
 /*
  * This routine was added, pretty much exclusively, for registering the
  * RPC glue vector for in-kernel short circuited tasks.  Rather than
@@ -3679,144 +4630,132 @@ task_send_trace_memory(
  */
 kern_return_t
 task_set_info(
  */
 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)
+       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);
-
+       if (task == TASK_NULL) {
+               return KERN_INVALID_ARGUMENT;
+       }
        switch (flavor) {
        switch (flavor) {
-
 #if CONFIG_ATM
 #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);
+       case TASK_TRACE_MEMORY_INFO:
+               return KERN_NOT_SUPPORTED;
+#endif // CONFIG_ATM
+       default:
+               return KERN_INVALID_ARGUMENT;
        }
        }
-       return (KERN_SUCCESS);
 }
 
 int radar_20146450 = 1;
 kern_return_t
 task_info(
 }
 
 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)
+       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;
 {
        kern_return_t error = KERN_SUCCESS;
-       mach_msg_type_number_t  original_task_info_count;
+       mach_msg_type_number_t  original_task_info_count;
+       bool is_kernel_task = (task == kernel_task);
 
 
-       if (task == TASK_NULL)
-               return (KERN_INVALID_ARGUMENT);
+       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);
 
        original_task_info_count = *task_info_count;
        task_lock(task);
 
        if ((task != current_task()) && (!task->active)) {
                task_unlock(task);
-               return (KERN_INVALID_ARGUMENT);
+               return KERN_INVALID_ARGUMENT;
        }
 
        }
 
-       switch (flavor) {
 
 
+       switch (flavor) {
        case TASK_BASIC_INFO_32:
        case TASK_BASIC2_INFO_32:
 #if defined(__arm__) || defined(__arm64__)
        case TASK_BASIC_INFO_64:
        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;
+#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;
-               }
+                       if (*task_info_count < TASK_BASIC_INFO_32_COUNT) {
+                               error = KERN_INVALID_ARGUMENT;
+                               break;
+                       }
 
 
-               basic_info = (task_basic_info_32_t)task_info_out;
+                       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;
+                       map = (task == kernel_task)? kernel_map: task->map;
+                       basic_info->virtual_size = (typeof(basic_info->virtual_size))vm_map_adjusted_size(map);
+                       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;
+                       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_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;
+                       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;
-       }
+                       *task_info_count = TASK_BASIC_INFO_32_COUNT;
+                       break;
+               }
 
 #if defined(__arm__) || defined(__arm64__)
        case TASK_BASIC_INFO_64_2:
        {
 
 #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;
+               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) {
 
                if (*task_info_count < TASK_BASIC_INFO_64_2_COUNT) {
-                   error = KERN_INVALID_ARGUMENT;
-                   break;
+                       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 = (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->virtual_size  = vm_map_adjusted_size(map);
                basic_info->resident_size =
                basic_info->resident_size =
-                       (mach_vm_size_t)(pmap_resident_count(map->pmap))
-                       * PAGE_SIZE_64;
+                   (mach_vm_size_t)(pmap_resident_count(map->pmap))
+                   * PAGE_SIZE_64;
 
                basic_info->policy = ((task != kernel_task)?
 
                basic_info->policy = ((task != kernel_task)?
-                                                                                 POLICY_TIMESHARE: POLICY_RR);
+                   POLICY_TIMESHARE: POLICY_RR);
                basic_info->suspend_count = task->user_stop_count;
 
                absolutetime_to_microtime(task->total_user_time, &secs, &usecs);
                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.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 =
                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;
+                   (typeof(basic_info->system_time.seconds))secs;
                basic_info->system_time.microseconds = usecs;
 
                *task_info_count = TASK_BASIC_INFO_64_2_COUNT;
                basic_info->system_time.microseconds = usecs;
 
                *task_info_count = TASK_BASIC_INFO_64_2_COUNT;
@@ -3826,36 +4765,36 @@ task_info(
 #else /* defined(__arm__) || defined(__arm64__) */
        case TASK_BASIC_INFO_64:
        {
 #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;
+               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) {
 
                if (*task_info_count < TASK_BASIC_INFO_64_COUNT) {
-                   error = KERN_INVALID_ARGUMENT;
-                   break;
+                       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 = (task_basic_info_64_t)task_info_out;
 
                map = (task == kernel_task)? kernel_map: task->map;
-               basic_info->virtual_size  = map->size;
+               basic_info->virtual_size  = vm_map_adjusted_size(map);
                basic_info->resident_size =
                basic_info->resident_size =
-                       (mach_vm_size_t)(pmap_resident_count(map->pmap))
-                       * PAGE_SIZE_64;
+                   (mach_vm_size_t)(pmap_resident_count(map->pmap))
+                   * PAGE_SIZE_64;
 
                basic_info->policy = ((task != kernel_task)?
 
                basic_info->policy = ((task != kernel_task)?
-                                                                                 POLICY_TIMESHARE: POLICY_RR);
+                   POLICY_TIMESHARE: POLICY_RR);
                basic_info->suspend_count = task->user_stop_count;
 
                absolutetime_to_microtime(task->total_user_time, &secs, &usecs);
                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.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 =
                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;
+                   (typeof(basic_info->system_time.seconds))secs;
                basic_info->system_time.microseconds = usecs;
 
                *task_info_count = TASK_BASIC_INFO_64_COUNT;
                basic_info->system_time.microseconds = usecs;
 
                *task_info_count = TASK_BASIC_INFO_64_COUNT;
@@ -3871,15 +4810,15 @@ task_info(
                clock_usec_t            usecs;
 
                if (*task_info_count < MACH_TASK_BASIC_INFO_COUNT) {
                clock_usec_t            usecs;
 
                if (*task_info_count < MACH_TASK_BASIC_INFO_COUNT) {
-                   error = KERN_INVALID_ARGUMENT;
-                   break;
+                       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 = (mach_task_basic_info_t)task_info_out;
 
                map = (task == kernel_task) ? kernel_map : task->map;
 
-               basic_info->virtual_size  = map->size;
+               basic_info->virtual_size  = vm_map_adjusted_size(map);
 
                basic_info->resident_size =
                    (mach_vm_size_t)(pmap_resident_count(map->pmap));
 
                basic_info->resident_size =
                    (mach_vm_size_t)(pmap_resident_count(map->pmap));
@@ -3889,13 +4828,13 @@ task_info(
                    (mach_vm_size_t)(pmap_resident_max(map->pmap));
                basic_info->resident_size_max *= PAGE_SIZE_64;
 
                    (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->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->suspend_count = task->user_stop_count;
 
                absolutetime_to_microtime(task->total_user_time, &secs, &usecs);
-               basic_info->user_time.seconds = 
+               basic_info->user_time.seconds =
                    (typeof(basic_info->user_time.seconds))secs;
                basic_info->user_time.microseconds = usecs;
 
                    (typeof(basic_info->user_time.seconds))secs;
                basic_info->user_time.microseconds = usecs;
 
@@ -3910,12 +4849,12 @@ task_info(
 
        case TASK_THREAD_TIMES_INFO:
        {
 
        case TASK_THREAD_TIMES_INFO:
        {
-               task_thread_times_info_t        times_info;
-               thread_t                                        thread;
+               task_thread_times_info_t        times_info;
+               thread_t                                        thread;
 
                if (*task_info_count < TASK_THREAD_TIMES_INFO_COUNT) {
 
                if (*task_info_count < TASK_THREAD_TIMES_INFO_COUNT) {
-                   error = KERN_INVALID_ARGUMENT;
-                   break;
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
                }
 
                times_info = (task_thread_times_info_t) task_info_out;
                }
 
                times_info = (task_thread_times_info_t) task_info_out;
@@ -3926,10 +4865,11 @@ task_info(
 
 
                queue_iterate(&task->threads, thread, thread_t, task_threads) {
 
 
                queue_iterate(&task->threads, thread, thread_t, task_threads) {
-                       time_value_t    user_time, system_time;
+                       time_value_t    user_time, system_time;
 
 
-                       if (thread->options & TH_OPT_IDLE_THREAD)
+                       if (thread->options & TH_OPT_IDLE_THREAD) {
                                continue;
                                continue;
+                       }
 
                        thread_read_times(thread, &user_time, &system_time, NULL);
 
 
                        thread_read_times(thread, &user_time, &system_time, NULL);
 
@@ -3943,8 +4883,8 @@ task_info(
 
        case TASK_ABSOLUTETIME_INFO:
        {
 
        case TASK_ABSOLUTETIME_INFO:
        {
-               task_absolutetime_info_t        info;
-               thread_t                        thread;
+               task_absolutetime_info_t        info;
+               thread_t                        thread;
 
                if (*task_info_count < TASK_ABSOLUTETIME_INFO_COUNT) {
                        error = KERN_INVALID_ARGUMENT;
 
                if (*task_info_count < TASK_ABSOLUTETIME_INFO_COUNT) {
                        error = KERN_INVALID_ARGUMENT;
@@ -3959,11 +4899,12 @@ task_info(
                info->total_system = task->total_system_time;
 
                queue_iterate(&task->threads, thread, thread_t, task_threads) {
                info->total_system = task->total_system_time;
 
                queue_iterate(&task->threads, thread, thread_t, task_threads) {
-                       uint64_t        tval;
-                       spl_t           x;
+                       uint64_t        tval;
+                       spl_t           x;
 
 
-                       if (thread->options & TH_OPT_IDLE_THREAD)
+                       if (thread->options & TH_OPT_IDLE_THREAD) {
                                continue;
                                continue;
+                       }
 
                        x = splsched();
                        thread_lock(thread);
 
                        x = splsched();
                        thread_lock(thread);
@@ -4002,7 +4943,7 @@ task_info(
                 * size of the expected result structure.
                 */
 #define TASK_LEGACY_DYLD_INFO_COUNT \
                 * size of the expected result structure.
                 */
 #define TASK_LEGACY_DYLD_INFO_COUNT \
-               offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
+               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;
 
                if (*task_info_count < TASK_LEGACY_DYLD_INFO_COUNT) {
                        error = KERN_INVALID_ARGUMENT;
@@ -4016,8 +4957,8 @@ task_info(
                /* 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) ?
                /* 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_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;
                        *task_info_count = TASK_DYLD_INFO_COUNT;
                } else {
                        *task_info_count = TASK_LEGACY_DYLD_INFO_COUNT;
@@ -4051,12 +4992,12 @@ task_info(
 
        case TASK_KERNELMEMORY_INFO:
        {
 
        case TASK_KERNELMEMORY_INFO:
        {
-               task_kernelmemory_info_t        tkm_info;
-               ledger_amount_t                 credit, debit;
+               task_kernelmemory_info_t        tkm_info;
+               ledger_amount_t                 credit, debit;
 
                if (*task_info_count < TASK_KERNELMEMORY_INFO_COUNT) {
 
                if (*task_info_count < TASK_KERNELMEMORY_INFO_COUNT) {
-                  error = KERN_INVALID_ARGUMENT;
-                  break;
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
                }
 
                tkm_info = (task_kernelmemory_info_t) task_info_out;
                }
 
                tkm_info = (task_kernelmemory_info_t) task_info_out;
@@ -4118,7 +5059,6 @@ task_info(
        /* OBSOLETE */
        case TASK_SCHED_FIFO_INFO:
        {
        /* OBSOLETE */
        case TASK_SCHED_FIFO_INFO:
        {
-
                if (*task_info_count < POLICY_FIFO_BASE_COUNT) {
                        error = KERN_INVALID_ARGUMENT;
                        break;
                if (*task_info_count < POLICY_FIFO_BASE_COUNT) {
                        error = KERN_INVALID_ARGUMENT;
                        break;
@@ -4131,7 +5071,7 @@ task_info(
        /* OBSOLETE */
        case TASK_SCHED_RR_INFO:
        {
        /* OBSOLETE */
        case TASK_SCHED_RR_INFO:
        {
-               policy_rr_base_t        rr_base;
+               policy_rr_base_t        rr_base;
                uint32_t quantum_time;
                uint64_t quantum_ns;
 
                uint32_t quantum_time;
                uint64_t quantum_ns;
 
@@ -4151,7 +5091,7 @@ task_info(
 
                quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
                absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
 
                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;
                rr_base->quantum = (uint32_t)(quantum_ns / 1000 / 1000);
 
                *task_info_count = POLICY_RR_BASE_COUNT;
@@ -4161,7 +5101,7 @@ task_info(
        /* OBSOLETE */
        case TASK_SCHED_TIMESHARE_INFO:
        {
        /* OBSOLETE */
        case TASK_SCHED_TIMESHARE_INFO:
        {
-               policy_timeshare_base_t ts_base;
+               policy_timeshare_base_t ts_base;
 
                if (*task_info_count < POLICY_TIMESHARE_BASE_COUNT) {
                        error = KERN_INVALID_ARGUMENT;
 
                if (*task_info_count < POLICY_TIMESHARE_BASE_COUNT) {
                        error = KERN_INVALID_ARGUMENT;
@@ -4183,11 +5123,11 @@ task_info(
 
        case TASK_SECURITY_TOKEN:
        {
 
        case TASK_SECURITY_TOKEN:
        {
-               security_token_t        *sec_token_p;
+               security_token_t        *sec_token_p;
 
                if (*task_info_count < TASK_SECURITY_TOKEN_COUNT) {
 
                if (*task_info_count < TASK_SECURITY_TOKEN_COUNT) {
-                   error = KERN_INVALID_ARGUMENT;
-                   break;
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
                }
 
                sec_token_p = (security_token_t *) task_info_out;
                }
 
                sec_token_p = (security_token_t *) task_info_out;
@@ -4197,14 +5137,14 @@ task_info(
                *task_info_count = TASK_SECURITY_TOKEN_COUNT;
                break;
        }
                *task_info_count = TASK_SECURITY_TOKEN_COUNT;
                break;
        }
-            
+
        case TASK_AUDIT_TOKEN:
        {
        case TASK_AUDIT_TOKEN:
        {
-               audit_token_t   *audit_token_p;
+               audit_token_t   *audit_token_p;
 
                if (*task_info_count < TASK_AUDIT_TOKEN_COUNT) {
 
                if (*task_info_count < TASK_AUDIT_TOKEN_COUNT) {
-                   error = KERN_INVALID_ARGUMENT;
-                   break;
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
                }
 
                audit_token_p = (audit_token_t *) task_info_out;
                }
 
                audit_token_p = (audit_token_t *) task_info_out;
@@ -4214,25 +5154,25 @@ task_info(
                *task_info_count = TASK_AUDIT_TOKEN_COUNT;
                break;
        }
                *task_info_count = TASK_AUDIT_TOKEN_COUNT;
                break;
        }
-            
+
        case TASK_SCHED_INFO:
                error = KERN_INVALID_ARGUMENT;
                break;
 
        case TASK_EVENTS_INFO:
        {
        case TASK_SCHED_INFO:
                error = KERN_INVALID_ARGUMENT;
                break;
 
        case TASK_EVENTS_INFO:
        {
-               task_events_info_t      events_info;
-               thread_t                        thread;
+               task_events_info_t      events_info;
+               thread_t                        thread;
 
                if (*task_info_count < TASK_EVENTS_INFO_COUNT) {
 
                if (*task_info_count < TASK_EVENTS_INFO_COUNT) {
-                  error = KERN_INVALID_ARGUMENT;
-                  break;
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
                }
 
                events_info = (task_events_info_t) task_info_out;
 
 
                }
 
                events_info = (task_events_info_t) task_info_out;
 
 
-               events_info->faults = task->faults;
+               events_info->faults = (int32_t) MIN(counter_load(&task->faults), INT32_MAX);
                events_info->pageins = task->pageins;
                events_info->cow_faults = task->cow_faults;
                events_info->messages_sent = task->messages_sent;
                events_info->pageins = task->pageins;
                events_info->cow_faults = task->cow_faults;
                events_info->messages_sent = task->messages_sent;
@@ -4243,7 +5183,7 @@ task_info(
                events_info->csw = task->c_switch;
 
                queue_iterate(&task->threads, thread, thread_t, task_threads) {
                events_info->csw = task->c_switch;
 
                queue_iterate(&task->threads, thread, thread_t, task_threads) {
-                       events_info->csw           += thread->c_switch;
+                       events_info->csw           += thread->c_switch;
                        events_info->syscalls_mach += thread->syscalls_mach;
                        events_info->syscalls_unix += thread->syscalls_unix;
                }
                        events_info->syscalls_mach += thread->syscalls_mach;
                        events_info->syscalls_unix += thread->syscalls_unix;
                }
@@ -4255,8 +5195,8 @@ task_info(
        case TASK_AFFINITY_TAG_INFO:
        {
                if (*task_info_count < TASK_AFFINITY_TAG_INFO_COUNT) {
        case TASK_AFFINITY_TAG_INFO:
        {
                if (*task_info_count < TASK_AFFINITY_TAG_INFO_COUNT) {
-                   error = KERN_INVALID_ARGUMENT;
-                   break;
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
                }
 
                error = task_affinity_info(task, task_info_out, task_info_count);
                }
 
                error = task_affinity_info(task, task_info_out, task_info_count);
@@ -4269,7 +5209,7 @@ task_info(
                        break;
                }
 
                        break;
                }
 
-               task_power_info_locked(task, (task_power_info_t)task_info_out, NULL, NULL);
+               task_power_info_locked(task, (task_power_info_t)task_info_out, NULL, NULL, NULL);
                break;
        }
 
                break;
        }
 
@@ -4280,32 +5220,73 @@ task_info(
                        break;
                }
                task_power_info_v2_t tpiv2 = (task_power_info_v2_t) task_info_out;
                        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);
+               task_power_info_locked(task, &tpiv2->cpu_energy, &tpiv2->gpu_energy, tpiv2, NULL);
                break;
        }
 
        case TASK_VM_INFO:
        case TASK_VM_INFO_PURGEABLE:
        {
                break;
        }
 
        case TASK_VM_INFO:
        case TASK_VM_INFO_PURGEABLE:
        {
-               task_vm_info_t          vm_info;
-               vm_map_t                map;
+               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_min_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) {
 
                if (*task_info_count < TASK_VM_INFO_REV0_COUNT) {
-                   error = KERN_INVALID_ARGUMENT;
-                   break;
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
                }
 
                vm_info = (task_vm_info_t)task_info_out;
 
                }
 
                vm_info = (task_vm_info_t)task_info_out;
 
-               if (task == kernel_task) {
+               /*
+                * Do not hold both the task and map locks,
+                * so convert the task lock into a map reference,
+                * drop the task lock, then lock the map.
+                */
+               if (is_kernel_task) {
                        map = kernel_map;
                        map = kernel_map;
-                       /* no lock */
+                       task_unlock(task);
+                       /* no lock, no reference */
                } else {
                        map = task->map;
                } else {
                        map = task->map;
+                       vm_map_reference(map);
+                       task_unlock(task);
                        vm_map_lock_read(map);
                }
 
                        vm_map_lock_read(map);
                }
 
-               vm_info->virtual_size = (typeof(vm_info->virtual_size))map->size;
+               vm_info->virtual_size = (typeof(vm_info->virtual_size))vm_map_adjusted_size(map);
                vm_info->region_count = map->hdr.nentries;
                vm_info->page_size = vm_map_page_size(map);
 
                vm_info->region_count = map->hdr.nentries;
                vm_info->page_size = vm_map_page_size(map);
 
@@ -4332,7 +5313,7 @@ task_info(
                vm_info->purgeable_volatile_pmap = 0;
                vm_info->purgeable_volatile_resident = 0;
                vm_info->purgeable_volatile_virtual = 0;
                vm_info->purgeable_volatile_pmap = 0;
                vm_info->purgeable_volatile_resident = 0;
                vm_info->purgeable_volatile_virtual = 0;
-               if (task == kernel_task) {
+               if (is_kernel_task) {
                        /*
                         * We do not maintain the detailed stats for the
                         * kernel_pmap, so just count everything as
                        /*
                         * We do not maintain the detailed stats for the
                         * kernel_pmap, so just count everything as
@@ -4349,14 +5330,14 @@ task_info(
                         * when the system is under memory pressure.
                         */
                        vm_info->internal -= (VM_PAGE_COMPRESSOR_COUNT *
                         * when the system is under memory pressure.
                         */
                        vm_info->internal -= (VM_PAGE_COMPRESSOR_COUNT *
-                                             PAGE_SIZE);
+                           PAGE_SIZE);
                } else {
                } 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;
+                       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(
 
                        if (flavor == TASK_VM_INFO_PURGEABLE) {
                                kr = vm_map_query_volatile(
@@ -4368,33 +5349,138 @@ task_info(
                                        &volatile_compressed_pmap_size);
                                if (kr == KERN_SUCCESS) {
                                        vm_info->purgeable_volatile_pmap =
                                        &volatile_compressed_pmap_size);
                                if (kr == KERN_SUCCESS) {
                                        vm_info->purgeable_volatile_pmap =
-                                               volatile_pmap_size;
+                                           volatile_pmap_size;
                                        if (radar_20146450) {
                                        if (radar_20146450) {
-                                       vm_info->compressed -=
-                                               volatile_compressed_pmap_size;
+                                               vm_info->compressed -=
+                                                   volatile_compressed_pmap_size;
                                        }
                                        vm_info->purgeable_volatile_resident =
                                        }
                                        vm_info->purgeable_volatile_resident =
-                                               volatile_resident_size;
+                                           volatile_resident_size;
                                        vm_info->purgeable_volatile_virtual =
                                        vm_info->purgeable_volatile_virtual =
-                                               volatile_virtual_size;
+                                           volatile_virtual_size;
                                }
                        }
                }
                *task_info_count = TASK_VM_INFO_REV0_COUNT;
 
                                }
                        }
                }
                *task_info_count = TASK_VM_INFO_REV0_COUNT;
 
+               if (original_task_info_count >= TASK_VM_INFO_REV2_COUNT) {
+                       /* must be captured while we still have the map lock */
+                       vm_info->min_address = map->min_offset;
+                       vm_info->max_address = map->max_offset;
+               }
+
+               /*
+                * Done with vm map things, can drop the map lock and reference,
+                * and take the task lock back.
+                *
+                * Re-validate that the task didn't die on us.
+                */
+               if (!is_kernel_task) {
+                       vm_map_unlock_read(map);
+                       vm_map_deallocate(map);
+               }
+               map = VM_MAP_NULL;
+
+               task_lock(task);
+
+               if ((task != current_task()) && (!task->active)) {
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
+               }
+
                if (original_task_info_count >= TASK_VM_INFO_REV1_COUNT) {
                        vm_info->phys_footprint =
                if (original_task_info_count >= TASK_VM_INFO_REV1_COUNT) {
                        vm_info->phys_footprint =
-                               (mach_vm_size_t) get_task_phys_footprint(task);
+                           (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) {
                        *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;
+                       /* data was captured above */
                        *task_info_count = TASK_VM_INFO_REV2_COUNT;
                }
 
                        *task_info_count = TASK_VM_INFO_REV2_COUNT;
                }
 
-               if (task != kernel_task) {
-                       vm_map_unlock_read(map);
+               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;
                }
 
                break;
                }
 
                break;
@@ -4402,16 +5488,16 @@ task_info(
 
        case TASK_WAIT_STATE_INFO:
        {
 
        case TASK_WAIT_STATE_INFO:
        {
-               /* 
-                * Deprecated flavor. Currently allowing some results until all users 
+               /*
+                * Deprecated flavor. Currently allowing some results until all users
                 * stop calling it. The results may not be accurate.
                 * stop calling it. The results may not be accurate.
-         */
-               task_wait_state_info_t  wait_state_info;
+                */
+               task_wait_state_info_t  wait_state_info;
                uint64_t total_sfi_ledger_val = 0;
 
                if (*task_info_count < TASK_WAIT_STATE_INFO_COUNT) {
                uint64_t total_sfi_ledger_val = 0;
 
                if (*task_info_count < TASK_WAIT_STATE_INFO_COUNT) {
-                  error = KERN_INVALID_ARGUMENT;
-                  break;
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
                }
 
                wait_state_info = (task_wait_state_info_t) task_info_out;
                }
 
                wait_state_info = (task_wait_state_info_t) task_info_out;
@@ -4423,23 +5509,23 @@ task_info(
                int i, prev_lentry = -1;
                int64_t  val_credit, val_debit;
 
                int i, prev_lentry = -1;
                int64_t  val_credit, val_debit;
 
-               for (i = 0; i < MAX_SFI_CLASS_ID; i++){
-                       val_credit =0;
+               for (i = 0; i < MAX_SFI_CLASS_ID; i++) {
+                       val_credit = 0;
                        /*
                        /*
-                        * checking with prev_lentry != entry ensures adjacent classes 
+                        * 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] &&
                         * 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)) {
+                           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 */
                                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; 
+               wait_state_info->total_wait_sfi_state_time = total_sfi_ledger_val;
                *task_info_count = TASK_WAIT_STATE_INFO_COUNT;
 
                break;
                *task_info_count = TASK_WAIT_STATE_INFO_COUNT;
 
                break;
@@ -4447,121 +5533,418 @@ task_info(
        case TASK_VM_INFO_PURGEABLE_ACCOUNT:
        {
 #if DEVELOPMENT || DEBUG
        case TASK_VM_INFO_PURGEABLE_ACCOUNT:
        {
 #if DEVELOPMENT || DEBUG
-               pvm_account_info_t      acnt_info;
+               pvm_account_info_t      acnt_info;
 
                if (*task_info_count < PVM_ACCOUNT_INFO_COUNT) {
                        error = KERN_INVALID_ARGUMENT;
                        break;
                }
 
 
                if (*task_info_count < PVM_ACCOUNT_INFO_COUNT) {
                        error = KERN_INVALID_ARGUMENT;
                        break;
                }
 
-               if (task_info_out == NULL) {
-                       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_read_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_read(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;
+}
+
+/*
+ * Routine: task_dyld_process_info_update_helper
+ *
+ * Release send rights in release_ports.
+ *
+ * If no active ports found in task's dyld notifier array, unset the magic value
+ * in user space to indicate so.
+ *
+ * Condition:
+ *      task's itk_lock is locked, and is unlocked upon return.
+ *      Global g_dyldinfo_mtx is locked, and is unlocked upon return.
+ */
+void
+task_dyld_process_info_update_helper(
+       task_t                  task,
+       size_t                  active_count,
+       vm_map_address_t        magic_addr,    /* a userspace address */
+       ipc_port_t             *release_ports,
+       size_t                  release_count)
+{
+       void *notifiers_ptr = NULL;
+
+       assert(release_count <= DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT);
+
+       if (active_count == 0) {
+               assert(task->itk_dyld_notify != NULL);
+               notifiers_ptr = task->itk_dyld_notify;
+               task->itk_dyld_notify = NULL;
+               itk_unlock(task);
+
+               kfree(notifiers_ptr, (vm_size_t)sizeof(ipc_port_t) * DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT);
+               (void)copyoutmap_atomic32(task->map, MACH_PORT_NULL, magic_addr); /* unset magic */
+       } else {
+               itk_unlock(task);
+               (void)copyoutmap_atomic32(task->map, (mach_port_name_t)DYLD_PROCESS_INFO_NOTIFY_MAGIC,
+                   magic_addr);     /* reset magic */
+       }
+
+       lck_mtx_unlock(&g_dyldinfo_mtx);
+
+       for (size_t i = 0; i < release_count; i++) {
+               ipc_port_release_send(release_ports[i]);
+       }
+}
+
+/*
+ * Routine: task_dyld_process_info_notify_register
+ *
+ * Insert a send right to target task's itk_dyld_notify array. Allocate kernel
+ * memory for the array if it's the first port to be registered. Also cleanup
+ * any dead rights found in the array.
+ *
+ * Consumes sright if returns KERN_SUCCESS, otherwise MIG will destroy it.
+ *
+ * Args:
+ *     task:   Target task for the registration.
+ *     sright: A send right.
+ *
+ * Returns:
+ *     KERN_SUCCESS: Registration succeeded.
+ *     KERN_INVALID_TASK: task is invalid.
+ *     KERN_INVALID_RIGHT: sright is invalid.
+ *     KERN_DENIED: Security policy denied this call.
+ *     KERN_RESOURCE_SHORTAGE: Kernel memory allocation failed.
+ *     KERN_NO_SPACE: No available notifier port slot left for this task.
+ *     KERN_RIGHT_EXISTS: The notifier port is already registered and active.
+ *
+ *     Other error code see task_info().
+ *
+ * See Also:
+ *     task_dyld_process_info_notify_get_trap() in mach_kernelrpc.c
+ */
+kern_return_t
+task_dyld_process_info_notify_register(
+       task_t                  task,
+       ipc_port_t              sright)
+{
+       struct task_dyld_info dyld_info;
+       mach_msg_type_number_t info_count = TASK_DYLD_INFO_COUNT;
+       ipc_port_t release_ports[DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT];
+       uint32_t release_count = 0, active_count = 0;
+       mach_vm_address_t ports_addr; /* a user space address */
+       kern_return_t kr;
+       boolean_t right_exists = false;
+       ipc_port_t *notifiers_ptr = NULL;
+       ipc_port_t *portp;
+
+       if (task == TASK_NULL || task == kernel_task) {
+               return KERN_INVALID_TASK;
+       }
+
+       if (!IP_VALID(sright)) {
+               return KERN_INVALID_RIGHT;
+       }
+
+#if CONFIG_MACF
+       if (mac_task_check_dyld_process_info_notify_register()) {
+               return KERN_DENIED;
+       }
+#endif
+
+       kr = task_info(task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &info_count);
+       if (kr) {
+               return kr;
+       }
 
 
-               acnt_info = (pvm_account_info_t) task_info_out;
+       if (dyld_info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32) {
+               ports_addr = (mach_vm_address_t)(dyld_info.all_image_info_addr +
+                   offsetof(struct user32_dyld_all_image_infos, notifyMachPorts));
+       } else {
+               ports_addr = (mach_vm_address_t)(dyld_info.all_image_info_addr +
+                   offsetof(struct user64_dyld_all_image_infos, notifyMachPorts));
+       }
 
 
-               error = vm_purgeable_account(task, acnt_info);
+       if (task->itk_dyld_notify == NULL) {
+               notifiers_ptr = (ipc_port_t *)
+                   kalloc_flags(sizeof(ipc_port_t) * DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT, Z_ZERO);
+               if (!notifiers_ptr) {
+                       return KERN_RESOURCE_SHORTAGE;
+               }
+       }
 
 
-               *task_info_count = PVM_ACCOUNT_INFO_COUNT;
+       lck_mtx_lock(&g_dyldinfo_mtx);
+       itk_lock(task);
 
 
-               break;
-#else /* DEVELOPMENT || DEBUG */
-               error = KERN_NOT_SUPPORTED;
-               break;
-#endif /* DEVELOPMENT || DEBUG */
+       if (task->itk_dyld_notify == NULL) {
+               task->itk_dyld_notify = notifiers_ptr;
+               notifiers_ptr = NULL;
        }
        }
-       case TASK_FLAGS_INFO:
-       {
-               task_flags_info_t               flags_info;
 
 
-               if (*task_info_count < TASK_FLAGS_INFO_COUNT) {
-                   error = KERN_INVALID_ARGUMENT;
-                   break;
+       assert(task->itk_dyld_notify != NULL);
+       /* First pass: clear dead names and check for duplicate registration */
+       for (int slot = 0; slot < DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT; slot++) {
+               portp = &task->itk_dyld_notify[slot];
+               if (*portp != IPC_PORT_NULL && !ip_active(*portp)) {
+                       release_ports[release_count++] = *portp;
+                       *portp = IPC_PORT_NULL;
+               } else if (*portp == sright) {
+                       /* the port is already registered and is active */
+                       right_exists = true;
                }
 
                }
 
-               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);
+               if (*portp != IPC_PORT_NULL) {
+                       active_count++;
+               }
+       }
 
 
-               *task_info_count = TASK_FLAGS_INFO_COUNT;
-               break;
+       if (right_exists) {
+               /* skip second pass */
+               kr = KERN_RIGHT_EXISTS;
+               goto out;
        }
 
        }
 
-       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;
+       /* Second pass: register the port */
+       kr = KERN_NO_SPACE;
+       for (int slot = 0; slot < DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT; slot++) {
+               portp = &task->itk_dyld_notify[slot];
+               if (*portp == IPC_PORT_NULL) {
+                       *portp = sright;
+                       active_count++;
+                       kr = KERN_SUCCESS;
                        break;
                }
                        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;
-               }
-               
-               dbg_info->suspend_count = task->suspend_count;
+out:
+       assert(active_count > 0);
 
 
-               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_dyld_process_info_update_helper(task, active_count,
+           (vm_map_address_t)ports_addr, release_ports, release_count);
+       /* itk_lock, g_dyldinfo_mtx are unlocked upon return */
+
+       if (notifiers_ptr) {
+               kfree(notifiers_ptr, sizeof(ipc_port_t) * DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT);
        }
 
        }
 
-       task_unlock(task);
-       return (error);
+       return kr;
 }
 
 /*
 }
 
 /*
- * task_info_from_user
+ * Routine: task_dyld_process_info_notify_deregister
  *
  *
- * 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.
+ * Remove a send right in target task's itk_dyld_notify array matching the receive
+ * right name passed in. Deallocate kernel memory for the array if it's the last port to
+ * be deregistered, or all ports have died. Also cleanup any dead rights found in the array.
  *
  *
- * In the case of TASK_DYLD_INFO, we require the more
- * privileged task_port not the less-privileged task_name_port.
+ * Does not consume any reference.
  *
  *
+ * Args:
+ *     task: Target task for the deregistration.
+ *     rcv_name: The name denoting the receive right in caller's space.
+ *
+ * Returns:
+ *     KERN_SUCCESS: A matching entry found and degistration succeeded.
+ *     KERN_INVALID_TASK: task is invalid.
+ *     KERN_INVALID_NAME: name is invalid.
+ *     KERN_DENIED: Security policy denied this call.
+ *     KERN_FAILURE: A matching entry is not found.
+ *     KERN_INVALID_RIGHT: The name passed in does not represent a valid rcv right.
+ *
+ *     Other error code see task_info().
+ *
+ * See Also:
+ *     task_dyld_process_info_notify_get_trap() in mach_kernelrpc.c
  */
 kern_return_t
  */
 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;
+task_dyld_process_info_notify_deregister(
+       task_t                  task,
+       mach_port_name_t        rcv_name)
+{
+       struct task_dyld_info dyld_info;
+       mach_msg_type_number_t info_count = TASK_DYLD_INFO_COUNT;
+       ipc_port_t release_ports[DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT];
+       uint32_t release_count = 0, active_count = 0;
+       boolean_t port_found = false;
+       mach_vm_address_t ports_addr; /* a user space address */
+       ipc_port_t sright;
+       kern_return_t kr;
+       ipc_port_t *portp;
 
 
-       if (flavor == TASK_DYLD_INFO)
-               task = convert_port_to_task(task_port);
-       else
-               task = convert_port_to_task_name(task_port);
+       if (task == TASK_NULL || task == kernel_task) {
+               return KERN_INVALID_TASK;
+       }
 
 
-       ret = task_info(task, flavor, task_info_out, task_info_count);
+       if (!MACH_PORT_VALID(rcv_name)) {
+               return KERN_INVALID_NAME;
+       }
 
 
-       task_deallocate(task);
+#if CONFIG_MACF
+       if (mac_task_check_dyld_process_info_notify_register()) {
+               return KERN_DENIED;
+       }
+#endif
 
 
-       return ret;
+       kr = task_info(task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &info_count);
+       if (kr) {
+               return kr;
+       }
+
+       if (dyld_info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32) {
+               ports_addr = (mach_vm_address_t)(dyld_info.all_image_info_addr +
+                   offsetof(struct user32_dyld_all_image_infos, notifyMachPorts));
+       } else {
+               ports_addr = (mach_vm_address_t)(dyld_info.all_image_info_addr +
+                   offsetof(struct user64_dyld_all_image_infos, notifyMachPorts));
+       }
+
+       kr = ipc_port_translate_receive(current_space(), rcv_name, &sright); /* does not produce port ref */
+       if (kr) {
+               return KERN_INVALID_RIGHT;
+       }
+
+       ip_reference(sright);
+       ip_unlock(sright);
+
+       assert(sright != IPC_PORT_NULL);
+
+       lck_mtx_lock(&g_dyldinfo_mtx);
+       itk_lock(task);
+
+       if (task->itk_dyld_notify == NULL) {
+               itk_unlock(task);
+               lck_mtx_unlock(&g_dyldinfo_mtx);
+               ip_release(sright);
+               return KERN_FAILURE;
+       }
+
+       for (int slot = 0; slot < DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT; slot++) {
+               portp = &task->itk_dyld_notify[slot];
+               if (*portp == sright) {
+                       release_ports[release_count++] = *portp;
+                       *portp = IPC_PORT_NULL;
+                       port_found = true;
+               } else if ((*portp != IPC_PORT_NULL && !ip_active(*portp))) {
+                       release_ports[release_count++] = *portp;
+                       *portp = IPC_PORT_NULL;
+               }
+
+               if (*portp != IPC_PORT_NULL) {
+                       active_count++;
+               }
+       }
+
+       task_dyld_process_info_update_helper(task, active_count,
+           (vm_map_address_t)ports_addr, release_ports, release_count);
+       /* itk_lock, g_dyldinfo_mtx are unlocked upon return */
+
+       ip_release(sright);
+
+       return port_found ? KERN_SUCCESS : KERN_FAILURE;
 }
 
 }
 
-/* 
+/*
  *     task_power_info
  *
  *     Returns power stats for the task.
  *     task_power_info
  *
  *     Returns power stats for the task.
@@ -4569,32 +5952,36 @@ task_info_from_user(
  */
 void
 task_power_info_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)
+       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;
+       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,
 
        task_lock_assert_owned(task);
 
        ledger_get_entries(task->ledger, task_ledgers.interrupt_wakeups,
-               (ledger_amount_t *)&info->task_interrupt_wakeups, &tmp);
+           (ledger_amount_t *)&info->task_interrupt_wakeups, &tmp);
        ledger_get_entries(task->ledger, task_ledgers.platform_idle_wakeups,
        ledger_get_entries(task->ledger, task_ledgers.platform_idle_wakeups,
-               (ledger_amount_t *)&info->task_platform_idle_wakeups, &tmp);
+           (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;
 
        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 defined(__arm__) || defined(__arm64__)
        if (infov2) {
                infov2->task_energy = task->task_energy;
        }
        if (infov2) {
                infov2->task_energy = task->task_energy;
        }
-#endif
+#endif /* defined(__arm__) || defined(__arm64__) */
 
        if (ginfo) {
                ginfo->task_gpu_utilisation = task->task_gpu_ns;
 
        if (ginfo) {
                ginfo->task_gpu_utilisation = task->task_gpu_ns;
@@ -4606,11 +5993,12 @@ task_power_info_locked(
        }
 
        queue_iterate(&task->threads, thread, thread_t, task_threads) {
        }
 
        queue_iterate(&task->threads, thread, thread_t, task_threads) {
-               uint64_t        tval;
-               spl_t           x;
+               uint64_t        tval;
+               spl_t           x;
 
 
-               if (thread->options & TH_OPT_IDLE_THREAD)
+               if (thread->options & TH_OPT_IDLE_THREAD) {
                        continue;
                        continue;
+               }
 
                x = splsched();
                thread_lock(thread);
 
                x = splsched();
                thread_lock(thread);
@@ -4618,11 +6006,11 @@ task_power_info_locked(
                info->task_timer_wakeups_bin_1 += thread->thread_timer_wakeups_bin_1;
                info->task_timer_wakeups_bin_2 += thread->thread_timer_wakeups_bin_2;
 
                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 defined(__arm__) || defined(__arm64__)
                if (infov2) {
                        infov2->task_energy += ml_energy_stat(thread);
                }
                if (infov2) {
                        infov2->task_energy += ml_energy_stat(thread);
                }
-#endif
+#endif /* defined(__arm__) || defined(__arm64__) */
 
                tval = timer_grab(&thread->user_timer);
                info->total_user += tval;
 
                tval = timer_grab(&thread->user_timer);
                info->total_user += tval;
@@ -4641,15 +6029,23 @@ task_power_info_locked(
                        info->total_user += tval;
                }
 
                        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 (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
  *     task_gpu_utilisation
  *
  *     Returns the total gpu time used by the all the threads of the task
@@ -4657,10 +6053,10 @@ task_power_info_locked(
  */
 uint64_t
 task_gpu_utilisation(
  */
 uint64_t
 task_gpu_utilisation(
-       task_t  task)
+       task_t  task)
 {
        uint64_t gpu_time = 0;
 {
        uint64_t gpu_time = 0;
-#if !CONFIG_EMBEDDED
+#if defined(__x86_64__)
        thread_t thread;
 
        task_lock(task);
        thread_t thread;
 
        task_lock(task);
@@ -4676,14 +6072,14 @@ task_gpu_utilisation(
        }
 
        task_unlock(task);
        }
 
        task_unlock(task);
-#else /* CONFIG_EMBEDDED */
+#else /* defined(__x86_64__) */
        /* silence compiler warning */
        (void)task;
        /* silence compiler warning */
        (void)task;
-#endif /* !CONFIG_EMBEDDED */
+#endif /* defined(__x86_64__) */
        return gpu_time;
 }
 
        return gpu_time;
 }
 
-/* 
+/*
  *     task_energy
  *
  *     Returns the total energy used by the all the threads of the task
  *     task_energy
  *
  *     Returns the total energy used by the all the threads of the task
@@ -4691,7 +6087,7 @@ task_gpu_utilisation(
  */
 uint64_t
 task_energy(
  */
 uint64_t
 task_energy(
-       task_t  task)
+       task_t  task)
 {
        uint64_t energy = 0;
        thread_t thread;
 {
        uint64_t energy = 0;
        thread_t thread;
@@ -4712,21 +6108,43 @@ task_energy(
        return energy;
 }
 
        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)
 {
 
 uint64_t
 task_cpu_ptime(
        __unused task_t  task)
 {
-    return 0;
+       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(
 
 /* 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,
+       task_t  task,
        uint64_t *eqos_stats,
        uint64_t *rqos_stats)
 {
        uint64_t *eqos_stats,
        uint64_t *rqos_stats)
 {
@@ -4769,11 +6187,12 @@ task_update_cpu_time_qos_stats(
 
 kern_return_t
 task_purgable_info(
 
 kern_return_t
 task_purgable_info(
-       task_t                  task,
-       task_purgable_info_t    *stats)
+       task_t                  task,
+       task_purgable_info_t    *stats)
 {
 {
-       if (task == TASK_NULL || stats == NULL)
+       if (task == TASK_NULL || stats == NULL) {
                return KERN_INVALID_ARGUMENT;
                return KERN_INVALID_ARGUMENT;
+       }
        /* Take task reference */
        task_reference(task);
        vm_purgeable_stats((vm_purgeable_info_t)stats, task);
        /* Take task reference */
        task_reference(task);
        vm_purgeable_stats((vm_purgeable_info_t)stats, task);
@@ -4784,26 +6203,26 @@ task_purgable_info(
 
 void
 task_vtimer_set(
 
 void
 task_vtimer_set(
-       task_t          task,
-       integer_t       which)
+       task_t          task,
+       integer_t       which)
 {
 {
-       thread_t        thread;
-       spl_t           x;
+       thread_t        thread;
+       spl_t           x;
 
        task_lock(task);
 
        task->vtimers |= which;
 
        switch (which) {
 
        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);
        case TASK_VTIMER_USER:
                queue_iterate(&task->threads, thread, thread_t, task_threads) {
                        x = splsched();
                        thread_lock(thread);
-                       if (thread->precise_user_kernel_time)
+                       if (thread->precise_user_kernel_time) {
                                thread->vtimer_user_save = timer_grab(&thread->user_timer);
                                thread->vtimer_user_save = timer_grab(&thread->user_timer);
-                       else
+                       } else {
                                thread->vtimer_user_save = timer_grab(&thread->system_timer);
                                thread->vtimer_user_save = timer_grab(&thread->system_timer);
+                       }
                        thread_unlock(thread);
                        splx(x);
                }
                        thread_unlock(thread);
                        splx(x);
                }
@@ -4837,8 +6256,8 @@ task_vtimer_set(
 
 void
 task_vtimer_clear(
 
 void
 task_vtimer_clear(
-       task_t          task,
-       integer_t       which)
+       task_t          task,
+       integer_t       which)
 {
        assert(task == current_task());
 
 {
        assert(task == current_task());
 
@@ -4851,15 +6270,15 @@ task_vtimer_clear(
 
 void
 task_vtimer_update(
 
 void
 task_vtimer_update(
-__unused
-       task_t          task,
-       integer_t       which,
-       uint32_t        *microsecs)
+       __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;
+       thread_t        thread = current_thread();
+       uint32_t        tdelt = 0;
+       clock_sec_t     secs = 0;
+       uint64_t        tsum;
 
        assert(task == current_task());
 
 
        assert(task == current_task());
 
@@ -4873,14 +6292,13 @@ __unused
        }
 
        switch (which) {
        }
 
        switch (which) {
-
        case TASK_VTIMER_USER:
                if (thread->precise_user_kernel_time) {
                        tdelt = (uint32_t)timer_delta(&thread->user_timer,
        case TASK_VTIMER_USER:
                if (thread->precise_user_kernel_time) {
                        tdelt = (uint32_t)timer_delta(&thread->user_timer,
-                                                               &thread->vtimer_user_save);
+                           &thread->vtimer_user_save);
                } else {
                        tdelt = (uint32_t)timer_delta(&thread->system_timer,
                } else {
                        tdelt = (uint32_t)timer_delta(&thread->system_timer,
-                                                               &thread->vtimer_user_save);
+                           &thread->vtimer_user_save);
                }
                absolutetime_to_microtime(tdelt, &secs, microsecs);
                break;
                }
                absolutetime_to_microtime(tdelt, &secs, microsecs);
                break;
@@ -4891,8 +6309,9 @@ __unused
                tdelt = (uint32_t)(tsum - thread->vtimer_prof_save);
                absolutetime_to_microtime(tdelt, &secs, microsecs);
                /* if the time delta is smaller than a usec, ignore */
                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)
+               if (*microsecs != 0) {
                        thread->vtimer_prof_save = tsum;
                        thread->vtimer_prof_save = tsum;
+               }
                break;
 
        case TASK_VTIMER_RLIM:
                break;
 
        case TASK_VTIMER_RLIM:
@@ -4915,11 +6334,11 @@ __unused
  */
 kern_return_t
 task_assign(
  */
 kern_return_t
 task_assign(
-       __unused task_t         task,
-       __unused processor_set_t        new_pset,
-       __unused boolean_t      assign_threads)
+       __unused task_t         task,
+       __unused processor_set_t        new_pset,
+       __unused boolean_t      assign_threads)
 {
 {
-       return(KERN_FAILURE);
+       return KERN_FAILURE;
 }
 
 /*
 }
 
 /*
@@ -4929,10 +6348,10 @@ task_assign(
  */
 kern_return_t
 task_assign_default(
  */
 kern_return_t
 task_assign_default(
-       task_t          task,
-       boolean_t       assign_threads)
+       task_t          task,
+       boolean_t       assign_threads)
 {
 {
-    return (task_assign(task, &pset0, assign_threads));
+       return task_assign(task, &pset0, assign_threads);
 }
 
 /*
 }
 
 /*
@@ -4942,11 +6361,12 @@ task_assign_default(
  */
 kern_return_t
 task_get_assignment(
  */
 kern_return_t
 task_get_assignment(
-       task_t          task,
-       processor_set_t *pset)
+       task_t          task,
+       processor_set_t *pset)
 {
 {
-       if (!task || !task->active)
+       if (!task || !task->active) {
                return KERN_FAILURE;
                return KERN_FAILURE;
+       }
 
        *pset = &pset0;
 
 
        *pset = &pset0;
 
@@ -4955,57 +6375,57 @@ task_get_assignment(
 
 uint64_t
 get_task_dispatchqueue_offset(
 
 uint64_t
 get_task_dispatchqueue_offset(
-               task_t          task)
+       task_t          task)
 {
        return task->dispatchqueue_offset;
 }
 
 /*
 {
        return task->dispatchqueue_offset;
 }
 
 /*
- *     task_policy
+ *      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
  *
  *     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. 
+ *     processor set. Change contained threads if requested.
  */
 kern_return_t
 task_policy(
  */
 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)
+       __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);
+       return KERN_FAILURE;
 }
 
 /*
  *     task_set_policy
  *
 }
 
 /*
  *     task_set_policy
  *
- *     Set scheduling policy and parameters, both base and limit, for 
+ *     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(
  *     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)
+       __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);
+       return KERN_FAILURE;
 }
 
 kern_return_t
 task_set_ras_pc(
 }
 
 kern_return_t
 task_set_ras_pc(
-       __unused task_t task,
-       __unused vm_offset_t    pc,
-       __unused vm_offset_t    endpc)
+       __unused task_t task,
+       __unused vm_offset_t    pc,
+       __unused vm_offset_t    endpc)
 {
        return KERN_FAILURE;
 }
 {
        return KERN_FAILURE;
 }
@@ -5020,29 +6440,29 @@ task_synchronizer_destroy_all(task_t task)
 }
 
 /*
 }
 
 /*
- * Install default (machine-dependent) initial thread state 
+ * 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()
  */
  * 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 
+kern_return_t
 task_set_state(
 task_set_state(
-       task_t task, 
-       int flavor, 
-       thread_state_t 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) {
        mach_msg_type_number_t state_count)
 {
        kern_return_t ret;
 
        if (task == TASK_NULL) {
-               return (KERN_INVALID_ARGUMENT);
+               return KERN_INVALID_ARGUMENT;
        }
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
        }
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        ret = machine_task_set_state(task, flavor, state, state_count);
        }
 
        ret = machine_task_set_state(task, flavor, state, state_count);
@@ -5052,28 +6472,28 @@ task_set_state(
 }
 
 /*
 }
 
 /*
- * Examine the default (machine-dependent) initial thread state 
+ * 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().
  */
  * 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 
+kern_return_t
 task_get_state(
 task_get_state(
-       task_t  task, 
-       int     flavor,
+       task_t  task,
+       int     flavor,
        thread_state_t state,
        mach_msg_type_number_t *state_count)
 {
        kern_return_t ret;
 
        if (task == TASK_NULL) {
        thread_state_t state,
        mach_msg_type_number_t *state_count)
 {
        kern_return_t ret;
 
        if (task == TASK_NULL) {
-               return (KERN_INVALID_ARGUMENT);
+               return KERN_INVALID_ARGUMENT;
        }
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
        }
 
        task_lock(task);
 
        if (!task->active) {
                task_unlock(task);
-               return (KERN_FAILURE);
+               return KERN_FAILURE;
        }
 
        ret = machine_task_get_state(task, flavor, state, state_count);
        }
 
        ret = machine_task_get_state(task, flavor, state, state_count);
@@ -5083,15 +6503,16 @@ task_get_state(
 }
 
 
 }
 
 
-static kern_return_t __attribute__((noinline,not_tail_called))
+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
 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
+       if (1 == proc_selfpid()) {
+               return KERN_NOT_SUPPORTED;              // initproc is immune
+       }
 #endif
        mach_exception_data_type_t codes[EXCEPTION_CODE_MAX] = {
                [0] = code,
 #endif
        mach_exception_data_type_t codes[EXCEPTION_CODE_MAX] = {
                [0] = code,
@@ -5123,19 +6544,19 @@ task_violated_guard(
 boolean_t
 task_get_memlimit_is_active(task_t task)
 {
 boolean_t
 task_get_memlimit_is_active(task_t task)
 {
-       assert (task != NULL);
+       assert(task != NULL);
 
        if (task->memlimit_is_active == 1) {
 
        if (task->memlimit_is_active == 1) {
-               return(TRUE);
+               return TRUE;
        } else {
        } else {
-               return (FALSE);
+               return FALSE;
        }
 }
 
 void
 task_set_memlimit_is_active(task_t task, boolean_t memlimit_is_active)
 {
        }
 }
 
 void
 task_set_memlimit_is_active(task_t task, boolean_t memlimit_is_active)
 {
-       assert (task != NULL);
+       assert(task != NULL);
 
        if (memlimit_is_active) {
                task->memlimit_is_active = 1;
 
        if (memlimit_is_active) {
                task->memlimit_is_active = 1;
@@ -5146,20 +6567,20 @@ task_set_memlimit_is_active(task_t task, boolean_t memlimit_is_active)
 
 boolean_t
 task_get_memlimit_is_fatal(task_t task)
 
 boolean_t
 task_get_memlimit_is_fatal(task_t task)
-{      
+{
        assert(task != NULL);
 
        if (task->memlimit_is_fatal == 1) {
        assert(task != NULL);
 
        if (task->memlimit_is_fatal == 1) {
-               return(TRUE);
-       } else {
-               return(FALSE);
-       }
+               return TRUE;
+       } else {
+               return FALSE;
+       }
 }
 
 void
 task_set_memlimit_is_fatal(task_t task, boolean_t memlimit_is_fatal)
 {
 }
 
 void
 task_set_memlimit_is_fatal(task_t task, boolean_t memlimit_is_fatal)
 {
-       assert (task != NULL);
+       assert(task != NULL);
 
        if (memlimit_is_fatal) {
                task->memlimit_is_fatal = 1;
 
        if (memlimit_is_fatal) {
                task->memlimit_is_fatal = 1;
@@ -5175,7 +6596,7 @@ task_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active)
 
        assert(task == current_task());
 
 
        assert(task == current_task());
 
-       /* 
+       /*
         * Returns true, if task has already triggered an exc_resource exception.
         */
 
         * Returns true, if task has already triggered an exc_resource exception.
         */
 
@@ -5185,7 +6606,7 @@ task_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active)
                triggered = (task->memlimit_inactive_exc_resource ? TRUE : FALSE);
        }
 
                triggered = (task->memlimit_inactive_exc_resource ? TRUE : FALSE);
        }
 
-       return(triggered);
+       return triggered;
 }
 
 void
 }
 
 void
@@ -5210,10 +6631,10 @@ task_mark_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active)
 void __attribute__((noinline))
 PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb, boolean_t is_fatal)
 {
 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];
+       task_t                                          task            = current_task();
+       int                                                     pid         = 0;
+       const char                                      *procname       = "unknown";
+       mach_exception_data_type_t      code[EXCEPTION_CODE_MAX];
        boolean_t send_sync_exc_resource = FALSE;
 
 #ifdef MACH_BSD
        boolean_t send_sync_exc_resource = FALSE;
 
 #ifdef MACH_BSD
@@ -5234,10 +6655,10 @@ PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb,
 #endif
 #if CONFIG_COREDUMP
        if (hwm_user_cores) {
 #endif
 #if CONFIG_COREDUMP
        if (hwm_user_cores) {
-               int                             error;
-               uint64_t                starttime, end;
-               clock_sec_t             secs = 0;
-               uint32_t                microsecs = 0;
+               int                             error;
+               uint64_t                starttime, end;
+               clock_sec_t             secs = 0;
+               uint32_t                microsecs = 0;
 
                starttime = mach_absolute_time();
                /*
 
                starttime = mach_absolute_time();
                /*
@@ -5249,20 +6670,20 @@ PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb,
                        printf("couldn't take coredump of %s[%d]: %d\n", procname, pid, error);
                }
                /*
                        printf("couldn't take coredump of %s[%d]: %d\n", procname, pid, error);
                }
                /*
-               * coredump() leaves the task suspended.
-               */
+                * coredump() leaves the task suspended.
+                */
                task_resume_internal(current_task());
 
                end = mach_absolute_time();
                absolutetime_to_microtime(end - starttime, &secs, &microsecs);
                printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
                task_resume_internal(current_task());
 
                end = mach_absolute_time();
                absolutetime_to_microtime(end - starttime, &secs, &microsecs);
                printf("coredump of %s[%d] taken in %d secs %d microsecs\n",
-                      proc_name_address(current_task()->bsd_info), pid, (int)secs, microsecs);
+                   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 "
        }
 #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);
+                   "supressed by a boot-arg.\n", procname, pid, max_footprint_mb);
                return;
        }
 
                return;
        }
 
@@ -5297,10 +6718,10 @@ PROC_CROSSED_HIGH_WATERMARK__SEND_EXC_RESOURCE_AND_SUSPEND(int max_footprint_mb,
        } else {
                if (audio_active) {
                        printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
        } else {
                if (audio_active) {
                        printf("process %s[%d] crossed memory high watermark (%d MB); EXC_RESOURCE "
-                       "supressed due to audio playback.\n", procname, pid, max_footprint_mb);
+                           "supressed due to audio playback.\n", procname, pid, max_footprint_mb);
                } else {
                        task_enqueue_exception_with_corpse(task, EXC_RESOURCE,
                } else {
                        task_enqueue_exception_with_corpse(task, EXC_RESOURCE,
-                               code, EXCEPTION_CODE_MAX, NULL);
+                           code, EXCEPTION_CODE_MAX, NULL);
                }
        }
 
                }
        }
 
@@ -5320,7 +6741,7 @@ task_footprint_exceeded(int warning, __unused const void *param0, __unused const
 {
        ledger_amount_t max_footprint, max_footprint_mb;
        task_t task;
 {
        ledger_amount_t max_footprint, max_footprint_mb;
        task_t task;
-        boolean_t is_warning;
+       boolean_t is_warning;
        boolean_t memlimit_is_active;
        boolean_t memlimit_is_fatal;
 
        boolean_t memlimit_is_active;
        boolean_t memlimit_is_fatal;
 
@@ -5329,19 +6750,19 @@ task_footprint_exceeded(int warning, __unused const void *param0, __unused const
                 * Task memory limits only provide a warning on the way up.
                 */
                return;
                 * Task memory limits only provide a warning on the way up.
                 */
                return;
-        } else if (warning == LEDGER_WARNING_ROSE_ABOVE) {
-                /*
-                 * This task is in danger of violating a memory limit,
-                 * It has exceeded a percentage level of the limit.
-                 */
-                is_warning = TRUE;
-        } else {
-                /*
-                 * The task has exceeded the physical footprint limit.
-                 * This is not a warning but a true limit violation.
-                 */
-                is_warning = FALSE;
-        }
+       } else if (warning == LEDGER_WARNING_ROSE_ABOVE) {
+               /*
+                * This task is in danger of violating a memory limit,
+                * It has exceeded a percentage level of the limit.
+                */
+               is_warning = TRUE;
+       } else {
+               /*
+                * The task has exceeded the physical footprint limit.
+                * This is not a warning but a true limit violation.
+                */
+               is_warning = FALSE;
+       }
 
        task = current_task();
 
 
        task = current_task();
 
@@ -5380,7 +6801,7 @@ task_set_phys_footprint_limit(
        boolean_t memlimit_is_fatal;
 
        if ((error = proc_check_footprint_priv())) {
        boolean_t memlimit_is_fatal;
 
        if ((error = proc_check_footprint_priv())) {
-               return (KERN_NO_ACCESS);
+               return KERN_NO_ACCESS;
        }
 
        /*
        }
 
        /*
@@ -5411,7 +6832,7 @@ task_convert_phys_footprint_limit(
                /* nothing to convert */
                *converted_limit_mb = limit_mb;
        }
                /* nothing to convert */
                *converted_limit_mb = limit_mb;
        }
-       return (KERN_SUCCESS);
+       return KERN_SUCCESS;
 }
 
 
 }
 
 
@@ -5423,17 +6844,22 @@ task_set_phys_footprint_limit_internal(
        boolean_t memlimit_is_active,
        boolean_t memlimit_is_fatal)
 {
        boolean_t memlimit_is_active,
        boolean_t memlimit_is_fatal)
 {
-       ledger_amount_t old;
+       ledger_amount_t old;
+       kern_return_t ret;
 
 
-       ledger_get_limit(task->ledger, task_ledgers.phys_footprint, &old);
+       ret = ledger_get_limit(task->ledger, task_ledgers.phys_footprint, &old);
+
+       if (ret != KERN_SUCCESS) {
+               return ret;
+       }
 
 
-       /* 
+       /*
         * Check that limit >> 20 will not give an "unexpected" 32-bit
         * result. There are, however, implicit assumptions that -1 mb limit
         * equates to LEDGER_LIMIT_INFINITY.
         */
        assert(((old & 0xFFF0000000000000LL) == 0) || (old == LEDGER_LIMIT_INFINITY));
         * Check that limit >> 20 will not give an "unexpected" 32-bit
         * result. There are, however, implicit assumptions that -1 mb limit
         * equates to LEDGER_LIMIT_INFINITY.
         */
        assert(((old & 0xFFF0000000000000LL) == 0) || (old == LEDGER_LIMIT_INFINITY));
-       
+
        if (old_limit_mb) {
                *old_limit_mb = (int)(old >> 20);
        }
        if (old_limit_mb) {
                *old_limit_mb = (int)(old >> 20);
        }
@@ -5443,19 +6869,19 @@ task_set_phys_footprint_limit_internal(
                 * Caller wishes to remove the limit.
                 */
                ledger_set_limit(task->ledger, task_ledgers.phys_footprint,
                 * Caller wishes to remove the limit.
                 */
                ledger_set_limit(task->ledger, task_ledgers.phys_footprint,
-                                max_task_footprint ? max_task_footprint : LEDGER_LIMIT_INFINITY,
-                                max_task_footprint ? max_task_footprint_warning_level : 0);
+                   max_task_footprint ? max_task_footprint : LEDGER_LIMIT_INFINITY,
+                   max_task_footprint ? (uint8_t)max_task_footprint_warning_level : 0);
 
                task_lock(task);
                task_set_memlimit_is_active(task, memlimit_is_active);
                task_set_memlimit_is_fatal(task, memlimit_is_fatal);
                task_unlock(task);
 
 
                task_lock(task);
                task_set_memlimit_is_active(task, memlimit_is_active);
                task_set_memlimit_is_fatal(task, memlimit_is_fatal);
                task_unlock(task);
 
-               return (KERN_SUCCESS);
+               return KERN_SUCCESS;
        }
 
 #ifdef CONFIG_NOMONITORS
        }
 
 #ifdef CONFIG_NOMONITORS
-       return (KERN_SUCCESS);
+       return KERN_SUCCESS;
 #endif /* CONFIG_NOMONITORS */
 
        task_lock(task);
 #endif /* CONFIG_NOMONITORS */
 
        task_lock(task);
@@ -5467,42 +6893,47 @@ task_set_phys_footprint_limit_internal(
                 * memlimit state is not changing
                 */
                task_unlock(task);
                 * memlimit state is not changing
                 */
                task_unlock(task);
-               return(KERN_SUCCESS);
+               return KERN_SUCCESS;
        }
 
        task_set_memlimit_is_active(task, memlimit_is_active);
        task_set_memlimit_is_fatal(task, memlimit_is_fatal);
 
        ledger_set_limit(task->ledger, task_ledgers.phys_footprint,
        }
 
        task_set_memlimit_is_active(task, memlimit_is_active);
        task_set_memlimit_is_fatal(task, memlimit_is_fatal);
 
        ledger_set_limit(task->ledger, task_ledgers.phys_footprint,
-               (ledger_amount_t)new_limit_mb << 20, PHYS_FOOTPRINT_WARNING_LEVEL);
+           (ledger_amount_t)new_limit_mb << 20, PHYS_FOOTPRINT_WARNING_LEVEL);
 
 
-        if (task == current_task()) {
-                ledger_check_new_balance(current_thread(), task->ledger,
-                                         task_ledgers.phys_footprint);
-        }
+       if (task == current_task()) {
+               ledger_check_new_balance(current_thread(), task->ledger,
+                   task_ledgers.phys_footprint);
+       }
 
        task_unlock(task);
 
 
        task_unlock(task);
 
-       return (KERN_SUCCESS);
+       return KERN_SUCCESS;
 }
 
 kern_return_t
 }
 
 kern_return_t
-task_get_phys_footprint_limit(         
+task_get_phys_footprint_limit(
        task_t task,
        int *limit_mb)
 {
        task_t task,
        int *limit_mb)
 {
-       ledger_amount_t limit;
-    
-       ledger_get_limit(task->ledger, task_ledgers.phys_footprint, &limit);
-       /* 
+       ledger_amount_t limit;
+       kern_return_t ret;
+
+       ret = ledger_get_limit(task->ledger, task_ledgers.phys_footprint, &limit);
+       if (ret != KERN_SUCCESS) {
+               return ret;
+       }
+
+       /*
         * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
         * result. There are, however, implicit assumptions that -1 mb limit
         * equates to LEDGER_LIMIT_INFINITY.
         */
        assert(((limit & 0xFFF0000000000000LL) == 0) || (limit == LEDGER_LIMIT_INFINITY));
        *limit_mb = (int)(limit >> 20);
         * Check that limit >> 20 will not give an "unexpected" signed, 32-bit
         * result. There are, however, implicit assumptions that -1 mb limit
         * equates to LEDGER_LIMIT_INFINITY.
         */
        assert(((limit & 0xFFF0000000000000LL) == 0) || (limit == LEDGER_LIMIT_INFINITY));
        *limit_mb = (int)(limit >> 20);
-       
-       return (KERN_SUCCESS);
+
+       return KERN_SUCCESS;
 }
 #else /* CONFIG_MEMORYSTATUS */
 kern_return_t
 }
 #else /* CONFIG_MEMORYSTATUS */
 kern_return_t
@@ -5511,15 +6942,15 @@ task_set_phys_footprint_limit(
        __unused int new_limit_mb,
        __unused int *old_limit_mb)
 {
        __unused int new_limit_mb,
        __unused int *old_limit_mb)
 {
-       return (KERN_FAILURE);
+       return KERN_FAILURE;
 }
 
 kern_return_t
 }
 
 kern_return_t
-task_get_phys_footprint_limit(         
+task_get_phys_footprint_limit(
        __unused task_t task,
        __unused int *limit_mb)
 {
        __unused task_t task,
        __unused int *limit_mb)
 {
-       return (KERN_FAILURE);
+       return KERN_FAILURE;
 }
 #endif /* CONFIG_MEMORYSTATUS */
 
 }
 #endif /* CONFIG_MEMORYSTATUS */
 
@@ -5534,51 +6965,150 @@ task_set_thread_limit(task_t task, uint16_t thread_limit)
        }
 }
 
        }
 }
 
+#if XNU_TARGET_OS_OSX
+boolean_t
+task_has_system_version_compat_enabled(task_t task)
+{
+       boolean_t enabled = FALSE;
+
+       task_lock(task);
+       enabled = (task->t_flags & TF_SYS_VERSION_COMPAT);
+       task_unlock(task);
+
+       return enabled;
+}
+
+void
+task_set_system_version_compat_enabled(task_t task, boolean_t enable_system_version_compat)
+{
+       assert(task == current_task());
+       assert(task != kernel_task);
+
+       task_lock(task);
+       if (enable_system_version_compat) {
+               task->t_flags |= TF_SYS_VERSION_COMPAT;
+       } else {
+               task->t_flags &= ~TF_SYS_VERSION_COMPAT;
+       }
+       task_unlock(task);
+}
+#endif /* XNU_TARGET_OS_OSX */
+
 /*
  * We need to export some functions to other components that
  * are currently implemented in macros within the osfmk
  * component.  Just export them as functions of the same name.
  */
 /*
  * We need to export some functions to other components that
  * are currently implemented in macros within the osfmk
  * component.  Just export them as functions of the same name.
  */
-boolean_t is_kerneltask(task_t t)
+boolean_t
+is_kerneltask(task_t t)
 {
 {
-       if (t == kernel_task)
-               return (TRUE);
+       if (t == kernel_task) {
+               return TRUE;
+       }
 
 
-       return (FALSE);
+       return FALSE;
 }
 
 }
 
-boolean_t is_corpsetask(task_t t)
+boolean_t
+is_corpsetask(task_t t)
 {
 {
-       return (task_is_a_corpse(t));
+       return task_is_a_corpse(t);
 }
 
 #undef current_task
 task_t current_task(void);
 }
 
 #undef current_task
 task_t current_task(void);
-task_t current_task(void)
+task_t
+current_task(void)
 {
 {
-       return (current_task_fast());
+       return current_task_fast();
 }
 
 #undef task_reference
 void task_reference(task_t task);
 void
 task_reference(
 }
 
 #undef task_reference
 void task_reference(task_t task);
 void
 task_reference(
-       task_t          task)
+       task_t          task)
 {
 {
-       if (task != TASK_NULL)
+       if (task != TASK_NULL) {
                task_reference_internal(task);
                task_reference_internal(task);
+       }
 }
 
 /* defined in bsd/kern/kern_prot.c */
 extern int get_audit_token_pid(audit_token_t *audit_token);
 
 }
 
 /* defined in bsd/kern/kern_prot.c */
 extern int get_audit_token_pid(audit_token_t *audit_token);
 
-int task_pid(task_t task)
-{
-       if (task)
-               return get_audit_token_pid(&task->audit_token);
-       return -1;
+int
+task_pid(task_t task)
+{
+       if (task) {
+               return get_audit_token_pid(&task->audit_token);
+       }
+       return -1;
+}
+
+#if __has_feature(ptrauth_calls)
+/*
+ * Get the shared region id and jop signing key for the task.
+ * The function will allocate a kalloc buffer and return
+ * it to caller, the caller needs to free it. This is used
+ * for getting the information via task port.
+ */
+char *
+task_get_vm_shared_region_id_and_jop_pid(task_t task, uint64_t *jop_pid)
+{
+       size_t len;
+       char *shared_region_id = NULL;
+
+       task_lock(task);
+       if (task->shared_region_id == NULL) {
+               task_unlock(task);
+               return NULL;
+       }
+       len = strlen(task->shared_region_id) + 1;
+
+       /* don't hold task lock while allocating */
+       task_unlock(task);
+       shared_region_id = kheap_alloc(KHEAP_DATA_BUFFERS, len, Z_WAITOK);
+       task_lock(task);
+
+       if (task->shared_region_id == NULL) {
+               task_unlock(task);
+               kheap_free(KHEAP_DATA_BUFFERS, shared_region_id, len);
+               return NULL;
+       }
+       assert(len == strlen(task->shared_region_id) + 1);      /* should never change */
+       strlcpy(shared_region_id, task->shared_region_id, len);
+       task_unlock(task);
+
+       /* find key from its auth pager */
+       if (jop_pid != NULL) {
+               *jop_pid = shared_region_find_key(shared_region_id);
+       }
+
+       return shared_region_id;
+}
+
+/*
+ * set the shared region id for a task
+ */
+void
+task_set_shared_region_id(task_t task, char *id)
+{
+       char *old_id;
+
+       task_lock(task);
+       old_id = task->shared_region_id;
+       task->shared_region_id = id;
+       task->shared_region_auth_remapped = FALSE;
+       task_unlock(task);
+
+       /* free any pre-existing shared region id */
+       if (old_id != NULL) {
+               shared_region_key_dealloc(old_id);
+               kheap_free(KHEAP_DATA_BUFFERS, old_id, strlen(old_id) + 1);
+       }
 }
 }
-
+#endif /* __has_feature(ptrauth_calls) */
 
 /*
  * This routine finds a thread in a task by its unique id
 
 /*
  * This routine finds a thread in a task by its unique id
@@ -5617,10 +7147,11 @@ task_findtid(task_t task, uint64_t tid)
 
        task_unlock(task);
 
 
        task_unlock(task);
 
-       return (found_thread);
+       return found_thread;
 }
 
 }
 
-int pid_from_task(task_t task)
+int
+pid_from_task(task_t task)
 {
        int pid = -1;
 
 {
        int pid = -1;
 
@@ -5660,8 +7191,8 @@ task_wakeups_monitor_ctl(task_t task, uint32_t *flags, int32_t *rate_hz)
 
        task_lock(task);
        if (*flags & WAKEMON_GET_PARAMS) {
 
        task_lock(task);
        if (*flags & WAKEMON_GET_PARAMS) {
-               ledger_amount_t limit;
-               uint64_t                period;
+               ledger_amount_t limit;
+               uint64_t                period;
 
                ledger_get_limit(ledger, task_ledgers.interrupt_wakeups, &limit);
                ledger_get_period(ledger, task_ledgers.interrupt_wakeups, &period);
 
                ledger_get_limit(ledger, task_ledgers.interrupt_wakeups, &limit);
                ledger_get_period(ledger, task_ledgers.interrupt_wakeups, &period);
@@ -5683,7 +7214,7 @@ task_wakeups_monitor_ctl(task_t task, uint32_t *flags, int32_t *rate_hz)
                /*
                 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
                 */
                /*
                 * If WAKEMON_GET_PARAMS is present in flags, all other flags are ignored.
                 */
-               task_unlock(task);
+               task_unlock(task);
                return KERN_SUCCESS;
        }
 
                return KERN_SUCCESS;
        }
 
@@ -5705,7 +7236,7 @@ task_wakeups_monitor_ctl(task_t task, uint32_t *flags, int32_t *rate_hz)
 
 #ifndef CONFIG_NOMONITORS
                ledger_set_limit(ledger, task_ledgers.interrupt_wakeups, *rate_hz * task_wakeups_monitor_interval,
 
 #ifndef CONFIG_NOMONITORS
                ledger_set_limit(ledger, task_ledgers.interrupt_wakeups, *rate_hz * task_wakeups_monitor_interval,
-                       task_wakeups_monitor_ustackshots_trigger_pct);
+                   (uint8_t)task_wakeups_monitor_ustackshots_trigger_pct);
                ledger_set_period(ledger, task_ledgers.interrupt_wakeups, task_wakeups_monitor_interval * NSEC_PER_SEC);
                ledger_enable_callback(ledger, task_ledgers.interrupt_wakeups);
 #endif /* CONFIG_NOMONITORS */
                ledger_set_period(ledger, task_ledgers.interrupt_wakeups, task_wakeups_monitor_interval * NSEC_PER_SEC);
                ledger_enable_callback(ledger, task_ledgers.interrupt_wakeups);
 #endif /* CONFIG_NOMONITORS */
@@ -5731,7 +7262,7 @@ void
 task_wakeups_rate_exceeded(int warning, __unused const void *param0, __unused const void *param1)
 {
        if (warning == LEDGER_WARNING_ROSE_ABOVE) {
 task_wakeups_rate_exceeded(int warning, __unused const void *param0, __unused const void *param1)
 {
        if (warning == LEDGER_WARNING_ROSE_ABOVE) {
-#if CONFIG_TELEMETRY           
+#if CONFIG_TELEMETRY
                /*
                 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
                 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
                /*
                 * This task is in danger of violating the wakeups monitor. Enable telemetry on this task
                 * so there are micro-stackshots available if and when EXC_RESOURCE is triggered.
@@ -5769,8 +7300,9 @@ SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
 
 #ifdef MACH_BSD
        pid = proc_selfpid();
 
 #ifdef MACH_BSD
        pid = proc_selfpid();
-       if (task->bsd_info != NULL)
+       if (task->bsd_info != NULL) {
                procname = proc_name_address(current_task()->bsd_info);
                procname = proc_name_address(current_task()->bsd_info);
+       }
 #endif
 
        ledger_get_entry_info(task->ledger, task_ledgers.interrupt_wakeups, &lei);
 #endif
 
        ledger_get_entry_info(task->ledger, task_ledgers.interrupt_wakeups, &lei);
@@ -5786,17 +7318,17 @@ SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
        fatal = task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON;
        trace_resource_violation(RMON_CPUWAKES_VIOLATED, &lei);
        os_log(OS_LOG_DEFAULT, "process %s[%d] caught waking the CPU %llu times "
        fatal = task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_FATAL_WAKEUPSMON;
        trace_resource_violation(RMON_CPUWAKES_VIOLATED, &lei);
        os_log(OS_LOG_DEFAULT, "process %s[%d] caught waking the CPU %llu times "
-              "over ~%llu seconds, averaging %llu wakes / second and "
-              "violating a %slimit of %llu wakes over %llu seconds.\n",
-              procname, pid,
-              lei.lei_balance, lei.lei_last_refill / NSEC_PER_SEC,
-                  lei.lei_last_refill == 0 ? 0 :
-                               (NSEC_PER_SEC * lei.lei_balance / lei.lei_last_refill),
-              fatal ? "FATAL " : "",
-                  lei.lei_limit, lei.lei_refill_period / NSEC_PER_SEC);
+           "over ~%llu seconds, averaging %llu wakes / second and "
+           "violating a %slimit of %llu wakes over %llu seconds.\n",
+           procname, pid,
+           lei.lei_balance, lei.lei_last_refill / NSEC_PER_SEC,
+           lei.lei_last_refill == 0 ? 0 :
+           (NSEC_PER_SEC * lei.lei_balance / lei.lei_last_refill),
+           fatal ? "FATAL " : "",
+           lei.lei_limit, lei.lei_refill_period / NSEC_PER_SEC);
 
        kr = send_resource_violation(send_cpu_wakes_violation, task, &lei,
 
        kr = send_resource_violation(send_cpu_wakes_violation, task, &lei,
-                                    fatal ? kRNFatalLimitFlag : 0);
+           fatal ? kRNFatalLimitFlag : 0);
        if (kr) {
                printf("send_resource_violation(CPU wakes, ...): error %#x\n", kr);
        }
        if (kr) {
                printf("send_resource_violation(CPU wakes, ...): error %#x\n", kr);
        }
@@ -5804,28 +7336,28 @@ SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
 #ifdef EXC_RESOURCE_MONITORS
        if (disable_exc_resource) {
                printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
 #ifdef EXC_RESOURCE_MONITORS
        if (disable_exc_resource) {
                printf("process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
-                       "supressed by a boot-arg\n", procname, pid);
+                   "supressed by a boot-arg\n", procname, pid);
                return;
        }
        if (audio_active) {
                os_log(OS_LOG_DEFAULT, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
                return;
        }
        if (audio_active) {
                os_log(OS_LOG_DEFAULT, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
-                      "supressed due to audio playback\n", procname, pid);
+                   "supressed due to audio playback\n", procname, pid);
                return;
        }
        if (lei.lei_last_refill == 0) {
                os_log(OS_LOG_DEFAULT, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
                return;
        }
        if (lei.lei_last_refill == 0) {
                os_log(OS_LOG_DEFAULT, "process %s[%d] caught causing excessive wakeups. EXC_RESOURCE "
-                      "supressed due to lei.lei_last_refill = 0 \n", procname, pid);
+                   "supressed due to lei.lei_last_refill = 0 \n", procname, pid);
        }
 
        code[0] = code[1] = 0;
        EXC_RESOURCE_ENCODE_TYPE(code[0], RESOURCE_TYPE_WAKEUPS);
        EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_WAKEUPS_MONITOR);
        EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code[0],
        }
 
        code[0] = code[1] = 0;
        EXC_RESOURCE_ENCODE_TYPE(code[0], RESOURCE_TYPE_WAKEUPS);
        EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_WAKEUPS_MONITOR);
        EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code[0],
-                           NSEC_PER_SEC * lei.lei_limit / lei.lei_refill_period);
+           NSEC_PER_SEC * lei.lei_limit / lei.lei_refill_period);
        EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code[0],
        EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code[0],
-                           lei.lei_last_refill);
+           lei.lei_last_refill);
        EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code[1],
        EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(code[1],
-                           NSEC_PER_SEC * lei.lei_balance / lei.lei_last_refill);
+           NSEC_PER_SEC * lei.lei_balance / lei.lei_last_refill);
        exception_triage(EXC_RESOURCE, code, EXCEPTION_CODE_MAX);
 #endif /* EXC_RESOURCE_MONITORS */
 
        exception_triage(EXC_RESOURCE, code, EXCEPTION_CODE_MAX);
 #endif /* EXC_RESOURCE_MONITORS */
 
@@ -5834,14 +7366,14 @@ SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MANY_WAKEUPS(void)
        }
 }
 
        }
 }
 
-static boolean_t 
-global_update_logical_writes(int64_t io_delta)
+static boolean_t
+global_update_logical_writes(int64_t io_delta, int64_t *global_write_count)
 {
        int64_t old_count, new_count;
        boolean_t needs_telemetry;
 {
        int64_t old_count, new_count;
        boolean_t needs_telemetry;
-       
+
        do {
        do {
-               new_count = old_count = global_logical_writes_count;
+               new_count = old_count = *global_write_count;
                new_count += io_delta;
                if (new_count >= io_telemetry_limit) {
                        new_count = 0;
                new_count += io_delta;
                if (new_count >= io_telemetry_limit) {
                        new_count = 0;
@@ -5849,45 +7381,110 @@ global_update_logical_writes(int64_t io_delta)
                } else {
                        needs_telemetry = FALSE;
                }
                } else {
                        needs_telemetry = FALSE;
                }
-       } while(!OSCompareAndSwap64(old_count, new_count, &global_logical_writes_count));
+       } while (!OSCompareAndSwap64(old_count, new_count, global_write_count));
        return needs_telemetry;
 }
 
        return needs_telemetry;
 }
 
-void task_update_logical_writes(task_t task, uint32_t io_size, int flags, void *vp)
+void
+task_update_physical_writes(__unused task_t task, __unused task_physical_write_flavor_t flavor, __unused uint64_t io_size, __unused task_balance_flags_t flags)
+{
+#if CONFIG_PHYS_WRITE_ACCT
+       if (!io_size) {
+               return;
+       }
+
+       /*
+        * task == NULL means that we have to update kernel_task ledgers
+        */
+       if (!task) {
+               task = kernel_task;
+       }
+
+       KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, VM_PHYS_WRITE_ACCT)) | DBG_FUNC_NONE,
+           task_pid(task), flavor, io_size, flags, 0);
+       DTRACE_IO4(physical_writes, struct task *, task, task_physical_write_flavor_t, flavor, uint64_t, io_size, task_balance_flags_t, flags);
+
+       if (flags & TASK_BALANCE_CREDIT) {
+               if (flavor == TASK_PHYSICAL_WRITE_METADATA) {
+                       OSAddAtomic64(io_size, (SInt64 *)&(task->task_fs_metadata_writes));
+                       ledger_credit_nocheck(task->ledger, task_ledgers.fs_metadata_writes, io_size);
+               }
+       } else if (flags & TASK_BALANCE_DEBIT) {
+               if (flavor == TASK_PHYSICAL_WRITE_METADATA) {
+                       OSAddAtomic64(-1 * io_size, (SInt64 *)&(task->task_fs_metadata_writes));
+                       ledger_debit_nocheck(task->ledger, task_ledgers.fs_metadata_writes, io_size);
+               }
+       }
+#endif /* CONFIG_PHYS_WRITE_ACCT */
+}
+
+void
+task_update_logical_writes(task_t task, uint32_t io_size, int flags, void *vp)
 {
        int64_t io_delta = 0;
 {
        int64_t io_delta = 0;
+       int64_t * global_counter_to_update;
        boolean_t needs_telemetry = FALSE;
        boolean_t needs_telemetry = FALSE;
+       boolean_t is_external_device = FALSE;
+       int ledger_to_update = 0;
+       struct task_writes_counters * writes_counters_to_update;
 
 
-       if ((!task) || (!io_size) || (!vp))
+       if ((!task) || (!io_size) || (!vp)) {
                return;
                return;
-       
-       KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, VM_DATA_WRITE)) | DBG_FUNC_NONE, 
-                                                       task_pid(task), io_size, flags, (uintptr_t)VM_KERNEL_ADDRPERM(vp), 0);
+       }
+
+       KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, VM_DATA_WRITE)) | DBG_FUNC_NONE,
+           task_pid(task), io_size, flags, (uintptr_t)VM_KERNEL_ADDRPERM(vp), 0);
        DTRACE_IO4(logical_writes, struct task *, task, uint32_t, io_size, int, flags, vnode *, vp);
        DTRACE_IO4(logical_writes, struct task *, task, uint32_t, io_size, int, flags, vnode *, vp);
-       switch(flags) {
-               case TASK_WRITE_IMMEDIATE:
-                       OSAddAtomic64(io_size, (SInt64 *)&(task->task_immediate_writes));
-                       ledger_credit(task->ledger, task_ledgers.logical_writes, io_size);
-                       break;
-               case TASK_WRITE_DEFERRED:
-                       OSAddAtomic64(io_size, (SInt64 *)&(task->task_deferred_writes));
-                       ledger_credit(task->ledger, task_ledgers.logical_writes, io_size);
-                       break;
-               case TASK_WRITE_INVALIDATED:
-                       OSAddAtomic64(io_size, (SInt64 *)&(task->task_invalidated_writes));
-                       ledger_debit(task->ledger, task_ledgers.logical_writes, io_size);
-                       break;
-               case TASK_WRITE_METADATA:
-                       OSAddAtomic64(io_size, (SInt64 *)&(task->task_metadata_writes));
-                       ledger_credit(task->ledger, task_ledgers.logical_writes, io_size);
-                       break;
+
+       // Is the drive backing this vnode internal or external to the system?
+       if (vnode_isonexternalstorage(vp) == false) {
+               global_counter_to_update = &global_logical_writes_count;
+               ledger_to_update = task_ledgers.logical_writes;
+               writes_counters_to_update = &task->task_writes_counters_internal;
+               is_external_device = FALSE;
+       } else {
+               global_counter_to_update = &global_logical_writes_to_external_count;
+               ledger_to_update = task_ledgers.logical_writes_to_external;
+               writes_counters_to_update = &task->task_writes_counters_external;
+               is_external_device = TRUE;
+       }
+
+       switch (flags) {
+       case TASK_WRITE_IMMEDIATE:
+               OSAddAtomic64(io_size, (SInt64 *)&(writes_counters_to_update->task_immediate_writes));
+               ledger_credit(task->ledger, ledger_to_update, io_size);
+               if (!is_external_device) {
+                       coalition_io_ledger_update(task, FLAVOR_IO_LOGICAL_WRITES, TRUE, io_size);
+               }
+               break;
+       case TASK_WRITE_DEFERRED:
+               OSAddAtomic64(io_size, (SInt64 *)&(writes_counters_to_update->task_deferred_writes));
+               ledger_credit(task->ledger, ledger_to_update, io_size);
+               if (!is_external_device) {
+                       coalition_io_ledger_update(task, FLAVOR_IO_LOGICAL_WRITES, TRUE, io_size);
+               }
+               break;
+       case TASK_WRITE_INVALIDATED:
+               OSAddAtomic64(io_size, (SInt64 *)&(writes_counters_to_update->task_invalidated_writes));
+               ledger_debit(task->ledger, ledger_to_update, io_size);
+               if (!is_external_device) {
+                       coalition_io_ledger_update(task, FLAVOR_IO_LOGICAL_WRITES, FALSE, io_size);
+               }
+               break;
+       case TASK_WRITE_METADATA:
+               OSAddAtomic64(io_size, (SInt64 *)&(writes_counters_to_update->task_metadata_writes));
+               ledger_credit(task->ledger, ledger_to_update, io_size);
+               if (!is_external_device) {
+                       coalition_io_ledger_update(task, FLAVOR_IO_LOGICAL_WRITES, TRUE, io_size);
+               }
+               break;
        }
 
        io_delta = (flags == TASK_WRITE_INVALIDATED) ? ((int64_t)io_size * -1ll) : ((int64_t)io_size);
        }
 
        io_delta = (flags == TASK_WRITE_INVALIDATED) ? ((int64_t)io_size * -1ll) : ((int64_t)io_size);
-       if (io_telemetry_limit != 0) {  
+       if (io_telemetry_limit != 0) {
                /* If io_telemetry_limit is 0, disable global updates and I/O telemetry */
                /* If io_telemetry_limit is 0, disable global updates and I/O telemetry */
-               needs_telemetry = global_update_logical_writes(io_delta);
-               if (needs_telemetry) { 
+               needs_telemetry = global_update_logical_writes(io_delta, global_counter_to_update);
+               if (needs_telemetry && !is_external_device) {
                        act_set_io_telemetry_ast(current_thread());
                }
        }
                        act_set_io_telemetry_ast(current_thread());
                }
        }
@@ -5903,22 +7500,15 @@ task_io_monitor_ctl(task_t task, uint32_t *flags)
 
        task_lock(task);
        if (*flags & IOMON_ENABLE) {
 
        task_lock(task);
        if (*flags & IOMON_ENABLE) {
-               /* Configure the physical I/O ledger */ 
+               /* Configure the physical I/O ledger */
                ledger_set_limit(ledger, task_ledgers.physical_writes, (task_iomon_limit_mb * 1024 * 1024), 0);
                ledger_set_period(ledger, task_ledgers.physical_writes, (task_iomon_interval_secs * NSEC_PER_SEC));
                ledger_set_limit(ledger, task_ledgers.physical_writes, (task_iomon_limit_mb * 1024 * 1024), 0);
                ledger_set_period(ledger, task_ledgers.physical_writes, (task_iomon_interval_secs * NSEC_PER_SEC));
-
-               /* Configure the logical I/O ledger */
-               ledger_set_limit(ledger, task_ledgers.logical_writes, (task_iomon_limit_mb * 1024 * 1024), 0);
-               ledger_set_period(ledger, task_ledgers.logical_writes, (task_iomon_interval_secs * NSEC_PER_SEC));
-               
        } else if (*flags & IOMON_DISABLE) {
                /*
                 * Caller wishes to disable I/O monitor on the task.
                 */
                ledger_disable_refill(ledger, task_ledgers.physical_writes);
                ledger_disable_callback(ledger, task_ledgers.physical_writes);
        } else if (*flags & IOMON_DISABLE) {
                /*
                 * Caller wishes to disable I/O monitor on the task.
                 */
                ledger_disable_refill(ledger, task_ledgers.physical_writes);
                ledger_disable_callback(ledger, task_ledgers.physical_writes);
-               ledger_disable_refill(ledger, task_ledgers.logical_writes);
-               ledger_disable_callback(ledger, task_ledgers.logical_writes);
        }
 
        task_unlock(task);
        }
 
        task_unlock(task);
@@ -5933,7 +7523,8 @@ task_io_rate_exceeded(int warning, const void *param0, __unused const void *para
        }
 }
 
        }
 }
 
-void __attribute__((noinline)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor)
+void __attribute__((noinline))
+SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO_MUCH_IO(int flavor)
 {
        int                             pid = 0;
        task_t                          task = current_task();
 {
        int                             pid = 0;
        task_t                          task = current_task();
@@ -5946,20 +7537,17 @@ void __attribute__((noinline)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO
 #ifdef MACH_BSD
        pid = proc_selfpid();
 #endif
 #ifdef MACH_BSD
        pid = proc_selfpid();
 #endif
-       /* 
-        * Get the ledger entry info. We need to do this before disabling the exception 
+       /*
+        * Get the ledger entry info. We need to do this before disabling the exception
         * to get correct values for all fields.
         */
         * to get correct values for all fields.
         */
-       switch(flavor) {
-               case FLAVOR_IO_PHYSICAL_WRITES:
-                       ledger_get_entry_info(task->ledger, task_ledgers.physical_writes, &lei);
-                       break;
-               case FLAVOR_IO_LOGICAL_WRITES:
-                       ledger_get_entry_info(task->ledger, task_ledgers.logical_writes, &lei);
-                       break;
+       switch (flavor) {
+       case FLAVOR_IO_PHYSICAL_WRITES:
+               ledger_get_entry_info(task->ledger, task_ledgers.physical_writes, &lei);
+               break;
        }
 
        }
 
-       
+
        /*
         * Disable the exception notification so we don't overwhelm
         * the listener with an endless stream of redundant exceptions.
        /*
         * Disable the exception notification so we don't overwhelm
         * the listener with an endless stream of redundant exceptions.
@@ -5972,7 +7560,7 @@ void __attribute__((noinline)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO
                trace_resource_violation(RMON_LOGWRITES_VIOLATED, &lei);
        }
        os_log(OS_LOG_DEFAULT, "process [%d] caught causing excessive I/O (flavor: %d). Task I/O: %lld MB. [Limit : %lld MB per %lld secs]\n",
                trace_resource_violation(RMON_LOGWRITES_VIOLATED, &lei);
        }
        os_log(OS_LOG_DEFAULT, "process [%d] caught causing excessive I/O (flavor: %d). Task I/O: %lld MB. [Limit : %lld MB per %lld secs]\n",
-               pid, flavor, (lei.lei_balance / (1024 * 1024)), (lei.lei_limit / (1024 * 1024)), (lei.lei_refill_period / NSEC_PER_SEC));
+           pid, flavor, (lei.lei_balance / (1024 * 1024)), (lei.lei_limit / (1024 * 1024)), (lei.lei_refill_period / NSEC_PER_SEC));
 
        kr = send_resource_violation(send_disk_writes_violation, task, &lei, kRNFlagsNone);
        if (kr) {
 
        kr = send_resource_violation(send_disk_writes_violation, task, &lei, kRNFlagsNone);
        if (kr) {
@@ -5988,47 +7576,53 @@ void __attribute__((noinline)) SENDING_NOTIFICATION__THIS_PROCESS_IS_CAUSING_TOO
        EXC_RESOURCE_IO_ENCODE_OBSERVED(code[1], (lei.lei_balance / (1024 * 1024)));
        exception_triage(EXC_RESOURCE, code, EXCEPTION_CODE_MAX);
 #endif /* EXC_RESOURCE_MONITORS */
        EXC_RESOURCE_IO_ENCODE_OBSERVED(code[1], (lei.lei_balance / (1024 * 1024)));
        exception_triage(EXC_RESOURCE, code, EXCEPTION_CODE_MAX);
 #endif /* EXC_RESOURCE_MONITORS */
-}      
+}
 
 /* Placeholders for the task set/get voucher interfaces */
 
 /* Placeholders for the task set/get voucher interfaces */
-kern_return_t 
+kern_return_t
 task_get_mach_voucher(
 task_get_mach_voucher(
-       task_t                  task,
+       task_t                  task,
        mach_voucher_selector_t __unused which,
        mach_voucher_selector_t __unused which,
-       ipc_voucher_t           *voucher)
+       ipc_voucher_t           *voucher)
 {
 {
-       if (TASK_NULL == task)
+       if (TASK_NULL == task) {
                return KERN_INVALID_TASK;
                return KERN_INVALID_TASK;
+       }
 
        *voucher = NULL;
        return KERN_SUCCESS;
 }
 
 
        *voucher = NULL;
        return KERN_SUCCESS;
 }
 
-kern_return_t 
+kern_return_t
 task_set_mach_voucher(
 task_set_mach_voucher(
-       task_t                  task,
-       ipc_voucher_t           __unused voucher)
+       task_t                  task,
+       ipc_voucher_t           __unused voucher)
 {
 {
-       if (TASK_NULL == task)
+       if (TASK_NULL == task) {
                return KERN_INVALID_TASK;
                return KERN_INVALID_TASK;
+       }
 
        return KERN_SUCCESS;
 }
 
 kern_return_t
 task_swap_mach_voucher(
 
        return KERN_SUCCESS;
 }
 
 kern_return_t
 task_swap_mach_voucher(
-       task_t                  task,
-       ipc_voucher_t           new_voucher,
-       ipc_voucher_t           *in_out_old_voucher)
+       __unused task_t         task,
+       __unused ipc_voucher_t  new_voucher,
+       ipc_voucher_t          *in_out_old_voucher)
 {
 {
-       if (TASK_NULL == task)
-               return KERN_INVALID_TASK;
-
-       *in_out_old_voucher = new_voucher;
-       return KERN_SUCCESS;
+       /*
+        * Currently this function is only called from a MIG generated
+        * routine which doesn't release the reference on the voucher
+        * addressed by in_out_old_voucher. To avoid leaking this reference,
+        * a call to release it has been added here.
+        */
+       ipc_voucher_release(*in_out_old_voucher);
+       return KERN_NOT_SUPPORTED;
 }
 
 }
 
-void task_set_gpu_denied(task_t task, boolean_t denied)
+void
+task_set_gpu_denied(task_t task, boolean_t denied)
 {
        task_lock(task);
 
 {
        task_lock(task);
 
@@ -6041,23 +7635,25 @@ void task_set_gpu_denied(task_t task, boolean_t denied)
        task_unlock(task);
 }
 
        task_unlock(task);
 }
 
-boolean_t task_is_gpu_denied(task_t task)
+boolean_t
+task_is_gpu_denied(task_t task)
 {
        /* We don't need the lock to read this flag */
        return (task->t_flags & TF_GPU_DENIED) ? TRUE : FALSE;
 }
 
 
 {
        /* We don't need the lock to read this flag */
        return (task->t_flags & TF_GPU_DENIED) ? TRUE : FALSE;
 }
 
 
-uint64_t get_task_memory_region_count(task_t task)
+uint64_t
+get_task_memory_region_count(task_t task)
 {
        vm_map_t map;
        map = (task == kernel_task) ? kernel_map: task->map;
 {
        vm_map_t map;
        map = (task == kernel_task) ? kernel_map: task->map;
-       return((uint64_t)get_map_nentries(map));
+       return (uint64_t)get_map_nentries(map);
 }
 
 static void
 kdebug_trace_dyld_internal(uint32_t base_code,
 }
 
 static void
 kdebug_trace_dyld_internal(uint32_t base_code,
-       struct dyld_kernel_image_info *info)
+    struct dyld_kernel_image_info *info)
 {
        static_assert(sizeof(info->uuid) >= 16);
 
 {
        static_assert(sizeof(info->uuid) >= 16);
 
@@ -6065,34 +7661,34 @@ kdebug_trace_dyld_internal(uint32_t base_code,
        uint64_t *uuid = (uint64_t *)&(info->uuid);
 
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
        uint64_t *uuid = (uint64_t *)&(info->uuid);
 
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
-               KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code), uuid[0],
-               uuid[1], info->load_addr,
-               (uint64_t)info->fsid.val[0] | ((uint64_t)info->fsid.val[1] << 32),
-               0);
+           KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code), uuid[0],
+           uuid[1], info->load_addr,
+           (uint64_t)info->fsid.val[0] | ((uint64_t)info->fsid.val[1] << 32),
+           0);
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
-               KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code + 1),
-               (uint64_t)info->fsobjid.fid_objno |
-               ((uint64_t)info->fsobjid.fid_generation << 32),
-               0, 0, 0, 0);
+           KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code + 1),
+           (uint64_t)info->fsobjid.fid_objno |
+           ((uint64_t)info->fsobjid.fid_generation << 32),
+           0, 0, 0, 0);
 #else /* defined(__LP64__) */
        uint32_t *uuid = (uint32_t *)&(info->uuid);
 
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
 #else /* defined(__LP64__) */
        uint32_t *uuid = (uint32_t *)&(info->uuid);
 
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
-               KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code + 2), uuid[0],
-               uuid[1], uuid[2], uuid[3], 0);
+           KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code + 2), uuid[0],
+           uuid[1], uuid[2], uuid[3], 0);
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
-               KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code + 3),
-               (uint32_t)info->load_addr, info->fsid.val[0], info->fsid.val[1],
-               info->fsobjid.fid_objno, 0);
+           KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code + 3),
+           (uint32_t)info->load_addr, info->fsid.val[0], info->fsid.val[1],
+           info->fsobjid.fid_objno, 0);
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
        KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
-               KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code + 4),
-               info->fsobjid.fid_generation, 0, 0, 0, 0);
+           KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, base_code + 4),
+           info->fsobjid.fid_generation, 0, 0, 0, 0);
 #endif /* !defined(__LP64__) */
 }
 
 static kern_return_t
 kdebug_trace_dyld(task_t task, uint32_t base_code,
 #endif /* !defined(__LP64__) */
 }
 
 static kern_return_t
 kdebug_trace_dyld(task_t task, uint32_t base_code,
-       vm_map_copy_t infos_copy, mach_msg_type_number_t infos_len)
+    vm_map_copy_t infos_copy, mach_msg_type_number_t infos_len)
 {
        kern_return_t kr;
        dyld_kernel_image_info_array_t infos;
 {
        kern_return_t kr;
        dyld_kernel_image_info_array_t infos;
@@ -6104,8 +7700,7 @@ kdebug_trace_dyld(task_t task, uint32_t base_code,
        }
 
        if (!kdebug_enable ||
        }
 
        if (!kdebug_enable ||
-               !kdebug_debugid_enabled(KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, 0)))
-       {
+           !kdebug_debugid_enabled(KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, 0))) {
                vm_map_copy_discard(infos_copy);
                return KERN_SUCCESS;
        }
                vm_map_copy_discard(infos_copy);
                return KERN_SUCCESS;
        }
@@ -6132,35 +7727,35 @@ kdebug_trace_dyld(task_t task, uint32_t base_code,
 
 kern_return_t
 task_register_dyld_image_infos(task_t task,
 
 kern_return_t
 task_register_dyld_image_infos(task_t task,
-                               dyld_kernel_image_info_array_t infos_copy,
-                               mach_msg_type_number_t infos_len)
+    dyld_kernel_image_info_array_t infos_copy,
+    mach_msg_type_number_t infos_len)
 {
        return kdebug_trace_dyld(task, DBG_DYLD_UUID_MAP_A,
 {
        return kdebug_trace_dyld(task, DBG_DYLD_UUID_MAP_A,
-               (vm_map_copy_t)infos_copy, infos_len);
+                  (vm_map_copy_t)infos_copy, infos_len);
 }
 
 kern_return_t
 task_unregister_dyld_image_infos(task_t task,
 }
 
 kern_return_t
 task_unregister_dyld_image_infos(task_t task,
-                                 dyld_kernel_image_info_array_t infos_copy,
-                                 mach_msg_type_number_t infos_len)
+    dyld_kernel_image_info_array_t infos_copy,
+    mach_msg_type_number_t infos_len)
 {
        return kdebug_trace_dyld(task, DBG_DYLD_UUID_UNMAP_A,
 {
        return kdebug_trace_dyld(task, DBG_DYLD_UUID_UNMAP_A,
-               (vm_map_copy_t)infos_copy, infos_len);
+                  (vm_map_copy_t)infos_copy, infos_len);
 }
 
 kern_return_t
 task_get_dyld_image_infos(__unused task_t task,
 }
 
 kern_return_t
 task_get_dyld_image_infos(__unused task_t task,
-                          __unused dyld_kernel_image_info_array_t * dyld_images,
-                          __unused mach_msg_type_number_t * dyld_imagesCnt)
+    __unused dyld_kernel_image_info_array_t * dyld_images,
+    __unused mach_msg_type_number_t * dyld_imagesCnt)
 {
        return KERN_NOT_SUPPORTED;
 }
 
 kern_return_t
 task_register_dyld_shared_cache_image_info(task_t task,
 {
        return KERN_NOT_SUPPORTED;
 }
 
 kern_return_t
 task_register_dyld_shared_cache_image_info(task_t task,
-                                           dyld_kernel_image_info_t cache_img,
-                                           __unused boolean_t no_cache,
-                                           __unused boolean_t private_cache)
+    dyld_kernel_image_info_t cache_img,
+    __unused boolean_t no_cache,
+    __unused boolean_t private_cache)
 {
        if (task == NULL || task != current_task()) {
                return KERN_INVALID_TASK;
 {
        if (task == NULL || task != current_task()) {
                return KERN_INVALID_TASK;
@@ -6172,21 +7767,21 @@ task_register_dyld_shared_cache_image_info(task_t task,
 
 kern_return_t
 task_register_dyld_set_dyld_state(__unused task_t task,
 
 kern_return_t
 task_register_dyld_set_dyld_state(__unused task_t task,
-                                  __unused uint8_t dyld_state)
+    __unused uint8_t dyld_state)
 {
        return KERN_NOT_SUPPORTED;
 }
 
 kern_return_t
 task_register_dyld_get_process_state(__unused task_t task,
 {
        return KERN_NOT_SUPPORTED;
 }
 
 kern_return_t
 task_register_dyld_get_process_state(__unused task_t task,
-                                     __unused dyld_kernel_process_info_t * dyld_process_state)
+    __unused dyld_kernel_process_info_t * dyld_process_state)
 {
        return KERN_NOT_SUPPORTED;
 }
 
 kern_return_t
 task_inspect(task_inspect_t task_insp, task_inspect_flavor_t flavor,
 {
        return KERN_NOT_SUPPORTED;
 }
 
 kern_return_t
 task_inspect(task_inspect_t task_insp, task_inspect_flavor_t flavor,
-               task_inspect_info_t info_out, mach_msg_type_number_t *size_in_out)
+    task_inspect_info_t info_out, mach_msg_type_number_t *size_in_out)
 {
 #if MONOTONIC
        task_t task = (task_t)task_insp;
 {
 #if MONOTONIC
        task_t task = (task_t)task_insp;
@@ -6202,7 +7797,7 @@ task_inspect(task_inspect_t task_insp, task_inspect_flavor_t flavor,
        switch (flavor) {
        case TASK_INSPECT_BASIC_COUNTS: {
                struct task_inspect_basic_counts *bc;
        switch (flavor) {
        case TASK_INSPECT_BASIC_COUNTS: {
                struct task_inspect_basic_counts *bc;
-               uint64_t task_counts[MT_CORE_NFIXED];
+               uint64_t task_counts[MT_CORE_NFIXED] = { 0 };
 
                if (size < TASK_INSPECT_BASIC_COUNTS_COUNT) {
                        kr = KERN_INVALID_ARGUMENT;
 
                if (size < TASK_INSPECT_BASIC_COUNTS_COUNT) {
                        kr = KERN_INVALID_ARGUMENT;
@@ -6240,8 +7835,8 @@ int num_tasks_can_use_secluded_mem = 0;
 
 void
 task_set_can_use_secluded_mem(
 
 void
 task_set_can_use_secluded_mem(
-       task_t          task,
-       boolean_t       can_use_secluded_mem)
+       task_t          task,
+       boolean_t       can_use_secluded_mem)
 {
        if (!task->task_could_use_secluded_mem) {
                return;
 {
        if (!task->task_could_use_secluded_mem) {
                return;
@@ -6253,8 +7848,8 @@ task_set_can_use_secluded_mem(
 
 void
 task_set_can_use_secluded_mem_locked(
 
 void
 task_set_can_use_secluded_mem_locked(
-       task_t          task,
-       boolean_t       can_use_secluded_mem)
+       task_t          task,
+       boolean_t       can_use_secluded_mem)
 {
        assert(task->task_could_use_secluded_mem);
        if (can_use_secluded_mem &&
 {
        assert(task->task_could_use_secluded_mem);
        if (can_use_secluded_mem &&
@@ -6262,37 +7857,37 @@ task_set_can_use_secluded_mem_locked(
            !task->task_can_use_secluded_mem) {
                assert(num_tasks_can_use_secluded_mem >= 0);
                OSAddAtomic(+1,
            !task->task_can_use_secluded_mem) {
                assert(num_tasks_can_use_secluded_mem >= 0);
                OSAddAtomic(+1,
-                           (volatile SInt32 *)&num_tasks_can_use_secluded_mem);
+                   (volatile SInt32 *)&num_tasks_can_use_secluded_mem);
                task->task_can_use_secluded_mem = TRUE;
        } else if (!can_use_secluded_mem &&
                task->task_can_use_secluded_mem = TRUE;
        } else if (!can_use_secluded_mem &&
-                  task->task_can_use_secluded_mem) {
+           task->task_can_use_secluded_mem) {
                assert(num_tasks_can_use_secluded_mem > 0);
                OSAddAtomic(-1,
                assert(num_tasks_can_use_secluded_mem > 0);
                OSAddAtomic(-1,
-                           (volatile SInt32 *)&num_tasks_can_use_secluded_mem);
+                   (volatile SInt32 *)&num_tasks_can_use_secluded_mem);
                task->task_can_use_secluded_mem = FALSE;
        }
 }
 
 void
 task_set_could_use_secluded_mem(
                task->task_can_use_secluded_mem = FALSE;
        }
 }
 
 void
 task_set_could_use_secluded_mem(
-       task_t          task,
-       boolean_t       could_use_secluded_mem)
+       task_t          task,
+       boolean_t       could_use_secluded_mem)
 {
 {
-       task->task_could_use_secluded_mem = could_use_secluded_mem;
+       task->task_could_use_secluded_mem = !!could_use_secluded_mem;
 }
 
 void
 task_set_could_also_use_secluded_mem(
 }
 
 void
 task_set_could_also_use_secluded_mem(
-       task_t          task,
-       boolean_t       could_also_use_secluded_mem)
+       task_t          task,
+       boolean_t       could_also_use_secluded_mem)
 {
 {
-       task->task_could_also_use_secluded_mem = could_also_use_secluded_mem;
+       task->task_could_also_use_secluded_mem = !!could_also_use_secluded_mem;
 }
 
 boolean_t
 task_can_use_secluded_mem(
 }
 
 boolean_t
 task_can_use_secluded_mem(
-       task_t          task,
-       boolean_t       is_alloc)
+       task_t          task,
+       boolean_t       is_alloc)
 {
        if (task->task_can_use_secluded_mem) {
                assert(task->task_could_use_secluded_mem);
 {
        if (task->task_can_use_secluded_mem) {
                assert(task->task_could_use_secluded_mem);
@@ -6306,13 +7901,17 @@ task_can_use_secluded_mem(
        }
 
        /*
        }
 
        /*
-        * If a single task is using more than some amount of
-        * memory, allow it to dip into secluded and also begin
-        * suppression of secluded memory until the tasks exits.
+        * If a single task is using more than some large amount of
+        * memory (i.e. secluded_shutoff_trigger) and is approaching
+        * its task limit, allow it to dip into secluded and begin
+        * suppression of rebuilding secluded memory until that task exits.
         */
        if (is_alloc && secluded_shutoff_trigger != 0) {
                uint64_t phys_used = get_task_phys_footprint(task);
         */
        if (is_alloc && secluded_shutoff_trigger != 0) {
                uint64_t phys_used = get_task_phys_footprint(task);
-               if (phys_used > secluded_shutoff_trigger) {
+               uint64_t limit = get_task_phys_footprint_limit(task);
+               if (phys_used > secluded_shutoff_trigger &&
+                   limit > secluded_shutoff_trigger &&
+                   phys_used > limit - secluded_shutoff_headroom) {
                        start_secluded_suppression(task);
                        return TRUE;
                }
                        start_secluded_suppression(task);
                        return TRUE;
                }
@@ -6323,16 +7922,29 @@ task_can_use_secluded_mem(
 
 boolean_t
 task_could_use_secluded_mem(
 
 boolean_t
 task_could_use_secluded_mem(
-       task_t  task)
+       task_t  task)
 {
        return task->task_could_use_secluded_mem;
 }
 {
        return task->task_could_use_secluded_mem;
 }
+
+boolean_t
+task_could_also_use_secluded_mem(
+       task_t  task)
+{
+       return task->task_could_also_use_secluded_mem;
+}
 #endif /* CONFIG_SECLUDED_MEMORY */
 
 queue_head_t *
 task_io_user_clients(task_t task)
 {
 #endif /* CONFIG_SECLUDED_MEMORY */
 
 queue_head_t *
 task_io_user_clients(task_t task)
 {
-    return (&task->io_user_clients);
+       return &task->io_user_clients;
+}
+
+void
+task_set_message_app_suspended(task_t task, boolean_t enable)
+{
+       task->message_app_suspended = enable;
 }
 
 void
 }
 
 void
@@ -6361,7 +7973,7 @@ void
 task_self_region_footprint_set(
        boolean_t newval)
 {
 task_self_region_footprint_set(
        boolean_t newval)
 {
-       task_t  curtask;
+       task_t  curtask;
 
        curtask = current_task();
        task_lock(curtask);
 
        curtask = current_task();
        task_lock(curtask);
@@ -6393,17 +8005,358 @@ boolean_t
 task_get_darkwake_mode(task_t task)
 {
        assert(task);
 task_get_darkwake_mode(task_t task)
 {
        assert(task);
-       return ((task->t_flags & TF_DARKWAKE_MODE) != 0);
+       return (task->t_flags & TF_DARKWAKE_MODE) != 0;
+}
+
+kern_return_t
+task_get_exc_guard_behavior(
+       task_t task,
+       task_exc_guard_behavior_t *behaviorp)
+{
+       if (task == TASK_NULL) {
+               return KERN_INVALID_TASK;
+       }
+       *behaviorp = task->task_exc_guard;
+       return KERN_SUCCESS;
+}
+
+#ifndef TASK_EXC_GUARD_ALL
+/* Temporary define until two branches are merged */
+#define TASK_EXC_GUARD_ALL (TASK_EXC_GUARD_VM_ALL | 0xf0)
+#endif
+
+kern_return_t
+task_set_exc_guard_behavior(
+       task_t task,
+       task_exc_guard_behavior_t behavior)
+{
+       if (task == TASK_NULL) {
+               return KERN_INVALID_TASK;
+       }
+       if (behavior & ~TASK_EXC_GUARD_ALL) {
+               return KERN_INVALID_VALUE;
+       }
+       task->task_exc_guard = behavior;
+       return KERN_SUCCESS;
 }
 
 #if __arm64__
 }
 
 #if __arm64__
+extern int legacy_footprint_entitlement_mode;
+extern void memorystatus_act_on_legacy_footprint_entitlement(struct proc *, boolean_t);
+extern void memorystatus_act_on_ios13extended_footprint_entitlement(struct proc *);
+
+
 void
 task_set_legacy_footprint(
 void
 task_set_legacy_footprint(
-       task_t          task,
-       boolean_t       new_val)
+       task_t task)
+{
+       task_lock(task);
+       task->task_legacy_footprint = TRUE;
+       task_unlock(task);
+}
+
+void
+task_set_extra_footprint_limit(
+       task_t task)
+{
+       if (task->task_extra_footprint_limit) {
+               return;
+       }
+       task_lock(task);
+       if (task->task_extra_footprint_limit) {
+               task_unlock(task);
+               return;
+       }
+       task->task_extra_footprint_limit = TRUE;
+       task_unlock(task);
+       memorystatus_act_on_legacy_footprint_entitlement(task->bsd_info, TRUE);
+}
+
+void
+task_set_ios13extended_footprint_limit(
+       task_t task)
 {
 {
+       if (task->task_ios13extended_footprint_limit) {
+               return;
+       }
        task_lock(task);
        task_lock(task);
-       task->task_legacy_footprint = new_val;
+       if (task->task_ios13extended_footprint_limit) {
+               task_unlock(task);
+               return;
+       }
+       task->task_ios13extended_footprint_limit = TRUE;
        task_unlock(task);
        task_unlock(task);
+       memorystatus_act_on_ios13extended_footprint_entitlement(task->bsd_info);
 }
 #endif /* __arm64__ */
 }
 #endif /* __arm64__ */
+
+static inline ledger_amount_t
+task_ledger_get_balance(
+       ledger_t        ledger,
+       int             ledger_idx)
+{
+       ledger_amount_t amount;
+       amount = 0;
+       ledger_get_balance(ledger, ledger_idx, &amount);
+       return amount;
+}
+
+/*
+ * Gather the amount of memory counted in a task's footprint due to
+ * being in a specific set of ledgers.
+ */
+void
+task_ledgers_footprint(
+       ledger_t        ledger,
+       ledger_amount_t *ledger_resident,
+       ledger_amount_t *ledger_compressed)
+{
+       *ledger_resident = 0;
+       *ledger_compressed = 0;
+
+       /* purgeable non-volatile memory */
+       *ledger_resident += task_ledger_get_balance(ledger, task_ledgers.purgeable_nonvolatile);
+       *ledger_compressed += task_ledger_get_balance(ledger, task_ledgers.purgeable_nonvolatile_compressed);
+
+       /* "default" tagged memory */
+       *ledger_resident += task_ledger_get_balance(ledger, task_ledgers.tagged_footprint);
+       *ledger_compressed += task_ledger_get_balance(ledger, task_ledgers.tagged_footprint_compressed);
+
+       /* "network" currently never counts in the footprint... */
+
+       /* "media" tagged memory */
+       *ledger_resident += task_ledger_get_balance(ledger, task_ledgers.media_footprint);
+       *ledger_compressed += task_ledger_get_balance(ledger, task_ledgers.media_footprint_compressed);
+
+       /* "graphics" tagged memory */
+       *ledger_resident += task_ledger_get_balance(ledger, task_ledgers.graphics_footprint);
+       *ledger_compressed += task_ledger_get_balance(ledger, task_ledgers.graphics_footprint_compressed);
+
+       /* "neural" tagged memory */
+       *ledger_resident += task_ledger_get_balance(ledger, task_ledgers.neural_footprint);
+       *ledger_compressed += task_ledger_get_balance(ledger, task_ledgers.neural_footprint_compressed);
+}
+
+void
+task_set_memory_ownership_transfer(
+       task_t    task,
+       boolean_t value)
+{
+       task_lock(task);
+       task->task_can_transfer_memory_ownership = !!value;
+       task_unlock(task);
+}
+
+void
+task_copy_vmobjects(task_t task, vm_object_query_t query, size_t len, size_t *num)
+{
+       vm_object_t find_vmo;
+       size_t size = 0;
+
+       task_objq_lock(task);
+       if (query != NULL) {
+               queue_iterate(&task->task_objq, find_vmo, vm_object_t, task_objq)
+               {
+                       vm_object_query_t p = &query[size++];
+
+                       /* make sure to not overrun */
+                       if (size * sizeof(vm_object_query_data_t) > len) {
+                               --size;
+                               break;
+                       }
+
+                       bzero(p, sizeof(*p));
+                       p->object_id = (vm_object_id_t) VM_KERNEL_ADDRPERM(find_vmo);
+                       p->virtual_size = find_vmo->internal ? find_vmo->vo_size : 0;
+                       p->resident_size = find_vmo->resident_page_count * PAGE_SIZE;
+                       p->wired_size = find_vmo->wired_page_count * PAGE_SIZE;
+                       p->reusable_size = find_vmo->reusable_page_count * PAGE_SIZE;
+                       p->vo_no_footprint = find_vmo->vo_no_footprint;
+                       p->vo_ledger_tag = find_vmo->vo_ledger_tag;
+                       p->purgable = find_vmo->purgable;
+
+                       if (find_vmo->internal && find_vmo->pager_created && find_vmo->pager != NULL) {
+                               p->compressed_size = vm_compressor_pager_get_count(find_vmo->pager) * PAGE_SIZE;
+                       } else {
+                               p->compressed_size = 0;
+                       }
+               }
+       } else {
+               size = (size_t)task->task_owned_objects;
+       }
+       task_objq_unlock(task);
+
+       *num = size;
+}
+
+void
+task_set_filter_msg_flag(
+       task_t task,
+       boolean_t flag)
+{
+       assert(task != TASK_NULL);
+
+       task_lock(task);
+       if (flag) {
+               task->t_flags |= TF_FILTER_MSG;
+       } else {
+               task->t_flags &= ~TF_FILTER_MSG;
+       }
+       task_unlock(task);
+}
+
+boolean_t
+task_get_filter_msg_flag(
+       task_t task)
+{
+       uint32_t flags = 0;
+
+       if (!task) {
+               return false;
+       }
+
+       flags = os_atomic_load(&task->t_flags, relaxed);
+       return (flags & TF_FILTER_MSG) ? TRUE : FALSE;
+}
+bool
+task_is_exotic(
+       task_t task)
+{
+       if (task == TASK_NULL) {
+               return false;
+       }
+       return vm_map_is_exotic(get_task_map(task));
+}
+
+bool
+task_is_alien(
+       task_t task)
+{
+       if (task == TASK_NULL) {
+               return false;
+       }
+       return vm_map_is_alien(get_task_map(task));
+}
+
+
+
+#if CONFIG_MACF
+/* Set the filter mask for Mach traps. */
+void
+mac_task_set_mach_filter_mask(task_t task, uint8_t *maskptr)
+{
+       assert(task);
+
+       task->mach_trap_filter_mask = maskptr;
+}
+
+/* Set the filter mask for kobject msgs. */
+void
+mac_task_set_kobj_filter_mask(task_t task, uint8_t *maskptr)
+{
+       assert(task);
+
+       task->mach_kobj_filter_mask = maskptr;
+}
+
+/* Hook for mach trap/sc filter evaluation policy. */
+mac_task_mach_filter_cbfunc_t mac_task_mach_trap_evaluate = NULL;
+
+/* Hook for kobj message filter evaluation policy. */
+mac_task_kobj_filter_cbfunc_t mac_task_kobj_msg_evaluate = NULL;
+
+/* Set the callback hooks for the filtering policy. */
+int
+mac_task_register_filter_callbacks(
+       const mac_task_mach_filter_cbfunc_t mach_cbfunc,
+       const mac_task_kobj_filter_cbfunc_t kobj_cbfunc)
+{
+       if (mach_cbfunc != NULL) {
+               if (mac_task_mach_trap_evaluate != NULL) {
+                       return KERN_FAILURE;
+               }
+               mac_task_mach_trap_evaluate = mach_cbfunc;
+       }
+       if (kobj_cbfunc != NULL) {
+               if (mac_task_kobj_msg_evaluate != NULL) {
+                       return KERN_FAILURE;
+               }
+               mac_task_kobj_msg_evaluate = kobj_cbfunc;
+       }
+
+       return KERN_SUCCESS;
+}
+#endif /* CONFIG_MACF */
+
+void
+task_transfer_mach_filter_bits(
+       task_t new_task,
+       task_t old_task)
+{
+#ifdef CONFIG_MACF
+       /* Copy mach trap and kernel object mask pointers to new task. */
+       new_task->mach_trap_filter_mask = old_task->mach_trap_filter_mask;
+       new_task->mach_kobj_filter_mask = old_task->mach_kobj_filter_mask;
+#endif
+       /* If filter message flag is set then set it in the new task. */
+       if (task_get_filter_msg_flag(old_task)) {
+               new_task->t_flags |= TF_FILTER_MSG;
+       }
+}
+
+
+#if __has_feature(ptrauth_calls)
+
+#define PAC_EXCEPTION_ENTITLEMENT "com.apple.private.pac.exception"
+
+void
+task_set_pac_exception_fatal_flag(
+       task_t task)
+{
+       assert(task != TASK_NULL);
+
+       if (!IOTaskHasEntitlement(task, PAC_EXCEPTION_ENTITLEMENT)) {
+               return;
+       }
+
+       task_lock(task);
+       task->t_flags |= TF_PAC_EXC_FATAL;
+       task_unlock(task);
+}
+
+bool
+task_is_pac_exception_fatal(
+       task_t task)
+{
+       uint32_t flags = 0;
+
+       assert(task != TASK_NULL);
+
+       flags = os_atomic_load(&task->t_flags, relaxed);
+       return (bool)(flags & TF_PAC_EXC_FATAL);
+}
+#endif /* __has_feature(ptrauth_calls) */
+
+void
+task_set_tecs(task_t task)
+{
+       if (task == TASK_NULL) {
+               task = current_task();
+       }
+
+       if (!machine_csv(CPUVN_CI)) {
+               return;
+       }
+
+       LCK_MTX_ASSERT(&task->lock, LCK_MTX_ASSERT_NOTOWNED);
+
+       task_lock(task);
+
+       task->t_flags |= TF_TECS;
+
+       thread_t thread;
+       queue_iterate(&task->threads, thread, thread_t, task_threads) {
+               machine_tecs(thread);
+       }
+       task_unlock(task);
+}