]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/task.c
xnu-2050.22.13.tar.gz
[apple/xnu.git] / osfmk / kern / task.c
index ea12d7b399f46b1fdf2cc2f115ff49f9ee2dcbb3..1378bf4150bc3c606fd76dda90ebeb7483e30b68 100644 (file)
@@ -1,23 +1,29 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * 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
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * 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.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE 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_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * @OSF_FREE_COPYRIGHT@
  * improvements that they make and grant CSL redistribution rights.
  *
  */
+/*
+ * NOTICE: This file was modified by McAfee Research in 2004 to introduce
+ * support for mandatory and extensible security protections.  This notice
+ * is included in support of clause 2.2 (b) of the Apple Public License,
+ * Version 2.0.
+ * Copyright (c) 2005 SPARTA, Inc.
+ */
 
-#include <mach_kdb.h>
-#include <mach_host.h>
-#include <mach_prof.h>
 #include <fast_tas.h>
-#include <task_swapper.h>
 #include <platforms.h>
 
+#include <mach/mach_types.h>
 #include <mach/boolean.h>
+#include <mach/host_priv.h>
 #include <mach/machine/vm_types.h>
 #include <mach/vm_param.h>
 #include <mach/semaphore.h>
 #include <mach/task_info.h>
 #include <mach/task_special_ports.h>
-#include <mach/mach_types.h>
+
+#include <ipc/ipc_types.h>
 #include <ipc/ipc_space.h>
 #include <ipc/ipc_entry.h>
+
+#include <kern/kern_types.h>
 #include <kern/mach_param.h>
 #include <kern/misc_protos.h>
 #include <kern/task.h>
 #include <kern/processor.h>
 #include <kern/sched_prim.h>   /* for thread_wakeup */
 #include <kern/ipc_tt.h>
-#include <kern/ledger.h>
 #include <kern/host.h>
-#include <vm/vm_kern.h>                /* for kernel_map, ipc_kernel_map */
-#include <kern/profile.h>
+#include <kern/clock.h>
+#include <kern/timer.h>
 #include <kern/assert.h>
 #include <kern/sync_lock.h>
-#if    MACH_KDB
-#include <ddb/db_sym.h>
-#endif /* MACH_KDB */
+#include <kern/affinity.h>
 
-#if    TASK_SWAPPER
-#include <kern/task_swap.h>
-#endif /* TASK_SWAPPER */
+#include <vm/pmap.h>
+#include <vm/vm_map.h>
+#include <vm/vm_kern.h>                /* for kernel_map, ipc_kernel_map */
+#include <vm/vm_pageout.h>
+#include <vm/vm_protos.h>
 
 /*
  * Exported interfaces
 #include <mach/task_server.h>
 #include <mach/mach_host_server.h>
 #include <mach/host_security_server.h>
-#include <vm/task_working_set.h>
+#include <mach/mach_port_server.h>
+#include <mach/security_server.h>
+
+#include <vm/vm_shared_region.h>
+
+#if CONFIG_MACF_MACH
+#include <security/mac_mach_internal.h>
+#endif
+
+#if CONFIG_COUNTERS
+#include <pmc/pmc.h>
+#endif /* CONFIG_COUNTERS */
+
+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;
+#if CONFIG_EMBEDDED
+lck_mtx_t      task_watch_mtx;
+#endif /* CONFIG_EMBEDDED */
+
+zinfo_usage_store_t tasks_tkm_private;
+zinfo_usage_store_t tasks_tkm_shared;
+
+static ledger_template_t task_ledger_template = NULL;
+struct _task_ledger_indices task_ledgers = {-1, -1, -1, -1, -1};
+void init_task_ledgers(void);
 
-task_t kernel_task;
-zone_t task_zone;
+
+int task_max = CONFIG_TASK_MAX; /* Max number of tasks */
+
+/* externs for BSD kernel */
+extern void proc_getexecutableuuid(void *, unsigned char *, unsigned long);
 
 /* Forwards */
 
 void           task_hold_locked(
                        task_t          task);
 void           task_wait_locked(
-                       task_t          task);
+                       task_t          task,
+                       boolean_t       until_not_runnable);
 void           task_release_locked(
                        task_t          task);
-void           task_collect_scan(void);
 void           task_free(
                        task_t          task );
 void           task_synchronizer_destroy_all(
                        task_t          task);
 
-kern_return_t  task_set_ledger(
-                       task_t          task,
-                       ledger_t        wired,
-                       ledger_t        paged);
+int check_for_tasksuspend(
+                       task_t task);
 
 void
-task_init(void)
+task_backing_store_privileged(
+                       task_t task)
 {
-       task_zone = zinit(
-                       sizeof(struct task),
-                       TASK_MAX * sizeof(struct task),
-                       TASK_CHUNK * sizeof(struct task),
-                       "tasks");
+       task_lock(task);
+       task->priv_flags |= VM_BACKING_STORE_PRIV;
+       task_unlock(task);
+       return;
+}
 
-       eml_init();
 
-       /*
-        * Create the kernel task as the first task.
-        * Task_create_local must assign to kernel_task as a side effect,
-        * for other initialization. (:-()
-        */
-       if (task_create_local(
-                       TASK_NULL, FALSE, FALSE, &kernel_task) != KERN_SUCCESS)
-               panic("task_init\n");
-       vm_map_deallocate(kernel_task->map);
-       kernel_task->map = kernel_map;
+void
+task_set_64bit(
+               task_t task,
+               boolean_t is64bit)
+{
+#if defined(__i386__) || defined(__x86_64__)
+       thread_t thread;
+#endif /* __i386__ */
+       int     vm_flags = 0;
 
-#if    MACH_ASSERT
-       if (watchacts & WA_TASK)
-           printf("task_init: kernel_task = %x map=%x\n",
-                               kernel_task, kernel_map);
-#endif /* MACH_ASSERT */
-}
+       if (is64bit) {
+               if (task_has_64BitAddr(task))
+                       return;
 
-#if    MACH_HOST
+               task_set_64BitAddr(task);
+       } else {
+               if ( !task_has_64BitAddr(task))
+                       return;
 
-#if 0
-static void
-task_freeze(
-       task_t task)
-{
-       task_lock(task);
-       /*
-        *      If may_assign is false, task is already being assigned,
-        *      wait for that to finish.
+               /*
+                * Deallocate all memory previously allocated
+                * above the 32-bit address space, since it won't
+                * be accessible anymore.
+                */
+               /* remove regular VM map entries & pmap mappings */
+               (void) vm_map_remove(task->map,
+                                    (vm_map_offset_t) VM_MAX_ADDRESS,
+                                    MACH_VM_MAX_ADDRESS,
+                                    0);
+               /* remove the higher VM mappings */
+               (void) vm_map_remove(task->map,
+                                    MACH_VM_MAX_ADDRESS,
+                                    0xFFFFFFFFFFFFF000ULL,
+                                    vm_flags);
+               task_clear_64BitAddr(task);
+       }
+       /* FIXME: On x86, the thread save state flavor can diverge from the
+        * task's 64-bit feature flag due to the 32-bit/64-bit register save
+        * state dichotomy. Since we can be pre-empted in this interval,
+        * certain routines may observe the thread as being in an inconsistent
+        * state with respect to its task's 64-bitness.
         */
-       while (task->may_assign == FALSE) {
-               wait_result_t res;
-
-               task->assign_active = TRUE;
-               res = thread_sleep_mutex((event_t) &task->assign_active,
-                                        &task->lock, THREAD_UNINT);
-               assert(res == THREAD_AWAKENED);
+#if defined(__i386__) || defined(__x86_64__)
+       task_lock(task);
+       queue_iterate(&task->threads, thread, thread_t, task_threads) {
+               thread_mtx_lock(thread);
+               machine_thread_switch_addrmode(thread);
+               thread_mtx_unlock(thread);
        }
-       task->may_assign = FALSE;
        task_unlock(task);
-       return;
+#endif /* __i386__ */
 }
-#else
-#define thread_freeze(thread)  assert(task->processor_set == &default_pset)
-#endif
 
-#if 0
-static void
-task_unfreeze(
-       task_t task)
+
+void
+task_set_dyld_info(task_t task, mach_vm_address_t addr, mach_vm_size_t size)
 {
        task_lock(task);
-       assert(task->may_assign == FALSE);
-       task->may_assign = TRUE;
-       if (task->assign_active == TRUE) {
-               task->assign_active = FALSE;
-               thread_wakeup((event_t)&task->assign_active);
-       }
+       task->all_image_info_addr = addr;
+       task->all_image_info_size = size;
        task_unlock(task);
-       return;
 }
+
+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);
+#if CONFIG_EMBEDDED
+       lck_mtx_init(&task_watch_mtx, &task_lck_grp, &task_lck_attr);
+#endif /* CONFIG_EMBEDDED */
+
+       task_zone = zinit(
+                       sizeof(struct task),
+                       task_max * sizeof(struct task),
+                       TASK_CHUNK * sizeof(struct task),
+                       "tasks");
+
+       zone_change(task_zone, Z_NOENCRYPT, TRUE);
+
+       init_task_ledgers();
+
+       /*
+        * Create the kernel task as the first task.
+        */
+#ifdef __LP64__
+       if (task_create_internal(TASK_NULL, FALSE, TRUE, &kernel_task) != KERN_SUCCESS)
 #else
-#define thread_unfreeze(thread)        assert(task->processor_set == &default_pset)
+       if (task_create_internal(TASK_NULL, FALSE, FALSE, &kernel_task) != KERN_SUCCESS)
 #endif
+               panic("task_init\n");
+
+       vm_map_deallocate(kernel_task->map);
+       kernel_task->map = kernel_map;
 
-#endif /* MACH_HOST */
+}
 
 /*
  * Create a task running in the kernel address space.  It may
@@ -227,115 +301,96 @@ task_unfreeze(
  */
 kern_return_t
 kernel_task_create(
-       task_t                  parent_task,
-       vm_offset_t             map_base,
-       vm_size_t               map_size,
-       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)
 {
-       kern_return_t           result;
-       task_t                  new_task;
-       vm_map_t                old_map;
-
-       /*
-        * Create the task.
-        */
-       result = task_create_local(parent_task, FALSE, TRUE, &new_task);
-       if (result != KERN_SUCCESS)
-               return (result);
-
-       /*
-        * Task_create_local creates the task with a user-space map.
-        * We attempt to replace the map and free it afterwards; else
-        * task_deallocate will free it (can NOT set map to null before
-        * task_deallocate, this impersonates a norma placeholder task).
-        * _Mark the memory as pageable_ -- this is what we
-        * want for images (like servers) loaded into the kernel.
-        */
-       if (map_size == 0) {
-               vm_map_deallocate(new_task->map);
-               new_task->map = kernel_map;
-               *child_task = new_task;
-       } else {
-               old_map = new_task->map;
-               if ((result = kmem_suballoc(kernel_map, &map_base,
-                                           map_size, TRUE, FALSE,
-                                           &new_task->map)) != KERN_SUCCESS) {
-                       /*
-                        * New task created with ref count of 2 -- decrement by
-                        * one to force task deletion.
-                        */
-                       printf("kmem_suballoc(%x,%x,%x,1,0,&new) Fails\n",
-                              kernel_map, map_base, map_size);
-                       --new_task->ref_count;
-                       task_deallocate(new_task);
-                       return (result);
-               }
-               vm_map_deallocate(old_map);
-               *child_task = new_task;
-       }
-       return (KERN_SUCCESS);
+       return (KERN_INVALID_ARGUMENT);
 }
 
 kern_return_t
 task_create(
-       task_t                  parent_task,
-        ledger_port_array_t    ledger_ports,
-        mach_msg_type_number_t num_ledger_ports,
-       boolean_t               inherit_memory,
-       task_t                  *child_task)            /* 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);
 
-       return task_create_local(
-                       parent_task, inherit_memory, FALSE, child_task);
+       /*
+        * No longer supported: too many calls assume that a task has a valid
+        * process attached.
+        */
+       return(KERN_FAILURE);
 }
 
 kern_return_t
 host_security_create_task_token(
-        host_security_t                host_security,
-       task_t                  parent_task,
-        security_token_t       sec_token,
-       host_priv_t             host_priv,
-        ledger_port_array_t    ledger_ports,
-        mach_msg_type_number_t num_ledger_ports,
-       boolean_t               inherit_memory,
-       task_t                  *child_task)            /* OUT */
+       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 */
 {
-        kern_return_t          result;
-        
        if (parent_task == TASK_NULL)
                return(KERN_INVALID_ARGUMENT);
 
        if (host_security == HOST_NULL)
                return(KERN_INVALID_SECURITY);
 
-       result = task_create_local(
-                       parent_task, inherit_memory, FALSE, child_task);
-
-        if (result != KERN_SUCCESS)
-                return(result);
-
-       result = host_security_set_task_token(host_security,
-                                             *child_task,
-                                             sec_token,
-                                             host_priv);
+       /*
+        * No longer supported.
+        */
+       return(KERN_FAILURE);
+}
 
