]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/task.c
xnu-517.3.15.tar.gz
[apple/xnu.git] / osfmk / kern / task.c
index bea0f27d0d95d2b4142d946500113cd7439c7def..63dce983a4d015227b4560bfa66630dd44c2c3ad 100644 (file)
@@ -1,21 +1,24 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_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.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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. 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@
  */
@@ -87,7 +90,6 @@
 #include <mach/task_info.h>
 #include <mach/task_special_ports.h>
 #include <mach/mach_types.h>
-#include <mach/machine/rpc.h>
 #include <ipc/ipc_space.h>
 #include <ipc/ipc_entry.h>
 #include <kern/mach_param.h>
 #include <kern/kalloc.h>
 #include <kern/processor.h>
 #include <kern/sched_prim.h>   /* for thread_wakeup */
-#include <kern/sf.h>
-#include <kern/mk_sp.h>        /*** ??? fix so this can be removed ***/
 #include <kern/ipc_tt.h>
 #include <kern/ledger.h>
 #include <kern/host.h>
 #include <kern/task_swap.h>
 #endif /* TASK_SWAPPER */
 
+#ifdef __ppc__
+#include <ppc/exception.h>
+#include <ppc/hw_perfmon.h>
+#endif
+
 /*
  * 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>
 
 task_t kernel_task;
 zone_t task_zone;
@@ -139,14 +145,22 @@ void              task_free(
                        task_t          task );
 void           task_synchronizer_destroy_all(
                        task_t          task);
-void           task_subsystem_destroy_all(
-                       task_t          task);
 
 kern_return_t  task_set_ledger(
                        task_t          task,
                        ledger_t        wired,
                        ledger_t        paged);
 
+void
+task_backing_store_privileged(
+                       task_t task)
+{
+       task_lock(task);
+       task->priv_flags |= VM_BACKING_STORE_PRIV;
+       task_unlock(task);
+       return;
+}
+
 void
 task_init(void)
 {
@@ -160,24 +174,18 @@ task_init(void)
 
        /*
         * 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)
+       if (task_create_internal(TASK_NULL, FALSE, &kernel_task) != KERN_SUCCESS)
                panic("task_init\n");
+
        vm_map_deallocate(kernel_task->map);
        kernel_task->map = kernel_map;
-
-#if    MACH_ASSERT
-       if (watchacts & WA_TASK)
-           printf("task_init: kernel_task = %x map=%x\n",
-                               kernel_task, kernel_map);
-#endif /* MACH_ASSERT */
 }
 
 #if    MACH_HOST
