]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/thread.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / kern / thread.c
index 9c208da92c5ae041fd1ced0ffce4293763b540ec..adff14820ad5a5d164e54a4cb459c9c50b58f541 100644 (file)
@@ -1,16 +1,19 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
- * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
- * 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.
+ * 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.
+ * 
+ * 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
@@ -20,7 +23,7 @@
  * 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@
@@ -57,7 +60,7 @@
  *     Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub
  *     Date:   1986
  *
- *     Thread/thread_shuttle management primitives implementation.
+ *     Thread management primitives implementation.
  */
 /*
  * Copyright (c) 1993 The University of Utah and
  *
  */
 
-#include <cpus.h>
-#include <mach_host.h>
-#include <simple_clock.h>
-#include <mach_debug.h>
-#include <mach_prof.h>
-
+#include <mach/mach_types.h>
 #include <mach/boolean.h>
 #include <mach/policy.h>
 #include <mach/thread_info.h>
 #include <mach/thread_status.h>
 #include <mach/time_value.h>
 #include <mach/vm_param.h>
-#include <kern/ast.h>
+
+#include <machine/thread.h>
+
+#include <kern/kern_types.h>
+#include <kern/kalloc.h>
 #include <kern/cpu_data.h>
 #include <kern/counters.h>
-#include <kern/etap_macros.h>
 #include <kern/ipc_mig.h>
 #include <kern/ipc_tt.h>
 #include <kern/mach_param.h>
 #include <kern/queue.h>
 #include <kern/sched.h>
 #include <kern/sched_prim.h>
-#include <kern/mk_sp.h>        /*** ??? fix so this can be removed ***/
+#include <kern/sync_lock.h>
+#include <kern/syscall_subr.h>
 #include <kern/task.h>
 #include <kern/thread.h>
-#include <kern/thread_act.h>
-#include <kern/thread_swap.h>
 #include <kern/host.h>
 #include <kern/zalloc.h>
-#include <vm/vm_kern.h>
+#include <kern/assert.h>
+
 #include <ipc/ipc_kmsg.h>
 #include <ipc/ipc_port.h>
-#include <machine/thread.h>            /* for MACHINE_STACK */
-#include <kern/profile.h>
-#include <kern/assert.h>
+
+#include <vm/vm_kern.h>
+#include <vm/vm_pageout.h>
+
 #include <sys/kdebug.h>
 
+#include <mach/sdt.h>
+
 /*
  * Exported interfaces
  */
-
+#include <mach/task_server.h>
 #include <mach/thread_act_server.h>
 #include <mach/mach_host_server.h>
+#include <mach/host_priv_server.h>
 
-/*
- * Per-Cpu stashed global state
- */
-vm_offset_t                    active_stacks[NCPUS];   /* per-cpu active stacks        */
-vm_offset_t                    kernel_stack[NCPUS];    /* top of active stacks         */
-thread_act_t           active_kloaded[NCPUS];  /*  + act if kernel loaded      */
-boolean_t                      first_thread;
+static struct zone                     *thread_zone;
+static lck_grp_attr_t          thread_lck_grp_attr;
+lck_attr_t                                     thread_lck_attr;
+lck_grp_t                                      thread_lck_grp;
 
-struct zone                    *thread_shuttle_zone;
+decl_simple_lock_data(static,thread_stack_lock)
+static queue_head_t            thread_stack_queue;
 
-queue_head_t           reaper_queue;
-decl_simple_lock_data(,reaper_lock)
+decl_simple_lock_data(static,thread_terminate_lock)
+static queue_head_t            thread_terminate_queue;
 
-extern int             tick;
+static struct thread   thread_template, init_thread;
 
-extern void            pcb_module_init(void);
+static void            sched_call_null(
+                                       int                     type,
+                                       thread_t        thread);
 
-struct thread_shuttle  pageout_thread;
+#ifdef MACH_BSD
+extern void proc_exit(void *);
+extern uint64_t get_dispatchqueue_offset_from_proc(void *);
+#endif /* MACH_BSD */
 
-/* private */
-static struct thread_shuttle   thr_sh_template;
+extern int debug_task;
+int thread_max = CONFIG_THREAD_MAX;    /* Max number of threads */
+int task_threadmax = CONFIG_THREAD_MAX;
 
-#if    MACH_DEBUG
+static uint64_t                thread_unique_id = 0;
 
-#ifdef MACHINE_STACK
-extern void    stack_statistics(
-                       unsigned int    *totalp,
-                       vm_size_t       *maxusagep);
-#endif /* MACHINE_STACK */
-#endif /* MACH_DEBUG */
+void
+thread_bootstrap(void)
+{
+       /*
+        *      Fill in a template thread for fast initialization.
+        */
 
-/* Forwards */
-void           thread_collect_scan(void);
+       thread_template.runq = PROCESSOR_NULL;
 
-kern_return_t thread_create_shuttle(
-       thread_act_t                    thr_act,
-       integer_t                               priority,
-       void                                    (*start)(void),
-       thread_t                                *new_thread);
+       thread_template.ref_count = 2;
 
-extern void            Load_context(
-       thread_t                thread);
+       thread_template.reason = AST_NONE;
+       thread_template.at_safe_point = FALSE;
+       thread_template.wait_event = NO_EVENT64;
+       thread_template.wait_queue = WAIT_QUEUE_NULL;
+       thread_template.wait_result = THREAD_WAITING;
+       thread_template.options = THREAD_ABORTSAFE;
+       thread_template.state = TH_WAIT | TH_UNINT;
+       thread_template.wake_active = FALSE;
+       thread_template.continuation = THREAD_CONTINUE_NULL;
+       thread_template.parameter = NULL;
 
+       thread_template.importance = 0;
+       thread_template.sched_mode = 0;
+       thread_template.safe_mode = 0;
+       thread_template.safe_release = 0;
 
-/*
- *     Machine-dependent code must define:
- *             thread_machine_init
- *             thread_machine_terminate
- *             thread_machine_collect
- *
- *     The thread->pcb field is reserved for machine-dependent code.
- */
+       thread_template.priority = 0;
+       thread_template.sched_pri = 0;
+       thread_template.max_priority = 0;
+       thread_template.task_priority = 0;
+       thread_template.promotions = 0;
+       thread_template.pending_promoter_index = 0;
+       thread_template.pending_promoter[0] =
+               thread_template.pending_promoter[1] = NULL;
 
-#ifdef MACHINE_STACK
-/*
- *     Machine-dependent code must define:
- *             stack_alloc_try
- *             stack_alloc
- *             stack_free
- *             stack_free_stack
- *             stack_collect
- *     and if MACH_DEBUG:
- *             stack_statistics
- */
-#else  /* MACHINE_STACK */
-/*
- *     We allocate stacks from generic kernel VM.
- *     Machine-dependent code must define:
- *             machine_kernel_stack_init
- *
- *     The stack_free_list can only be accessed at splsched,
- *     because stack_alloc_try/thread_invoke operate at splsched.
- */
+       thread_template.realtime.deadline = UINT64_MAX;
 
-decl_simple_lock_data(,stack_lock_data)         /* splsched only */
-#define stack_lock()   simple_lock(&stack_lock_data)
-#define stack_unlock() simple_unlock(&stack_lock_data)
+       thread_template.current_quantum = 0;
 
-mutex_t stack_map_lock;                                /* Lock when allocating stacks maps */
-vm_map_t stack_map;                                    /* Map for allocating stacks */
-vm_offset_t stack_free_list;           /* splsched only */
-unsigned int stack_free_max = 0;
-unsigned int stack_free_count = 0;     /* splsched only */
-unsigned int stack_free_limit = 1;     /* Arbitrary  */
+       thread_template.computation_metered = 0;
+       thread_template.computation_epoch = 0;
 
-unsigned int stack_alloc_hits = 0;     /* debugging */
-unsigned int stack_alloc_misses = 0;   /* debugging */
+       thread_template.sched_stamp = 0;
+       thread_template.sched_usage = 0;
+       thread_template.pri_shift = INT8_MAX;
+       thread_template.cpu_usage = thread_template.cpu_delta = 0;
+       thread_template.c_switch = thread_template.p_switch = thread_template.ps_switch = 0;
 
-unsigned int stack_alloc_total = 0;
-unsigned int stack_alloc_hiwater = 0;
-unsigned int stack_alloc_bndry = 0;
+       thread_template.bound_processor = PROCESSOR_NULL;
+       thread_template.last_processor = PROCESSOR_NULL;
 
+       thread_template.sched_call = sched_call_null;
 
-/*
- *     The next field is at the base of the stack,
- *     so the low end is left unsullied.
- */
+       timer_init(&thread_template.user_timer);
+       timer_init(&thread_template.system_timer);
+       thread_template.user_timer_save = 0;
+       thread_template.system_timer_save = 0;
+       thread_template.vtimer_user_save = 0;
+       thread_template.vtimer_prof_save = 0;
+       thread_template.vtimer_rlim_save = 0;
 
-#define stack_next(stack) (*((vm_offset_t *)((stack) + KERNEL_STACK_SIZE) - 1))
+       thread_template.wait_timer_is_set = FALSE;
+       thread_template.wait_timer_active = 0;
 
-/*
- *     stack_alloc:
- *
- *     Allocate a kernel stack for an activation.
- *     May block.
- */
-vm_offset_t
-stack_alloc(
-       thread_t thread,
-       void (*start_pos)(thread_t))
-{
-       vm_offset_t     stack = thread->kernel_stack;
-       spl_t                   s;
+       thread_template.depress_timer_active = 0;
 
-       if (stack)
-               return (stack);
+       thread_template.special_handler.handler = special_handler;
+       thread_template.special_handler.next = NULL;
 
-/*
- *     We first try the free list.  It is probably empty, or
- *     stack_alloc_try would have succeeded, but possibly a stack was
- *     freed before the swapin thread got to us.
- *
- *     We allocate stacks from their own map which is submaps of the
- *     kernel map.  Because we want to have a guard page (at least) in
- *     front of each stack to catch evil code that overruns its stack, we
- *     allocate the stack on aligned boundaries.  The boundary is
- *     calculated as the next power of 2 above the stack size. For
- *     example, a stack of 4 pages would have a boundry of 8, likewise 5
- *     would also be 8.
- *
- *     We limit the number of stacks to be one allocation chunk
- *     (THREAD_CHUNK) more than the maximum number of threads
- *     (THREAD_MAX).  The extra is to allow for priviliged threads that
- *     can sometimes have 2 stacks.
- *
- */
+       thread_template.funnel_lock = THR_FUNNEL_NULL;
+       thread_template.funnel_state = 0;
+       thread_template.recover = (vm_offset_t)NULL;
+       
+       thread_template.map = VM_MAP_NULL;
 
-       s = splsched();
-       stack_lock();
-       stack = stack_free_list;
-       if (stack != 0) {
-               stack_free_list = stack_next(stack);
-               stack_free_count--;
-       }
-       stack_unlock();
-       splx(s);
+#if CONFIG_DTRACE
+       thread_template.t_dtrace_predcache = 0;
+       thread_template.t_dtrace_vtime = 0;
+       thread_template.t_dtrace_tracing = 0;
+#endif /* CONFIG_DTRACE */
 
-       if (stack != 0) {                                                       /* Did we find a free one? */
-               stack_attach(thread, stack, start_pos); /* Initialize it */
-               return (stack);                                                 /* Send it on home */
-       }
-               
-       if (kernel_memory_allocate(
-                                       stack_map, &stack,
-                                               KERNEL_STACK_SIZE, stack_alloc_bndry - 1,
-                                                                               KMA_KOBJECT) != KERN_SUCCESS)
-               panic("stack_alloc: no space left for stack maps");
-
-       stack_alloc_total++;
-       if (stack_alloc_total > stack_alloc_hiwater)
-               stack_alloc_hiwater = stack_alloc_total;
-
-       stack_attach(thread, stack, start_pos);
-       return (stack);
-}
+       thread_template.t_chud = 0;
+       thread_template.t_page_creation_count = 0;
+       thread_template.t_page_creation_time = 0;
 
-/*
- *     stack_free:
- *
- *     Free a kernel stack.
- *     Called at splsched.
- */
+       thread_template.affinity_set = NULL;
+       
+       init_thread = thread_template;
+       machine_set_current_thread(&init_thread);
+}
 
 void