-       if (result != KERN_SUCCESS)
-               return(result);
+void
+init_task_ledgers(void)
+{
+       ledger_template_t t;
+       
+       assert(task_ledger_template == NULL);
+       assert(kernel_task == TASK_NULL);
+
+       if ((t = ledger_template_create("Per-task ledger")) == NULL)
+               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",
+           "physmem", "bytes");
+       task_ledgers.tkm_shared = ledger_entry_add(t, "tkm_shared", "physmem",
+           "bytes");
+       task_ledgers.phys_mem = ledger_entry_add(t, "phys_mem", "physmem",
+           "bytes");
+       task_ledgers.wired_mem = ledger_entry_add(t, "wired_mem", "physmem",
+           "bytes");
+
+       if ((task_ledgers.cpu_time < 0) || (task_ledgers.tkm_private < 0) ||
+           (task_ledgers.tkm_shared < 0) || (task_ledgers.phys_mem < 0) ||
+           (task_ledgers.wired_mem < 0)) {
+               panic("couldn't create entries for task ledger template");
+       }
 
-       return(result);
+       task_ledger_template = t;
 }
 
 kern_return_t
-task_create_local(
+task_create_internal(
        task_t          parent_task,
        boolean_t       inherit_memory,
-       boolean_t       kernel_loaded,
+       boolean_t       is_64bit,
        task_t          *child_task)            /* OUT */
 {
-       task_t          new_task;
-       processor_set_t pset;
+       task_t                  new_task;
+       vm_shared_region_t      shared_region;
+       ledger_t                ledger = NULL;
 
        new_task = (task_t) zalloc(task_zone);
 
@@ -345,102 +400,154 @@ task_create_local(
        /* one ref for just being alive; one for our caller */
        new_task->ref_count = 2;
 
+       /* allocate with active entries */
+       assert(task_ledger_template != NULL);
+       if ((ledger = ledger_instantiate(task_ledger_template,
+                       LEDGER_CREATE_ACTIVE_ENTRIES)) == NULL) {
+               zfree(task_zone, new_task);
+               return(KERN_RESOURCE_SHORTAGE);
+       }
+       new_task->ledger = ledger;
+
+       /* if inherit_memory is true, parent_task MUST not be NULL */
        if (inherit_memory)
-               new_task->map = vm_map_fork(parent_task->map);
+               new_task->map = vm_map_fork(ledger, parent_task->map);
        else
-               new_task->map = vm_map_create(pmap_create(0),
-                                       round_page(VM_MIN_ADDRESS),
-                                       trunc_page(VM_MAX_ADDRESS), TRUE);
+               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);
+
+       /* Inherit memlock limit from parent */
+       if (parent_task)
+               vm_map_set_user_wire_limit(new_task->map, (vm_size_t)parent_task->map->user_wire_limit);
 
-       mutex_init(&new_task->lock, ETAP_THREAD_TASK_NEW);
-       queue_init(&new_task->thr_acts);
+       lck_mtx_init(&new_task->lock, &task_lck_grp, &task_lck_attr);
+       queue_init(&new_task->threads);
        new_task->suspend_count = 0;
-       new_task->thr_act_count = 0;
-       new_task->res_act_count = 0;
-       new_task->active_act_count = 0;
+       new_task->thread_count = 0;
+       new_task->active_thread_count = 0;
        new_task->user_stop_count = 0;
        new_task->role = TASK_UNSPECIFIED;
        new_task->active = TRUE;
-       new_task->kernel_loaded = kernel_loaded;
-       new_task->user_data = 0;
+       new_task->halting = FALSE;
+       new_task->user_data = NULL;
        new_task->faults = 0;
        new_task->cow_faults = 0;
        new_task->pageins = 0;
        new_task->messages_sent = 0;
        new_task->messages_received = 0;
        new_task->syscalls_mach = 0;
+       new_task->priv_flags = 0;
        new_task->syscalls_unix=0;
-       new_task->csw=0;
-       new_task->dynamic_working_set = 0;
-       
-       task_working_set_create(new_task, TWS_SMALL_HASH_LINE_COUNT, 
-                                               0, TWS_HASH_STYLE_DEFAULT);
+       new_task->c_switch = new_task->p_switch = new_task->ps_switch = 0;
+       new_task->taskFeatures[0] = 0;                          /* Init task features */
+       new_task->taskFeatures[1] = 0;                          /* Init task features */
+
+       zinfo_task_init(new_task);
 
 #ifdef MACH_BSD
-       new_task->bsd_info = 0;
+       new_task->bsd_info = NULL;
 #endif /* MACH_BSD */
 
-#if    TASK_SWAPPER
-       new_task->swap_state = TASK_SW_IN;
-       new_task->swap_flags = 0;
-       new_task->swap_ast_waiting = 0;
-       new_task->swap_stamp = sched_tick;
-       new_task->swap_rss = 0;
-       new_task->swap_nswap = 0;
-#endif /* TASK_SWAPPER */
+#if defined(__i386__) || defined(__x86_64__)
+       new_task->i386_ldt = 0;
+       new_task->task_debug = NULL;
+#endif
+
 
        queue_init(&new_task->semaphore_list);
        queue_init(&new_task->lock_set_list);
        new_task->semaphores_owned = 0;
        new_task->lock_sets_owned = 0;
 
-#if    MACH_HOST
-       new_task->may_assign = TRUE;
-       new_task->assign_active = FALSE;
-#endif /* MACH_HOST */
-       eml_task_reference(new_task, parent_task);
+#if CONFIG_MACF_MACH
+       new_task->label = labelh_new(1);
+       mac_task_label_init (&new_task->maclabel);
+#endif
 
        ipc_task_init(new_task, parent_task);
 
-       new_task->total_user_time.seconds = 0;
-       new_task->total_user_time.microseconds = 0;
-       new_task->total_system_time.seconds = 0;
-       new_task->total_system_time.microseconds = 0;
+       new_task->total_user_time = 0;
+       new_task->total_system_time = 0;
 
-       task_prof_init(new_task);
+       new_task->vtimers = 0;
 
-       if (parent_task != TASK_NULL) {
-#if    MACH_HOST
-               /*
-                * Freeze the parent, so that parent_task->processor_set
-                * cannot change.
-                */
-               task_freeze(parent_task);
-#endif /* MACH_HOST */
-               pset = parent_task->processor_set;
-               if (!pset->active)
-                       pset = &default_pset;
+       new_task->shared_region = NULL;
+
+       new_task->affinity_space = NULL;
+
+#if CONFIG_COUNTERS
+       new_task->t_chud = 0U;
+#endif
+
+       new_task->pidsuspended = FALSE;
+       new_task->frozen = FALSE;
+       new_task->rusage_cpu_flags = 0;
+       new_task->rusage_cpu_percentage = 0;
+       new_task->rusage_cpu_interval = 0;
+       new_task->rusage_cpu_deadline = 0;
+       new_task->rusage_cpu_callt = NULL;
+       new_task->proc_terminate = 0;
+#if CONFIG_EMBEDDED
+       queue_init(&new_task->task_watchers);
+       new_task->appstate = TASK_APPSTATE_ACTIVE;
+       new_task->num_taskwatchers  = 0;
+       new_task->watchapplying  = 0;
+#endif /* CONFIG_EMBEDDED */
+
+       new_task->uexc_range_start = new_task->uexc_range_size = new_task->uexc_handler = 0;
 
+       if (parent_task != TASK_NULL) {
                new_task->sec_token = parent_task->sec_token;
+               new_task->audit_token = parent_task->audit_token;
+
+               /* inherit the parent's shared region */
+               shared_region = vm_shared_region_get(parent_task);
+               vm_shared_region_set(new_task, shared_region);
 
-               shared_region_mapping_ref(parent_task->system_shared_region);
-               new_task->system_shared_region = parent_task->system_shared_region;
+               if(task_has_64BitAddr(parent_task))
+                       task_set_64BitAddr(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->wired_ledger_port = ledger_copy(
-                       convert_port_to_ledger(parent_task->wired_ledger_port));
-               new_task->paged_ledger_port = ledger_copy(
-                       convert_port_to_ledger(parent_task->paged_ledger_port));
+#if defined(__i386__) || defined(__x86_64__)
+               if (inherit_memory && parent_task->i386_ldt)
+                       new_task->i386_ldt = user_ldt_copy(parent_task->i386_ldt);
+#endif
+               if (inherit_memory && parent_task->affinity_space)
+                       task_affinity_create(parent_task, new_task);
+
+               new_task->pset_hint = parent_task->pset_hint = task_choose_pset(parent_task);
+               new_task->policystate = parent_task->policystate;
+               /* inherit the self action state */
+               new_task->appliedstate = parent_task->appliedstate;
+               new_task->ext_policystate = parent_task->ext_policystate;
+#if NOTYET
+               /* till the child lifecycle is cleared do not inherit external action */
+               new_task->ext_appliedstate = parent_task->ext_appliedstate;
+#else
+               new_task->ext_appliedstate = default_task_null_policy;
+#endif
        }
        else {
-               pset = &default_pset;
-
                new_task->sec_token = KERNEL_SECURITY_TOKEN;
-               new_task->wired_ledger_port = ledger_copy(root_wired_ledger);
-               new_task->paged_ledger_port = ledger_copy(root_paged_ledger);
+               new_task->audit_token = KERNEL_AUDIT_TOKEN;
+#ifdef __LP64__
+               if(is_64bit)
+                       task_set_64BitAddr(new_task);
+#endif
+               new_task->all_image_info_addr = (mach_vm_address_t)0;
+               new_task->all_image_info_size = (mach_vm_size_t)0;
+
+               new_task->pset_hint = PROCESSOR_SET_NULL;
+               new_task->policystate = default_task_proc_policy;
+               new_task->ext_policystate = default_task_proc_policy;
+               new_task->appliedstate = default_task_null_policy;
+               new_task->ext_appliedstate = default_task_null_policy;
        }
 
        if (kernel_task == TASK_NULL) {
-               new_task->priority = MINPRI_KERNEL;
+               new_task->priority = BASEPRI_KERNEL;
                new_task->max_priority = MAXPRI_KERNEL;
        }
        else {
@@ -448,121 +555,92 @@ task_create_local(
                new_task->max_priority = MAXPRI_USER;
        }
 
-       pset_lock(pset);
-       pset_add_task(pset, new_task);
-       pset_unlock(pset);
-#if    MACH_HOST
-       if (parent_task != TASK_NULL)
-               task_unfreeze(parent_task);
-#endif /* MACH_HOST */
+       bzero(&new_task->extmod_statistics, sizeof(new_task->extmod_statistics));
+       
+       lck_mtx_lock(&tasks_threads_lock);
+       queue_enter(&tasks, new_task, task_t, tasks);
+       tasks_count++;
+       lck_mtx_unlock(&tasks_threads_lock);
 
-#if    FAST_TAS
-       if (inherit_memory) {
-               new_task->fast_tas_base = parent_task->fast_tas_base;
-               new_task->fast_tas_end  = parent_task->fast_tas_end;
-       } else {
-               new_task->fast_tas_base = (vm_offset_t)0;
-               new_task->fast_tas_end  = (vm_offset_t)0;
-       }
-#endif /* FAST_TAS */
+       if (vm_backing_store_low && parent_task != NULL)
+               new_task->priv_flags |= (parent_task->priv_flags&VM_BACKING_STORE_PRIV);
 
        ipc_task_enable(new_task);
 
-#if    TASK_SWAPPER
-       task_swapout_eligible(new_task);
-#endif /* TASK_SWAPPER */
-
-#if    MACH_ASSERT
-       if (watchacts & WA_TASK)
-           printf("*** task_create_local(par=%x inh=%x) == 0x%x\n",
-                       parent_task, inherit_memory, new_task);
-#endif /* MACH_ASSERT */
-
        *child_task = new_task;
        return(KERN_SUCCESS);
 }
 
 /*
- *     task_deallocate
+ *     task_deallocate:
  *
- *     Drop a reference on a task
- *     Task is locked.
+ *     Drop a reference on a task.
  */
 void
 task_deallocate(
        task_t          task)
 {
-       processor_set_t pset;
-       int refs;
+       ledger_amount_t credit, debit;
 
        if (task == TASK_NULL)
            return;
 
-       task_lock(task);
-       refs = --task->ref_count;
-       task_unlock(task);
-
-       if (refs > 0)
+       if (task_deallocate_internal(task) > 0)
                return;
 
-#if    TASK_SWAPPER
-       /* task_terminate guarantees that this task is off the list */
-       assert((task->swap_state & TASK_SW_ELIGIBLE) == 0);
-#endif /* TASK_SWAPPER */
+       lck_mtx_lock(&tasks_threads_lock);
+       queue_remove(&terminated_tasks, task, task_t, tasks);
+       lck_mtx_unlock(&tasks_threads_lock);
 
-       eml_task_deallocate(task);
+       /*
+        *      Give the machine dependent code a chance
+        *      to perform cleanup before ripping apart
+        *      the task.
+        */
+       machine_task_terminate(task);
 
        ipc_task_terminate(task);
 
-#if MACH_HOST
-       task_freeze(task);
-#endif
-
-       pset = task->processor_set;
-       pset_lock(pset);
-       pset_remove_task(pset,task);
-       pset_unlock(pset);
-       pset_deallocate(pset);
+       if (task->affinity_space)
+               task_affinity_deallocate(task);
 
-#if MACH_HOST
-       task_unfreeze(task);
-#endif
-
-       if (task->kernel_loaded)
-           vm_map_remove(kernel_map, task->map->min_offset,
-                         task->map->max_offset, VM_MAP_NO_FLAGS);
        vm_map_deallocate(task->map);
        is_release(task->itk_space);
-       task_prof_deallocate(task);
-       zfree(task_zone, (vm_offset_t) task);
-}
 
+       lck_mtx_destroy(&task->lock, &task_lck_grp);
 
-void
-task_reference(
-       task_t          task)
-{
-       if (task != TASK_NULL) {
-               task_lock(task);
-               task->ref_count++;
-               task_unlock(task);
+#if CONFIG_MACF_MACH
+       labelh_release(task->label);
+#endif
+
+       if (!ledger_get_entries(task->ledger, task_ledgers.tkm_private, &credit,
+           &debit)) {
+               OSAddAtomic64(credit, (int64_t *)&tasks_tkm_private.alloc);
+               OSAddAtomic64(debit, (int64_t *)&tasks_tkm_private.free);
+       }
+       if (!ledger_get_entries(task->ledger, task_ledgers.tkm_shared, &credit,
+           &debit)) {
+               OSAddAtomic64(credit, (int64_t *)&tasks_tkm_shared.alloc);
+               OSAddAtomic64(debit, (int64_t *)&tasks_tkm_shared.free);
        }
+       ledger_dereference(task->ledger);
+       zinfo_task_free(task);
+       zfree(task_zone, task);
 }
 
-boolean_t
-task_reference_try(
-       task_t          task)
+/*
+ *     task_name_deallocate:
+ *
+ *     Drop a reference on a task name.
+ */
+void
+task_name_deallocate(
+       task_name_t             task_name)
 {
-       if (task != TASK_NULL) {
-               if (task_lock_try(task)) {
-                       task->ref_count++;
-                       task_unlock(task);
-                       return TRUE;
-               }
-       }
-       return FALSE;
+       return(task_deallocate((task_t)task_name));
 }
 
+
 /*
  *     task_terminate:
  *
@@ -575,71 +653,59 @@ task_terminate(
        task_t          task)
 {
        if (task == TASK_NULL)
-               return(KERN_INVALID_ARGUMENT);
+               return (KERN_INVALID_ARGUMENT);
+
        if (task->bsd_info)
-               return(KERN_FAILURE);
+               return (KERN_FAILURE);
+
        return (task_terminate_internal(task));
 }
 
 kern_return_t
 task_terminate_internal(
-       task_t          task)
+       task_t                  task)
 {
-       thread_act_t    thr_act, cur_thr_act;
-       task_t          cur_task;
-       boolean_t       interrupt_save;
+       thread_t                        thread, self;
+       task_t                          self_task;
+       boolean_t                       interrupt_save;
 
        assert(task != kernel_task);
 
-       cur_thr_act = current_act();
-       cur_task = cur_thr_act->task;
-
-#if    TASK_SWAPPER
-       /*
-        *      If task is not resident (swapped out, or being swapped
-        *      out), we want to bring it back in (this can block).
-        *      NOTE: The only way that this can happen in the current
-        *      system is if the task is swapped while it has a thread
-        *      in exit(), and the thread does not hit a clean point
-        *      to swap itself before getting here.  
-        *      Terminating other tasks is another way to this code, but
-        *      it is not yet fully supported.
-        *      The task_swapin is unconditional.  It used to be done
-        *      only if the task is not resident.  Swapping in a
-        *      resident task will prevent it from being swapped out 
-        *      while it terminates.
-        */
-       task_swapin(task, TRUE);        /* TRUE means make it unswappable */
-#endif /* TASK_SWAPPER */
+       self = current_thread();
+       self_task = self->task;
 
        /*
         *      Get the task locked and make sure that we are not racing
         *      with someone else trying to terminate us.
         */
-       if (task == cur_task) {
+       if (task == self_task)
                task_lock(task);
-       } else if (task < cur_task) {
+       else
+       if (task < self_task) {
                task_lock(task);
-               task_lock(cur_task);
-       } else {
-               task_lock(cur_task);
+               task_lock(self_task);
+       }
+       else {
+               task_lock(self_task);
                task_lock(task);
        }
 
-       if (!task->active || !cur_thr_act->active) {
+       if (!task->active) {
                /*
-                *      Task or current act is already being terminated.
+                *      Task is already being terminated.
                 *      Just return an error. If we are dying, this will
                 *      just get us to our AST special handler and that
                 *      will get us to finalize the termination of ourselves.
                 */
                task_unlock(task);
-               if (cur_task != task)
-                       task_unlock(cur_task);
-               return(KERN_FAILURE);
+               if (self_task != task)
+                       task_unlock(self_task);
+
+               return (KERN_FAILURE);
        }
-       if (cur_task != task)
-               task_unlock(cur_task);
+
+       if (self_task != task)
+               task_unlock(self_task);
 
        /*
         * Make sure the current thread does not get aborted out of
@@ -660,28 +726,20 @@ task_terminate_internal(
        ipc_task_disable(task);
 
        /*
-        *      Terminate each activation in the task.
-        *
-        *      Each terminated activation will run it's special handler
-        *      when its current kernel context is unwound.  That will
-        *      clean up most of the thread resources.  Then it will be
-        *      handed over to the reaper, who will finally remove the
-        *      thread from the task list and free the structures.
-         */
-       queue_iterate(&task->thr_acts, thr_act, thread_act_t, thr_acts) {
-                       thread_terminate_internal(thr_act);
+        *      Terminate each thread in the task.
+        */
+       queue_iterate(&task->threads, thread, thread_t, task_threads) {
+                       thread_terminate_internal(thread);
        }
 
+       task_unlock(task);
+
+#if CONFIG_EMBEDDED
        /*
-        *      Clean up any virtual machine state/resources associated
-        *      with the current activation because it may hold wiring
-        *      and other references on resources we will be trying to
-        *      release below.
+        * remove all task watchers 
         */
-       if (cur_thr_act->task == task)
-               act_virtual_machine_destroy(cur_thr_act);
-
-       task_unlock(task);
+       task_removewatchers(task);
+#endif /* CONFIG_EMBEDDED */
 
        /*
         *      Destroy all synchronizers owned by the task.
@@ -691,8 +749,10 @@ task_terminate_internal(
        /*
         *      Destroy the IPC space, leaving just a reference for it.
         */
-       if (!task->kernel_loaded)
-               ipc_space_destroy(task->itk_space);
+       ipc_space_terminate(task->itk_space);
+
+       if (vm_map_has_4GB_pagezero(task->map))
+               vm_map_clear_4GB_pagezero(task->map);
 
        /*
         * If the current thread is a member of the task
@@ -702,14 +762,19 @@ task_terminate_internal(
         * expense of removing the address space regions
         * at reap time, we do it explictly here.
         */
-       (void) vm_map_remove(task->map,
-                            task->map->min_offset,
-                            task->map->max_offset, VM_MAP_NO_FLAGS);
+       vm_map_remove(task->map,
+                     task->map->min_offset,
+                     task->map->max_offset,
+                     VM_MAP_NO_FLAGS);
 
-       shared_region_mapping_dealloc(task->system_shared_region);
+       /* release our shared region */
+       vm_shared_region_set(task, NULL);
 
-       if(task->dynamic_working_set)
-               tws_hash_destroy((tws_hash_t)task->dynamic_working_set);
+       lck_mtx_lock(&tasks_threads_lock);
+       queue_remove(&tasks, task, task_t, tasks);
+       queue_enter(&terminated_tasks, task, task_t, tasks);
+       tasks_count--;
+       lck_mtx_unlock(&tasks_threads_lock);
 
        /*
         * We no longer need to guard against being aborted, so restore
@@ -722,44 +787,33 @@ task_terminate_internal(
         */
        task_deallocate(task);
 
-       return(KERN_SUCCESS);
+       return (KERN_SUCCESS);
 }
 
 /*
- * task_halt - Shut the current task down (except for the current thread) in
- *             preparation for dramatic changes to the task (probably exec).
- *             We hold the task, terminate all other threads in the task and
- *             wait for them to terminate, clean up the portspace, and when
- *             all done, let the current thread go.
+ * task_start_halt:
+ *
+ *     Shut the current task down (except for the current thread) in
+ *     preparation for dramatic changes to the task (probably exec).
+ *     We hold the task and mark all other threads in the task for
+ *     termination.
  */
 kern_return_t
-task_halt(
+task_start_halt(
        task_t          task)
 {
-       thread_act_t    thr_act, cur_thr_act;
-       task_t          cur_task;
+       thread_t        thread, self;
 
        assert(task != kernel_task);
 
-       cur_thr_act = current_act();
-       cur_task = cur_thr_act->task;
-
-       if (task != cur_task) {
-               return(KERN_INVALID_ARGUMENT);
-       }
+       self = current_thread();
 
-#if    TASK_SWAPPER
-       /*
-        *      If task is not resident (swapped out, or being swapped
-        *      out), we want to bring it back in and make it unswappable.
-        *      This can block, so do it early.
-        */
-       task_swapin(task, TRUE);        /* TRUE means make it unswappable */
-#endif /* TASK_SWAPPER */
+       if (task != self->task)
+               return (KERN_INVALID_ARGUMENT);
 
        task_lock(task);
 
-       if (!task->active || !cur_thr_act->active) {
+       if (task->halting || !task->active || !self->active) {
                /*
                 *      Task or current thread is already being terminated.
                 *      Hurry up and return out of the current kernel context
@@ -767,10 +821,14 @@ task_halt(
                 *      ourselves.
                 */
                task_unlock(task);
-               return(KERN_FAILURE);
+
+               return (KERN_FAILURE);
        }
 
-       if (task->thr_act_count > 1) {
+       task->halting = TRUE;
+
+       if (task->thread_count > 1) {
+
                /*
                 * Mark all the threads to keep them from starting any more
                 * user-level execution.  The thread_terminate_internal code
@@ -780,30 +838,54 @@ task_halt(
                task_hold_locked(task);
 
                /*
-                *      Terminate all the other activations in the task.
-                *
-                *      Each terminated activation will run it's special handler
-                *      when its current kernel context is unwound.  That will
-                *      clean up most of the thread resources.  Then it will be
-                *      handed over to the reaper, who will finally remove the
-                *      thread from the task list and free the structures.
+                *      Terminate all the other threads in the task.
                 */
-               queue_iterate(&task->thr_acts, thr_act, thread_act_t,thr_acts) {
-                       if (thr_act != cur_thr_act)
-                               thread_terminate_internal(thr_act);
+               queue_iterate(&task->threads, thread, thread_t, task_threads) {
+                       if (thread != self)
+                               thread_terminate_internal(thread);
                }
+
                task_release_locked(task);
        }
+       task_unlock(task);
+       return KERN_SUCCESS;
+}
+
+
+/*
+ * task_complete_halt:
+ *
+ *     Complete task halt by waiting for threads to terminate, then clean
+ *     up task resources (VM, port namespace, etc...) and then let the
+ *     current thread go in the (practically empty) task context.
+ */
+void
+task_complete_halt(task_t task)
+{
+       task_lock(task);
+       assert(task->halting);
+       assert(task == current_task());
 
        /*
-        *      If the current thread has any virtual machine state
-        *      associated with it, we need to explicitly clean that
-        *      up now (because we did not terminate the current act)
-        *      before we try to clean up the task VM and port spaces.
+        *      Wait for the other threads to get shut down.
+        *      When the last other thread is reaped, we'll be
+        *      woken up.
         */
-       act_virtual_machine_destroy(cur_thr_act);
+       if (task->thread_count > 1) {
+               assert_wait((event_t)&task->halting, THREAD_UNINT);
+               task_unlock(task);
+               thread_block(THREAD_CONTINUE_NULL);
+       } else {
+               task_unlock(task);
+       }
 
-       task_unlock(task);
+       /*
+        *      Give the machine dependent code a chance
+        *      to perform cleanup of task-level resources
+        *      associated with the current thread before
+        *      ripping apart the task.
+        */
+       machine_task_terminate(task);
 
        /*
         *      Destroy all synchronizers owned by the task.
@@ -814,18 +896,16 @@ task_halt(
         *      Destroy the contents of the IPC space, leaving just
         *      a reference for it.
         */
-       if (!task->kernel_loaded)
-               ipc_space_clean(task->itk_space);
+       ipc_space_clean(task->itk_space);
 
        /*
         * Clean out the address space, as we are going to be
         * getting a new one.
         */
-       (void) vm_map_remove(task->map,
-                            task->map->min_offset,
-                            task->map->max_offset, VM_MAP_NO_FLAGS);
+       vm_map_remove(task->map, task->map->min_offset,
+                     task->map->max_offset, VM_MAP_NO_FLAGS);
 
-       return KERN_SUCCESS;
+       task->halting = FALSE;
 }
 
 /*
@@ -839,9 +919,9 @@ task_halt(
  */
 void
 task_hold_locked(
-       register task_t task)
+       register task_t         task)
 {
-       register thread_act_t   thr_act;
+       register thread_t       thread;
 
        assert(task->active);
 
@@ -849,12 +929,12 @@ task_hold_locked(
                return;
 
        /*
-        *      Iterate through all the thread_act's and hold them.
+        *      Iterate through all the threads and hold them.
         */
-       queue_iterate(&task->thr_acts, thr_act, thread_act_t, thr_acts) {
-               act_lock_thread(thr_act);
-               thread_hold(thr_act);
-               act_unlock_thread(thr_act);
+       queue_iterate(&task->threads, thread, thread_t, task_threads) {
+               thread_mtx_lock(thread);
+               thread_hold(thread);
+               thread_mtx_unlock(thread);
        }
 }
 
@@ -870,25 +950,51 @@ task_hold_locked(
  *     CONDITIONS: the caller holds a reference on the task
  */
 kern_return_t
-task_hold(task_t task)
+task_hold(
+       register task_t         task)
+{
+       if (task == TASK_NULL)
+               return (KERN_INVALID_ARGUMENT);
+
+       task_lock(task);
+
+       if (!task->active) {
+               task_unlock(task);
+
+               return (KERN_FAILURE);
+       }
+
+       task_hold_locked(task);
+       task_unlock(task);
+
+       return (KERN_SUCCESS);
+}
+
+kern_return_t
+task_wait(
+               task_t          task,
+               boolean_t       until_not_runnable)
 {
-        kern_return_t kret;
-        
        if (task == TASK_NULL)
                return (KERN_INVALID_ARGUMENT);
+
        task_lock(task);
+
        if (!task->active) {
                task_unlock(task);
+
                return (KERN_FAILURE);
        }
-        task_hold_locked(task);
-        task_unlock(task);
 
-        return(KERN_SUCCESS);
+       task_wait_locked(task, until_not_runnable);
+       task_unlock(task);
+
+       return (KERN_SUCCESS);
 }
 
 /*
- * Routine:    task_wait_locked
+ *     task_wait_locked:
+ *
  *     Wait for all threads in task to stop.
  *
  * Conditions:
@@ -896,27 +1002,24 @@ task_hold(task_t task)
  */
 void
 task_wait_locked(
-       register task_t         task)
+       register task_t         task,
+       boolean_t               until_not_runnable)
 {
-       register thread_act_t   thr_act, cur_thr_act;
+       register thread_t       thread, self;
 
        assert(task->active);
        assert(task->suspend_count > 0);
 
-       cur_thr_act = current_act();
+       self = current_thread();
+
        /*
-        *      Iterate through all the thread's and wait for them to
+        *      Iterate through all the threads and wait for them to
         *      stop.  Do not wait for the current thread if it is within
         *      the task.
         */
-       queue_iterate(&task->thr_acts, thr_act, thread_act_t, thr_acts) {
-               if (thr_act != cur_thr_act) {
-                       thread_shuttle_t thr_shuttle;
-
-                       thr_shuttle = act_lock_thread(thr_act);
-                       thread_wait(thr_shuttle);
-                       act_unlock_thread(thr_act);
-               }
+       queue_iterate(&task->threads, thread, thread_t, task_threads) {
+               if (thread != self)
+                       thread_wait(thread, until_not_runnable);
        }
 }
 
@@ -929,9 +1032,9 @@ task_wait_locked(
  */
 void
 task_release_locked(
-       register task_t task)
+       register task_t         task)
 {
-       register thread_act_t   thr_act;
+       register thread_t       thread;
 
        assert(task->active);
        assert(task->suspend_count > 0);
@@ -939,15 +1042,10 @@ task_release_locked(
        if (--task->suspend_count > 0)
                return;
 
-       /*
-        *      Iterate through all the thread_act's and hold them.
-        *      Do not hold the current thread_act if it is within the
-        *      task.
-        */
-       queue_iterate(&task->thr_acts, thr_act, thread_act_t, thr_acts) {
-               act_lock_thread(thr_act);
-               thread_release(thr_act);
-               act_unlock_thread(thr_act);
+       queue_iterate(&task->threads, thread, thread_t, task_threads) {
+               thread_mtx_lock(thread);
+               thread_release(thread);
+               thread_mtx_unlock(thread);
        }
 }
 
@@ -960,56 +1058,59 @@ task_release_locked(
  *     CONDITIONS: The caller holds a reference to the task
  */
 kern_return_t
-task_release(task_t task)
+task_release(
+       task_t          task)
 {
-        kern_return_t kret;
-        
        if (task == TASK_NULL)
                return (KERN_INVALID_ARGUMENT);
+
        task_lock(task);
+
        if (!task->active) {
                task_unlock(task);
+
                return (KERN_FAILURE);
        }
-        task_release_locked(task);
-        task_unlock(task);
 
-        return(KERN_SUCCESS);
+       task_release_locked(task);
+       task_unlock(task);
+
+       return (KERN_SUCCESS);
 }
 
 kern_return_t
 task_threads(
-       task_t                  task,
-       thread_act_array_t      *thr_act_list,
+       task_t                                  task,
+       thread_act_array_t              *threads_out,
        mach_msg_type_number_t  *count)
 {
-       unsigned int            actual; /* this many thr_acts */
-       thread_act_t            thr_act;
-       thread_act_t            *thr_acts;
-       thread_t                thread;
-       int                     i, j;
-
-       vm_size_t size, size_needed;
-       vm_offset_t addr;
+       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;
+               return (KERN_INVALID_ARGUMENT);
 
-       size = 0; addr = 0;
+       size = 0; addr = NULL;
 
        for (;;) {
                task_lock(task);
                if (!task->active) {
                        task_unlock(task);
+
                        if (size != 0)
                                kfree(addr, size);
-                       return KERN_FAILURE;
+
+                       return (KERN_FAILURE);
                }
 
-               actual = task->thr_act_count;
+               actual = task->thread_count;
 
                /* do we have the memory we need? */
-               size_needed = actual * sizeof(mach_port_t);
+               size_needed = actual * sizeof (mach_port_t);
                if (size_needed <= size)
                        break;
 
@@ -1024,95 +1125,81 @@ task_threads(
 
                addr = kalloc(size);
                if (addr == 0)
-                       return KERN_RESOURCE_SHORTAGE;
+                       return (KERN_RESOURCE_SHORTAGE);
        }
 
        /* OK, have memory and the task is locked & active */
-       thr_acts = (thread_act_t *) addr;
-
-       for (i = j = 0, thr_act = (thread_act_t) queue_first(&task->thr_acts);
-            i < actual;
-            i++, thr_act = (thread_act_t) queue_next(&thr_act->thr_acts)) {
-               act_lock(thr_act);
-               if (thr_act->ref_count > 0) {
-                       act_locked_act_reference(thr_act);
-                       thr_acts[j++] = thr_act;
-               }
-               act_unlock(thr_act);
+       thread_list = (thread_t *)addr;
+
+       i = j = 0;
+
+       for (thread = (thread_t)queue_first(&task->threads); i < actual;
+                               ++i, thread = (thread_t)queue_next(&thread->task_threads)) {
+               thread_reference_internal(thread);
+               thread_list[j++] = thread;
        }
-       assert(queue_end(&task->thr_acts, (queue_entry_t) thr_act));
+
+       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 thr_act refs */
+       /* can unlock task now that we've got the thread refs */
        task_unlock(task);
 
        if (actual == 0) {
-               /* no thr_acts, so return null pointer and deallocate memory */
+               /* no threads, so return null pointer and deallocate memory */
 
-               *thr_act_list = 0;
+               *threads_out = NULL;
                *count = 0;
 
                if (size != 0)
                        kfree(addr, size);
-       } else {
+       }
+       else {
                /* if we allocated too much, must copy */
 
                if (size_needed < size) {
-                       vm_offset_t newaddr;
+                       void *newaddr;
 
                        newaddr = kalloc(size_needed);
                        if (newaddr == 0) {
-                               for (i = 0; i < actual; i++)
-                                       act_deallocate(thr_acts[i]);
+                               for (i = 0; i < actual; ++i)
+                                       thread_deallocate(thread_list[i]);
                                kfree(addr, size);
-                               return KERN_RESOURCE_SHORTAGE;
+                               return (KERN_RESOURCE_SHORTAGE);
                        }
 
-                       bcopy((char *) addr, (char *) newaddr, size_needed);
+                       bcopy(addr, newaddr, size_needed);
                        kfree(addr, size);
-                       thr_acts = (thread_act_t *) newaddr;
+                       thread_list = (thread_t *)newaddr;
                }
 
-               *thr_act_list = thr_acts;
+               *threads_out = thread_list;
                *count = actual;
 
                /* do the conversion that Mig should handle */
 
-               for (i = 0; i < actual; i++)
-                       ((ipc_port_t *) thr_acts)[i] =
-                               convert_act_to_port(thr_acts[i]);
+               for (i = 0; i < actual; ++i)
+                       ((ipc_port_t *) thread_list)[i] = convert_thread_to_port(thread_list[i]);
        }
 
-       return KERN_SUCCESS;
+       return (KERN_SUCCESS);
 }
 
-/*
- * Routine:    task_suspend
- *     Implement a user-level suspension on a task.
- *
- * Conditions:
- *     The caller holds a reference to the task
- */
-kern_return_t
-task_suspend(
+static kern_return_t
+place_task_hold    (
        register task_t         task)
-{
-       if (task == TASK_NULL)
-               return (KERN_INVALID_ARGUMENT);
-
-       task_lock(task);
+{    
        if (!task->active) {
-               task_unlock(task);
                return (KERN_FAILURE);
        }
-       if ((task->user_stop_count)++ > 0) {
+
+       if (task->user_stop_count++ > 0) {
                /*
                 *      If the stop count was positive, the task is
                 *      already stopped and we can exit.
                 */
-               task_unlock(task);
                return (KERN_SUCCESS);
        }
 
@@ -1123,58 +1210,279 @@ task_suspend(
         * to stop executing user code.
         */
        task_hold_locked(task);
-       task_wait_locked(task);
-       task_unlock(task);
+       task_wait_locked(task, TRUE);
+       
        return (KERN_SUCCESS);
 }
 
+static kern_return_t
+release_task_hold    (
+       register task_t         task,
+       boolean_t           pidresume)
+{
+       register boolean_t release = FALSE;
+    
+       if (!task->active) {
+               return (KERN_FAILURE);
+       }
+       
+       if (pidresume) {
+           if (task->pidsuspended == FALSE) {
+            return (KERN_FAILURE);
+           }
+           task->pidsuspended = FALSE;
+       }
+
+    if (task->user_stop_count > (task->pidsuspended ? 1 : 0)) {
+               if (--task->user_stop_count == 0) {
+                       release = TRUE;
+               }
+       }
+       else {
+               return (KERN_FAILURE);
+       }
+
+       /*
+        *      Release the task if necessary.
+        */
+       if (release)
+               task_release_locked(task);
+               
+    return (KERN_SUCCESS);
+}
+
+/*
+ *     task_suspend:
+ *
+ *     Implement a user-level suspension on a task.
+ *
+ * Conditions:
+ *     The caller holds a reference to the task
+ */
+kern_return_t
+task_suspend(
+       register task_t         task)
+{
+       kern_return_t    kr;
+       
+       if (task == TASK_NULL || task == kernel_task)
+               return (KERN_INVALID_ARGUMENT);
+
+       task_lock(task);
+
+       kr = place_task_hold(task);
+
+       task_unlock(task);
+
+       return (kr);
+}
+
 /*
- * Routine:    task_resume
+ *     task_resume:
  *             Release a kernel hold on a task.
  *             
  * Conditions:
  *             The caller holds a reference to the task
  */
 kern_return_t 
-task_resume(register task_t task)
+task_resume(
+       register task_t task)
 {
-       register boolean_t      release;
+       kern_return_t    kr;
 
-       if (task == TASK_NULL)
-               return(KERN_INVALID_ARGUMENT);
+       if (task == TASK_NULL || task == kernel_task)
+               return (KERN_INVALID_ARGUMENT);
 
-       release = FALSE;
        task_lock(task);
-       if (!task->active) {
-               task_unlock(task);
-               return(KERN_FAILURE);
+
+       kr = release_task_hold(task, FALSE);
+
+       task_unlock(task);
+
+       return (kr);
+}
+
+kern_return_t
+task_pidsuspend_locked(task_t task)
+{
+       kern_return_t kr;
+
+       if (task->pidsuspended) {
+               kr = KERN_FAILURE;
+               goto out;
        }
-       if (task->user_stop_count > 0) {
-               if (--(task->user_stop_count) == 0)
-                       release = TRUE;
+
+       task->pidsuspended = TRUE;
+
+       kr = place_task_hold(task);
+       if (kr != KERN_SUCCESS) {
+               task->pidsuspended = FALSE;
        }
-       else {
-               task_unlock(task);
-               return(KERN_FAILURE);
+out:
+       return(kr);
+}
+
+
+/*
+ *     task_pidsuspend:
+ *
+ *     Suspends a task by placing a hold on its threads.
+ *
+ * Conditions:
+ *     The caller holds a reference to the task
+ */
+kern_return_t
+task_pidsuspend(
+       register task_t         task)
+{
+       kern_return_t    kr;
+    
+       if (task == TASK_NULL || task == kernel_task)
+               return (KERN_INVALID_ARGUMENT);
+
+       task_lock(task);
+
+       kr = task_pidsuspend_locked(task);
+
+       task_unlock(task);
+
+       return (kr);
+}
+
+/* If enabled, we bring all the frozen pages back in prior to resumption; otherwise, they're faulted back in on demand */
+#define THAW_ON_RESUME 1
+
+/*
+ *     task_pidresume:
+ *             Resumes a previously suspended task.
+ *             
+ * Conditions:
+ *             The caller holds a reference to the task
+ */
+kern_return_t 
+task_pidresume(
+       register task_t task)
+{
+       kern_return_t    kr;
+#if (CONFIG_FREEZE && THAW_ON_RESUME)
+    boolean_t frozen;
+#endif
+
+       if (task == TASK_NULL || task == kernel_task)
+               return (KERN_INVALID_ARGUMENT);
+
+       task_lock(task);
+       
+#if (CONFIG_FREEZE && THAW_ON_RESUME)
+    frozen = task->frozen;
+    task->frozen = FALSE;
+#endif
+
+       kr = release_task_hold(task, TRUE);
+
+       task_unlock(task);
+
+#if (CONFIG_FREEZE && THAW_ON_RESUME)
+       if ((kr == KERN_SUCCESS) && (frozen == TRUE)) {
+           kr = vm_map_thaw(task->map);
        }
+#endif
 
-       /*
-        *      Release the task if necessary.
-        */
-       if (release)
-               task_release_locked(task);
+       return (kr);
+}
+
+#if CONFIG_FREEZE
+
+/*
+ *     task_freeze:
+ *
+ *     Freeze a task.
+ *
+ * Conditions:
+ *     The caller holds a reference to the task
+ */
+kern_return_t
+task_freeze(
+       register task_t    task,
+       uint32_t           *purgeable_count,
+       uint32_t           *wired_count,
+       uint32_t           *clean_count,
+       uint32_t           *dirty_count,
+       uint32_t           dirty_budget,
+       boolean_t          *shared,
+       boolean_t          walk_only)
+{
+       kern_return_t kr;
+    
+       if (task == TASK_NULL || task == kernel_task)
+               return (KERN_INVALID_ARGUMENT);
+
+       task_lock(task);
+
+       if (task->frozen) {
+           task_unlock(task);
+           return (KERN_FAILURE);
+       }
+
+    if (walk_only == FALSE) {
+           task->frozen = TRUE;
+    }
 
        task_unlock(task);
-       return(KERN_SUCCESS);
+
+       if (walk_only) {
+               kr = vm_map_freeze_walk(task->map, purgeable_count, wired_count, clean_count, dirty_count, dirty_budget, shared);               
+       } else {
+               kr = vm_map_freeze(task->map, purgeable_count, wired_count, clean_count, dirty_count, dirty_budget, shared);
+       }
+
+       return (kr);
+}
+
+/*
+ *     task_thaw:
+ *
+ *     Thaw a currently frozen task.
+ *
+ * Conditions:
+ *     The caller holds a reference to the task
+ */
+kern_return_t
+task_thaw(
+       register task_t         task)
+{
+       kern_return_t kr;
+    
+       if (task == TASK_NULL || task == kernel_task)
+               return (KERN_INVALID_ARGUMENT);
+
+       task_lock(task);
+       
+       if (!task->frozen) {
+           task_unlock(task);
+           return (KERN_FAILURE);
+       }
+
+       task->frozen = FALSE;
+
+       task_unlock(task);
+
+       kr = vm_map_thaw(task->map);
+
+       return (kr);
 }
 
+#endif /* CONFIG_FREEZE */
+
 kern_return_t
 host_security_set_task_token(
         host_security_t  host_security,
         task_t          task,
         security_token_t sec_token,
+       audit_token_t    audit_token,
        host_priv_t      host_priv)
 {
+       ipc_port_t       host_port;
        kern_return_t    kr;
 
        if (task == TASK_NULL)
@@ -1185,46 +1493,19 @@ host_security_set_task_token(
 
         task_lock(task);
         task->sec_token = sec_token;
+       task->audit_token = audit_token;
         task_unlock(task);
 
        if (host_priv != HOST_PRIV_NULL) {
-               kr = task_set_special_port(task,
-                               TASK_HOST_PORT,
-                               ipc_port_make_send(realhost.host_priv_self));
+               kr = host_get_host_priv_port(host_priv, &host_port);
        } else {
-               kr = task_set_special_port(task,
-                               TASK_HOST_PORT,
-                               ipc_port_make_send(realhost.host_self));
+               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);
 }
 
-/*
- * Utility routine to set a ledger
- */
-kern_return_t
-task_set_ledger(
-        task_t         task,
-        ledger_t       wired,
-        ledger_t       paged)
-{
-       if (task == TASK_NULL)
-               return(KERN_INVALID_ARGUMENT);
-
-        task_lock(task);
-        if (wired) {
-                ipc_port_release_send(task->wired_ledger_port);
-                task->wired_ledger_port = ledger_copy(wired);
-        }                
-        if (paged) {
-                ipc_port_release_send(task->paged_ledger_port);
-                task->paged_ledger_port = ledger_copy(paged);
-        }                
-        task_unlock(task);
-
-        return(KERN_SUCCESS);
-}
-
 /*
  * This routine was added, pretty much exclusively, for registering the
  * RPC glue vector for in-kernel short circuited tasks.  Rather than
@@ -1237,11 +1518,9 @@ kern_return_t
 task_set_info(
        task_t          task,
        task_flavor_t   flavor,
-       task_info_t     task_info_in,           /* pointer to IN array */
-       mach_msg_type_number_t  task_info_count)
+       __unused task_info_t    task_info_in,           /* pointer to IN array */
+       __unused mach_msg_type_number_t task_info_count)
 {
-       vm_map_t                map;
-
        if (task == TASK_NULL)
                return(KERN_INVALID_ARGUMENT);
 
@@ -1254,188 +1533,473 @@ task_set_info(
 
 kern_return_t
 task_info(
-       task_t                  task,
-       task_flavor_t           flavor,
-       task_info_t             task_info_out,
+       task_t                                  task,
+       task_flavor_t                   flavor,
+       task_info_t                             task_info_out,
        mach_msg_type_number_t  *task_info_count)
 {
-       thread_t        thread;
-       vm_map_t        map;
+       kern_return_t error = KERN_SUCCESS;
 
        if (task == TASK_NULL)
-               return(KERN_INVALID_ARGUMENT);
+               return (KERN_INVALID_ARGUMENT);
 
-       switch (flavor) {
+       task_lock(task);
+
+       if ((task != current_task()) && (!task->active)) {
+               task_unlock(task);
+               return (KERN_INVALID_ARGUMENT);
+       }
 
-           case TASK_BASIC_INFO:
-           {
-               register task_basic_info_t      basic_info;
+       switch (flavor) {
 
-               if (*task_info_count < TASK_BASIC_INFO_COUNT) {
-                   return(KERN_INVALID_ARGUMENT);
+       case TASK_BASIC_INFO_32:
+       case TASK_BASIC2_INFO_32:
+       {
+               task_basic_info_32_t    basic_info;
+               vm_map_t                                map;
+               clock_sec_t                             secs;
+               clock_usec_t                    usecs;
+
+               if (*task_info_count < TASK_BASIC_INFO_32_COUNT) {
+                   error = KERN_INVALID_ARGUMENT;
+                   break;
                }
 
-               basic_info = (task_basic_info_t) task_info_out;
-
-               map = (task == kernel_task) ? kernel_map : task->map;
+               basic_info = (task_basic_info_32_t)task_info_out;
 
-               basic_info->virtual_size  = map->size;
-               basic_info->resident_size = pmap_resident_count(map->pmap)
-                                                  * PAGE_SIZE;
+               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;
 
-               task_lock(task);
                basic_info->policy = ((task != kernel_task)?
                                                                                  POLICY_TIMESHARE: POLICY_RR);
                basic_info->suspend_count = task->user_stop_count;
-               basic_info->user_time.seconds
-                               = task->total_user_time.seconds;
-               basic_info->user_time.microseconds
-                               = task->total_user_time.microseconds;
-               basic_info->system_time.seconds
-                               = task->total_system_time.seconds;
-               basic_info->system_time.microseconds 
-                               = task->total_system_time.microseconds;
-               task_unlock(task);
 
-               *task_info_count = TASK_BASIC_INFO_COUNT;
+               absolutetime_to_microtime(task->total_user_time, &secs, &usecs);
+               basic_info->user_time.seconds = 
+                       (typeof(basic_info->user_time.seconds))secs;
+               basic_info->user_time.microseconds = usecs;
+
+               absolutetime_to_microtime(task->total_system_time, &secs, &usecs);
+               basic_info->system_time.seconds = 
+                       (typeof(basic_info->system_time.seconds))secs;
+               basic_info->system_time.microseconds = usecs;
+
+               *task_info_count = TASK_BASIC_INFO_32_COUNT;
                break;
-           }
+       }
 
-           case TASK_THREAD_TIMES_INFO:
-           {
-               register task_thread_times_info_t times_info;
-               register thread_t       thread;
-               register thread_act_t   thr_act;
+       case TASK_BASIC_INFO_64:
+       {
+               task_basic_info_64_t    basic_info;
+               vm_map_t                                map;
+               clock_sec_t                             secs;
+               clock_usec_t                    usecs;
 
-               if (*task_info_count < TASK_THREAD_TIMES_INFO_COUNT) {
-                   return (KERN_INVALID_ARGUMENT);
+               if (*task_info_count < TASK_BASIC_INFO_64_COUNT) {
+                   error = KERN_INVALID_ARGUMENT;
+                   break;
                }
 
-               times_info = (task_thread_times_info_t) task_info_out;
-               times_info->user_time.seconds = 0;
-               times_info->user_time.microseconds = 0;
-               times_info->system_time.seconds = 0;
-               times_info->system_time.microseconds = 0;
+               basic_info = (task_basic_info_64_t)task_info_out;
 
-               task_lock(task);
-               queue_iterate(&task->thr_acts, thr_act,
-                             thread_act_t, thr_acts)
-               {
-                   time_value_t user_time, system_time;
-                   spl_t        s;
+               map = (task == kernel_task)? kernel_map: task->map;
+               basic_info->virtual_size  = map->size;
+               basic_info->resident_size =
+                       (mach_vm_size_t)(pmap_resident_count(map->pmap))
+                       * PAGE_SIZE_64;
 
-                   thread = act_lock_thread(thr_act);
+               basic_info->policy = ((task != kernel_task)?
+                                                                                 POLICY_TIMESHARE: POLICY_RR);
+               basic_info->suspend_count = task->user_stop_count;
 
-                   /* JMM - add logic to skip threads that have migrated
-                    * into this task?
-                    */
+               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;
 
-                   assert(thread);  /* Must have thread */
-                   s = splsched();
-                   thread_lock(thread);
+               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;
 
-                   thread_read_times(thread, &user_time, &system_time);
+               *task_info_count = TASK_BASIC_INFO_64_COUNT;
+               break;
+       }
 
-                   thread_unlock(thread);
-                   splx(s);
-                   act_unlock_thread(thr_act);
+       case MACH_TASK_BASIC_INFO:
+       {
+               mach_task_basic_info_t  basic_info;
+               vm_map_t                map;
+               clock_sec_t             secs;
+               clock_usec_t            usecs;
 
-                   time_value_add(&times_info->user_time, &user_time);
-                   time_value_add(&times_info->system_time, &system_time);
+               if (*task_info_count < MACH_TASK_BASIC_INFO_COUNT) {
+                   error = KERN_INVALID_ARGUMENT;
+                   break;
                }
-               task_unlock(task);
+
+               basic_info = (mach_task_basic_info_t)task_info_out;
+
+               map = (task == kernel_task) ? kernel_map : task->map;
+
+               basic_info->virtual_size  = map->size;
+
+               basic_info->resident_size =
+                   (mach_vm_size_t)(pmap_resident_count(map->pmap));
+               basic_info->resident_size *= PAGE_SIZE_64;
+
+               basic_info->resident_size_max =
+                   (mach_vm_size_t)(pmap_resident_max(map->pmap));
+               basic_info->resident_size_max *= PAGE_SIZE_64;
+
+               basic_info->policy = ((task != kernel_task) ? 
+                                     POLICY_TIMESHARE : POLICY_RR);
+
+               basic_info->suspend_count = task->user_stop_count;
+
+               absolutetime_to_microtime(task->total_user_time, &secs, &usecs);
+               basic_info->user_time.seconds = 
+                   (typeof(basic_info->user_time.seconds))secs;
+               basic_info->user_time.microseconds = usecs;
+
+               absolutetime_to_microtime(task->total_system_time, &secs, &usecs);
+               basic_info->system_time.seconds =
+                   (typeof(basic_info->system_time.seconds))secs;
+               basic_info->system_time.microseconds = usecs;
+
+               *task_info_count = MACH_TASK_BASIC_INFO_COUNT;
+               break;
+       }
+
+       case TASK_THREAD_TIMES_INFO:
+       {
+               register task_thread_times_info_t       times_info;
+               register thread_t                                       thread;
+
+               if (*task_info_count < TASK_THREAD_TIMES_INFO_COUNT) {
+                   error = KERN_INVALID_ARGUMENT;
+                   break;
+               }
+
+               times_info = (task_thread_times_info_t) task_info_out;
+               times_info->user_time.seconds = 0;
+               times_info->user_time.microseconds = 0;
+               times_info->system_time.seconds = 0;
+               times_info->system_time.microseconds = 0;
+
+
+               queue_iterate(&task->threads, thread, thread_t, task_threads) {
+                   time_value_t        user_time, system_time;
+
+                   thread_read_times(thread, &user_time, &system_time);
+
+                   time_value_add(&times_info->user_time, &user_time);
+                   time_value_add(&times_info->system_time, &system_time);
+               }
+
 
                *task_info_count = TASK_THREAD_TIMES_INFO_COUNT;
                break;
-           }
+       }
+
+       case TASK_ABSOLUTETIME_INFO:
+       {
+               task_absolutetime_info_t        info;
+               register thread_t                       thread;
 
-           case TASK_SCHED_FIFO_INFO:
-           {
+               if (*task_info_count < TASK_ABSOLUTETIME_INFO_COUNT) {
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
+               }
 
-               if (*task_info_count < POLICY_FIFO_BASE_COUNT)
-                       return(KERN_INVALID_ARGUMENT);
+               info = (task_absolutetime_info_t)task_info_out;
+               info->threads_user = info->threads_system = 0;
 
-               return(KERN_INVALID_POLICY);
-           }
 
-           case TASK_SCHED_RR_INFO:
-           {
+               info->total_user = task->total_user_time;
+               info->total_system = task->total_system_time;
+
+               queue_iterate(&task->threads, thread, thread_t, task_threads) {
+                       uint64_t        tval;
+                       spl_t           x;
+
+                       x = splsched();
+                       thread_lock(thread);
+
+                       tval = timer_grab(&thread->user_timer);
+                       info->threads_user += tval;
+                       info->total_user += tval;
+
+                       tval = timer_grab(&thread->system_timer);
+                       if (thread->precise_user_kernel_time) {
+                               info->threads_system += tval;
+                               info->total_system += tval;
+                       } else {
+                               /* system_timer may represent either sys or user */
+                               info->threads_user += tval;
+                               info->total_user += tval;
+                       }
+
+                       thread_unlock(thread);
+                       splx(x);
+               }
+
+
+               *task_info_count = TASK_ABSOLUTETIME_INFO_COUNT;
+               break;
+       }
+
+       case TASK_DYLD_INFO:
+       {
+               task_dyld_info_t info;
+
+               /*
+                * We added the format field to TASK_DYLD_INFO output.  For
+                * temporary backward compatibility, accept the fact that
+                * clients may ask for the old version - distinquished by the
+                * size of the expected result structure.
+                */
+#define TASK_LEGACY_DYLD_INFO_COUNT \
+               offsetof(struct task_dyld_info, all_image_info_format)/sizeof(natural_t)
+
+               if (*task_info_count < TASK_LEGACY_DYLD_INFO_COUNT) {
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
+               }
+
+               info = (task_dyld_info_t)task_info_out;
+               info->all_image_info_addr = task->all_image_info_addr;
+               info->all_image_info_size = task->all_image_info_size;
+
+               /* only set format on output for those expecting it */
+               if (*task_info_count >= TASK_DYLD_INFO_COUNT) {
+                       info->all_image_info_format = task_has_64BitAddr(task) ?
+                                                TASK_DYLD_ALL_IMAGE_INFO_64 : 
+                                                TASK_DYLD_ALL_IMAGE_INFO_32 ;
+                       *task_info_count = TASK_DYLD_INFO_COUNT;
+               } else {
+                       *task_info_count = TASK_LEGACY_DYLD_INFO_COUNT;
+               }
+               break;
+       }
+
+       case TASK_EXTMOD_INFO:
+       {
+               task_extmod_info_t info;
+               void *p;
+
+               if (*task_info_count < TASK_EXTMOD_INFO_COUNT) {
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
+               }
+
+               info = (task_extmod_info_t)task_info_out;
+
+               p = get_bsdtask_info(task);
+               if (p) {
+                       proc_getexecutableuuid(p, info->task_uuid, sizeof(info->task_uuid));
+               } else {
+                       bzero(info->task_uuid, sizeof(info->task_uuid));
+               }
+               info->extmod_statistics = task->extmod_statistics;
+               *task_info_count = TASK_EXTMOD_INFO_COUNT;
+
+               break;
+       }
+
+       case TASK_KERNELMEMORY_INFO:
+       {
+               task_kernelmemory_info_t        tkm_info;
+               ledger_amount_t                 credit, debit;
+
+               if (*task_info_count < TASK_KERNELMEMORY_INFO_COUNT) {
+                  error = KERN_INVALID_ARGUMENT;
+                  break;
+               }
+
+               tkm_info = (task_kernelmemory_info_t) task_info_out;
+               tkm_info->total_palloc = 0;
+               tkm_info->total_pfree = 0;
+               tkm_info->total_salloc = 0;
+               tkm_info->total_sfree = 0;
+
+               if (task == kernel_task) {
+                       /*
+                        * All shared allocs/frees from other tasks count against
+                        * the kernel private memory usage.  If we are looking up
+                        * info for the kernel task, gather from everywhere.
+                        */
+                       task_unlock(task);
+
+                       /* start by accounting for all the terminated tasks against the kernel */
+                       tkm_info->total_palloc = tasks_tkm_private.alloc + tasks_tkm_shared.alloc;
+                       tkm_info->total_pfree = tasks_tkm_private.free + tasks_tkm_shared.free;
+
+                       /* count all other task/thread shared alloc/free against the kernel */
+                       lck_mtx_lock(&tasks_threads_lock);
+
+                       /* XXX this really shouldn't be using the function parameter 'task' as a local var! */
+                       queue_iterate(&tasks, task, task_t, tasks) {
+                               if (task == kernel_task) {
+                                       if (ledger_get_entries(task->ledger,
+                                           task_ledgers.tkm_private, &credit,
+                                           &debit) == KERN_SUCCESS) {
+                                               tkm_info->total_palloc += credit;
+                                               tkm_info->total_pfree += debit;
+                                       }
+                               }
+                               if (!ledger_get_entries(task->ledger,
+                                   task_ledgers.tkm_shared, &credit, &debit)) {
+                                       tkm_info->total_palloc += credit;
+                                       tkm_info->total_pfree += debit;
+                               }
+                       }
+                       lck_mtx_unlock(&tasks_threads_lock);
+               } else {
+                       if (!ledger_get_entries(task->ledger,
+                           task_ledgers.tkm_private, &credit, &debit)) {
+                               tkm_info->total_palloc = credit;
+                               tkm_info->total_pfree = debit;
+                       }
+                       if (!ledger_get_entries(task->ledger,
+                           task_ledgers.tkm_shared, &credit, &debit)) {
+                               tkm_info->total_salloc = credit;
+                               tkm_info->total_sfree = debit;
+                       }
+                       task_unlock(task);
+               }
+
+               *task_info_count = TASK_KERNELMEMORY_INFO_COUNT;
+               return KERN_SUCCESS;
+       }
+
+       /* OBSOLETE */
+       case TASK_SCHED_FIFO_INFO:
+       {
+
+               if (*task_info_count < POLICY_FIFO_BASE_COUNT) {
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
+               }
+
+               error = KERN_INVALID_POLICY;
+               break;
+       }
+
+       /* OBSOLETE */
+       case TASK_SCHED_RR_INFO:
+       {
                register policy_rr_base_t       rr_base;
+               uint32_t quantum_time;
+               uint64_t quantum_ns;
 
-               if (*task_info_count < POLICY_RR_BASE_COUNT)
-                       return(KERN_INVALID_ARGUMENT);
+               if (*task_info_count < POLICY_RR_BASE_COUNT) {
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
+               }
 
                rr_base = (policy_rr_base_t) task_info_out;
 
-               task_lock(task);
                if (task != kernel_task) {
-                       task_unlock(task);
-                       return(KERN_INVALID_POLICY);
+                       error = KERN_INVALID_POLICY;
+                       break;
                }
 
                rr_base->base_priority = task->priority;
-               task_unlock(task);
 
-               rr_base->quantum = tick / 1000;
+               quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
+               absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
+               
+               rr_base->quantum = (uint32_t)(quantum_ns / 1000 / 1000);
 
                *task_info_count = POLICY_RR_BASE_COUNT;
                break;
-           }
+       }
 
-           case TASK_SCHED_TIMESHARE_INFO:
-           {
+       /* OBSOLETE */
+       case TASK_SCHED_TIMESHARE_INFO:
+       {
                register policy_timeshare_base_t        ts_base;
 
-               if (*task_info_count < POLICY_TIMESHARE_BASE_COUNT)
-                       return(KERN_INVALID_ARGUMENT);
+               if (*task_info_count < POLICY_TIMESHARE_BASE_COUNT) {
+                       error = KERN_INVALID_ARGUMENT;
+                       break;
+               }
 
                ts_base = (policy_timeshare_base_t) task_info_out;
 
-               task_lock(task);
                if (task == kernel_task) {
-                       task_unlock(task);
-                       return(KERN_INVALID_POLICY);
+                       error = KERN_INVALID_POLICY;
+                       break;
                }
 
                ts_base->base_priority = task->priority;
-               task_unlock(task);
 
                *task_info_count = POLICY_TIMESHARE_BASE_COUNT;
                break;
-           }
+       }
 
-            case TASK_SECURITY_TOKEN:
-           {
-                register security_token_t      *sec_token_p;
+       case TASK_SECURITY_TOKEN:
+       {
+               register security_token_t       *sec_token_p;
 
                if (*task_info_count < TASK_SECURITY_TOKEN_COUNT) {
-                   return(KERN_INVALID_ARGUMENT);
+                   error = KERN_INVALID_ARGUMENT;
+                   break;
                }
 
                sec_token_p = (security_token_t *) task_info_out;
 
-               task_lock(task);
                *sec_token_p = task->sec_token;
-               task_unlock(task);
 
                *task_info_count = TASK_SECURITY_TOKEN_COUNT;
-                break;
-            }
+               break;
+       }
+            
+       case TASK_AUDIT_TOKEN:
+       {
+               register audit_token_t  *audit_token_p;
+
+               if (*task_info_count < TASK_AUDIT_TOKEN_COUNT) {
+                   error = KERN_INVALID_ARGUMENT;
+                   break;
+               }
+
+               audit_token_p = (audit_token_t *) task_info_out;
+
+               *audit_token_p = task->audit_token;
+
+               *task_info_count = TASK_AUDIT_TOKEN_COUNT;
+               break;
+       }
             
-           case TASK_SCHED_INFO:
-                       return(KERN_INVALID_ARGUMENT);
+       case TASK_SCHED_INFO:
+               error = KERN_INVALID_ARGUMENT;
+               break;
 
-           case TASK_EVENTS_INFO:
-           {
+       case TASK_EVENTS_INFO:
+       {
                register task_events_info_t     events_info;
+               register thread_t                       thread;
 
                if (*task_info_count < TASK_EVENTS_INFO_COUNT) {
-                   return(KERN_INVALID_ARGUMENT);
+                  error = KERN_INVALID_ARGUMENT;
+                  break;
                }
 
                events_info = (task_events_info_t) task_info_out;
 
-               task_lock(task);
+
                events_info->faults = task->faults;
                events_info->pageins = task->pageins;
                events_info->cow_faults = task->cow_faults;
@@ -1443,18 +2007,156 @@ task_info(
                events_info->messages_received = task->messages_received;
                events_info->syscalls_mach = task->syscalls_mach;
                events_info->syscalls_unix = task->syscalls_unix;
-               events_info->csw = task->csw;
-               task_unlock(task);
+
+               events_info->csw = task->c_switch;
+
+               queue_iterate(&task->threads, thread, thread_t, task_threads) {
+                       events_info->csw           += thread->c_switch;
+                       events_info->syscalls_mach += thread->syscalls_mach;
+                       events_info->syscalls_unix += thread->syscalls_unix;
+               }
+
 
                *task_info_count = TASK_EVENTS_INFO_COUNT;
                break;
-           }
+       }
+       case TASK_AFFINITY_TAG_INFO:
+       {
+               if (*task_info_count < TASK_AFFINITY_TAG_INFO_COUNT) {
+                   error = KERN_INVALID_ARGUMENT;
+                   break;
+               }
 
-           default:
-               return (KERN_INVALID_ARGUMENT);
+               error = task_affinity_info(task, task_info_out, task_info_count);
+               break;
+       }
+       default:
+               error = KERN_INVALID_ARGUMENT;
+       }
+
+       task_unlock(task);
+       return (error);
+}
+
+void
+task_vtimer_set(
+       task_t          task,
+       integer_t       which)
+{
+       thread_t        thread;
+       spl_t           x;
+
+       /* assert(task == current_task()); */ /* bogus assert 4803227 4807483 */
+
+       task_lock(task);
+
+       task->vtimers |= which;
+
+       switch (which) {
+
+       case TASK_VTIMER_USER:
+               queue_iterate(&task->threads, thread, thread_t, task_threads) {
+                       x = splsched();
+                       thread_lock(thread);
+                       if (thread->precise_user_kernel_time)
+                               thread->vtimer_user_save = timer_grab(&thread->user_timer);
+                       else
+                               thread->vtimer_user_save = timer_grab(&thread->system_timer);
+                       thread_unlock(thread);
+                       splx(x);
+               }
+               break;
+
+       case TASK_VTIMER_PROF:
+               queue_iterate(&task->threads, thread, thread_t, task_threads) {
+                       x = splsched();
+                       thread_lock(thread);
+                       thread->vtimer_prof_save = timer_grab(&thread->user_timer);
+                       thread->vtimer_prof_save += timer_grab(&thread->system_timer);
+                       thread_unlock(thread);
+                       splx(x);
+               }
+               break;
+
+       case TASK_VTIMER_RLIM:
+               queue_iterate(&task->threads, thread, thread_t, task_threads) {
+                       x = splsched();
+                       thread_lock(thread);
+                       thread->vtimer_rlim_save = timer_grab(&thread->user_timer);
+                       thread->vtimer_rlim_save += timer_grab(&thread->system_timer);
+                       thread_unlock(thread);
+                       splx(x);
+               }
+               break;
+       }
+
+       task_unlock(task);
+}
+
+void
+task_vtimer_clear(
+       task_t          task,
+       integer_t       which)
+{
+       assert(task == current_task());
+
+       task_lock(task);
+
+       task->vtimers &= ~which;
+
+       task_unlock(task);
+}
+
+void
+task_vtimer_update(
+__unused
+       task_t          task,
+       integer_t       which,
+       uint32_t        *microsecs)
+{
+       thread_t        thread = current_thread();
+       uint32_t        tdelt;
+       clock_sec_t     secs;
+       uint64_t        tsum;
+
+       assert(task == current_task());
+
+       assert(task->vtimers & which);
+
+       secs = tdelt = 0;
+
+       switch (which) {
+
+       case TASK_VTIMER_USER:
+               if (thread->precise_user_kernel_time) {
+                       tdelt = (uint32_t)timer_delta(&thread->user_timer,
+                                                               &thread->vtimer_user_save);
+               } else {
+                       tdelt = (uint32_t)timer_delta(&thread->system_timer,
+                                                               &thread->vtimer_user_save);
+               }
+               absolutetime_to_microtime(tdelt, &secs, microsecs);
+               break;
+
+       case TASK_VTIMER_PROF:
+               tsum = timer_grab(&thread->user_timer);
+               tsum += timer_grab(&thread->system_timer);
+               tdelt = (uint32_t)(tsum - thread->vtimer_prof_save);
+               absolutetime_to_microtime(tdelt, &secs, microsecs);
+               /* if the time delta is smaller than a usec, ignore */
+               if (*microsecs != 0)
+                       thread->vtimer_prof_save = tsum;
+               break;
+
+       case TASK_VTIMER_RLIM:
+               tsum = timer_grab(&thread->user_timer);
+               tsum += timer_grab(&thread->system_timer);
+               tdelt = (uint32_t)(tsum - thread->vtimer_rlim_save);
+               thread->vtimer_rlim_save = tsum;
+               absolutetime_to_microtime(tdelt, &secs, microsecs);
+               break;
        }
 
-       return(KERN_SUCCESS);
 }
 
 /*
@@ -1464,13 +2166,10 @@ task_info(
  */
 kern_return_t
 task_assign(
-       task_t          task,
-       processor_set_t new_pset,
-       boolean_t       assign_threads)
+       __unused task_t         task,
+       __unused processor_set_t        new_pset,
+       __unused boolean_t      assign_threads)
 {
-#ifdef lint
-       task++; new_pset++; assign_threads++;
-#endif /* lint */
        return(KERN_FAILURE);
 }
 
@@ -1484,7 +2183,7 @@ task_assign_default(
        task_t          task,
        boolean_t       assign_threads)
 {
-    return (task_assign(task, &default_pset, assign_threads));
+    return (task_assign(task, &pset0, assign_threads));
 }
 
 /*
@@ -1500,9 +2199,9 @@ task_get_assignment(
        if (!task->active)
                return(KERN_FAILURE);
 
-       *pset = task->processor_set;
-       pset_reference(*pset);
-       return(KERN_SUCCESS);
+       *pset = &pset0;
+
+       return (KERN_SUCCESS);
 }
 
 
@@ -1515,12 +2214,12 @@ task_get_assignment(
  */
 kern_return_t
 task_policy(
-       task_t                                  task,
-       policy_t                                policy_id,
-       policy_base_t                   base,
-       mach_msg_type_number_t  count,
-       boolean_t                               set_limit,
-       boolean_t                               change)
+       __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);
 }
@@ -1535,110 +2234,25 @@ task_policy(
  */
 kern_return_t
 task_set_policy(
-       task_t                                  task,
-       processor_set_t                 pset,
-       policy_t                                policy_id,
-       policy_base_t                   base,
-       mach_msg_type_number_t  base_count,
-       policy_limit_t                  limit,
-       mach_msg_type_number_t  limit_count,
-       boolean_t                               change)
+       __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);
 }
 
-/*
- *     task_collect_scan:
- *
- *     Attempt to free resources owned by tasks.
- */
-
-void
-task_collect_scan(void)
-{
-       register task_t         task, prev_task;
-       processor_set_t         pset = &default_pset;
-
-       pset_lock(pset);
-       pset->ref_count++;
-       task = (task_t) queue_first(&pset->tasks);
-       while (!queue_end(&pset->tasks, (queue_entry_t) task)) {
-               task_lock(task);
-               if (task->ref_count > 0) {
-
-                       task_reference_locked(task);
-                       task_unlock(task);
-
-#if MACH_HOST
-                       /*
-                        *      While we still have the pset locked, freeze the task in
-                        *      this pset.  That way, when we get back from collecting
-                        *      it, we can dereference the pset_tasks chain for the task
-                        *      and be assured that we are still in this chain.
-                        */
-                       task_freeze(task);
-#endif
-
-                       pset_unlock(pset);
-
-                       pmap_collect(task->map->pmap);
-
-                       pset_lock(pset);
-                       prev_task = task;
-                       task = (task_t) queue_next(&task->pset_tasks);
-
-#if MACH_HOST
-                       task_unfreeze(prev_task);
-#endif
-
-                       task_deallocate(prev_task);
-               } else {
-                       task_unlock(task);
-                       task = (task_t) queue_next(&task->pset_tasks);
-               }
-       }
-
-       pset_unlock(pset);
-
-       pset_deallocate(pset);
-}
-
-/* Also disabled in vm/vm_pageout.c */
-boolean_t task_collect_allowed = FALSE;
-unsigned task_collect_last_tick = 0;
-unsigned task_collect_max_rate = 0;            /* in ticks */
-
-/*
- *     consider_task_collect:
- *
- *     Called by the pageout daemon when the system needs more free pages.
- */
-
-void
-consider_task_collect(void)
-{
-       /*
-        *      By default, don't attempt task collection more frequently
-        *      than once per second.
-        */
-
-       if (task_collect_max_rate == 0)
-               task_collect_max_rate = (1 << SCHED_TICK_SHIFT) + 1;
-
-       if (task_collect_allowed &&
-           (sched_tick > (task_collect_last_tick + task_collect_max_rate))) {
-               task_collect_last_tick = sched_tick;
-               task_collect_scan();
-       }
-}
-
+#if    FAST_TAS
 kern_return_t
 task_set_ras_pc(
        task_t          task,
        vm_offset_t     pc,
        vm_offset_t     endpc)
 {
-#if    FAST_TAS
        extern int fast_tas_debug;
  
        if (fast_tas_debug) {
@@ -1650,18 +2264,17 @@ task_set_ras_pc(
        task->fast_tas_end =  endpc;
        task_unlock(task);
        return KERN_SUCCESS;
+} 
 #else  /* FAST_TAS */
-#ifdef lint
-       task++;
-       pc++;
-       endpc++;
-#endif /* lint */
+kern_return_t
+task_set_ras_pc(
+       __unused task_t task,
+       __unused vm_offset_t    pc,
+       __unused vm_offset_t    endpc)
+{
        return KERN_FAILURE;
-#endif /* FAST_TAS */
 }
+#endif /* FAST_TAS */
 
 void
 task_synchronizer_destroy_all(task_t task)
@@ -1689,25 +2302,69 @@ task_synchronizer_destroy_all(task_t task)
 }
 
 /*
- *     task_set_port_space:
- *
- *     Set port name space of task to specified size.
+ * Install default (machine-dependent) initial thread state 
+ * on the task.  Subsequent thread creation will have this initial
+ * state set on the thread by machine_thread_inherit_taskwide().
+ * Flavors and structures are exactly the same as those to thread_set_state()
  */
+kern_return_t 
+task_set_state(
+       task_t task, 
+       int flavor, 
+       thread_state_t state, 
+       mach_msg_type_number_t state_count)
+{
+       kern_return_t ret;
 
-kern_return_t
-task_set_port_space(
-       task_t          task,
-       int             table_entries)
+       if (task == TASK_NULL) {
+               return (KERN_INVALID_ARGUMENT);
+       }
+
+       task_lock(task);
+
+       if (!task->active) {
+               task_unlock(task);
+               return (KERN_FAILURE);
+       }
+
+       ret = machine_task_set_state(task, flavor, state, state_count);
+
+       task_unlock(task);
+       return ret;
+}
+
+/*
+ * Examine the default (machine-dependent) initial thread state 
+ * on the task, as set by task_set_state().  Flavors and structures
+ * are exactly the same as those passed to thread_get_state().
+ */
+kern_return_t 
+task_get_state(
+       task_t  task, 
+       int     flavor,
+       thread_state_t state,
+       mach_msg_type_number_t *state_count)
 {
-       kern_return_t kr;
-       
-       is_write_lock(task->itk_space);
-       kr = ipc_entry_grow_table(task->itk_space, table_entries);
-       if (kr == KERN_SUCCESS)
-               is_write_unlock(task->itk_space);
-       return kr;
+       kern_return_t ret;
+
+       if (task == TASK_NULL) {
+               return (KERN_INVALID_ARGUMENT);
+       }
+
+       task_lock(task);
+
+       if (!task->active) {
+               task_unlock(task);
+               return (KERN_FAILURE);
+       }
+
+       ret = machine_task_get_state(task, flavor, state, state_count);
+
+       task_unlock(task);
+       return ret;
 }
 
+
 /*
  * We need to export some functions to other components that
  * are currently implemented in macros within the osfmk
@@ -1716,13 +2373,113 @@ task_set_port_space(
 boolean_t is_kerneltask(task_t t)
 {
        if (t == kernel_task)
-               return(TRUE);
-       else
-               return((t->kernel_loaded));
+               return (TRUE);
+
+       return (FALSE);
+}
+
+int
+check_for_tasksuspend(task_t task)
+{
+
+       if (task == TASK_NULL)
+               return (0);
+
+       return (task->suspend_count > 0);
 }
 
 #undef current_task
-task_t current_task()
+task_t current_task(void);
+task_t current_task(void)
 {
        return (current_task_fast());
 }
+
+#undef task_reference
+void task_reference(task_t task);
+void
+task_reference(
+       task_t          task)
+{
+       if (task != TASK_NULL)
+               task_reference_internal(task);
+}
+
+/* 
+ * This routine is called always with task lock held.
+ * And it returns a thread handle without reference as the caller
+ * operates on it under the task lock held.
+ */
+thread_t
+task_findtid(task_t task, uint64_t tid)
+{
+       thread_t thread= THREAD_NULL;
+
+       queue_iterate(&task->threads, thread, thread_t, task_threads) {
+                       if (thread->thread_id == tid)
+                               return(thread);
+       }
+       return(THREAD_NULL);
+}
+
+
+#if CONFIG_MACF_MACH
+/*
+ * Protect 2 task labels against modification by adding a reference on
+ * both label handles. The locks do not actually have to be held while
+ * using the labels as only labels with one reference can be modified
+ * in place.
+ */
+
+void
+tasklabel_lock2(
+       task_t a,
+       task_t b)
+{
+       labelh_reference(a->label);
+       labelh_reference(b->label);
+}
+
+void
+tasklabel_unlock2(
+       task_t a,
+       task_t b)
+{
+       labelh_release(a->label);
+       labelh_release(b->label);
+}
+
+void
+mac_task_label_update_internal(
+       struct label    *pl,
+       struct task     *task)
+{
+
+       tasklabel_lock(task);
+       task->label = labelh_modify(task->label);
+       mac_task_label_update(pl, &task->maclabel);
+       tasklabel_unlock(task);
+       ip_lock(task->itk_self);
+       mac_port_label_update_cred(pl, &task->itk_self->ip_label);
+       ip_unlock(task->itk_self);
+}
+
+void
+mac_task_label_modify(
+       struct task     *task,
+       void            *arg,
+       void (*f)       (struct label *l, void *arg))
+{
+
+       tasklabel_lock(task);
+       task->label = labelh_modify(task->label);
+       (*f)(&task->maclabel, arg);
+       tasklabel_unlock(task);
+}
+
+struct label *
+mac_task_get_label(struct task *task)
+{
+       return (&task->maclabel);
+}
+#endif