-void
+
+#if 0
+static void
 task_freeze(
        task_t task)
 {
@@ -187,18 +195,23 @@ task_freeze(
         *      wait for that to finish.
         */
        while (task->may_assign == FALSE) {
+               wait_result_t res;
+
                task->assign_active = TRUE;
-               thread_sleep_mutex((event_t) &task->assign_active,
-                                       &task->lock, THREAD_INTERRUPTIBLE);
-               task_lock(task);
+               res = thread_sleep_mutex((event_t) &task->assign_active,
+                                        &task->lock, THREAD_UNINT);
+               assert(res == THREAD_AWAKENED);
        }
        task->may_assign = FALSE;
        task_unlock(task);
-
        return;
 }
+#else
+#define thread_freeze(thread)  assert(task->processor_set == &default_pset)
+#endif
 
-void
+#if 0
+static void
 task_unfreeze(
        task_t task)
 {
@@ -210,9 +223,12 @@ task_unfreeze(
                thread_wakeup((event_t)&task->assign_active);
        }
        task_unlock(task);
-
        return;
 }
+#else
+#define thread_unfreeze(thread)        assert(task->processor_set == &default_pset)
+#endif
+
 #endif /* MACH_HOST */
 
 /*
@@ -226,48 +242,7 @@ kernel_task_create(
        vm_size_t               map_size,
        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
@@ -281,8 +256,8 @@ task_create(
        if (parent_task == TASK_NULL)
                return(KERN_INVALID_ARGUMENT);
 
-       return task_create_local(
-                       parent_task, inherit_memory, FALSE, child_task);
+       return task_create_internal(
+                       parent_task, inherit_memory, child_task);
 }
 
 kern_return_t
@@ -290,6 +265,7 @@ host_security_create_task_token(
         host_security_t                host_security,
        task_t                  parent_task,
         security_token_t       sec_token,
+       audit_token_t           audit_token,
        host_priv_t             host_priv,
         ledger_port_array_t    ledger_ports,
         mach_msg_type_number_t num_ledger_ports,
@@ -304,8 +280,8 @@ host_security_create_task_token(
        if (host_security == HOST_NULL)
                return(KERN_INVALID_SECURITY);
 
-       result = task_create_local(
-                       parent_task, inherit_memory, FALSE, child_task);
+       result = task_create_internal(
+                       parent_task, inherit_memory, child_task);
 
         if (result != KERN_SUCCESS)
                 return(result);
@@ -313,6 +289,7 @@ host_security_create_task_token(
        result = host_security_set_task_token(host_security,
                                              *child_task,
                                              sec_token,
+                                             audit_token,
                                              host_priv);
 
        if (result != KERN_SUCCESS)
@@ -322,10 +299,9 @@ host_security_create_task_token(
 }
 
 kern_return_t
-task_create_local(
+task_create_internal(
        task_t          parent_task,
        boolean_t       inherit_memory,
-       boolean_t       kernel_loaded,
        task_t          *child_task)            /* OUT */
 {
        task_t          new_task;
@@ -343,20 +319,18 @@ task_create_local(
                new_task->map = vm_map_fork(parent_task->map);
        else
                new_task->map = vm_map_create(pmap_create(0),
-                                       round_page(VM_MIN_ADDRESS),
-                                       trunc_page(VM_MAX_ADDRESS), TRUE);
+                                       round_page_32(VM_MIN_ADDRESS),
+                                       trunc_page_32(VM_MAX_ADDRESS), TRUE);
 
        mutex_init(&new_task->lock, ETAP_THREAD_TASK_NEW);
-       queue_init(&new_task->subsystem_list);
-       queue_init(&new_task->thr_acts);
+       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->res_thread_count = 0;
+       new_task->active_thread_count = 0;
        new_task->user_stop_count = 0;
-       new_task->importance = 0;
+       new_task->role = TASK_UNSPECIFIED;
        new_task->active = TRUE;
-       new_task->kernel_loaded = kernel_loaded;
        new_task->user_data = 0;
        new_task->faults = 0;
        new_task->cow_faults = 0;
@@ -364,13 +338,24 @@ task_create_local(
        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->taskFeatures[0] = 0;                          /* Init task features */
+       new_task->taskFeatures[1] = 0;                          /* Init task features */
+       new_task->dynamic_working_set = 0;
+       
+       task_working_set_create(new_task, TWS_SMALL_HASH_LINE_COUNT, 
+                                               0, TWS_HASH_STYLE_DEFAULT);
 
 #ifdef MACH_BSD
        new_task->bsd_info = 0;
 #endif /* MACH_BSD */
 
+#ifdef __ppc__
+       if(per_proc_info[0].pf.Available & pf64Bit) new_task->taskFeatures[0] |= tf64BitData;   /* If 64-bit machine, show we have 64-bit registers at least */
+#endif
+
 #if    TASK_SWAPPER
        new_task->swap_state = TASK_SW_IN;
        new_task->swap_flags = 0;
@@ -412,12 +397,8 @@ task_create_local(
                if (!pset->active)
                        pset = &default_pset;
 
-               new_task->policy = parent_task->policy;
-
-               new_task->priority = parent_task->priority;
-               new_task->max_priority = parent_task->max_priority;
-
                new_task->sec_token = parent_task->sec_token;
+               new_task->audit_token = parent_task->audit_token;
 
                shared_region_mapping_ref(parent_task->system_shared_region);
                new_task->system_shared_region = parent_task->system_shared_region;
@@ -430,24 +411,21 @@ task_create_local(
        else {
                pset = &default_pset;
 
-               if (kernel_task == TASK_NULL) {
-                       new_task->policy = POLICY_RR;
-
-                       new_task->priority = MINPRI_KERNBAND;
-                       new_task->max_priority = MAXPRI_KERNBAND;
-               }
-               else {
-                       new_task->policy = POLICY_TIMESHARE;
-
-                       new_task->priority = BASEPRI_DEFAULT;
-                       new_task->max_priority = MAXPRI_HIGHBAND;
-               }
-
                new_task->sec_token = KERNEL_SECURITY_TOKEN;
+               new_task->audit_token = KERNEL_AUDIT_TOKEN;
                new_task->wired_ledger_port = ledger_copy(root_wired_ledger);
                new_task->paged_ledger_port = ledger_copy(root_paged_ledger);
        }
 
+       if (kernel_task == TASK_NULL) {
+               new_task->priority = BASEPRI_KERNEL;
+               new_task->max_priority = MAXPRI_KERNEL;
+       }
+       else {
+               new_task->priority = BASEPRI_DEFAULT;
+               new_task->max_priority = MAXPRI_USER;
+       }
+
        pset_lock(pset);
        pset_add_task(pset, new_task);
        pset_unlock(pset);
@@ -456,123 +434,70 @@ task_create_local(
                task_unfreeze(parent_task);
 #endif /* MACH_HOST */
 
-#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_free:
+ *     task_deallocate
  *
- *     Called by task_deallocate when the task's reference count drops to zero.
+ *     Drop a reference on a task
  *     Task is locked.
  */
 void
-task_free(
+task_deallocate(
        task_t          task)
 {
        processor_set_t pset;
+       int refs;
+
+       if (task == TASK_NULL)
+           return;
 
-#if    MACH_ASSERT
-       assert(task != 0);
-       if (watchacts & (WA_EXIT|WA_TASK))
-           printf("task_free(%x(%d)) map ref %d\n", task, task->ref_count,
-                       task->map->ref_count);
-#endif /* MACH_ASSERT */
+       task_lock(task);
+       refs = --task->ref_count;
+       task_unlock(task);
+
+       if (refs > 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 */
 
+       if(task->dynamic_working_set)
+               tws_hash_destroy((tws_hash_t)task->dynamic_working_set);
+
        eml_task_deallocate(task);
 
-       /*
-        * Temporarily restore the reference we dropped above, then
-        * freeze the task so that the task->processor_set field
-        * cannot change. In the !MACH_HOST case, the logic can be
-        * simplified, since the default_pset is the only pset.
-        */
-       ++task->ref_count;
-       task_unlock(task);
-#if    MACH_HOST
+       ipc_task_terminate(task);
+
+#if MACH_HOST
        task_freeze(task);
-#endif /* MACH_HOST */
-       
+#endif
+
        pset = task->processor_set;
        pset_lock(pset);
-       task_lock(task);
-       if (--task->ref_count > 0) {
-               /*
-                * A new reference appeared (probably from the pset).
-                * Back out. Must unfreeze inline since we'already
-                * dropped our reference.
-                */
-#if    MACH_HOST
-               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);
-               }
-#endif /* MACH_HOST */
-               task_unlock(task);
-               pset_unlock(pset);
-               return;
-       }
        pset_remove_task(pset,task);
-       task_unlock(task);
        pset_unlock(pset);
        pset_deallocate(pset);
 
-       ipc_task_terminate(task);
-       shared_region_mapping_dealloc(task->system_shared_region);
+#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);
 }
 
-void
-task_deallocate(
-       task_t          task)
-{
-       if (task != TASK_NULL) {
-               int     c;
-
-               task_lock(task);
-               c = --task->ref_count;
-               if (c == 0)
-                   task_free(task);    /* unlocks task */
-               else
-                   task_unlock(task);
-       }
-}
 
 void
 task_reference(
@@ -623,7 +548,6 @@ task_terminate_internal(
 {
        thread_act_t    thr_act, cur_thr_act;
        task_t          cur_task;
-       thread_t        cur_thread;
        boolean_t       interrupt_save;
 
        assert(task != kernel_task);
@@ -682,9 +606,7 @@ task_terminate_internal(
         * Make sure the current thread does not get aborted out of
         * the waits inside these operations.
         */
-       cur_thread = current_thread();
-       interrupt_save = cur_thread->interruptible;
-       cur_thread->interruptible = FALSE;
+       interrupt_save = thread_interrupt_level(THREAD_UNINT);
 
        /*
         *      Indicate that we want all the threads to stop executing
@@ -707,18 +629,17 @@ task_terminate_internal(
         *      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) {
+       queue_iterate(&task->threads, thr_act, thread_act_t, task_threads) {
                        thread_terminate_internal(thr_act);
        }
 
        /*
-        *      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.
+        *      Give the machine dependent code a chance
+        *      to perform cleanup before ripping apart
+        *      the task.
         */
        if (cur_thr_act->task == task)
-               act_virtual_machine_destroy(cur_thr_act);
+               machine_thread_terminate_self();
 
        task_unlock(task);
 
@@ -727,16 +648,10 @@ task_terminate_internal(
         */
        task_synchronizer_destroy_all(task);
 
-       /*
-        *      Deallocate all subsystems owned by the task.
-        */
-       task_subsystem_destroy_all(task);
-
        /*
         *      Destroy the IPC space, leaving just a reference for it.
         */
-       if (!task->kernel_loaded)
-               ipc_space_destroy(task->itk_space);
+       ipc_space_destroy(task->itk_space);
 
        /*
         * If the current thread is a member of the task
@@ -750,11 +665,24 @@ task_terminate_internal(
                             task->map->min_offset,
                             task->map->max_offset, VM_MAP_NO_FLAGS);
 
+       shared_region_mapping_dealloc(task->system_shared_region);
+
+       /*
+        * Flush working set here to avoid I/O in reaper thread
+        */
+       if(task->dynamic_working_set)
+               tws_hash_ws_flush((tws_hash_t)
+                               task->dynamic_working_set);
+
        /*
         * We no longer need to guard against being aborted, so restore
         * the previous interruptible state.
         */
-       cur_thread->interruptible = interrupt_save;
+       thread_interrupt_level(interrupt_save);
+
+#if __ppc__
+    perfmon_release_facility(task); // notify the perfmon facility
+#endif
 
        /*
         * Get rid of the task active reference on itself.
@@ -809,7 +737,7 @@ task_halt(
                return(KERN_FAILURE);
        }
 
-       if (task->thr_act_count > 1) {
+       if (task->thread_count > 1) {
                /*
                 * Mark all the threads to keep them from starting any more
                 * user-level execution.  The thread_terminate_internal code
@@ -827,7 +755,7 @@ task_halt(
                 *      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) {
+               queue_iterate(&task->threads, thr_act, thread_act_t, task_threads) {
                        if (thr_act != cur_thr_act)
                                thread_terminate_internal(thr_act);
                }
@@ -835,12 +763,11 @@ task_halt(
        }
 
        /*
-        *      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.
+        *      Give the machine dependent code a chance
+        *      to perform cleanup before ripping apart
+        *      the task.
         */
-       act_virtual_machine_destroy(cur_thr_act);
+       machine_thread_terminate_self();
 
        task_unlock(task);
 
@@ -850,25 +777,10 @@ task_halt(
        task_synchronizer_destroy_all(task);
 
        /*
-        *      Deallocate all subsystems owned by the task.
+        *      Destroy the contents of the IPC space, leaving just
+        *      a reference for it.
         */
-       task_subsystem_destroy_all(task);
-
-#if 0
-       /*
-        *      Destroy the IPC space, leaving just a reference for it.
-        */
-       /*
-        * Lookupd will break if we enable this cleaning, because it
-        * uses a slimey trick that depends upon the portspace not
-        * being cleaned up across exec (it passes the lookupd server
-        * port to the child after a restart using knowledge of this
-        * bug in past implementations).  We need to fix lookupd to
-        * keep from leaking ports across exec.
-        */
-       if (!task->kernel_loaded)
-               ipc_space_clean(task->itk_space);
-#endif
+       ipc_space_clean(task->itk_space);
 
        /*
         * Clean out the address space, as we are going to be
@@ -898,12 +810,13 @@ task_hold_locked(
 
        assert(task->active);
 
-       task->suspend_count++;
+       if (task->suspend_count++ > 0)
+               return;
 
        /*
         *      Iterate through all the thread_act's and hold them.
         */
-       queue_iterate(&task->thr_acts, thr_act, thread_act_t, thr_acts) {
+       queue_iterate(&task->threads, thr_act, thread_act_t, task_threads) {
                act_lock_thread(thr_act);
                thread_hold(thr_act);
                act_unlock_thread(thr_act);
@@ -961,12 +874,12 @@ task_wait_locked(
         *      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) {
+       queue_iterate(&task->threads, thr_act, thread_act_t, task_threads) {
                if (thr_act != cur_thr_act) {
-                       thread_shuttle_t thr_shuttle;
+                       thread_t thread;
 
-                       thr_shuttle = act_lock_thread(thr_act);
-                       thread_wait(thr_shuttle);
+                       thread = act_lock_thread(thr_act);
+                       thread_wait(thread);
                        act_unlock_thread(thr_act);
                }
        }
@@ -986,16 +899,17 @@ task_release_locked(
        register thread_act_t   thr_act;
 
        assert(task->active);
+       assert(task->suspend_count > 0);
 
-       task->suspend_count--;
-       assert(task->suspend_count >= 0);
+       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) {
+       queue_iterate(&task->threads, thr_act, thread_act_t, task_threads) {
                act_lock_thread(thr_act);
                thread_release(thr_act);
                act_unlock_thread(thr_act);
@@ -1057,7 +971,7 @@ task_threads(
                        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);
@@ -1081,17 +995,17 @@ task_threads(
        /* 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);
+       for (i = j = 0, thr_act = (thread_act_t) queue_first(&task->threads);
             i < actual;
-            i++, thr_act = (thread_act_t) queue_next(&thr_act->thr_acts)) {
+            i++, thr_act = (thread_act_t) queue_next(&thr_act->task_threads)) {
                act_lock(thr_act);
-               if (thr_act->ref_count > 0) {
-                       act_locked_act_reference(thr_act);
+               if (thr_act->act_ref_count > 0) {
+                       act_reference_locked(thr_act);
                        thr_acts[j++] = thr_act;
                }
                act_unlock(thr_act);
        }
-       assert(queue_end(&task->thr_acts, (queue_entry_t) thr_act));
+       assert(queue_end(&task->threads, (queue_entry_t) thr_act));
 
        actual = j;
        size_needed = actual * sizeof(mach_port_t);
@@ -1224,8 +1138,10 @@ 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)
@@ -1236,17 +1152,16 @@ 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);
 }
 
@@ -1335,7 +1250,8 @@ task_info(
                                                   * PAGE_SIZE;
 
                task_lock(task);
-               basic_info->policy = task->policy;
+               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;
@@ -1368,22 +1284,19 @@ task_info(
                times_info->system_time.microseconds = 0;
 
                task_lock(task);
-               queue_iterate(&task->thr_acts, thr_act,
-                             thread_act_t, thr_acts)
+               queue_iterate(&task->threads, thr_act,
+                             thread_act_t, task_threads)
                {
                    time_value_t user_time, system_time;
                    spl_t        s;
 
                    thread = act_lock_thread(thr_act);
 
-                   /* Skip empty threads and threads that have migrated
-                    * into this task:
+                   /* JMM - add logic to skip threads that have migrated
+                    * into this task?
                     */
-                   if (!thread || thr_act->pool_port) {
-                       act_unlock_thread(thr_act);
-                       continue;
-                   }
-                   assert(thread);  /* Must have thread, if no thread_pool*/
+
+                   assert(thread);  /* Must have thread */
                    s = splsched();
                    thread_lock(thread);
 
@@ -1404,24 +1317,11 @@ task_info(
 
            case TASK_SCHED_FIFO_INFO:
            {
-               register policy_fifo_base_t     fifo_base;
 
                if (*task_info_count < POLICY_FIFO_BASE_COUNT)
                        return(KERN_INVALID_ARGUMENT);
 
-               fifo_base = (policy_fifo_base_t) task_info_out;
-
-               task_lock(task);
-               if (task->policy != POLICY_FIFO) {
-                       task_unlock(task);
-                       return(KERN_INVALID_POLICY);
-               }
-
-               fifo_base->base_priority = task->priority;
-               task_unlock(task);
-
-               *task_info_count = POLICY_FIFO_BASE_COUNT;
-               break;
+               return(KERN_INVALID_POLICY);
            }
 
            case TASK_SCHED_RR_INFO:
@@ -1434,7 +1334,7 @@ task_info(
                rr_base = (policy_rr_base_t) task_info_out;
 
                task_lock(task);
-               if (task->policy != POLICY_RR) {
+               if (task != kernel_task) {
                        task_unlock(task);
                        return(KERN_INVALID_POLICY);
                }
@@ -1442,7 +1342,7 @@ task_info(
                rr_base->base_priority = task->priority;
                task_unlock(task);
 
-               rr_base->quantum = (min_quantum * tick) / 1000;
+               rr_base->quantum = tick / 1000;
 
                *task_info_count = POLICY_RR_BASE_COUNT;
                break;
@@ -1458,7 +1358,7 @@ task_info(
                ts_base = (policy_timeshare_base_t) task_info_out;
 
                task_lock(task);
-               if (task->policy != POLICY_TIMESHARE) {
+               if (task == kernel_task) {
                        task_unlock(task);
                        return(KERN_INVALID_POLICY);
                }
@@ -1488,6 +1388,24 @@ task_info(
                 break;
             }
             
+            case TASK_AUDIT_TOKEN:
+           {
+                register audit_token_t *audit_token_p;
+
+               if (*task_info_count < TASK_AUDIT_TOKEN_COUNT) {
+                   return(KERN_INVALID_ARGUMENT);
+               }
+
+               audit_token_p = (audit_token_t *) task_info_out;
+
+               task_lock(task);
+               *audit_token_p = task->audit_token;
+               task_unlock(task);
+
+               *task_info_count = TASK_AUDIT_TOKEN_COUNT;
+                break;
+            }
+            
            case TASK_SCHED_INFO:
                        return(KERN_INVALID_ARGUMENT);
 
@@ -1625,32 +1543,51 @@ task_collect_scan(void)
        register task_t         task, prev_task;
        processor_set_t         pset = &default_pset;
 
-       prev_task = TASK_NULL;
-
        pset_lock(pset);
        pset->ref_count++;
        task = (task_t) queue_first(&pset->tasks);
        while (!queue_end(&pset->tasks, (queue_entry_t) task)) {
-               task_reference(task);
-               pset_unlock(pset);
+               task_lock(task);
+               if (task->ref_count > 0) {
 
-               pmap_collect(task->map->pmap);
+                       task_reference_locked(task);
+                       task_unlock(task);
 
-               if (prev_task != TASK_NULL)
-                       task_deallocate(prev_task);
-               prev_task = 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);
-               task = (task_t) queue_next(&task->pset_tasks);
+                       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);
-
-       if (prev_task != TASK_NULL)
-               task_deallocate(prev_task);
 }
 
+/* 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 */
@@ -1670,7 +1607,7 @@ consider_task_collect(void)
         */
 
        if (task_collect_max_rate == 0)
-               task_collect_max_rate = (2 << SCHED_TICK_SHIFT);
+               task_collect_max_rate = (1 << SCHED_TICK_SHIFT) + 1;
 
        if (task_collect_allowed &&
            (sched_tick > (task_collect_last_tick + task_collect_max_rate))) {
@@ -1735,21 +1672,6 @@ task_synchronizer_destroy_all(task_t task)
        }
 }
 
-void
-task_subsystem_destroy_all(task_t task)
-{
-       subsystem_t     subsystem;
-
-       /*
-        *  Destroy owned subsystems
-        */
-
-       while (!queue_empty(&task->subsystem_list)) {
-               subsystem = (subsystem_t) queue_first(&task->subsystem_list);
-               subsystem_deallocate(subsystem);
-       }
-}
-
 /*
  *     task_set_port_space:
  *
@@ -1770,6 +1692,25 @@ task_set_port_space(
        return kr;
 }
 
+/*
+ *     Routine:        
+ *                     task_is_classic
+ *     Purpose:        
+ *                     Returns true if the task is a P_CLASSIC task.
+ */
+boolean_t
+task_is_classic(
+       task_t  task)   
+{
+       boolean_t result = FALSE;
+
+       if (task) {
+               struct proc *p = get_bsdtask_info(task);
+               result = proc_is_classic(p) ? TRUE : FALSE;
+       }
+       return result;
+}
+
 /*
  * We need to export some functions to other components that
  * are currently implemented in macros within the osfmk
@@ -1778,9 +1719,9 @@ 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);
 }
 
 #undef current_task