-stack_free(
-       thread_t thread)
+thread_init(void)
 {
-    vm_offset_t stack = stack_detach(thread);
-
-       assert(stack);
-       if (stack != thread->stack_privilege) {
-               stack_lock();
-               stack_next(stack) = stack_free_list;
-               stack_free_list = stack;
-               if (++stack_free_count > stack_free_max)
-                       stack_free_max = stack_free_count;
-               stack_unlock();
-       }
+       thread_zone = zinit(
+                       sizeof(struct thread),
+                       thread_max * sizeof(struct thread),
+                       THREAD_CHUNK * sizeof(struct thread),
+                       "threads");
+       zone_change(thread_zone, Z_NOENCRYPT, TRUE);
+       
+       lck_grp_attr_setdefault(&thread_lck_grp_attr);
+       lck_grp_init(&thread_lck_grp, "thread", &thread_lck_grp_attr);
+       lck_attr_setdefault(&thread_lck_attr);
+       
+       stack_init();
+
+       /*
+        *      Initialize any machine-dependent
+        *      per-thread structures necessary.
+        */
+       machine_thread_init();
 }
 
 static void
-stack_free_stack(
-       vm_offset_t             stack)
+thread_terminate_continue(void)
 {
-       spl_t   s;
-
-       s = splsched();
-       stack_lock();
-       stack_next(stack) = stack_free_list;
-       stack_free_list = stack;
-       if (++stack_free_count > stack_free_max)
-               stack_free_max = stack_free_count;
-       stack_unlock();
-       splx(s);
+       panic("thread_terminate_continue");
+       /*NOTREACHED*/
 }
 
 /*
- *     stack_collect:
- *
- *     Free excess kernel stacks.
- *     May block.
+ *     thread_terminate_self:
  */
-
 void
-stack_collect(void)
+thread_terminate_self(void)
 {
-       vm_offset_t     stack;
-       int                     i;
-       spl_t           s;
+       thread_t                thread = current_thread();
+       task_t                  task;
+       spl_t                   s;
+       int threadcnt;
+
+       DTRACE_PROC(lwp__exit);
+
+       thread_mtx_lock(thread);
+
+       ulock_release_all(thread);
+
+       ipc_thread_disable(thread);
+       
+       thread_mtx_unlock(thread);
 
        s = splsched();
-       stack_lock();
-       while (stack_free_count > stack_free_limit) {
-               stack = stack_free_list;
-               stack_free_list = stack_next(stack);
-               stack_free_count--;
-               stack_unlock();
+       thread_lock(thread);
+
+       /*
+        *      Cancel priority depression, wait for concurrent expirations
+        *      on other processors.
+        */
+       if (thread->sched_mode & TH_MODE_ISDEPRESSED) {
+               thread->sched_mode &= ~TH_MODE_ISDEPRESSED;
+
+               if (timer_call_cancel(&thread->depress_timer))
+                       thread->depress_timer_active--;
+       }
+
+       while (thread->depress_timer_active > 0) {
+               thread_unlock(thread);
                splx(s);
 
-               if (vm_map_remove(
-                                       stack_map, stack, stack + KERNEL_STACK_SIZE,
-                                                                       VM_MAP_REMOVE_KUNWIRE) != KERN_SUCCESS)
-                       panic("stack_collect: vm_map_remove failed");
+               delay(1);
 
                s = splsched();
-               stack_lock();
-               stack_alloc_total--;
+               thread_lock(thread);
        }
-       stack_unlock();
-       splx(s);
-}
 
+       thread_sched_call(thread, NULL);
 
-#if    MACH_DEBUG
-/*
- *     stack_statistics:
- *
- *     Return statistics on cached kernel stacks.
- *     *maxusagep must be initialized by the caller.
- */
+       thread_unlock(thread);
+       splx(s);
 
-void
-stack_statistics(
-       unsigned int    *totalp,
-       vm_size_t       *maxusagep)
-{
-       spl_t   s;
+       thread_policy_reset(thread);
 
-       s = splsched();
-       stack_lock();
+       task = thread->task;
+       uthread_cleanup(task, thread->uthread, task->bsd_info);
+       threadcnt = hw_atomic_sub(&task->active_thread_count, 1);
 
-       *totalp = stack_free_count;
-       *maxusagep = 0;
+       /*
+        * If we are the last thread to terminate and the task is
+        * associated with a BSD process, perform BSD process exit.
+        */
+       if (threadcnt == 0 && task->bsd_info != NULL)
+               proc_exit(task->bsd_info);
 
-       stack_unlock();
-       splx(s);
-}
-#endif /* MACH_DEBUG */
+       uthread_cred_free(thread->uthread);
 
-#endif /* MACHINE_STACK */
+       s = splsched();
+       thread_lock(thread);
 
+       /*
+        *      Cancel wait timer, and wait for
+        *      concurrent expirations.
+        */
+       if (thread->wait_timer_is_set) {
+               thread->wait_timer_is_set = FALSE;
 
-stack_fake_zone_info(int *count, vm_size_t *cur_size, vm_size_t *max_size, vm_size_t *elem_size,
-                    vm_size_t *alloc_size, int *collectable, int *exhaustable)
-{
-        *count      = stack_alloc_total - stack_free_count;
-       *cur_size   = KERNEL_STACK_SIZE * stack_alloc_total;
-       *max_size   = KERNEL_STACK_SIZE * stack_alloc_hiwater;
-       *elem_size  = KERNEL_STACK_SIZE;
-       *alloc_size = KERNEL_STACK_SIZE;
-       *collectable = 1;
-       *exhaustable = 0;
-}
+               if (timer_call_cancel(&thread->wait_timer))
+                       thread->wait_timer_active--;
+       }
 
+       while (thread->wait_timer_active > 0) {
+               thread_unlock(thread);
+               splx(s);
 
-/*
- *     stack_privilege:
- *
- *     stack_alloc_try on this thread must always succeed.
- */
+               delay(1);
+
+               s = splsched();
+               thread_lock(thread);
+       }
 
-void
-stack_privilege(
-       register thread_t thread)
-{
        /*
-        *      This implementation only works for the current thread.
+        *      If there is a reserved stack, release it.
         */
+       if (thread->reserved_stack != 0) {
+               if (thread->reserved_stack != thread->kernel_stack)
+                       stack_free_stack(thread->reserved_stack);
+               thread->reserved_stack = 0;
+       }
 
-       if (thread != current_thread())
-               panic("stack_privilege");
+       /*
+        *      Mark thread as terminating, and block.
+        */
+       thread->state |= TH_TERMINATE;
+       thread_mark_wait_locked(thread, THREAD_UNINT);
+       assert(thread->promotions == 0);
+       thread_unlock(thread);
+       /* splsched */
 
-       if (thread->stack_privilege == 0)
-               thread->stack_privilege = current_stack();
+       thread_block((thread_continue_t)thread_terminate_continue);
+       /*NOTREACHED*/
 }
 
-/*
- *     stack_alloc_try:
- *
- *     Non-blocking attempt to allocate a kernel stack.
- *     Called at splsched with the thread locked.
- */
-
-boolean_t stack_alloc_try(
-       thread_t        thread,
-       void            (*start_pos)(thread_t))
+void
+thread_deallocate(
+       thread_t                        thread)
 {
-       register vm_offset_t stack = thread->stack_privilege;
+       task_t                          task;
 
-       if (stack == 0) {
-               stack_lock();
+       if (thread == THREAD_NULL)
+               return;
 
-               stack = stack_free_list;
-               if (stack != (vm_offset_t)0) {
-                       stack_free_list = stack_next(stack);
-                       stack_free_count--;
-               }
+       if (thread_deallocate_internal(thread) > 0)
+               return;
 
-               stack_unlock();
-       }
+       ipc_thread_terminate(thread);
 
-       if (stack != 0) {
-               stack_attach(thread, stack, start_pos);
-               stack_alloc_hits++;
+       task = thread->task;
 
-               return (TRUE);
-       }
-       else {
-               stack_alloc_misses++;
+#ifdef MACH_BSD 
+       {
+               void *ut = thread->uthread;
 
-               return (FALSE);
+               thread->uthread = NULL;
+               uthread_zone_free(ut);
        }
-}
+#endif  /* MACH_BSD */   
 
-uint64_t                       max_unsafe_computation;
-extern int                     max_unsafe_quanta;
+       task_deallocate(task);
 
-uint32_t                       sched_safe_duration;
+       if (thread->kernel_stack != 0)
+               stack_free(thread);
 
-uint64_t                       max_poll_computation;
-extern int                     max_poll_quanta;
+       lck_mtx_destroy(&thread->mutex, &thread_lck_grp);
+       machine_thread_destroy(thread);
 
-uint32_t                       std_quantum;
-uint32_t                       min_std_quantum;
-
-uint32_t                       max_rt_quantum;
-uint32_t                       min_rt_quantum;
+       zfree(thread_zone, thread);
+}
 
-void
-thread_init(void)
+/*
+ *     thread_terminate_daemon:
+ *
+ *     Perform final clean up for terminating threads.
+ */
+static void
+thread_terminate_daemon(void)
 {
-       kern_return_t ret;
-       unsigned int stack;
-       
-       thread_shuttle_zone = zinit(
-                       sizeof(struct thread_shuttle),
-                       THREAD_MAX * sizeof(struct thread_shuttle),
-                       THREAD_CHUNK * sizeof(struct thread_shuttle),
-                       "threads");
+       thread_t                        thread;
+       task_t                          task;
 
-       /*
-        *      Fill in a template thread_shuttle for fast initialization.
-        *      [Fields that must be (or are typically) reset at
-        *      time of creation are so noted.]
-        */
+       (void)splsched();
+       simple_lock(&thread_terminate_lock);
 
-       /* thr_sh_template.links (none) */
-       thr_sh_template.runq = RUN_QUEUE_NULL;
-
-
-       /* thr_sh_template.task (later) */
-       /* thr_sh_template.thread_list (later) */
-       /* thr_sh_template.pset_threads (later) */
-
-       /* reference for activation */
-       thr_sh_template.ref_count = 1;
-
-       thr_sh_template.reason = AST_NONE;
-       thr_sh_template.at_safe_point = FALSE;
-       thr_sh_template.wait_event = NO_EVENT64;
-       thr_sh_template.wait_queue = WAIT_QUEUE_NULL;
-       thr_sh_template.wait_result = THREAD_WAITING;
-       thr_sh_template.interrupt_level = THREAD_ABORTSAFE;
-       thr_sh_template.state = TH_STACK_HANDOFF | TH_WAIT | TH_UNINT;
-       thr_sh_template.wake_active = FALSE;
-       thr_sh_template.active_callout = FALSE;
-       thr_sh_template.continuation = (void (*)(void))0;
-       thr_sh_template.top_act = THR_ACT_NULL;
-
-       thr_sh_template.importance = 0;
-       thr_sh_template.sched_mode = 0;
-       thr_sh_template.safe_mode = 0;
-
-       thr_sh_template.priority = 0;
-       thr_sh_template.sched_pri = 0;
-       thr_sh_template.max_priority = 0;
-       thr_sh_template.task_priority = 0;
-       thr_sh_template.promotions = 0;
-       thr_sh_template.pending_promoter_index = 0;
-       thr_sh_template.pending_promoter[0] =
-               thr_sh_template.pending_promoter[1] = NULL;
-
-       thr_sh_template.current_quantum = 0;
-
-       thr_sh_template.computation_metered = 0;
-       thr_sh_template.computation_epoch = 0;
-
-       thr_sh_template.cpu_usage = 0;
-       thr_sh_template.cpu_delta = 0;
-       thr_sh_template.sched_usage = 0;
-       thr_sh_template.sched_delta = 0;
-       thr_sh_template.sched_stamp = 0;
-       thr_sh_template.sleep_stamp = 0;
-       thr_sh_template.safe_release = 0;
-
-       thr_sh_template.bound_processor = PROCESSOR_NULL;
-       thr_sh_template.last_processor = PROCESSOR_NULL;
-       thr_sh_template.last_switch = 0;
-
-       thr_sh_template.vm_privilege = FALSE;
-
-       timer_init(&(thr_sh_template.user_timer));
-       timer_init(&(thr_sh_template.system_timer));
-       thr_sh_template.user_timer_save.low = 0;
-       thr_sh_template.user_timer_save.high = 0;
-       thr_sh_template.system_timer_save.low = 0;
-       thr_sh_template.system_timer_save.high = 0;
-
-       thr_sh_template.active = FALSE; /* reset */
-
-       thr_sh_template.processor_set = PROCESSOR_SET_NULL;
-#if    MACH_HOST
-       thr_sh_template.may_assign = TRUE;
-       thr_sh_template.assign_active = FALSE;
-#endif /* MACH_HOST */
-       thr_sh_template.funnel_state = 0;
+       while ((thread = (thread_t)dequeue_head(&thread_terminate_queue)) != THREAD_NULL) {
+               simple_unlock(&thread_terminate_lock);
+               (void)spllo();
 
-       /*
-        *      Initialize other data structures used in
-        *      this module.
-        */
+               task = thread->task;
 
-       queue_init(&reaper_queue);
-       simple_lock_init(&reaper_lock, ETAP_THREAD_REAPER);
-    thr_sh_template.funnel_lock = THR_FUNNEL_NULL;
+               task_lock(task);
+               task->total_user_time += timer_grab(&thread->user_timer);
+               task->total_system_time += timer_grab(&thread->system_timer);
 
-#ifndef MACHINE_STACK
-       simple_lock_init(&stack_lock_data, ETAP_THREAD_STACK);  /* Initialize the stack lock */
-       
-       if (KERNEL_STACK_SIZE < round_page_32(KERNEL_STACK_SIZE)) {     /* Kernel stacks must be multiples of pages */
-               panic("thread_init: kernel stack size (%08X) must be a multiple of page size (%08X)\n", 
-                       KERNEL_STACK_SIZE, PAGE_SIZE);
-       }
-       
-       for(stack_alloc_bndry = PAGE_SIZE; stack_alloc_bndry <= KERNEL_STACK_SIZE; stack_alloc_bndry <<= 1);    /* Find next power of 2 above stack size */
+               task->c_switch += thread->c_switch;
+               task->p_switch += thread->p_switch;
+               task->ps_switch += thread->ps_switch;
 
-       ret = kmem_suballoc(kernel_map,                 /* Suballocate from the kernel map */
+               queue_remove(&task->threads, thread, thread_t, task_threads);
+               task->thread_count--;
 
-               &stack,
-               (stack_alloc_bndry * (2*THREAD_MAX + 64)),      /* Allocate enough for all of it */
-               FALSE,                                                          /* Say not pageable so that it is wired */
-               TRUE,                                                           /* Allocate from anywhere */
-               &stack_map);                                            /* Allocate a submap */
-               
-       if(ret != KERN_SUCCESS) {                               /* Did we get one? */
-               panic("thread_init: kmem_suballoc for stacks failed - ret = %d\n", ret);        /* Die */
-       }       
-       stack = vm_map_min(stack_map);                  /* Make sure we skip the first hunk */
-       ret = vm_map_enter(stack_map, &stack, PAGE_SIZE, 0,     /* Make sure there is nothing at the start */
-               0,                                                                      /* Force it at start */
-               VM_OBJECT_NULL, 0,                                      /* No object yet */
-               FALSE,                                                          /* No copy */
-               VM_PROT_NONE,                                           /* Allow no access */
-               VM_PROT_NONE,                                           /* Allow no access */
-               VM_INHERIT_DEFAULT);                            /* Just be normal */
-               
-       if(ret != KERN_SUCCESS) {                                       /* Did it work? */
-               panic("thread_init: dummy alignment allocation failed; ret = %d\n", ret);
-       }
-               
-#endif  /* MACHINE_STACK */
+               /* 
+                * If the task is being halted, and there is only one thread
+                * left in the task after this one, then wakeup that thread.
+                */
+               if (task->thread_count == 1 && task->halting)
+                       thread_wakeup((event_t)&task->halting);
 
-#if    MACH_LDEBUG
-       thr_sh_template.mutex_count = 0;
-#endif /* MACH_LDEBUG */
+               task_unlock(task);
 
-       {
-               uint64_t                        abstime;
-
-               clock_interval_to_absolutetime_interval(
-                                                       std_quantum_us, NSEC_PER_USEC, &abstime);
-               assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
-               std_quantum = abstime;
-
-               /* 250 us */
-               clock_interval_to_absolutetime_interval(250, NSEC_PER_USEC, &abstime);
-               assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
-               min_std_quantum = abstime;
-
-               /* 50 us */
-               clock_interval_to_absolutetime_interval(50, NSEC_PER_USEC, &abstime);
-               assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
-               min_rt_quantum = abstime;
-
-               /* 50 ms */
-               clock_interval_to_absolutetime_interval(
-                                                                               50, 1000*NSEC_PER_USEC, &abstime);
-               assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
-               max_rt_quantum = abstime;
-
-               max_unsafe_computation = max_unsafe_quanta * std_quantum;
-               max_poll_computation = max_poll_quanta * std_quantum;
-
-               sched_safe_duration = 2 * max_unsafe_quanta *
-                                                                               (std_quantum_us / (1000 * 1000)) *
-                                                                                               (1 << SCHED_TICK_SHIFT);
+               lck_mtx_lock(&tasks_threads_lock);
+               queue_remove(&threads, thread, thread_t, threads);
+               threads_count--;
+               lck_mtx_unlock(&tasks_threads_lock);
+
+               thread_deallocate(thread);
+
+               (void)splsched();
+               simple_lock(&thread_terminate_lock);
        }
 
-       first_thread = TRUE;
-       /*
-        *      Initialize any machine-dependent
-        *      per-thread structures necessary.
-        */
-       thread_machine_init();
+       assert_wait((event_t)&thread_terminate_queue, THREAD_UNINT);
+       simple_unlock(&thread_terminate_lock);
+       /* splsched */
+
+       thread_block((thread_continue_t)thread_terminate_daemon);
+       /*NOTREACHED*/
 }
 
 /*
- * Called at splsched.
+ *     thread_terminate_enqueue:
+ *
+ *     Enqueue a terminating thread for final disposition.
+ *
+ *     Called at splsched.
  */
 void
-thread_reaper_enqueue(
+thread_terminate_enqueue(
        thread_t                thread)
 {
-       simple_lock(&reaper_lock);
-       enqueue_tail(&reaper_queue, (queue_entry_t)thread);
-       simple_unlock(&reaper_lock);
-
-       thread_wakeup((event_t)&reaper_queue);
-}
+       simple_lock(&thread_terminate_lock);
+       enqueue_tail(&thread_terminate_queue, (queue_entry_t)thread);
+       simple_unlock(&thread_terminate_lock);
 
-void
-thread_termination_continue(void)
-{
-       panic("thread_termination_continue");
-       /*NOTREACHED*/
+       thread_wakeup((event_t)&thread_terminate_queue);
 }
 
 /*
- *     Routine: thread_terminate_self
- *
- *             This routine is called by a thread which has unwound from
- *             its current RPC and kernel contexts and found that it's
- *             root activation has been marked for extinction.  This lets
- *             it clean up the last few things that can only be cleaned
- *             up in this context and then impale itself on the reaper
- *             queue.
+ *     thread_stack_daemon:
  *
- *             When the reaper gets the thread, it will deallocate the
- *             thread_act's reference on itself, which in turn will release
- *             its own reference on this thread. By doing things in that
- *             order, a thread_act will always have a valid thread - but the
- *             thread may persist beyond having a thread_act (but must never
- *             run like that).
+ *     Perform stack allocation as required due to
+ *     invoke failures.
  */
-void
-thread_terminate_self(void)
+static void
+thread_stack_daemon(void)
 {
-       thread_act_t    thr_act = current_act();
        thread_t                thread;
-       task_t                  task = thr_act->task;
-       long                    active_acts;
-       spl_t                   s;
 
-       /*
-        * We should be at the base of the inheritance chain.
-        */
-       thread = act_lock_thread(thr_act);
-       assert(thr_act->thread == thread);
+       simple_lock(&thread_stack_lock);
 
-       /* This will allow no more control ops on this thr_act. */
-       ipc_thr_act_disable(thr_act);
+       while ((thread = (thread_t)dequeue_head(&thread_stack_queue)) != THREAD_NULL) {
+               simple_unlock(&thread_stack_lock);
 
-       /* Clean-up any ulocks that are still owned by the thread
-        * activation (acquired but not released or handed-off).
-        */
-       act_ulock_release_all(thr_act);
-       
-       act_unlock_thread(thr_act);
+               stack_alloc(thread);
+               
+               (void)splsched();
+               thread_lock(thread);
+               thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
+               thread_unlock(thread);
+               (void)spllo();
 
-       _mk_sp_thread_depress_abort(thread, TRUE);
+               simple_lock(&thread_stack_lock);
+       }
 
-       /*
-        * Check to see if this is the last active activation.  By
-        * this we mean the last activation to call thread_terminate_self.
-        * If so, and the task is associated with a BSD process, we
-        * need to call BSD and let them clean up.
-        */
-       active_acts = hw_atomic_sub(&task->active_act_count, 1);
+       assert_wait((event_t)&thread_stack_queue, THREAD_UNINT);
+       simple_unlock(&thread_stack_lock);
 
-       if (active_acts == 0 && task->bsd_info)
-               proc_exit(task->bsd_info);
+       thread_block((thread_continue_t)thread_stack_daemon);
+       /*NOTREACHED*/
+}
 
-       /* JMM - for now, no migration */
-       assert(!thr_act->lower);
+/*
+ *     thread_stack_enqueue:
+ *
+ *     Enqueue a thread for stack allocation.
+ *
+ *     Called at splsched.
+ */
+void
+thread_stack_enqueue(
+       thread_t                thread)
+{
+       simple_lock(&thread_stack_lock);
+       enqueue_tail(&thread_stack_queue, (queue_entry_t)thread);
+       simple_unlock(&thread_stack_lock);
 
-       s = splsched();
-       thread_lock(thread);
-       thread->active = FALSE;
-       thread_unlock(thread);
-       splx(s);
+       thread_wakeup((event_t)&thread_stack_queue);
+}
 
-       thread_timer_terminate();
+void
+thread_daemon_init(void)
+{
+       kern_return_t   result;
+       thread_t                thread;
 
-       /* flush any lazy HW state while in own context */
-       thread_machine_flush(thr_act);
+       simple_lock_init(&thread_terminate_lock, 0);
+       queue_init(&thread_terminate_queue);
 
-       ipc_thread_terminate(thread);
+       result = kernel_thread_start_priority((thread_continue_t)thread_terminate_daemon, NULL, MINPRI_KERNEL, &thread);
+       if (result != KERN_SUCCESS)
+               panic("thread_daemon_init: thread_terminate_daemon");
 
-       s = splsched();
-       thread_lock(thread);
-       thread->state |= TH_TERMINATE;
-       assert((thread->state & TH_UNINT) == 0);
-       thread_mark_wait_locked(thread, THREAD_UNINT);
-       assert(thread->promotions == 0);
-       thread_unlock(thread);
-       /* splx(s); */
+       thread_deallocate(thread);
 
-       ETAP_SET_REASON(thread, BLOCKED_ON_TERMINATION);
-       thread_block(thread_termination_continue);
-       /*NOTREACHED*/
+       simple_lock_init(&thread_stack_lock, 0);
+       queue_init(&thread_stack_queue);
+
+       result = kernel_thread_start_priority((thread_continue_t)thread_stack_daemon, NULL, BASEPRI_PREEMPT, &thread);
+       if (result != KERN_SUCCESS)
+               panic("thread_daemon_init: thread_stack_daemon");
+
+       thread_deallocate(thread);
 }
 
 /*
  * Create a new thread.
- * Doesn't start the thread running; It first must be attached to
- * an activation - then use thread_go to start it.
+ * Doesn't start the thread running.
  */
-kern_return_t
-thread_create_shuttle(
-       thread_act_t                    thr_act,
+static kern_return_t
+thread_create_internal(
+       task_t                                  parent_task,
        integer_t                               priority,
-       void                                    (*start)(void),
-       thread_t                                *new_thread)
+       thread_continue_t               continuation,
+       int                                             options,
+#define TH_OPTION_NONE         0x00
+#define TH_OPTION_NOCRED       0x01
+#define TH_OPTION_NOSUSP       0x02
+       thread_t                                *out_thread)
 {
-       kern_return_t                   result;
-       thread_t                                new_shuttle;
-       task_t                                  parent_task = thr_act->task;
-       processor_set_t                 pset;
+       thread_t                                new_thread;
+       static thread_t                 first_thread;
 
        /*
         *      Allocate a thread and initialize static fields
         */
-       if (first_thread) {
-               new_shuttle = &pageout_thread;
-               first_thread = FALSE;
-       } else
-               new_shuttle = (thread_t)zalloc(thread_shuttle_zone);
-       if (new_shuttle == THREAD_NULL)
+       if (first_thread == THREAD_NULL)
+               new_thread = first_thread = current_thread();
+       else
+               new_thread = (thread_t)zalloc(thread_zone);
+       if (new_thread == THREAD_NULL)
                return (KERN_RESOURCE_SHORTAGE);
 
-#ifdef  DEBUG
-       if (new_shuttle != &pageout_thread)
-               assert(!thr_act->thread);
-#endif
+       if (new_thread != first_thread)
+               *new_thread = thread_template;
 
-       *new_shuttle = thr_sh_template;
+#ifdef MACH_BSD
+       new_thread->uthread = uthread_alloc(parent_task, new_thread, (options & TH_OPTION_NOCRED) != 0);
+       if (new_thread->uthread == NULL) {
+               zfree(thread_zone, new_thread);
+               return (KERN_RESOURCE_SHORTAGE);
+       }
+#endif  /* MACH_BSD */
 
-       thread_lock_init(new_shuttle);
-       wake_lock_init(new_shuttle);
-       new_shuttle->sleep_stamp = sched_tick;
+       if (machine_thread_create(new_thread, parent_task) != KERN_SUCCESS) {
+#ifdef MACH_BSD
+               void *ut = new_thread->uthread;
 
-       /*
-        *      Thread still isn't runnable yet (our caller will do
-        *      that).  Initialize runtime-dependent fields here.
-        */
-       result = thread_machine_create(new_shuttle, thr_act, thread_continue);
-       assert (result == KERN_SUCCESS);
+               new_thread->uthread = NULL;
+               /* cred free may not be necessary */
+               uthread_cleanup(parent_task, ut, parent_task->bsd_info);
+               uthread_cred_free(ut);
+               uthread_zone_free(ut);
+#endif  /* MACH_BSD */
 
-       thread_start(new_shuttle, start);
-       thread_timer_setup(new_shuttle);
-       ipc_thread_init(new_shuttle);
+               zfree(thread_zone, new_thread);
+               return (KERN_FAILURE);
+       }
 
-       pset = parent_task->processor_set;
-       assert(pset == &default_pset);
-       pset_lock(pset);
+    new_thread->task = parent_task;
 
+       thread_lock_init(new_thread);
+       wake_lock_init(new_thread);
+
+       lck_mtx_init(&new_thread->mutex, &thread_lck_grp, &thread_lck_attr);
+
+       ipc_thread_init(new_thread);
+       queue_init(&new_thread->held_ulocks);
+
+       new_thread->continuation = continuation;
+
+       lck_mtx_lock(&tasks_threads_lock);
        task_lock(parent_task);
-       assert(parent_task->processor_set == pset);
 
-       /*
-        *      Don't need to initialize because the context switch
-        *      code will set it before it can be used.
-        */
-       if (!parent_task->active) {
+       if (    !parent_task->active || parent_task->halting ||
+                       ((options & TH_OPTION_NOSUSP) != 0 &&
+                               parent_task->suspend_count > 0) ||
+                       (parent_task->thread_count >= task_threadmax &&
+                               parent_task != kernel_task)             ) {
                task_unlock(parent_task);
-               pset_unlock(pset);
-               thread_machine_destroy(new_shuttle);
-               zfree(thread_shuttle_zone, (vm_offset_t) new_shuttle);
+               lck_mtx_unlock(&tasks_threads_lock);
+
+#ifdef MACH_BSD
+               {
+                       void *ut = new_thread->uthread;
+
+                       new_thread->uthread = NULL;
+                       uthread_cleanup(parent_task, ut, parent_task->bsd_info);
+                       /* cred free may not be necessary */
+                       uthread_cred_free(ut);
+                       uthread_zone_free(ut);
+               }
+#endif  /* MACH_BSD */
+               ipc_thread_disable(new_thread);
+               ipc_thread_terminate(new_thread);
+               lck_mtx_destroy(&new_thread->mutex, &thread_lck_grp);
+               machine_thread_destroy(new_thread);
+               zfree(thread_zone, new_thread);
                return (KERN_FAILURE);
        }
 
-       act_attach(thr_act, new_shuttle, 0);
+       /* New threads inherit any default state on the task */
+       machine_thread_inherit_taskwide(new_thread, parent_task);
+
+       task_reference_internal(parent_task);
+
+       /* Cache the task's map */
+       new_thread->map = parent_task->map;
 
-       /* Chain the thr_act onto the task's list */
-       queue_enter(&parent_task->thr_acts, thr_act, thread_act_t, thr_acts);
-       parent_task->thr_act_count++;
-       parent_task->res_act_count++;
+       /* Chain the thread onto the task's list */
+       queue_enter(&parent_task->threads, new_thread, thread_t, task_threads);
+       parent_task->thread_count++;
        
        /* So terminating threads don't need to take the task lock to decrement */
-       hw_atomic_add(&parent_task->active_act_count, 1);
+       hw_atomic_add(&parent_task->active_thread_count, 1);
 
-       /* Associate the thread with the processor set */
-       pset_add_thread(pset, new_shuttle);
+       /* Protected by the tasks_threads_lock */
+       new_thread->thread_id = ++thread_unique_id;
+
+       queue_enter(&threads, new_thread, thread_t, threads);
+       threads_count++;
+
+       timer_call_setup(&new_thread->wait_timer, thread_timer_expire, new_thread);
+       timer_call_setup(&new_thread->depress_timer, thread_depress_expire, new_thread);
+
+#if CONFIG_COUNTERS
+       /*
+        * If parent task has any reservations, they need to be propagated to this
+        * thread.
+        */
+       new_thread->t_chud = (TASK_PMC_FLAG == (parent_task->t_chud & TASK_PMC_FLAG)) ? 
+               THREAD_PMC_FLAG : 0U;
+#endif
 
        /* Set the thread's scheduling parameters */
        if (parent_task != kernel_task)
-               new_shuttle->sched_mode |= TH_MODE_TIMESHARE;
-       new_shuttle->max_priority = parent_task->max_priority;
-       new_shuttle->task_priority = parent_task->priority;
-       new_shuttle->priority = (priority < 0)? parent_task->priority: priority;
-       if (new_shuttle->priority > new_shuttle->max_priority)
-               new_shuttle->priority = new_shuttle->max_priority;
-       new_shuttle->importance =
-                                       new_shuttle->priority - new_shuttle->task_priority;
-       new_shuttle->sched_stamp = sched_tick;
-       compute_priority(new_shuttle, FALSE);
-
-#if    ETAP_EVENT_MONITOR
-       new_thread->etap_reason = 0;
-       new_thread->etap_trace  = FALSE;
-#endif /* ETAP_EVENT_MONITOR */
-
-       new_shuttle->active = TRUE;
-       thr_act->active = TRUE;
-
-       *new_thread = new_shuttle;
+               new_thread->sched_mode |= TH_MODE_TIMESHARE;
+       new_thread->max_priority = parent_task->max_priority;
+       new_thread->task_priority = parent_task->priority;
+       new_thread->priority = (priority < 0)? parent_task->priority: priority;
+       if (new_thread->priority > new_thread->max_priority)
+               new_thread->priority = new_thread->max_priority;
+       new_thread->importance =
+                                       new_thread->priority - new_thread->task_priority;
+       new_thread->sched_stamp = sched_tick;
+       new_thread->pri_shift = sched_pri_shift;
+       compute_priority(new_thread, FALSE);
+
+       new_thread->active = TRUE;
+
+       *out_thread = new_thread;
 
        {
                long    dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4;
 
+               kdbg_trace_data(parent_task->bsd_info, &dbg_arg2);
+
                KERNEL_DEBUG_CONSTANT(
                                        TRACEDBG_CODE(DBG_TRACE_DATA, 1) | DBG_FUNC_NONE,
-                                                       (vm_address_t)new_shuttle, 0, 0, 0, 0);
+                                                       (vm_address_t)(uintptr_t)thread_tid(new_thread), dbg_arg2, 0, 0, 0);
 
                kdbg_trace_string(parent_task->bsd_info,
                                                        &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4);
@@ -888,42 +746,35 @@ thread_create_shuttle(
                                                        dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0);
        }
 
+       DTRACE_PROC1(lwp__create, thread_t, *out_thread);
+
        return (KERN_SUCCESS);
 }
 
-extern void                    thread_bootstrap_return(void);
-
 kern_return_t
 thread_create(
        task_t                          task,
-       thread_act_t            *new_act)
+       thread_t                        *new_thread)
 {
        kern_return_t           result;
        thread_t                        thread;
-       thread_act_t            act;
 
-       if (task == TASK_NULL)
-               return KERN_INVALID_ARGUMENT;
+       if (task == TASK_NULL || task == kernel_task)
+               return (KERN_INVALID_ARGUMENT);
 
-       result = act_create(task, &act);
+       result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
        if (result != KERN_SUCCESS)
                return (result);
 
-       result = thread_create_shuttle(act, -1, thread_bootstrap_return, &thread);
-       if (result != KERN_SUCCESS) {
-               act_deallocate(act);
-               return (result);
-       }
-
-       act->user_stop_count = 1;
-       thread_hold(act);
+       thread->user_stop_count = 1;
+       thread_hold(thread);
        if (task->suspend_count > 0)
-               thread_hold(act);
+               thread_hold(thread);
 
-       pset_unlock(task->processor_set);
        task_unlock(task);
+       lck_mtx_unlock(&tasks_threads_lock);
        
-       *new_act = act;
+       *new_thread = thread;
 
        return (KERN_SUCCESS);
 }
@@ -934,202 +785,173 @@ thread_create_running(
        int                     flavor,
        thread_state_t          new_state,
        mach_msg_type_number_t  new_state_count,
-       thread_act_t                    *new_act)                       /* OUT */
+       thread_t                                *new_thread)
 {
        register kern_return_t  result;
        thread_t                                thread;
-       thread_act_t                    act;
 
-       if (task == TASK_NULL)
-               return KERN_INVALID_ARGUMENT;
+       if (task == TASK_NULL || task == kernel_task)
+               return (KERN_INVALID_ARGUMENT);
 
-       result = act_create(task, &act);
+       result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
        if (result != KERN_SUCCESS)
                return (result);
 
-       result = thread_create_shuttle(act, -1, thread_bootstrap_return, &thread);
-       if (result != KERN_SUCCESS) {
-               act_deallocate(act);
-               return (result);
-       }
-
-       act_lock(act);
-       result = act_machine_set_state(act, flavor, new_state, new_state_count);
+       result = machine_thread_set_state(
+                                               thread, flavor, new_state, new_state_count);
        if (result != KERN_SUCCESS) {
-               act_unlock(act);
-               pset_unlock(task->processor_set);
                task_unlock(task);
+               lck_mtx_unlock(&tasks_threads_lock);
 
-               (void)thread_terminate(act);
+               thread_terminate(thread);
+               thread_deallocate(thread);
                return (result);
        }
 
-       clear_wait(thread, THREAD_AWAKENED);
-       act->inited = TRUE;
-       act_unlock(act);
-       pset_unlock(task->processor_set);
+       thread_mtx_lock(thread);
+       thread_start_internal(thread);
+       thread_mtx_unlock(thread);
+
        task_unlock(task);
+       lck_mtx_unlock(&tasks_threads_lock);
 
-       *new_act = act;
+       *new_thread = thread;
 
        return (result);
 }
 
-/*
- *     kernel_thread:
- *
- *     Create and kernel thread in the specified task, and
- *     optionally start it running.
- */
-thread_t
-kernel_thread_with_priority(
+kern_return_t
+thread_create_workq(
        task_t                          task,
-       integer_t                       priority,
-       void                            (*start)(void),
-       boolean_t                       alloc_stack,
-       boolean_t                       start_running)
+       thread_continue_t               thread_return,
+       thread_t                        *new_thread)
 {
        kern_return_t           result;
        thread_t                        thread;
-       thread_act_t            act;
 
-       result = act_create(task, &act);
+       if (task == TASK_NULL || task == kernel_task)
+               return (KERN_INVALID_ARGUMENT);
+
+       result = thread_create_internal(task, -1, thread_return, TH_OPTION_NOCRED | TH_OPTION_NOSUSP, &thread);
        if (result != KERN_SUCCESS)
-               return (THREAD_NULL);
+               return (result);
 
-       result = thread_create_shuttle(act, priority, start, &thread);
-       if (result != KERN_SUCCESS) {
-               act_deallocate(act);
-               return (THREAD_NULL);
-       }
+       thread->user_stop_count = 1;
+       thread_hold(thread);
+       if (task->suspend_count > 0)
+               thread_hold(thread);
 
-       pset_unlock(task->processor_set);
        task_unlock(task);
+       lck_mtx_unlock(&tasks_threads_lock);
+       
+       *new_thread = thread;
 
-       if (alloc_stack)
-               thread_doswapin(thread);
-
-       act_lock(act);
-       if (start_running)
-               clear_wait(thread, THREAD_AWAKENED);
-       act->inited = TRUE;
-       act_unlock(act);
-
-       act_deallocate(act);
-
-       return (thread);
+       return (KERN_SUCCESS);
 }
 
-thread_t
-kernel_thread(
-       task_t                  task,
-       void                    (*start)(void))
+/*
+ *     kernel_thread_create:
+ *
+ *     Create a thread in the kernel task
+ *     to execute in kernel context.
+ */
+kern_return_t
+kernel_thread_create(
+       thread_continue_t       continuation,
+       void                            *parameter,
+       integer_t                       priority,
+       thread_t                        *new_thread)
 {
-       return kernel_thread_with_priority(task, -1, start, FALSE, TRUE);
-}
-
-unsigned int c_weird_pset_ref_exit = 0;        /* pset code raced us */
-
-#if    MACH_HOST
-/* Preclude thread processor set assignement */
-#define thread_freeze(thread)  assert((thread)->processor_set == &default_pset)
-
-/* Allow thread processor set assignement */
-#define thread_unfreeze(thread)        assert((thread)->processor_set == &default_pset)
-
-#endif /* MACH_HOST */
+       kern_return_t           result;
+       thread_t                        thread;
+       task_t                          task = kernel_task;
 
-void
-thread_deallocate(
-       thread_t                        thread)
-{
-       task_t                          task;
-       processor_set_t         pset;
-       int                                     refs;
-       spl_t                           s;
+       result = thread_create_internal(task, priority, continuation, TH_OPTION_NONE, &thread);
+       if (result != KERN_SUCCESS)
+               return (result);
 
-       if (thread == THREAD_NULL)
-               return;
+       task_unlock(task);
+       lck_mtx_unlock(&tasks_threads_lock);
 
-       /*
-        *      First, check for new count > 0 (the common case).
-        *      Only the thread needs to be locked.
-        */
-       s = splsched();
-       thread_lock(thread);
-       refs = --thread->ref_count;
-       thread_unlock(thread);
-       splx(s);
+       stack_alloc(thread);
+       assert(thread->kernel_stack != 0);
+#if CONFIG_EMBEDDED
+       if (priority > BASEPRI_KERNEL)
+#endif
+       thread->reserved_stack = thread->kernel_stack;
 
-       if (refs > 0)
-               return;
+       thread->parameter = parameter;
 
-       if (thread == current_thread())
-           panic("thread deallocating itself");
+if(debug_task & 1)
+       kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread, continuation);
+       *new_thread = thread;
 
-       /*
-        *      There is a dangling pointer to the thread from the
-        *      processor_set.  To clean it up, we freeze the thread
-        *      in the pset (because pset destruction can cause even
-        *      reference-less threads to be reassigned to the default
-        *      pset) and then remove it.
-        */
+       return (result);
+}
 
-#if MACH_HOST
-       thread_freeze(thread);
-#endif
+kern_return_t
+kernel_thread_start_priority(
+       thread_continue_t       continuation,
+       void                            *parameter,
+       integer_t                       priority,
+       thread_t                        *new_thread)
+{
+       kern_return_t   result;
+       thread_t                thread;
 
-       pset = thread->processor_set;
-       pset_lock(pset);
-       pset_remove_thread(pset, thread);
-       pset_unlock(pset);
+       result = kernel_thread_create(continuation, parameter, priority, &thread);
+       if (result != KERN_SUCCESS)
+               return (result);
 
-#if MACH_HOST
-       thread_unfreeze(thread);
-#endif
+       *new_thread = thread;   
 
-       pset_deallocate(pset);
+       thread_mtx_lock(thread);
+       thread_start_internal(thread);
+       thread_mtx_unlock(thread);
 
-       if (thread->stack_privilege != 0) {
-               if (thread->stack_privilege != thread->kernel_stack)
-                       stack_free_stack(thread->stack_privilege);
-               thread->stack_privilege = 0;
-       }
-       /* frees kernel stack & other MD resources */
-       thread_machine_destroy(thread);
+       return (result);
+}
 
-       zfree(thread_shuttle_zone, (vm_offset_t) thread);
+kern_return_t
+kernel_thread_start(
+       thread_continue_t       continuation,
+       void                            *parameter,
+       thread_t                        *new_thread)
+{
+       return kernel_thread_start_priority(continuation, parameter, -1, new_thread);
 }
 
-void
-thread_reference(
-       thread_t        thread)
+#ifndef        __LP64__
+
+thread_t
+kernel_thread(
+       task_t                  task,
+       void                    (*start)(void))
 {
-       spl_t           s;
+       kern_return_t   result;
+       thread_t                thread;
 
-       if (thread == THREAD_NULL)
-               return;
+       if (task != kernel_task)
+               panic("kernel_thread");
 
-       s = splsched();
-       thread_lock(thread);
-       thread_reference_locked(thread);
-       thread_unlock(thread);
-       splx(s);
+       result = kernel_thread_start_priority((thread_continue_t)start, NULL, -1, &thread);
+       if (result != KERN_SUCCESS)
+               return (THREAD_NULL);
+
+       thread_deallocate(thread);
+
+       return (thread);
 }
 
-/*
- * Called with "appropriate" thread-related locks held on
- * thread and its top_act for synchrony with RPC (see
- * act_lock_thread()).
- */
+#endif /* __LP64__ */
+
 kern_return_t
-thread_info_shuttle(
-       register thread_act_t   thr_act,
+thread_info_internal(
+       register thread_t               thread,
        thread_flavor_t                 flavor,
        thread_info_t                   thread_info_out,        /* ptr to OUT array */
        mach_msg_type_number_t  *thread_info_count)     /*IN/OUT*/
 {
-       register thread_t               thread = thr_act->thread;
        int                                             state, flags;
        spl_t                                   s;
 
@@ -1165,24 +987,21 @@ thread_info_shuttle(
                 *      then for 5/8 ageing.  The correction factor [3/5] is
                 *      (1/(5/8) - 1).
                 */
-               basic_info->cpu_usage = (thread->cpu_usage << SCHED_TICK_SHIFT) /
-                                                                                               (TIMER_RATE / TH_USAGE_SCALE);
+               basic_info->cpu_usage = (integer_t)(((uint64_t)thread->cpu_usage
+                                                                       * TH_USAGE_SCALE) /     sched_tick_interval);
                basic_info->cpu_usage = (basic_info->cpu_usage * 3) / 5;
-#if    SIMPLE_CLOCK
-               /*
-                *      Clock drift compensation.
-                */
-               basic_info->cpu_usage = (basic_info->cpu_usage * 1000000) / sched_usec;
-#endif /* SIMPLE_CLOCK */
+
+               if (basic_info->cpu_usage > TH_USAGE_SCALE)
+                       basic_info->cpu_usage = TH_USAGE_SCALE;
 
                basic_info->policy = ((thread->sched_mode & TH_MODE_TIMESHARE)?
                                                                                                POLICY_TIMESHARE: POLICY_RR);
 
            flags = 0;
-               if (thread->state & TH_IDLE)
+               if (thread->bound_processor != PROCESSOR_NULL && thread->bound_processor->idle_thread == thread)
                        flags |= TH_FLAGS_IDLE;
 
-           if (thread->state & TH_STACK_HANDOFF)
+           if (!thread->kernel_stack)
                        flags |= TH_FLAGS_SWAPPED;
 
            state = 0;
@@ -1204,7 +1023,7 @@ thread_info_shuttle(
            basic_info->run_state = state;
            basic_info->flags = flags;
 
-           basic_info->suspend_count = thr_act->user_stop_count;
+           basic_info->suspend_count = thread->user_stop_count;
 
            thread_unlock(thread);
            splx(s);
@@ -1214,6 +1033,36 @@ thread_info_shuttle(
            return (KERN_SUCCESS);
        }
        else
+       if (flavor == THREAD_IDENTIFIER_INFO) {
+           register thread_identifier_info_t   identifier_info;
+
+           if (*thread_info_count < THREAD_IDENTIFIER_INFO_COUNT)
+                       return (KERN_INVALID_ARGUMENT);
+
+           identifier_info = (thread_identifier_info_t) thread_info_out;
+
+           s = splsched();
+           thread_lock(thread);
+
+           identifier_info->thread_id = thread->thread_id;
+#if defined(__ppc__) || defined(__arm__)
+           identifier_info->thread_handle = thread->machine.cthread_self;
+#else
+           identifier_info->thread_handle = thread->machine.pcb->cthread_self;
+#endif
+           if(thread->task->bsd_info) {
+               identifier_info->dispatch_qaddr =  identifier_info->thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
+           } else {
+                   thread_unlock(thread);
+                   splx(s);
+                   return KERN_INVALID_ARGUMENT;
+           }
+
+           thread_unlock(thread);
+           splx(s);
+           return KERN_SUCCESS;
+       }
+       else
        if (flavor == THREAD_SCHED_TIMESHARE_INFO) {
                policy_timeshare_info_t         ts_info;
 
@@ -1303,87 +1152,29 @@ thread_info_shuttle(
 }
 
 void
-thread_doreap(
-       register thread_t       thread)
-{
-       thread_act_t            thr_act;
-
-
-       thr_act = thread_lock_act(thread);
-       assert(thr_act && thr_act->thread == thread);
-
-       act_locked_act_reference(thr_act);
-
-       /*
-        * Replace `act_unlock_thread()' with individual
-        * calls.  (`act_detach()' can change fields used
-        * to determine which locks are held, confusing
-        * `act_unlock_thread()'.)
-        */
-       act_unlock(thr_act);
-
-       /* Remove the reference held by a rooted thread */
-       act_deallocate(thr_act);
-
-       /* Remove the reference held by the thread: */
-       act_deallocate(thr_act);
-}
-
-/*
- *     reaper_thread:
- *
- *     This kernel thread runs forever looking for terminating
- *     threads, releasing their "self" references.
- */
-static void
-reaper_thread_continue(void)
+thread_read_times(
+       thread_t                thread,
+       time_value_t    *user_time,
+       time_value_t    *system_time)
 {
-       register thread_t       thread;
-
-       (void)splsched();
-       simple_lock(&reaper_lock);
-
-       while ((thread = (thread_t) dequeue_head(&reaper_queue)) != THREAD_NULL) {
-               simple_unlock(&reaper_lock);
-               (void)spllo();
-
-               thread_doreap(thread);
-
-               (void)splsched();
-               simple_lock(&reaper_lock);
-       }
-
-       assert_wait((event_t)&reaper_queue, THREAD_UNINT);
-       simple_unlock(&reaper_lock);
-       (void)spllo();
-
-       thread_block(reaper_thread_continue);
-       /*NOTREACHED*/
-}
-
-static void
-reaper_thread(void)
-{
-       thread_t        self = current_thread();
-
-       stack_privilege(self);
+       clock_sec_t             secs;
+       clock_usec_t    usecs;
 
-       reaper_thread_continue();
-       /*NOTREACHED*/
-}
+       absolutetime_to_microtime(timer_grab(&thread->user_timer), &secs, &usecs);
+       user_time->seconds = (typeof(user_time->seconds))secs;
+       user_time->microseconds = usecs;
 
-void
-thread_reaper_init(void)
-{
-       kernel_thread(kernel_task, reaper_thread);
+       absolutetime_to_microtime(timer_grab(&thread->system_timer), &secs, &usecs);
+       system_time->seconds = (typeof(system_time->seconds))secs;
+       system_time->microseconds = usecs;
 }
 
 kern_return_t
 thread_assign(
-       thread_act_t    thr_act,
-       processor_set_t new_pset)
+       __unused thread_t                       thread,
+       __unused processor_set_t        new_pset)
 {
-       return(KERN_FAILURE);
+       return (KERN_FAILURE);
 }
 
 /*
@@ -1394,9 +1185,9 @@ thread_assign(
  */
 kern_return_t
 thread_assign_default(
-       thread_act_t    thr_act)
+       thread_t                thread)
 {
-       return (thread_assign(thr_act, &default_pset));
+       return (thread_assign(thread, &pset0));
 }
 
 /*
@@ -1406,292 +1197,90 @@ thread_assign_default(
  */        
 kern_return_t
 thread_get_assignment(
-       thread_act_t    thr_act,
+       thread_t                thread,
        processor_set_t *pset)
 {
-       thread_t        thread;
-
-       if (thr_act == THR_ACT_NULL)
-               return(KERN_INVALID_ARGUMENT);
-       thread = act_lock_thread(thr_act);
-       if (thread == THREAD_NULL) {
-               act_unlock_thread(thr_act);
-               return(KERN_INVALID_ARGUMENT);
-       }
-       *pset = thread->processor_set;
-       act_unlock_thread(thr_act);
-       pset_reference(*pset);
-       return(KERN_SUCCESS);
+       if (thread == NULL)
+               return (KERN_INVALID_ARGUMENT);
+
+       *pset = &pset0;
+
+       return (KERN_SUCCESS);
 }
 
 /*
- *     thread_wire:
+ *     thread_wire_internal:
  *
  *     Specify that the target thread must always be able
  *     to run and to allocate memory.
  */
 kern_return_t
-thread_wire(
-       host_priv_t     host_priv,
-       thread_act_t    thr_act,
-       boolean_t       wired)
+thread_wire_internal(
+       host_priv_t             host_priv,
+       thread_t                thread,
+       boolean_t               wired,
+       boolean_t               *prev_state)
 {
-       spl_t           s;
-       thread_t        thread;
-       extern void vm_page_free_reserve(int pages);
-
-       if (thr_act == THR_ACT_NULL || host_priv == HOST_PRIV_NULL)
+       if (host_priv == NULL || thread != current_thread())
                return (KERN_INVALID_ARGUMENT);
 
        assert(host_priv == &realhost);
 
-       thread = act_lock_thread(thr_act);
-       if (thread ==THREAD_NULL) {
-               act_unlock_thread(thr_act);
-               return(KERN_INVALID_ARGUMENT);
-       }
-
-       /*
-        * This implementation only works for the current thread.
-        * See stack_privilege.
-        */
-       if (thr_act != current_act())
-           return KERN_INVALID_ARGUMENT;
-
-       s = splsched();
-       thread_lock(thread);
-
+       if (prev_state)
+           *prev_state = (thread->options & TH_OPT_VMPRIV) != 0;
+       
        if (wired) {
-           if (thread->vm_privilege == FALSE
+           if (!(thread->options & TH_OPT_VMPRIV)
                    vm_page_free_reserve(1);    /* XXX */
-           thread->vm_privilege = TRUE;
-       } else {
-           if (thread->vm_privilege == TRUE) 
+           thread->options |= TH_OPT_VMPRIV;
+       }
+       else {
+           if (thread->options & TH_OPT_VMPRIV) 
                    vm_page_free_reserve(-1);   /* XXX */
-           thread->vm_privilege = FALSE;
+           thread->options &= ~TH_OPT_VMPRIV;
        }
 
-       thread_unlock(thread);
-       splx(s);
-       act_unlock_thread(thr_act);
-
-       return KERN_SUCCESS;
-}
-
-/*
- *     thread_collect_scan:
- *
- *     Attempt to free resources owned by threads.
- */
-
-void
-thread_collect_scan(void)
-{
-       /* This code runs very quickly! */
+       return (KERN_SUCCESS);
 }
 
-/* Also disabled in vm/vm_pageout.c */
-boolean_t thread_collect_allowed = FALSE;
-unsigned thread_collect_last_tick = 0;
-unsigned thread_collect_max_rate = 0;          /* in ticks */
 
 /*
- *     consider_thread_collect:
+ *     thread_wire:
  *
- *     Called by the pageout daemon when the system needs more free pages.
+ *     User-api wrapper for thread_wire_internal()
  */
-
-void
-consider_thread_collect(void)
-{
-       /*
-        *      By default, don't attempt thread collection more frequently
-        *      than once a second.
-        */
-
-       if (thread_collect_max_rate == 0)
-               thread_collect_max_rate = (1 << SCHED_TICK_SHIFT) + 1;
-
-       if (thread_collect_allowed &&
-           (sched_tick >
-            (thread_collect_last_tick + thread_collect_max_rate))) {
-               thread_collect_last_tick = sched_tick;
-               thread_collect_scan();
-       }
-}
-
 kern_return_t
-host_stack_usage(
-       host_t          host,
-       vm_size_t       *reservedp,
-       unsigned int    *totalp,
-       vm_size_t       *spacep,
-       vm_size_t       *residentp,
-       vm_size_t       *maxusagep,
-       vm_offset_t     *maxstackp)
+thread_wire(
+       host_priv_t     host_priv,
+       thread_t        thread,
+       boolean_t       wired)
 {
-#if !MACH_DEBUG
-        return KERN_NOT_SUPPORTED;
-#else
-       unsigned int total;
-       vm_size_t maxusage;
-
-       if (host == HOST_NULL)
-               return KERN_INVALID_HOST;
-
-       maxusage = 0;
-
-       stack_statistics(&total, &maxusage);
-
-       *reservedp = 0;
-       *totalp = total;
-       *spacep = *residentp = total * round_page_32(KERNEL_STACK_SIZE);
-       *maxusagep = maxusage;
-       *maxstackp = 0;
-       return KERN_SUCCESS;
-
-#endif /* MACH_DEBUG */
+    return (thread_wire_internal(host_priv, thread, wired, NULL));
 }
 
-/*
- * Return info on stack usage for threads in a specific processor set
- */
-kern_return_t
-processor_set_stack_usage(
-       processor_set_t pset,
-       unsigned int    *totalp,
-       vm_size_t       *spacep,
-       vm_size_t       *residentp,
-       vm_size_t       *maxusagep,
-       vm_offset_t     *maxstackp)
-{
-#if !MACH_DEBUG
-        return KERN_NOT_SUPPORTED;
-#else
-       unsigned int total;
-       vm_size_t maxusage;
-       vm_offset_t maxstack;
-
-       register thread_t *threads;
-       register thread_t thread;
-
-       unsigned int actual;    /* this many things */
-       unsigned int i;
-
-       vm_size_t size, size_needed;
-       vm_offset_t addr;
-
-       spl_t s;
-
-       if (pset == PROCESSOR_SET_NULL)
-               return KERN_INVALID_ARGUMENT;
-
-       size = 0; addr = 0;
-
-       for (;;) {
-               pset_lock(pset);
-               if (!pset->active) {
-                       pset_unlock(pset);
-                       return KERN_INVALID_ARGUMENT;
-               }
-
-               actual = pset->thread_count;
-
-               /* do we have the memory we need? */
-
-               size_needed = actual * sizeof(thread_t);
-               if (size_needed <= size)
-                       break;
-
-               /* unlock the pset and allocate more memory */
-               pset_unlock(pset);
-
-               if (size != 0)
-                       kfree(addr, size);
-
-               assert(size_needed > 0);
-               size = size_needed;
-
-               addr = kalloc(size);
-               if (addr == 0)
-                       return KERN_RESOURCE_SHORTAGE;
-       }
-
-       /* OK, have memory and the processor_set is locked & active */
-       s = splsched();
-       threads = (thread_t *) addr;
-       for (i = 0, thread = (thread_t) queue_first(&pset->threads);
-            !queue_end(&pset->threads, (queue_entry_t) thread);
-            thread = (thread_t) queue_next(&thread->pset_threads)) {
-               thread_lock(thread);
-               if (thread->ref_count > 0) {
-                       thread_reference_locked(thread);
-                       threads[i++] = thread;
-               }
-               thread_unlock(thread);
-       }
-       splx(s);
-       assert(i <= actual);
-
-       /* can unlock processor set now that we have the thread refs */
-       pset_unlock(pset);
-
-       /* calculate maxusage and free thread references */
-
-       total = 0;
-       maxusage = 0;
-       maxstack = 0;
-       while (i > 0) {
-               int cpu;
-               thread_t thread = threads[--i];
-               vm_offset_t stack = 0;
-
-               /*
-                *      thread->kernel_stack is only accurate if the
-                *      thread isn't swapped and is not executing.
-                *
-                *      Of course, we don't have the appropriate locks
-                *      for these shenanigans.
-                */
-
-               stack = thread->kernel_stack;
-
-               for (cpu = 0; cpu < NCPUS; cpu++)
-                       if (cpu_to_processor(cpu)->cpu_data->active_thread == thread) {
-                               stack = active_stacks[cpu];
-                               break;
-                       }
+int            split_funnel_off = 0;
+lck_grp_t      *funnel_lck_grp = LCK_GRP_NULL;
+lck_grp_attr_t *funnel_lck_grp_attr;
+lck_attr_t     *funnel_lck_attr;
 
-               if (stack != 0) {
-                       total++;
-               }
-
-               thread_deallocate(thread);
-       }
-
-       if (size != 0)
-               kfree(addr, size);
-
-       *totalp = total;
-       *residentp = *spacep = total * round_page_32(KERNEL_STACK_SIZE);
-       *maxusagep = maxusage;
-       *maxstackp = maxstack;
-       return KERN_SUCCESS;
-
-#endif /* MACH_DEBUG */
-}
-
-int split_funnel_off = 0;
 funnel_t *
 funnel_alloc(
        int type)
 {
-       mutex_t *m;
-       funnel_t * fnl;
+       lck_mtx_t       *m;
+       funnel_t        *fnl;
+
+       if (funnel_lck_grp == LCK_GRP_NULL) {
+               funnel_lck_grp_attr = lck_grp_attr_alloc_init();
+
+               funnel_lck_grp = lck_grp_alloc_init("Funnel",  funnel_lck_grp_attr);
+
+               funnel_lck_attr = lck_attr_alloc_init();
+       }
        if ((fnl = (funnel_t *)kalloc(sizeof(funnel_t))) != 0){
                bzero((void *)fnl, sizeof(funnel_t));
-               if ((m = mutex_alloc(0)) == (mutex_t *)NULL) {
-                       kfree((vm_offset_t)fnl, sizeof(funnel_t));
+               if ((m = lck_mtx_alloc_init(funnel_lck_grp, funnel_lck_attr)) == (lck_mtx_t *)NULL) {
+                       kfree(fnl, sizeof(funnel_t));
                        return(THR_FUNNEL_NULL);
                }
                fnl->fnl_mutex = m;
@@ -1704,34 +1293,26 @@ void
 funnel_free(
        funnel_t * fnl)
 {
-       mutex_free(fnl->fnl_mutex);
+       lck_mtx_free(fnl->fnl_mutex, funnel_lck_grp);
        if (fnl->fnl_oldmutex)
-               mutex_free(fnl->fnl_oldmutex);
-       kfree((vm_offset_t)fnl, sizeof(funnel_t));
+               lck_mtx_free(fnl->fnl_oldmutex, funnel_lck_grp);
+       kfree(fnl, sizeof(funnel_t));
 }
 
 void 
 funnel_lock(
        funnel_t * fnl)
 {
-       mutex_t * m;
-
-       m = fnl->fnl_mutex;
-restart:
-       mutex_lock(m);
+       lck_mtx_lock(fnl->fnl_mutex);
        fnl->fnl_mtxholder = current_thread();
-       if (split_funnel_off && (m != fnl->fnl_mutex)) {
-               mutex_unlock(m);
-               m = fnl->fnl_mutex;     
-               goto restart;
-       }
 }
 
 void 
 funnel_unlock(
        funnel_t * fnl)
 {
-       mutex_unlock(fnl->fnl_mutex);
+       lck_mtx_unlock(fnl->fnl_mutex);
+       fnl->fnl_mtxholder = NULL;
        fnl->fnl_mtxrelease = current_thread();
 }
 
@@ -1764,7 +1345,7 @@ thread_funnel_set(
 
                if (funneled == TRUE) {
                        if (cur_thread->funnel_lock)
-                               panic("Funnel lock called when holding one %x", cur_thread->funnel_lock);
+                               panic("Funnel lock called when holding one %p", cur_thread->funnel_lock);
                        KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE,
                                                                                        fnl, 1, 0, 0, 0);
                        funnel_lock(fnl);
@@ -1794,68 +1375,193 @@ thread_funnel_set(
        return(funnel_state_prev);
 }
 
-boolean_t
-thread_funnel_merge(
-       funnel_t * fnl,
-       funnel_t * otherfnl)
+static void
+sched_call_null(
+__unused       int                     type,
+__unused       thread_t        thread)
 {
-       mutex_t * m;
-       mutex_t * otherm;
-       funnel_t * gfnl;
-       extern int disable_funnel;
-
-       if ((gfnl = thread_funnel_get()) == THR_FUNNEL_NULL)
-               panic("thread_funnel_merge called with no funnels held");
-
-       if (gfnl->fnl_type != 1)
-               panic("thread_funnel_merge called from non kernel funnel");
-
-       if (gfnl != fnl)
-               panic("thread_funnel_merge incorrect invocation");
-
-       if (disable_funnel || split_funnel_off)
-               return (KERN_FAILURE);
-
-       m = fnl->fnl_mutex;
-       otherm = otherfnl->fnl_mutex;
-
-       /* Acquire other funnel mutex */
-       mutex_lock(otherm);
-       split_funnel_off = 1;
-       disable_funnel = 1;
-       otherfnl->fnl_mutex = m;
-       otherfnl->fnl_type = fnl->fnl_type;
-       otherfnl->fnl_oldmutex = otherm;        /* save this for future use */
+       return;
+}
 
-       mutex_unlock(otherm);
-       return(KERN_SUCCESS);
+void
+thread_sched_call(
+       thread_t                thread,
+       sched_call_t    call)
+{
+       thread->sched_call = (call != NULL)? call: sched_call_null;
 }
 
 void
-thread_set_cont_arg(
-       int                             arg)
+thread_static_param(
+       thread_t                thread,
+       boolean_t               state)
 {
-       thread_t                self = current_thread();
+       thread_mtx_lock(thread);
+       thread->static_param = state;
+       thread_mtx_unlock(thread);
+}
 
-       self->saved.misc = arg; 
+uint64_t
+thread_tid(
+       thread_t        thread)
+{
+       return (thread != THREAD_NULL? thread->thread_id: 0);
 }
 
-int
-thread_get_cont_arg(void)
+uint64_t
+thread_dispatchqaddr(
+       thread_t                thread)
 {
-       thread_t                self = current_thread();
+       uint64_t        dispatchqueue_addr = 0;
+       uint64_t        thread_handle = 0;
+
+       if (thread != THREAD_NULL) {
+#if defined(__ppc__) || defined(__arm__)
+               thread_handle = thread->machine.cthread_self;
+#else
+               thread_handle = thread->machine.pcb->cthread_self;
+#endif
+
+               if (thread->task->bsd_info)
+                       dispatchqueue_addr = thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
+       }
 
-       return (self->saved.misc); 
+       return (dispatchqueue_addr);
 }
 
 /*
  * Export routines to other components for things that are done as macros
  * within the osfmk component.
  */
+
+#undef thread_reference
+void thread_reference(thread_t thread);
+void
+thread_reference(
+       thread_t        thread)
+{
+       if (thread != THREAD_NULL)
+               thread_reference_internal(thread);
+}
+
 #undef thread_should_halt
+
 boolean_t
 thread_should_halt(
-       thread_shuttle_t th)
+       thread_t                th)
+{
+       return (thread_should_halt_fast(th));
+}
+
+#if CONFIG_DTRACE
+uint32_t dtrace_get_thread_predcache(thread_t thread)
+{
+       if (thread != THREAD_NULL)
+               return thread->t_dtrace_predcache;
+       else
+               return 0;
+}
+
+int64_t dtrace_get_thread_vtime(thread_t thread)
+{
+       if (thread != THREAD_NULL)
+               return thread->t_dtrace_vtime;
+       else
+               return 0;
+}
+
+int64_t dtrace_get_thread_tracing(thread_t thread)
+{
+       if (thread != THREAD_NULL)
+               return thread->t_dtrace_tracing;
+       else
+               return 0;
+}
+
+boolean_t dtrace_get_thread_reentering(thread_t thread)
+{
+       if (thread != THREAD_NULL)
+               return (thread->options & TH_OPT_DTRACE) ? TRUE : FALSE;
+       else
+               return 0;
+}
+
+vm_offset_t dtrace_get_kernel_stack(thread_t thread)
+{
+       if (thread != THREAD_NULL)
+               return thread->kernel_stack;
+       else
+               return 0;
+}
+
+int64_t dtrace_calc_thread_recent_vtime(thread_t thread)
+{
+#if STAT_TIME
+       if (thread != THREAD_NULL) {
+               return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer));
+       } else
+               return 0;
+#else
+       if (thread != THREAD_NULL) {
+               processor_t             processor = current_processor();
+               uint64_t                                abstime = mach_absolute_time();
+               timer_t                                 timer;
+
+               timer = PROCESSOR_DATA(processor, thread_timer);
+
+               return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer)) +
+                               (abstime - timer->tstamp); /* XXX need interrupts off to prevent missed time? */
+       } else
+               return 0;
+#endif
+}
+
+void dtrace_set_thread_predcache(thread_t thread, uint32_t predcache)
+{
+       if (thread != THREAD_NULL)
+               thread->t_dtrace_predcache = predcache;
+}
+
+void dtrace_set_thread_vtime(thread_t thread, int64_t vtime)
+{
+       if (thread != THREAD_NULL)
+               thread->t_dtrace_vtime = vtime;
+}
+
+void dtrace_set_thread_tracing(thread_t thread, int64_t accum)
+{
+       if (thread != THREAD_NULL)
+               thread->t_dtrace_tracing = accum;
+}
+
+void dtrace_set_thread_reentering(thread_t thread, boolean_t vbool)
+{
+       if (thread != THREAD_NULL) {
+               if (vbool)
+                       thread->options |= TH_OPT_DTRACE;
+               else
+                       thread->options &= (~TH_OPT_DTRACE);
+       }
+}
+
+vm_offset_t dtrace_set_thread_recover(thread_t thread, vm_offset_t recover)
+{
+       vm_offset_t prev = 0;
+
+       if (thread != THREAD_NULL) {
+               prev = thread->recover;
+               thread->recover = recover;
+       }
+       return prev;
+}
+
+void dtrace_thread_bootstrap(void)
 {
-       return(thread_should_halt_fast(th));
+       task_t task = current_task();
+       if(task->thread_count == 1) {
+               DTRACE_PROC(start);
+       }
+       DTRACE_PROC(lwp__start);
+
 }
+#endif /* CONFIG_DTRACE */