]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/i386/pcb.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / i386 / pcb.c
index b75cf689c861cc17af0a576c2fa3913f4a63990f..fe5d56b8e12d1f367c80ea76ad2744544e5635b7 100644 (file)
@@ -1,55 +1,59 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2016 Apple 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.
- * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * @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. 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
  * 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.
- * 
- * @APPLE_LICENSE_HEADER_END@
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * @OSF_COPYRIGHT@
  */
-/* 
+/*
  * Mach Operating System
  * Copyright (c) 1991,1990 Carnegie Mellon University
  * All Rights Reserved.
- * 
+ *
  * Permission to use, copy, modify and distribute this software and its
  * documentation is hereby granted, provided that both the copyright
  * notice and this permission notice appear in all copies of the
  * software, derivative works or modified versions, and any portions
  * thereof, and that both notices appear in supporting documentation.
- * 
+ *
  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- * 
+ *
  * Carnegie Mellon requests users of this software to return to
- * 
+ *
  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  *  School of Computer Science
  *  Carnegie Mellon University
  *  Pittsburgh PA 15213-3890
- * 
+ *
  * any improvements or extensions that they make and grant Carnegie Mellon
  * the rights to redistribute these changes.
  */
 
-#include <cpus.h>
-#include <mach_rt.h>
 #include <mach_debug.h>
 #include <mach_ldebug.h>
 
 #include <mach/kern_return.h>
 #include <mach/thread_status.h>
 #include <mach/vm_param.h>
-#include <mach/rpc.h>
 
 #include <kern/counters.h>
+#include <kern/kalloc.h>
 #include <kern/mach_param.h>
+#include <kern/processor.h>
+#include <kern/cpu_data.h>
+#include <kern/cpu_number.h>
 #include <kern/task.h>
 #include <kern/thread.h>
-#include <kern/thread_act.h>
-#include <kern/thread_swap.h>
 #include <kern/sched_prim.h>
 #include <kern/misc_protos.h>
 #include <kern/assert.h>
 #include <kern/spl.h>
+#include <kern/machine.h>
+#include <kern/kpc.h>
 #include <ipc/ipc_port.h>
 #include <vm/vm_kern.h>
+#include <vm/vm_map.h>
 #include <vm/pmap.h>
+#include <vm/vm_protos.h>
 
-#include <i386/thread.h>
+#include <i386/cpu_data.h>
+#include <i386/cpu_number.h>
 #include <i386/eflags.h>
 #include <i386/proc_reg.h>
-#include <i386/seg.h>
-#include <i386/tss.h>
-#include <i386/user_ldt.h>
 #include <i386/fpu.h>
-#include <i386/iopb_entries.h>
+#include <i386/misc_protos.h>
+#include <i386/mp_desc.h>
+#include <i386/thread.h>
+#include <i386/machine_routines.h>
+#include <i386/lapic.h> /* LAPIC_PMC_SWI_VECTOR */
+#include <i386/seg.h>
+
+#if HYPERVISOR
+#include <kern/hv_support.h>
+#endif
 
 /*
  * Maps state flavor to number of words in the state:
  */
-unsigned int state_count[] = {
-       /* FLAVOR_LIST */ 0,
-       i386_NEW_THREAD_STATE_COUNT,
-       i386_FLOAT_STATE_COUNT,
-       i386_ISA_PORT_MAP_STATE_COUNT,
-       i386_V86_ASSIST_STATE_COUNT,
-       i386_REGS_SEGS_STATE_COUNT,
-       i386_THREAD_SYSCALL_STATE_COUNT,
-       /* THREAD_STATE_NONE */ 0,
-       i386_SAVED_STATE_COUNT,
+unsigned int _MachineStateCount[] = {
+       [x86_THREAD_STATE32]      = x86_THREAD_STATE32_COUNT,
+       [x86_THREAD_STATE64]      = x86_THREAD_STATE64_COUNT,
+       [x86_THREAD_FULL_STATE64] = x86_THREAD_FULL_STATE64_COUNT,
+       [x86_THREAD_STATE]        = x86_THREAD_STATE_COUNT,
+       [x86_FLOAT_STATE32]       = x86_FLOAT_STATE32_COUNT,
+       [x86_FLOAT_STATE64]       = x86_FLOAT_STATE64_COUNT,
+       [x86_FLOAT_STATE]         = x86_FLOAT_STATE_COUNT,
+       [x86_EXCEPTION_STATE32]   = x86_EXCEPTION_STATE32_COUNT,
+       [x86_EXCEPTION_STATE64]   = x86_EXCEPTION_STATE64_COUNT,
+       [x86_EXCEPTION_STATE]     = x86_EXCEPTION_STATE_COUNT,
+       [x86_DEBUG_STATE32]       = x86_DEBUG_STATE32_COUNT,
+       [x86_DEBUG_STATE64]       = x86_DEBUG_STATE64_COUNT,
+       [x86_DEBUG_STATE]         = x86_DEBUG_STATE_COUNT,
+       [x86_AVX_STATE32]         = x86_AVX_STATE32_COUNT,
+       [x86_AVX_STATE64]         = x86_AVX_STATE64_COUNT,
+       [x86_AVX_STATE]           = x86_AVX_STATE_COUNT,
+#if !defined(RC_HIDE_XNU_J137)
+       [x86_AVX512_STATE32]      = x86_AVX512_STATE32_COUNT,
+       [x86_AVX512_STATE64]      = x86_AVX512_STATE64_COUNT,
+       [x86_AVX512_STATE]        = x86_AVX512_STATE_COUNT,
+#endif /* not RC_HIDE_XNU_J137 */
 };
 
+zone_t          iss_zone;               /* zone for saved_state area */
+zone_t          ids_zone;               /* zone for debug_state area */
+
+extern int      allow_64bit_proc_LDT_ops;
+
 /* Forward */
 
-void act_machine_throughcall(thread_act_t thr_act);
-extern thread_t                Switch_context(
-                               thread_t                old,
-                               void                    (*cont)(void),
-                               thread_t                new);
-extern void            Thread_continue(void);
-extern void            Load_context(
-                               thread_t                thread);
+extern void             Thread_continue(void);
+extern void             Load_context(
+       thread_t                        thread) __attribute__((noreturn));
 
-/*
- * consider_machine_collect:
- *
- *     Try to collect machine-dependent pages
- */
-void
-consider_machine_collect()
-{
-}
+static void
+get_exception_state32(thread_t thread, x86_exception_state32_t *es);
 
-void
-consider_machine_adjust()
+static void
+get_exception_state64(thread_t thread, x86_exception_state64_t *es);
+
+static void
+get_thread_state32(thread_t thread, x86_thread_state32_t *ts);
+
+static void
+get_thread_state64(thread_t thread, void *ts, boolean_t full);
+
+static int
+set_thread_state32(thread_t thread, x86_thread_state32_t *ts);
+
+static int
+set_thread_state64(thread_t thread, void *ts, boolean_t full);
+
+#if HYPERVISOR
+static inline void
+ml_hv_cswitch(thread_t old, thread_t new)
 {
-}
+       if (old->hv_thread_target) {
+               hv_callbacks.preempt(old->hv_thread_target);
+       }
 
+       if (new->hv_thread_target) {
+               hv_callbacks.dispatch(new->hv_thread_target);
+       }
+}
+#endif
 
 /*
- *     machine_kernel_stack_init:
- *
- *     Initialize a kernel stack which has already been
- *     attached to its thread_activation.
+ * Don't let an illegal value for the lower 32-bits of dr7 get set.
+ * Specifically, check for undefined settings.  Setting these bit patterns
+ * result in undefined behaviour and can lead to an unexpected
+ * TRCTRAP.
  */
-
-void
-machine_kernel_stack_init(
-       thread_t        thread,
-       void            (*start_pos)(thread_t))
+static boolean_t
+dr7d_is_valid(uint32_t *dr7d)
 {
-       thread_act_t    thr_act = thread->top_act;
-       vm_offset_t     stack;
+       int i;
+       uint32_t mask1, mask2;
 
-       assert(thr_act);
-       stack = thread->kernel_stack;
-       assert(stack);
+       /*
+        * If the DE bit is set in CR4, R/W0-3 can be pattern
+        * "10B" to indicate i/o reads and write
+        */
+       if (!(get_cr4() & CR4_DE)) {
+               for (i = 0, mask1 = 0x3 << 16, mask2 = 0x2 << 16; i < 4;
+                   i++, mask1 <<= 4, mask2 <<= 4) {
+                       if ((*dr7d & mask1) == mask2) {
+                               return FALSE;
+                       }
+               }
+       }
 
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB) {
-               printf("machine_kernel_stack_init(thr=%x,stk=%x,start_pos=%x)\n",
-                               thread,stack,start_pos);
-               printf("\tstack_iks=%x, stack_iel=%x\n",
-                       STACK_IKS(stack), STACK_IEL(stack));
+       /*
+        * if we are doing an instruction execution break (indicated
+        * by r/w[x] being "00B"), then the len[x] must also be set
+        * to "00B"
+        */
+       for (i = 0; i < 4; i++) {
+               if (((((*dr7d >> (16 + i * 4))) & 0x3) == 0) &&
+                   ((((*dr7d >> (18 + i * 4))) & 0x3) != 0)) {
+                       return FALSE;
+               }
        }
-#endif /* MACH_ASSERT */
 
        /*
-        *      We want to run at start_pos, giving it as an argument
-        *      the return value from Load_context/Switch_context.
-        *      Thread_continue takes care of the mismatch between
-        *      the argument-passing/return-value conventions.
-        *      This function will not return normally,
-        *      so we don`t have to worry about a return address.
+        * Intel docs have these bits fixed.
         */
-       STACK_IKS(stack)->k_eip = (int) Thread_continue;
-       STACK_IKS(stack)->k_ebx = (int) start_pos;
-       STACK_IKS(stack)->k_esp = (int) STACK_IEL(stack);
+       *dr7d |= 0x1 << 10; /* set bit 10 to 1 */
+       *dr7d &= ~(0x1 << 11); /* set bit 11 to 0 */
+       *dr7d &= ~(0x1 << 12); /* set bit 12 to 0 */
+       *dr7d &= ~(0x1 << 14); /* set bit 14 to 0 */
+       *dr7d &= ~(0x1 << 15); /* set bit 15 to 0 */
 
        /*
-        *      Point top of kernel stack to user`s registers.
+        * We don't allow anything to set the global breakpoints.
         */
-       STACK_IEL(stack)->saved_state = &thr_act->mact.pcb->iss;
-}
 
+       if (*dr7d & 0x2) {
+               return FALSE;
+       }
+
+       if (*dr7d & (0x2 << 2)) {
+               return FALSE;
+       }
 
-#if    NCPUS > 1
-#define        curr_gdt(mycpu)         (mp_gdt[mycpu])
-#define        curr_ktss(mycpu)        (mp_ktss[mycpu])
-#else
-#define        curr_gdt(mycpu)         (gdt)
-#define        curr_ktss(mycpu)        (&ktss)
-#endif
+       if (*dr7d & (0x2 << 4)) {
+               return FALSE;
+       }
 
-#define        gdt_desc_p(mycpu,sel) \
-       ((struct real_descriptor *)&curr_gdt(mycpu)[sel_idx(sel)])
+       if (*dr7d & (0x2 << 6)) {
+               return FALSE;
+       }
 
-void
-act_machine_switch_pcb( thread_act_t new_act )
+       return TRUE;
+}
+
+extern void set_64bit_debug_regs(x86_debug_state64_t *ds);
+
+boolean_t
+debug_state_is_valid32(x86_debug_state32_t *ds)
 {
-       pcb_t                   pcb = new_act->mact.pcb;
-       int                     mycpu;
-    {
-       register iopb_tss_t     tss = pcb->ims.io_tss;
-       vm_offset_t             pcb_stack_top;
+       if (!dr7d_is_valid(&ds->dr7)) {
+               return FALSE;
+       }
 
-        assert(new_act->thread != NULL);
-        assert(new_act->thread->kernel_stack != 0);
-        STACK_IEL(new_act->thread->kernel_stack)->saved_state =
-                &new_act->mact.pcb->iss;
+       return TRUE;
+}
 
-       /*
-        *      Save a pointer to the top of the "kernel" stack -
-        *      actually the place in the PCB where a trap into
-        *      kernel mode will push the registers.
-        *      The location depends on V8086 mode.  If we are
-        *      not in V8086 mode, then a trap into the kernel
-        *      won`t save the v86 segments, so we leave room.
-        */
+boolean_t
+debug_state_is_valid64(x86_debug_state64_t *ds)
+{
+       if (!dr7d_is_valid((uint32_t *)&ds->dr7)) {
+               return FALSE;
+       }
 
-       pcb_stack_top = (pcb->iss.efl & EFL_VM)
-                       ? (int) (&pcb->iss + 1)
-                       : (int) (&pcb->iss.v86_segs);
-
-       mp_disable_preemption();
-       mycpu = cpu_number();
-
-       if (tss == 0) {
-           /*
-            *  No per-thread IO permissions.
-            *  Use standard kernel TSS.
-            */
-           if (!(gdt_desc_p(mycpu,KERNEL_TSS)->access & ACC_TSS_BUSY))
-               set_tr(KERNEL_TSS);
-           curr_ktss(mycpu)->esp0 = pcb_stack_top;
-       }
-       else {
-           /*
-            * Set the IO permissions.  Use this thread`s TSS.
-            */
-           *gdt_desc_p(mycpu,USER_TSS)
-               = *(struct real_descriptor *)tss->iopb_desc;
-           tss->tss.esp0 = pcb_stack_top;
-           set_tr(USER_TSS);
-           gdt_desc_p(mycpu,KERNEL_TSS)->access &= ~ ACC_TSS_BUSY;
-       }
-    }
-
-    {
-       register user_ldt_t     ldt = pcb->ims.ldt;
        /*
-        * Set the thread`s LDT.
+        * Don't allow the user to set debug addresses above their max
+        * value
         */
-       if (ldt == 0) {
-           /*
-            * Use system LDT.
-            */
-           set_ldt(KERNEL_LDT);
-       }
-       else {
-           /*
-            * Thread has its own LDT.
-            */
-           *gdt_desc_p(mycpu,USER_LDT) = ldt->desc;
-           set_ldt(USER_LDT);
-       }
-    }
-       mp_enable_preemption();
-       /*
-        * Load the floating-point context, if necessary.
-        */
-       fpu_load_context(pcb);
+       if (ds->dr7 & 0x1) {
+               if (ds->dr0 >= VM_MAX_PAGE_ADDRESS) {
+                       return FALSE;
+               }
+       }
+
+       if (ds->dr7 & (0x1 << 2)) {
+               if (ds->dr1 >= VM_MAX_PAGE_ADDRESS) {
+                       return FALSE;
+               }
+       }
+
+       if (ds->dr7 & (0x1 << 4)) {
+               if (ds->dr2 >= VM_MAX_PAGE_ADDRESS) {
+                       return FALSE;
+               }
+       }
+
+       if (ds->dr7 & (0x1 << 6)) {
+               if (ds->dr3 >= VM_MAX_PAGE_ADDRESS) {
+                       return FALSE;
+               }
+       }
+
+       /* For x86-64, we must ensure the upper 32-bits of DR7 are clear */
+       ds->dr7 &= 0xffffffffULL;
 
+       return TRUE;
 }
 
-/*
- * flush out any lazily evaluated HW state in the
- * owning thread's context, before termination.
- */
-void
-thread_machine_flush( thread_act_t cur_act )
+
+static kern_return_t
+set_debug_state32(thread_t thread, x86_debug_state32_t *ds)
+{
+       x86_debug_state32_t *new_ids;
+       pcb_t pcb;
+
+       pcb = THREAD_TO_PCB(thread);
+
+       if (debug_state_is_valid32(ds) != TRUE) {
+               return KERN_INVALID_ARGUMENT;
+       }
+
+       if (pcb->ids == NULL) {
+               new_ids = zalloc(ids_zone);
+               bzero(new_ids, sizeof *new_ids);
+
+               simple_lock(&pcb->lock, LCK_GRP_NULL);
+               /* make sure it wasn't already alloc()'d elsewhere */
+               if (pcb->ids == NULL) {
+                       pcb->ids = new_ids;
+                       simple_unlock(&pcb->lock);
+               } else {
+                       simple_unlock(&pcb->lock);
+                       zfree(ids_zone, new_ids);
+               }
+       }
+
+
+       copy_debug_state32(ds, pcb->ids, FALSE);
+
+       return KERN_SUCCESS;
+}
+
+static kern_return_t
+set_debug_state64(thread_t thread, x86_debug_state64_t *ds)
+{
+       x86_debug_state64_t *new_ids;
+       pcb_t pcb;
+
+       pcb = THREAD_TO_PCB(thread);
+
+       if (debug_state_is_valid64(ds) != TRUE) {
+               return KERN_INVALID_ARGUMENT;
+       }
+
+       if (pcb->ids == NULL) {
+               new_ids = zalloc(ids_zone);
+               bzero(new_ids, sizeof *new_ids);
+
+#if HYPERVISOR
+               if (thread->hv_thread_target) {
+                       hv_callbacks.volatile_state(thread->hv_thread_target,
+                           HV_DEBUG_STATE);
+               }
+#endif
+
+               simple_lock(&pcb->lock, LCK_GRP_NULL);
+               /* make sure it wasn't already alloc()'d elsewhere */
+               if (pcb->ids == NULL) {
+                       pcb->ids = new_ids;
+                       simple_unlock(&pcb->lock);
+               } else {
+                       simple_unlock(&pcb->lock);
+                       zfree(ids_zone, new_ids);
+               }
+       }
+
+       copy_debug_state64(ds, pcb->ids, FALSE);
+
+       return KERN_SUCCESS;
+}
+
+static void
+get_debug_state32(thread_t thread, x86_debug_state32_t *ds)
+{
+       x86_debug_state32_t *saved_state;
+
+       saved_state = thread->machine.ids;
+
+       if (saved_state) {
+               copy_debug_state32(saved_state, ds, TRUE);
+       } else {
+               bzero(ds, sizeof *ds);
+       }
+}
+
+static void
+get_debug_state64(thread_t thread, x86_debug_state64_t *ds)
 {
-    fpflush(cur_act);
+       x86_debug_state64_t *saved_state;
+
+       saved_state = (x86_debug_state64_t *)thread->machine.ids;
+
+       if (saved_state) {
+               copy_debug_state64(saved_state, ds, TRUE);
+       } else {
+               bzero(ds, sizeof *ds);
+       }
 }
 
 /*
- * Switch to the first thread on a CPU.
+ * consider_machine_collect:
+ *
+ *     Try to collect machine-dependent pages
  */
 void
-load_context(
-       thread_t                new)
+consider_machine_collect(void)
 {
-       act_machine_switch_pcb(new->top_act);
-       Load_context(new);
 }
 
-/*
- * Number of times we needed to swap an activation back in before
- * switching to it.
- */
-int switch_act_swapins = 0;
+void
+consider_machine_adjust(void)
+{
+}
 
 /*
- * machine_switch_act
- *
- * Machine-dependent details of activation switching.  Called with
- * RPC locks held and preemption disabled.
+ * Switch to the first thread on a CPU.
  */
 void
-machine_switch_act( 
-       thread_t        thread,
-       thread_act_t    old,
-       thread_act_t    new,
-       int                             cpu)
+machine_load_context(
+       thread_t                new)
 {
-       /*
-        *      Switch the vm, ast and pcb context. 
-        *      Save FP registers if in use and set TS (task switch) bit.
-        */
-       fpu_save_context(thread);
-
-       active_stacks[cpu] = thread->kernel_stack;
-       ast_context(new, cpu);
+       new->machine.specFlags |= OnProc;
+       act_machine_switch_pcb(NULL, new);
+       Load_context(new);
+}
 
-       PMAP_SWITCH_CONTEXT(old, new, cpu);
-       act_machine_switch_pcb(new);
+static inline void
+pmap_switch_context(thread_t ot, thread_t nt, int cnum)
+{
+       pmap_assert(ml_get_interrupts_enabled() == FALSE);
+       vm_map_t nmap = nt->map, omap = ot->map;
+       if ((omap != nmap) || (nmap->pmap->pagezero_accessible)) {
+               PMAP_DEACTIVATE_MAP(omap, ot, cnum);
+               PMAP_ACTIVATE_MAP(nmap, nt, cnum);
+       }
 }
 
 /*
@@ -320,202 +436,450 @@ machine_switch_act(
  * and return it.
  */
 thread_t
-switch_context(
-       thread_t                old,
-       void                    (*continuation)(void),
-       thread_t                new)
+machine_switch_context(
+       thread_t                        old,
+       thread_continue_t       continuation,
+       thread_t                        new)
 {
-       register thread_act_t   old_act = old->top_act,
-                               new_act = new->top_act;
+       assert(current_cpu_datap()->cpu_active_stack == old->kernel_stack);
 
-#if MACH_RT
-        assert(old_act->kernel_loaded ||
-               active_stacks[cpu_number()] == old_act->thread->kernel_stack);
-        assert (get_preemption_level() == 1);
-#endif
-       check_simple_locks();
+#if KPC
+       kpc_off_cpu(old);
+#endif /* KPC */
 
        /*
         *      Save FP registers if in use.
         */
-       fpu_save_context(old);
+       fpu_switch_context(old, new);
+
+       old->machine.specFlags &= ~OnProc;
+       new->machine.specFlags |= OnProc;
 
-#if    MACH_ASSERT
-       if (watchacts & WA_SWITCH)
-               printf("\tswitch_context(old=%x con=%x new=%x)\n",
-                                           old, continuation, new);
-#endif /* MACH_ASSERT */
+       /*
+        * Monitor the stack depth and report new max,
+        * not worrying about races.
+        */
+       vm_offset_t     depth = current_stack_depth();
+       if (depth > kernel_stack_depth_max) {
+               kernel_stack_depth_max = depth;
+               KERNEL_DEBUG_CONSTANT(
+                       MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_DEPTH),
+                       (long) depth, 0, 0, 0, 0);
+       }
 
        /*
         *      Switch address maps if need be, even if not switching tasks.
         *      (A server activation may be "borrowing" a client map.)
         */
-    {
-       int     mycpu = cpu_number();
-
-       PMAP_SWITCH_CONTEXT(old_act, new_act, mycpu)
-    }
+       pmap_switch_context(old, new, cpu_number());
 
        /*
         *      Load the rest of the user state for the new thread
         */
-       act_machine_switch_pcb(new_act);
-       return(Switch_context(old, continuation, new));
+       act_machine_switch_pcb(old, new);
+
+#if HYPERVISOR
+       ml_hv_cswitch(old, new);
+#endif
+
+       return Switch_context(old, continuation, new);
 }
 
-void
-pcb_module_init(void)
+thread_t
+machine_processor_shutdown(
+       thread_t        thread,
+       void            (*doshutdown)(processor_t),
+       processor_t     processor)
 {
-       fpu_module_init();
-       iopb_init();
+#if CONFIG_VMX
+       vmx_suspend();
+#endif
+       fpu_switch_context(thread, NULL);
+       pmap_switch_context(thread, processor->idle_thread, cpu_number());
+       return Shutdown_context(thread, doshutdown, processor);
 }
 
-void
-pcb_init( register thread_act_t thr_act )
+
+/*
+ * This is where registers that are not normally specified by the mach-o
+ * file on an execve would be nullified, perhaps to avoid a covert channel.
+ */
+kern_return_t
+machine_thread_state_initialize(
+       thread_t thread)
+{
+       /*
+        * If there's an fpu save area, free it.
+        * The initialized state will then be lazily faulted-in, if required.
+        * And if we're target, re-arm the no-fpu trap.
+        */
+       if (thread->machine.ifps) {
+               (void) fpu_set_fxstate(thread, NULL, x86_FLOAT_STATE64);
+
+               if (thread == current_thread()) {
+                       clear_fpu();
+               }
+       }
+
+       if (thread->machine.ids) {
+               zfree(ids_zone, thread->machine.ids);
+               thread->machine.ids = NULL;
+       }
+
+       return KERN_SUCCESS;
+}
+
+uint32_t
+get_eflags_exportmask(void)
+{
+       return EFL_USER_SET;
+}
+
+/*
+ * x86_SAVED_STATE32    - internal save/restore general register state on 32/64 bit processors
+ *                        for 32bit tasks only
+ * x86_SAVED_STATE64    - internal save/restore general register state on 64 bit processors
+ *                        for 64bit tasks only
+ * x86_THREAD_STATE32   - external set/get general register state on 32/64 bit processors
+ *                        for 32bit tasks only
+ * x86_THREAD_STATE64   - external set/get general register state on 64 bit processors
+ *                        for 64bit tasks only
+ * x86_SAVED_STATE      - external set/get general register state on 32/64 bit processors
+ *                        for either 32bit or 64bit tasks
+ * x86_FLOAT_STATE32    - internal/external save/restore float and xmm state on 32/64 bit processors
+ *                        for 32bit tasks only
+ * x86_FLOAT_STATE64    - internal/external save/restore float and xmm state on 64 bit processors
+ *                        for 64bit tasks only
+ * x86_FLOAT_STATE      - external save/restore float and xmm state on 32/64 bit processors
+ *                        for either 32bit or 64bit tasks
+ * x86_EXCEPTION_STATE32 - external get exception state on 32/64 bit processors
+ *                        for 32bit tasks only
+ * x86_EXCEPTION_STATE64 - external get exception state on 64 bit processors
+ *                        for 64bit tasks only
+ * x86_EXCEPTION_STATE   - external get exception state on 323/64 bit processors
+ *                        for either 32bit or 64bit tasks
+ */
+
+
+static void
+get_exception_state64(thread_t thread, x86_exception_state64_t *es)
 {
-       register pcb_t pcb;
+       x86_saved_state64_t *saved_state;
 
-       assert(thr_act->mact.pcb == (pcb_t)0);
-       pcb = thr_act->mact.pcb = &thr_act->mact.xxx_pcb;
+       saved_state = USER_REGS64(thread);
+
+       es->trapno = saved_state->isf.trapno;
+       es->cpu = saved_state->isf.cpu;
+       es->err = (typeof(es->err))saved_state->isf.err;
+       es->faultvaddr = saved_state->cr2;
+}
+
+static void
+get_exception_state32(thread_t thread, x86_exception_state32_t *es)
+{
+       x86_saved_state32_t *saved_state;
+
+       saved_state = USER_REGS32(thread);
+
+       es->trapno = saved_state->trapno;
+       es->cpu = saved_state->cpu;
+       es->err = saved_state->err;
+       es->faultvaddr = saved_state->cr2;
+}
 
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("pcb_init(%x) pcb=%x\n", thr_act, pcb);
-#endif /* MACH_ASSERT */
+
+static int
+set_thread_state32(thread_t thread, x86_thread_state32_t *ts)
+{
+       x86_saved_state32_t     *saved_state;
+
+       pal_register_cache_state(thread, DIRTY);
+
+       saved_state = USER_REGS32(thread);
 
        /*
-        *      We can't let random values leak out to the user.
-        * (however, act_create() zeroed the entire thr_act, mact, pcb)
-        * bzero((char *) pcb, sizeof *pcb);
+        * Scrub segment selector values:
         */
-       simple_lock_init(&pcb->lock, ETAP_MISC_PCB);
+       ts->cs = USER_CS;
+       /*
+        * On a 64 bit kernel, we always override the data segments,
+        * as the actual selector numbers have changed. This also
+        * means that we don't support setting the data segments
+        * manually any more.
+        */
+       ts->ss = USER_DS;
+       ts->ds = USER_DS;
+       ts->es = USER_DS;
+
+       /* Set GS to CTHREAD only if's been established */
+       ts->gs = thread->machine.cthread_self ? USER_CTHREAD : NULL_SEG;
+
+       /* Check segment selectors are safe */
+       if (!valid_user_segment_selectors(ts->cs,
+           ts->ss,
+           ts->ds,
+           ts->es,
+           ts->fs,
+           ts->gs)) {
+               return KERN_INVALID_ARGUMENT;
+       }
+
+       saved_state->eax = ts->eax;
+       saved_state->ebx = ts->ebx;
+       saved_state->ecx = ts->ecx;
+       saved_state->edx = ts->edx;
+       saved_state->edi = ts->edi;
+       saved_state->esi = ts->esi;
+       saved_state->ebp = ts->ebp;
+       saved_state->uesp = ts->esp;
+       saved_state->efl = (ts->eflags & ~EFL_USER_CLEAR) | EFL_USER_SET;
+       saved_state->eip = ts->eip;
+       saved_state->cs = ts->cs;
+       saved_state->ss = ts->ss;
+       saved_state->ds = ts->ds;
+       saved_state->es = ts->es;
+       saved_state->fs = ts->fs;
+       saved_state->gs = ts->gs;
 
        /*
-        *      Guarantee that the bootstrapped thread will be in user
-        *      mode.
+        * If the trace trap bit is being set,
+        * ensure that the user returns via iret
+        * - which is signaled thusly:
         */
-       pcb->iss.cs = USER_CS;
-       pcb->iss.ss = USER_DS;
-       pcb->iss.ds = USER_DS;
-       pcb->iss.es = USER_DS;
-       pcb->iss.fs = USER_DS;
-       pcb->iss.gs = USER_DS;
-       pcb->iss.efl = EFL_USER_SET;
+       if ((saved_state->efl & EFL_TF) && saved_state->cs == SYSENTER_CS) {
+               saved_state->cs = SYSENTER_TF_CS;
+       }
+
+       return KERN_SUCCESS;
 }
 
-/*
- * Adjust saved register state for thread belonging to task
- * created with kernel_task_create().
- */
-void
-pcb_user_to_kernel(
-       thread_act_t thr_act)
+static int
+set_thread_state64(thread_t thread, void *state, int full)
+{
+       x86_thread_state64_t *ts;
+       x86_saved_state64_t     *saved_state;
+
+       if (full == TRUE) {
+               ts = &((x86_thread_full_state64_t *)state)->ss64;
+       } else {
+               ts = (x86_thread_state64_t *)state;
+       }
+
+       pal_register_cache_state(thread, DIRTY);
+
+       saved_state = USER_REGS64(thread);
+
+       if (!IS_USERADDR64_CANONICAL(ts->rsp) ||
+           !IS_USERADDR64_CANONICAL(ts->rip)) {
+               return KERN_INVALID_ARGUMENT;
+       }
+
+       saved_state->r8 = ts->r8;
+       saved_state->r9 = ts->r9;
+       saved_state->r10 = ts->r10;
+       saved_state->r11 = ts->r11;
+       saved_state->r12 = ts->r12;
+       saved_state->r13 = ts->r13;
+       saved_state->r14 = ts->r14;
+       saved_state->r15 = ts->r15;
+       saved_state->rax = ts->rax;
+       saved_state->rbx = ts->rbx;
+       saved_state->rcx = ts->rcx;
+       saved_state->rdx = ts->rdx;
+       saved_state->rdi = ts->rdi;
+       saved_state->rsi = ts->rsi;
+       saved_state->rbp = ts->rbp;
+       saved_state->isf.rsp = ts->rsp;
+       saved_state->isf.rflags = (ts->rflags & ~EFL_USER_CLEAR) | EFL_USER_SET;
+       saved_state->isf.rip = ts->rip;
+
+       if (full == FALSE) {
+               saved_state->isf.cs = USER64_CS;
+       } else {
+               saved_state->isf.cs = ((x86_thread_full_state64_t *)ts)->ss64.cs;
+               saved_state->isf.ss = ((x86_thread_full_state64_t *)ts)->ss;
+               saved_state->ds = (uint32_t)((x86_thread_full_state64_t *)ts)->ds;
+               saved_state->es = (uint32_t)((x86_thread_full_state64_t *)ts)->es;
+               machine_thread_set_tsd_base(thread,
+                   ((x86_thread_full_state64_t *)ts)->gsbase);
+       }
+
+       saved_state->fs = (uint32_t)ts->fs;
+       saved_state->gs = (uint32_t)ts->gs;
+
+       return KERN_SUCCESS;
+}
+
+
+
+static void
+get_thread_state32(thread_t thread, x86_thread_state32_t *ts)
 {
-       register pcb_t pcb = thr_act->mact.pcb;
-
-       pcb->iss.cs = KERNEL_CS;
-       pcb->iss.ss = KERNEL_DS;
-       pcb->iss.ds = KERNEL_DS;
-       pcb->iss.es = KERNEL_DS;
-       pcb->iss.fs = KERNEL_DS;
-       pcb->iss.gs = CPU_DATA;
+       x86_saved_state32_t     *saved_state;
+
+       pal_register_cache_state(thread, VALID);
+
+       saved_state = USER_REGS32(thread);
+
+       ts->eax = saved_state->eax;
+       ts->ebx = saved_state->ebx;
+       ts->ecx = saved_state->ecx;
+       ts->edx = saved_state->edx;
+       ts->edi = saved_state->edi;
+       ts->esi = saved_state->esi;
+       ts->ebp = saved_state->ebp;
+       ts->esp = saved_state->uesp;
+       ts->eflags = saved_state->efl;
+       ts->eip = saved_state->eip;
+       ts->cs = saved_state->cs;
+       ts->ss = saved_state->ss;
+       ts->ds = saved_state->ds;
+       ts->es = saved_state->es;
+       ts->fs = saved_state->fs;
+       ts->gs = saved_state->gs;
 }
 
-void
-pcb_terminate(
-       register thread_act_t thr_act)
+
+static void
+get_thread_state64(thread_t thread, void *state, boolean_t full)
 {
-       register pcb_t  pcb = thr_act->mact.pcb;
+       x86_thread_state64_t    *ts;
+       x86_saved_state64_t     *saved_state;
+
+       if (full == TRUE) {
+               ts = &((x86_thread_full_state64_t *)state)->ss64;
+       } else {
+               ts = (x86_thread_state64_t *)state;
+       }
 
-       assert(pcb);
+       pal_register_cache_state(thread, VALID);
+
+       saved_state = USER_REGS64(thread);
+
+       ts->r8 = saved_state->r8;
+       ts->r9 = saved_state->r9;
+       ts->r10 = saved_state->r10;
+       ts->r11 = saved_state->r11;
+       ts->r12 = saved_state->r12;
+       ts->r13 = saved_state->r13;
+       ts->r14 = saved_state->r14;
+       ts->r15 = saved_state->r15;
+       ts->rax = saved_state->rax;
+       ts->rbx = saved_state->rbx;
+       ts->rcx = saved_state->rcx;
+       ts->rdx = saved_state->rdx;
+       ts->rdi = saved_state->rdi;
+       ts->rsi = saved_state->rsi;
+       ts->rbp = saved_state->rbp;
+       ts->rsp = saved_state->isf.rsp;
+       ts->rflags = saved_state->isf.rflags;
+       ts->rip = saved_state->isf.rip;
+       ts->cs = saved_state->isf.cs;
+
+       if (full == TRUE) {
+               ((x86_thread_full_state64_t *)state)->ds = saved_state->ds;
+               ((x86_thread_full_state64_t *)state)->es = saved_state->es;
+               ((x86_thread_full_state64_t *)state)->ss = saved_state->isf.ss;
+               ((x86_thread_full_state64_t *)state)->gsbase =
+                   thread->machine.cthread_self;
+       }
 
-       if (pcb->ims.io_tss != 0)
-               iopb_destroy(pcb->ims.io_tss);
-       if (pcb->ims.ifps != 0)
-               fp_free(pcb->ims.ifps);
-       if (pcb->ims.ldt != 0)
-               user_ldt_free(pcb->ims.ldt);
-       thr_act->mact.pcb = (pcb_t)0;
+       ts->fs = saved_state->fs;
+       ts->gs = saved_state->gs;
 }
 
-/*
- *     pcb_collect:
- *
- *     Attempt to free excess pcb memory.
- */
+kern_return_t
+machine_thread_state_convert_to_user(
+       __unused thread_t thread,
+       __unused thread_flavor_t flavor,
+       __unused thread_state_t tstate,
+       __unused mach_msg_type_number_t *count)
+{
+       // No conversion to userspace representation on this platform
+       return KERN_SUCCESS;
+}
 
-void
-pcb_collect(
-       register thread_act_t  thr_act)
+kern_return_t
+machine_thread_state_convert_from_user(
+       __unused thread_t thread,
+       __unused thread_flavor_t flavor,
+       __unused thread_state_t tstate,
+       __unused mach_msg_type_number_t count)
 {
-       /* accomplishes very little */
+       // No conversion from userspace representation on this platform
+       return KERN_SUCCESS;
 }
 
-/*
- * act_machine_sv_free
- * release saveareas associated with an act. if flag is true, release
- * user level savearea(s) too, else don't
- */
-void
-act_machine_sv_free(thread_act_t act, int flag)
+kern_return_t
+machine_thread_siguctx_pointer_convert_to_user(
+       __unused thread_t thread,
+       __unused user_addr_t *uctxp)
 {
+       // No conversion to userspace representation on this platform
+       return KERN_SUCCESS;
+}
 
+kern_return_t
+machine_thread_function_pointers_convert_from_user(
+       __unused thread_t thread,
+       __unused user_addr_t *fptrs,
+       __unused uint32_t count)
+{
+       // No conversion from userspace representation on this platform
+       return KERN_SUCCESS;
 }
 
 /*
  *     act_machine_set_state:
  *
- *     Set the status of the specified thread.  Called with "appropriate"
- *     thread-related locks held (see act_lock_thread()), so
- *     thr_act->thread is guaranteed not to change.
+ *     Set the status of the specified thread.
  */
 
 kern_return_t
-act_machine_set_state(
-       thread_act_t thr_act,
+machine_thread_set_state(
+       thread_t thr_act,
        thread_flavor_t flavor,
        thread_state_t tstate,
        mach_msg_type_number_t count)
 {
-       int                     kernel_act = thr_act->kernel_loading ||
-                                       thr_act->kernel_loaded;
-
-#if    MACH_ASSERT
-       if (watchacts & WA_STATE)
-           printf("act_%x act_m_set_state(thr_act=%x,flav=%x,st=%x,cnt=%x)\n",
-                   current_act(), thr_act, flavor, tstate, count);
-#endif /* MACH_ASSERT */
-
        switch (flavor) {
-           case THREAD_SYSCALL_STATE:
-           {
-               register struct thread_syscall_state *state;
-               register struct i386_saved_state *saved_state = USER_REGS(thr_act);
+       case x86_SAVED_STATE32:
+       {
+               x86_saved_state32_t     *state;
+               x86_saved_state32_t     *saved_state;
 
-               state = (struct thread_syscall_state *) tstate;
-               saved_state->eax = state->eax;
-               saved_state->edx = state->edx;
-               if (kernel_act)
-                       saved_state->efl = state->efl;
-               else
-                       saved_state->efl = (state->efl & ~EFL_USER_CLEAR) | EFL_USER_SET;
-               saved_state->eip = state->eip;
-               saved_state->uesp = state->esp;
-               break;
-           }
+               if (count < x86_SAVED_STATE32_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-           case i386_SAVED_STATE:
-           {
-               register struct i386_saved_state        *state;
-               register struct i386_saved_state        *saved_state;
+               state = (x86_saved_state32_t *) tstate;
 
-               if (count < i386_SAVED_STATE_COUNT) {
-                   return(KERN_INVALID_ARGUMENT);
+               /*
+                * Allow a thread in a 64-bit process to set
+                * 32-bit state iff the code segment originates
+                * in the LDT (the implication is that only
+                * 32-bit code segments are allowed there, so
+                * setting 32-bit state implies a switch to
+                * compatibility mode on resume-to-user).
+                */
+               if (thread_is_64bit_addr(thr_act) &&
+                   thr_act->task->i386_ldt == 0) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               /* Check segment selectors are safe */
+               if (!valid_user_segment_selectors(state->cs,
+                   state->ss,
+                   state->ds,
+                   state->es,
+                   state->fs,
+                   state->gs)) {
+                       return KERN_INVALID_ARGUMENT;
                }
 
-               state = (struct i386_saved_state *) tstate;
+               pal_register_cache_state(thr_act, DIRTY);
 
-               saved_state = USER_REGS(thr_act);
+               saved_state = USER_REGS32(thr_act);
 
                /*
                 * General registers
@@ -529,883 +893,1072 @@ act_machine_set_state(
                saved_state->ecx = state->ecx;
                saved_state->eax = state->eax;
                saved_state->eip = state->eip;
-               if (kernel_act)
-                       saved_state->efl = state->efl;
-               else
-                       saved_state->efl = (state->efl & ~EFL_USER_CLEAR)
-                                               | EFL_USER_SET;
+
+               saved_state->efl = (state->efl & ~EFL_USER_CLEAR) | EFL_USER_SET;
 
                /*
-                * Segment registers.  Set differently in V8086 mode.
+                * If the trace trap bit is being set,
+                * ensure that the user returns via iret
+                * - which is signaled thusly:
                 */
-               if (state->efl & EFL_VM) {
-                   /*
-                    * Set V8086 mode segment registers.
-                    */
-                   saved_state->cs = state->cs & 0xffff;
-                   saved_state->ss = state->ss & 0xffff;
-                   saved_state->v86_segs.v86_ds = state->ds & 0xffff;
-                   saved_state->v86_segs.v86_es = state->es & 0xffff;
-                   saved_state->v86_segs.v86_fs = state->fs & 0xffff;
-                   saved_state->v86_segs.v86_gs = state->gs & 0xffff;
-
-                   /*
-                    * Zero protected mode segment registers.
-                    */
-                   saved_state->ds = 0;
-                   saved_state->es = 0;
-                   saved_state->fs = 0;
-                   saved_state->gs = 0;
-
-                   if (thr_act->mact.pcb->ims.v86s.int_table) {
-                       /*
-                        * Hardware assist on.
-                        */
-                       thr_act->mact.pcb->ims.v86s.flags =
-                           state->efl & (EFL_TF | EFL_IF);
-                   }
-               }
-               else if (!kernel_act) {
-                   /*
-                    * 386 mode.  Set segment registers for flat
-                    * 32-bit address space.
-                    */
-                   saved_state->cs = USER_CS;
-                   saved_state->ss = USER_DS;
-                   saved_state->ds = USER_DS;
-                   saved_state->es = USER_DS;
-                   saved_state->fs = USER_DS;
-                   saved_state->gs = USER_DS;
-               }
-               else {
-                   /*
-                    * User setting segment registers.
-                    * Code and stack selectors have already been
-                    * checked.  Others will be reset by 'iret'
-                    * if they are not valid.
-                    */
-                   saved_state->cs = state->cs;
-                   saved_state->ss = state->ss;
-                   saved_state->ds = state->ds;
-                   saved_state->es = state->es;
-                   saved_state->fs = state->fs;
-                   saved_state->gs = state->gs;
+               if ((saved_state->efl & EFL_TF) && state->cs == SYSENTER_CS) {
+                       state->cs = SYSENTER_TF_CS;
                }
+
+               /*
+                * User setting segment registers.
+                * Code and stack selectors have already been
+                * checked.  Others will be reset by 'iret'
+                * if they are not valid.
+                */
+               saved_state->cs = state->cs;
+               saved_state->ss = state->ss;
+               saved_state->ds = state->ds;
+               saved_state->es = state->es;
+               saved_state->fs = state->fs;
+               saved_state->gs = state->gs;
+
                break;
-           }
-
-           case i386_NEW_THREAD_STATE:
-           case i386_REGS_SEGS_STATE:
-           {
-               register struct i386_new_thread_state   *state;
-               register struct i386_saved_state        *saved_state;
-
-               if (count < i386_NEW_THREAD_STATE_COUNT) {
-                   return(KERN_INVALID_ARGUMENT);
-               }
-
-               if (flavor == i386_REGS_SEGS_STATE) {
-                   /*
-                    * Code and stack selectors must not be null,
-                    * and must have user protection levels.
-                    * Only the low 16 bits are valid.
-                    */
-                   state->cs &= 0xffff;
-                   state->ss &= 0xffff;
-                   state->ds &= 0xffff;
-                   state->es &= 0xffff;
-                   state->fs &= 0xffff;
-                   state->gs &= 0xffff;
-
-                   if (!kernel_act &&
-                       (state->cs == 0 || (state->cs & SEL_PL) != SEL_PL_U
-                       || state->ss == 0 || (state->ss & SEL_PL) != SEL_PL_U))
+       }
+
+       case x86_SAVED_STATE64:
+       {
+               x86_saved_state64_t     *state;
+               x86_saved_state64_t     *saved_state;
+
+               if (count < x86_SAVED_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               state = (x86_saved_state64_t *) tstate;
+
+               /* Verify that the supplied code segment selector is
+                * valid. In 64-bit mode, the FS and GS segment overrides
+                * use the FS.base and GS.base MSRs to calculate
+                * base addresses, and the trampolines don't directly
+                * restore the segment registers--hence they are no
+                * longer relevant for validation.
+                */
+               if (!valid_user_code_selector(state->isf.cs)) {
                        return KERN_INVALID_ARGUMENT;
                }
 
-               state = (struct i386_new_thread_state *) tstate;
+               /* Check pc and stack are canonical addresses */
+               if (!IS_USERADDR64_CANONICAL(state->isf.rsp) ||
+                   !IS_USERADDR64_CANONICAL(state->isf.rip)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               saved_state = USER_REGS(thr_act);
+               pal_register_cache_state(thr_act, DIRTY);
+
+               saved_state = USER_REGS64(thr_act);
 
                /*
                 * General registers
                 */
-               saved_state->edi = state->edi;
-               saved_state->esi = state->esi;
-               saved_state->ebp = state->ebp;
-               saved_state->uesp = state->uesp;
-               saved_state->ebx = state->ebx;
-               saved_state->edx = state->edx;
-               saved_state->ecx = state->ecx;
-               saved_state->eax = state->eax;
-               saved_state->eip = state->eip;
-               if (kernel_act)
-                       saved_state->efl = state->efl;
-               else
-                       saved_state->efl = (state->efl & ~EFL_USER_CLEAR)
-                                               | EFL_USER_SET;
+               saved_state->r8 = state->r8;
+               saved_state->r9 = state->r9;
+               saved_state->r10 = state->r10;
+               saved_state->r11 = state->r11;
+               saved_state->r12 = state->r12;
+               saved_state->r13 = state->r13;
+               saved_state->r14 = state->r14;
+               saved_state->r15 = state->r15;
+               saved_state->rdi = state->rdi;
+               saved_state->rsi = state->rsi;
+               saved_state->rbp = state->rbp;
+               saved_state->rbx = state->rbx;
+               saved_state->rdx = state->rdx;
+               saved_state->rcx = state->rcx;
+               saved_state->rax = state->rax;
+               saved_state->isf.rsp = state->isf.rsp;
+               saved_state->isf.rip = state->isf.rip;
+
+               saved_state->isf.rflags = (state->isf.rflags & ~EFL_USER_CLEAR) | EFL_USER_SET;
 
                /*
-                * Segment registers.  Set differently in V8086 mode.
+                * User setting segment registers.
+                * Code and stack selectors have already been
+                * checked.  Others will be reset by 'sys'
+                * if they are not valid.
                 */
-               if (state->efl & EFL_VM) {
-                   /*
-                    * Set V8086 mode segment registers.
-                    */
-                   saved_state->cs = state->cs & 0xffff;
-                   saved_state->ss = state->ss & 0xffff;
-                   saved_state->v86_segs.v86_ds = state->ds & 0xffff;
-                   saved_state->v86_segs.v86_es = state->es & 0xffff;
-                   saved_state->v86_segs.v86_fs = state->fs & 0xffff;
-                   saved_state->v86_segs.v86_gs = state->gs & 0xffff;
-
-                   /*
-                    * Zero protected mode segment registers.
-                    */
-                   saved_state->ds = 0;
-                   saved_state->es = 0;
-                   saved_state->fs = 0;
-                   saved_state->gs = 0;
-
-                   if (thr_act->mact.pcb->ims.v86s.int_table) {
-                       /*
-                        * Hardware assist on.
-                        */
-                       thr_act->mact.pcb->ims.v86s.flags =
-                           state->efl & (EFL_TF | EFL_IF);
-                   }
-               }
-               else if (flavor == i386_NEW_THREAD_STATE && !kernel_act) {
-                   /*
-                    * 386 mode.  Set segment registers for flat
-                    * 32-bit address space.
-                    */
-                   saved_state->cs = USER_CS;
-                   saved_state->ss = USER_DS;
-                   saved_state->ds = USER_DS;
-                   saved_state->es = USER_DS;
-                   saved_state->fs = USER_DS;
-                   saved_state->gs = USER_DS;
-               }
-               else {
-                   /*
-                    * User setting segment registers.
-                    * Code and stack selectors have already been
-                    * checked.  Others will be reset by 'iret'
-                    * if they are not valid.
-                    */
-                   saved_state->cs = state->cs;
-                   saved_state->ss = state->ss;
-                   saved_state->ds = state->ds;
-                   saved_state->es = state->es;
-                   saved_state->fs = state->fs;
-                   saved_state->gs = state->gs;
-               }
+               saved_state->isf.cs = state->isf.cs;
+               saved_state->isf.ss = state->isf.ss;
+               saved_state->fs = state->fs;
+               saved_state->gs = state->gs;
+
                break;
-           }
+       }
+
+       case x86_FLOAT_STATE32:
+       case x86_AVX_STATE32:
+#if !defined(RC_HIDE_XNU_J137)
+       case x86_AVX512_STATE32:
+#endif /* not RC_HIDE_XNU_J137 */
+               {
+                       if (count != _MachineStateCount[flavor]) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
+
+                       if (thread_is_64bit_addr(thr_act)) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
+
+                       return fpu_set_fxstate(thr_act, tstate, flavor);
+               }
+
+       case x86_FLOAT_STATE64:
+       case x86_AVX_STATE64:
+#if !defined(RC_HIDE_XNU_J137)
+       case x86_AVX512_STATE64:
+#endif /* not RC_HIDE_XNU_J137 */
+               {
+                       if (count != _MachineStateCount[flavor]) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
+
+                       if (!thread_is_64bit_addr(thr_act)) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
+
+                       return fpu_set_fxstate(thr_act, tstate, flavor);
+               }
 
-           case i386_FLOAT_STATE: {
+       case x86_FLOAT_STATE:
+       {
+               x86_float_state_t       *state;
 
-               if (count < i386_FLOAT_STATE_COUNT)
-                       return(KERN_INVALID_ARGUMENT);
+               if (count != x86_FLOAT_STATE_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               return fpu_set_state(thr_act,(struct i386_float_state*)tstate);
-           }
+               state = (x86_float_state_t *)tstate;
+               if (state->fsh.flavor == x86_FLOAT_STATE64 && state->fsh.count == x86_FLOAT_STATE64_COUNT &&
+                   thread_is_64bit_addr(thr_act)) {
+                       return fpu_set_fxstate(thr_act, (thread_state_t)&state->ufs.fs64, x86_FLOAT_STATE64);
+               }
+               if (state->fsh.flavor == x86_FLOAT_STATE32 && state->fsh.count == x86_FLOAT_STATE32_COUNT &&
+                   !thread_is_64bit_addr(thr_act)) {
+                       return fpu_set_fxstate(thr_act, (thread_state_t)&state->ufs.fs32, x86_FLOAT_STATE32);
+               }
+               return KERN_INVALID_ARGUMENT;
+       }
 
-           /*
-            * Temporary - replace by i386_io_map
-            */
-           case i386_ISA_PORT_MAP_STATE: {
-               register struct i386_isa_port_map_state *state;
-               register iopb_tss_t     tss;
+       case x86_AVX_STATE:
+#if !defined(RC_HIDE_XNU_J137)
+       case x86_AVX512_STATE:
+#endif
+               {
+                       x86_avx_state_t       *state;
+
+                       if (count != _MachineStateCount[flavor]) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
+
+                       state = (x86_avx_state_t *)tstate;
+                       /* Flavors are defined to have sequential values: 32-bit, 64-bit, non-specific */
+                       /* 64-bit flavor? */
+                       if (state->ash.flavor == (flavor - 1) &&
+                           state->ash.count == _MachineStateCount[flavor - 1] &&
+                           thread_is_64bit_addr(thr_act)) {
+                               return fpu_set_fxstate(thr_act,
+                                          (thread_state_t)&state->ufs.as64,
+                                          flavor - 1);
+                       }
+                       /* 32-bit flavor? */
+                       if (state->ash.flavor == (flavor - 2) &&
+                           state->ash.count == _MachineStateCount[flavor - 2] &&
+                           !thread_is_64bit_addr(thr_act)) {
+                               return fpu_set_fxstate(thr_act,
+                                          (thread_state_t)&state->ufs.as32,
+                                          flavor - 2);
+                       }
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               if (count < i386_ISA_PORT_MAP_STATE_COUNT)
-                       return(KERN_INVALID_ARGUMENT);
+       case x86_THREAD_STATE32:
+       {
+               if (count != x86_THREAD_STATE32_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               break;
-           }
+               if (thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               return set_thread_state32(thr_act, (x86_thread_state32_t *)tstate);
+       }
 
-           case i386_V86_ASSIST_STATE:
-           {
-               register struct i386_v86_assist_state *state;
-               vm_offset_t     int_table;
-               int             int_count;
+       case x86_THREAD_STATE64:
+       {
+               if (count != x86_THREAD_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               if (count < i386_V86_ASSIST_STATE_COUNT)
-                   return KERN_INVALID_ARGUMENT;
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               state = (struct i386_v86_assist_state *) tstate;
-               int_table = state->int_table;
-               int_count = state->int_count;
+               return set_thread_state64(thr_act, tstate, FALSE);
+       }
 
-               if (int_table >= VM_MAX_ADDRESS ||
-                   int_table +
-                       int_count * sizeof(struct v86_interrupt_table)
-                           > VM_MAX_ADDRESS)
-                   return KERN_INVALID_ARGUMENT;
+       case x86_THREAD_FULL_STATE64:
+       {
+               if (!allow_64bit_proc_LDT_ops) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               thr_act->mact.pcb->ims.v86s.int_table = int_table;
-               thr_act->mact.pcb->ims.v86s.int_count = int_count;
+               if (count != x86_THREAD_FULL_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               thr_act->mact.pcb->ims.v86s.flags =
-                       USER_REGS(thr_act)->efl & (EFL_TF | EFL_IF);
-               break;
-           }
-
-       case i386_THREAD_STATE: {
-               struct i386_saved_state *saved_state;
-               i386_thread_state_t     *state25;
-
-               saved_state = USER_REGS(thr_act);
-               state25 = (i386_thread_state_t *)tstate;
-
-               saved_state->eax = state25->eax;
-               saved_state->ebx = state25->ebx;
-               saved_state->ecx = state25->ecx;
-               saved_state->edx = state25->edx;
-               saved_state->edi = state25->edi;
-               saved_state->esi = state25->esi;
-               saved_state->ebp = state25->ebp;
-               saved_state->uesp = state25->esp;
-               saved_state->efl = (state25->eflags & ~EFL_USER_CLEAR)
-                                               | EFL_USER_SET;
-               saved_state->eip = state25->eip;
-               saved_state->cs = USER_CS;      /* FIXME? */
-               saved_state->ss = USER_DS;
-               saved_state->ds = USER_DS;
-               saved_state->es = USER_DS;
-               saved_state->fs = USER_DS;
-               saved_state->gs = USER_DS;
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               return set_thread_state64(thr_act, tstate, TRUE);
        }
-               break;
 
-           default:
-               return(KERN_INVALID_ARGUMENT);
+       case x86_THREAD_STATE:
+       {
+               x86_thread_state_t      *state;
+
+               if (count != x86_THREAD_STATE_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               state = (x86_thread_state_t *)tstate;
+
+               if (state->tsh.flavor == x86_THREAD_STATE64 &&
+                   state->tsh.count == x86_THREAD_STATE64_COUNT &&
+                   thread_is_64bit_addr(thr_act)) {
+                       return set_thread_state64(thr_act, &state->uts.ts64, FALSE);
+               } else if (state->tsh.flavor == x86_THREAD_FULL_STATE64 &&
+                   state->tsh.count == x86_THREAD_FULL_STATE64_COUNT &&
+                   thread_is_64bit_addr(thr_act)) {
+                       return set_thread_state64(thr_act, &state->uts.ts64, TRUE);
+               } else if (state->tsh.flavor == x86_THREAD_STATE32 &&
+                   state->tsh.count == x86_THREAD_STATE32_COUNT &&
+                   !thread_is_64bit_addr(thr_act)) {
+                       return set_thread_state32(thr_act, &state->uts.ts32);
+               } else {
+                       return KERN_INVALID_ARGUMENT;
+               }
+       }
+       case x86_DEBUG_STATE32:
+       {
+               x86_debug_state32_t *state;
+               kern_return_t ret;
+
+               if (thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               state = (x86_debug_state32_t *)tstate;
+
+               ret = set_debug_state32(thr_act, state);
+
+               return ret;
        }
+       case x86_DEBUG_STATE64:
+       {
+               x86_debug_state64_t *state;
+               kern_return_t ret;
 
-       return(KERN_SUCCESS);
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               state = (x86_debug_state64_t *)tstate;
+
+               ret = set_debug_state64(thr_act, state);
+
+               return ret;
+       }
+       case x86_DEBUG_STATE:
+       {
+               x86_debug_state_t *state;
+               kern_return_t ret = KERN_INVALID_ARGUMENT;
+
+               if (count != x86_DEBUG_STATE_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               state = (x86_debug_state_t *)tstate;
+               if (state->dsh.flavor == x86_DEBUG_STATE64 &&
+                   state->dsh.count == x86_DEBUG_STATE64_COUNT &&
+                   thread_is_64bit_addr(thr_act)) {
+                       ret = set_debug_state64(thr_act, &state->uds.ds64);
+               } else if (state->dsh.flavor == x86_DEBUG_STATE32 &&
+                   state->dsh.count == x86_DEBUG_STATE32_COUNT &&
+                   !thread_is_64bit_addr(thr_act)) {
+                       ret = set_debug_state32(thr_act, &state->uds.ds32);
+               }
+               return ret;
+       }
+       default:
+               return KERN_INVALID_ARGUMENT;
+       }
+
+       return KERN_SUCCESS;
 }
 
+
+
 /*
  *     thread_getstatus:
  *
  *     Get the status of the specified thread.
  */
 
-
 kern_return_t
-act_machine_get_state(
-       thread_act_t thr_act,
+machine_thread_get_state(
+       thread_t thr_act,
        thread_flavor_t flavor,
        thread_state_t tstate,
        mach_msg_type_number_t *count)
 {
-#if    MACH_ASSERT
-       if (watchacts & WA_STATE)
-           printf("act_%x act_m_get_state(thr_act=%x,flav=%x,st=%x,cnt@%x=%x)\n",
-               current_act(), thr_act, flavor, tstate,
-               count, (count ? *count : 0));
-#endif /* MACH_ASSERT */
+       switch (flavor) {
+       case THREAD_STATE_FLAVOR_LIST:
+       {
+               if (*count < 3) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               tstate[0] = i386_THREAD_STATE;
+               tstate[1] = i386_FLOAT_STATE;
+               tstate[2] = i386_EXCEPTION_STATE;
+
+               *count = 3;
+               break;
+       }
+
+       case THREAD_STATE_FLAVOR_LIST_NEW:
+       {
+               if (*count < 4) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               tstate[0] = x86_THREAD_STATE;
+               tstate[1] = x86_FLOAT_STATE;
+               tstate[2] = x86_EXCEPTION_STATE;
+               tstate[3] = x86_DEBUG_STATE;
+
+               *count = 4;
+               break;
+       }
+
+       case THREAD_STATE_FLAVOR_LIST_10_9:
+       {
+               if (*count < 5) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               tstate[0] = x86_THREAD_STATE;
+               tstate[1] = x86_FLOAT_STATE;
+               tstate[2] = x86_EXCEPTION_STATE;
+               tstate[3] = x86_DEBUG_STATE;
+               tstate[4] = x86_AVX_STATE;
+
+               *count = 5;
+               break;
+       }
+
+#if !defined(RC_HIDE_XNU_J137)
+       case THREAD_STATE_FLAVOR_LIST_10_13:
+       {
+               if (*count < 6) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               tstate[0] = x86_THREAD_STATE;
+               tstate[1] = x86_FLOAT_STATE;
+               tstate[2] = x86_EXCEPTION_STATE;
+               tstate[3] = x86_DEBUG_STATE;
+               tstate[4] = x86_AVX_STATE;
+               tstate[5] = x86_AVX512_STATE;
+
+               *count = 6;
+               break;
+       }
 
-       switch (flavor)  {
+#endif
+       case x86_SAVED_STATE32:
+       {
+               x86_saved_state32_t     *state;
+               x86_saved_state32_t     *saved_state;
 
-           case i386_SAVED_STATE:
-           {
-               register struct i386_saved_state        *state;
-               register struct i386_saved_state        *saved_state;
+               if (*count < x86_SAVED_STATE32_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               if (*count < i386_SAVED_STATE_COUNT)
-                   return(KERN_INVALID_ARGUMENT);
+               if (thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               state = (struct i386_saved_state *) tstate;
-               saved_state = USER_REGS(thr_act);
+               state = (x86_saved_state32_t *) tstate;
+               saved_state = USER_REGS32(thr_act);
 
                /*
                 * First, copy everything:
                 */
                *state = *saved_state;
+               state->ds = saved_state->ds & 0xffff;
+               state->es = saved_state->es & 0xffff;
+               state->fs = saved_state->fs & 0xffff;
+               state->gs = saved_state->gs & 0xffff;
 
-               if (saved_state->efl & EFL_VM) {
-                   /*
-                    * V8086 mode.
-                    */
-                   state->ds = saved_state->v86_segs.v86_ds & 0xffff;
-                   state->es = saved_state->v86_segs.v86_es & 0xffff;
-                   state->fs = saved_state->v86_segs.v86_fs & 0xffff;
-                   state->gs = saved_state->v86_segs.v86_gs & 0xffff;
-
-                   if (thr_act->mact.pcb->ims.v86s.int_table) {
-                       /*
-                        * Hardware assist on
-                        */
-                       if ((thr_act->mact.pcb->ims.v86s.flags &
-                                       (EFL_IF|V86_IF_PENDING)) == 0)
-                           state->efl &= ~EFL_IF;
-                   }
-               }
-               else {
-                   /*
-                    * 386 mode.
-                    */
-                   state->ds = saved_state->ds & 0xffff;
-                   state->es = saved_state->es & 0xffff;
-                   state->fs = saved_state->fs & 0xffff;
-                   state->gs = saved_state->gs & 0xffff;
-               }
-               *count = i386_SAVED_STATE_COUNT;
+               *count = x86_SAVED_STATE32_COUNT;
                break;
-           }
+       }
+
+       case x86_SAVED_STATE64:
+       {
+               x86_saved_state64_t     *state;
+               x86_saved_state64_t     *saved_state;
 
-           case i386_NEW_THREAD_STATE:
-           case i386_REGS_SEGS_STATE:
-           {
-               register struct i386_new_thread_state   *state;
-               register struct i386_saved_state        *saved_state;
+               if (*count < x86_SAVED_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               if (*count < i386_NEW_THREAD_STATE_COUNT)
-                   return(KERN_INVALID_ARGUMENT);
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               state = (struct i386_new_thread_state *) tstate;
-               saved_state = USER_REGS(thr_act);
+               state = (x86_saved_state64_t *)tstate;
+               saved_state = USER_REGS64(thr_act);
 
                /*
-                * General registers.
+                * First, copy everything:
                 */
-               state->edi = saved_state->edi;
-               state->esi = saved_state->esi;
-               state->ebp = saved_state->ebp;
-               state->ebx = saved_state->ebx;
-               state->edx = saved_state->edx;
-               state->ecx = saved_state->ecx;
-               state->eax = saved_state->eax;
-               state->eip = saved_state->eip;
-               state->efl = saved_state->efl;
-               state->uesp = saved_state->uesp;
+               *state = *saved_state;
+               state->ds = saved_state->ds & 0xffff;
+               state->es = saved_state->es & 0xffff;
+               state->fs = saved_state->fs & 0xffff;
+               state->gs = saved_state->gs & 0xffff;
 
-               state->cs = saved_state->cs;
-               state->ss = saved_state->ss;
-               if (saved_state->efl & EFL_VM) {
-                   /*
-                    * V8086 mode.
-                    */
-                   state->ds = saved_state->v86_segs.v86_ds & 0xffff;
-                   state->es = saved_state->v86_segs.v86_es & 0xffff;
-                   state->fs = saved_state->v86_segs.v86_fs & 0xffff;
-                   state->gs = saved_state->v86_segs.v86_gs & 0xffff;
-
-                   if (thr_act->mact.pcb->ims.v86s.int_table) {
-                       /*
-                        * Hardware assist on
-                        */
-                       if ((thr_act->mact.pcb->ims.v86s.flags &
-                                       (EFL_IF|V86_IF_PENDING)) == 0)
-                           state->efl &= ~EFL_IF;
-                   }
-               }
-               else {
-                   /*
-                    * 386 mode.
-                    */
-                   state->ds = saved_state->ds & 0xffff;
-                   state->es = saved_state->es & 0xffff;
-                   state->fs = saved_state->fs & 0xffff;
-                   state->gs = saved_state->gs & 0xffff;
-               }
-               *count = i386_NEW_THREAD_STATE_COUNT;
+               *count = x86_SAVED_STATE64_COUNT;
                break;
-           }
+       }
+
+       case x86_FLOAT_STATE32:
+       {
+               if (*count < x86_FLOAT_STATE32_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-           case THREAD_SYSCALL_STATE:
-           {
-               register struct thread_syscall_state *state;
-               register struct i386_saved_state *saved_state = USER_REGS(thr_act);
+               if (thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               state = (struct thread_syscall_state *) tstate;
-               state->eax = saved_state->eax;
-               state->edx = saved_state->edx;
-               state->efl = saved_state->efl;
-               state->eip = saved_state->eip;
-               state->esp = saved_state->uesp;
-               *count = i386_THREAD_SYSCALL_STATE_COUNT;
-               break;
-           }
+               *count = x86_FLOAT_STATE32_COUNT;
 
-           case THREAD_STATE_FLAVOR_LIST:
-               if (*count < 5)
-                   return (KERN_INVALID_ARGUMENT);
-               tstate[0] = i386_NEW_THREAD_STATE;
-               tstate[1] = i386_FLOAT_STATE;
-               tstate[2] = i386_ISA_PORT_MAP_STATE;
-               tstate[3] = i386_V86_ASSIST_STATE;
-               tstate[4] = THREAD_SYSCALL_STATE;
-               *count = 5;
-               break;
+               return fpu_get_fxstate(thr_act, tstate, flavor);
+       }
 
-           case i386_FLOAT_STATE: {
+       case x86_FLOAT_STATE64:
+       {
+               if (*count < x86_FLOAT_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               if (*count < i386_FLOAT_STATE_COUNT)
-                       return(KERN_INVALID_ARGUMENT);
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               *count = i386_FLOAT_STATE_COUNT;
-               return fpu_get_state(thr_act,(struct i386_float_state *)tstate);
-           }
+               *count = x86_FLOAT_STATE64_COUNT;
 
-           /*
-            * Temporary - replace by i386_io_map
-            */
-           case i386_ISA_PORT_MAP_STATE: {
-               register struct i386_isa_port_map_state *state;
-               register iopb_tss_t tss;
+               return fpu_get_fxstate(thr_act, tstate, flavor);
+       }
 
-               if (*count < i386_ISA_PORT_MAP_STATE_COUNT)
-                       return(KERN_INVALID_ARGUMENT);
+       case x86_FLOAT_STATE:
+       {
+               x86_float_state_t       *state;
+               kern_return_t           kret;
 
-               state = (struct i386_isa_port_map_state *) tstate;
-               tss = thr_act->mact.pcb->ims.io_tss;
+               if (*count < x86_FLOAT_STATE_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               if (tss == 0) {
-                   int i;
+               state = (x86_float_state_t *)tstate;
 
-                   /*
-                    *  The thread has no ktss, so no IO permissions.
-                    */
+               /*
+                * no need to bzero... currently
+                * x86_FLOAT_STATE64_COUNT == x86_FLOAT_STATE32_COUNT
+                */
+               if (thread_is_64bit_addr(thr_act)) {
+                       state->fsh.flavor = x86_FLOAT_STATE64;
+                       state->fsh.count  = x86_FLOAT_STATE64_COUNT;
 
-                   for (i = 0; i < sizeof state->pm; i++)
-                       state->pm[i] = 0xff;
+                       kret = fpu_get_fxstate(thr_act, (thread_state_t)&state->ufs.fs64, x86_FLOAT_STATE64);
                } else {
-                   /*
-                    *  The thread has its own ktss.
-                    */
+                       state->fsh.flavor = x86_FLOAT_STATE32;
+                       state->fsh.count  = x86_FLOAT_STATE32_COUNT;
 
-                   bcopy((char *) tss->bitmap,
-                         (char *) state->pm,
-                         sizeof state->pm);
+                       kret = fpu_get_fxstate(thr_act, (thread_state_t)&state->ufs.fs32, x86_FLOAT_STATE32);
                }
+               *count = x86_FLOAT_STATE_COUNT;
 
-               *count = i386_ISA_PORT_MAP_STATE_COUNT;
-               break;
-           }
+               return kret;
+       }
+
+       case x86_AVX_STATE32:
+#if !defined(RC_HIDE_XNU_J137)
+       case x86_AVX512_STATE32:
+#endif
+               {
+                       if (*count != _MachineStateCount[flavor]) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
+
+                       if (thread_is_64bit_addr(thr_act)) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
+
+                       *count = _MachineStateCount[flavor];
+
+                       return fpu_get_fxstate(thr_act, tstate, flavor);
+               }
+
+       case x86_AVX_STATE64:
+#if !defined(RC_HIDE_XNU_J137)
+       case x86_AVX512_STATE64:
+#endif
+               {
+                       if (*count != _MachineStateCount[flavor]) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
+
+                       if (!thread_is_64bit_addr(thr_act)) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
 
-           case i386_V86_ASSIST_STATE:
-           {
-               register struct i386_v86_assist_state *state;
+                       *count = _MachineStateCount[flavor];
 
-               if (*count < i386_V86_ASSIST_STATE_COUNT)
-                   return KERN_INVALID_ARGUMENT;
+                       return fpu_get_fxstate(thr_act, tstate, flavor);
+               }
+
+       case x86_AVX_STATE:
+#if !defined(RC_HIDE_XNU_J137)
+       case x86_AVX512_STATE:
+#endif
+               {
+                       x86_avx_state_t         *state;
+                       thread_state_t          fstate;
+
+                       if (*count < _MachineStateCount[flavor]) {
+                               return KERN_INVALID_ARGUMENT;
+                       }
+
+                       *count = _MachineStateCount[flavor];
+                       state = (x86_avx_state_t *)tstate;
+
+                       bzero((char *)state, *count * sizeof(int));
+
+                       if (thread_is_64bit_addr(thr_act)) {
+                               flavor -= 1; /* 64-bit flavor */
+                               fstate = (thread_state_t) &state->ufs.as64;
+                       } else {
+                               flavor -= 2; /* 32-bit flavor */
+                               fstate = (thread_state_t) &state->ufs.as32;
+                       }
+                       state->ash.flavor = flavor;
+                       state->ash.count  = _MachineStateCount[flavor];
+
+                       return fpu_get_fxstate(thr_act, fstate, flavor);
+               }
+
+       case x86_THREAD_STATE32:
+       {
+               if (*count < x86_THREAD_STATE32_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               if (thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-               state = (struct i386_v86_assist_state *) tstate;
-               state->int_table = thr_act->mact.pcb->ims.v86s.int_table;
-               state->int_count = thr_act->mact.pcb->ims.v86s.int_count;
+               *count = x86_THREAD_STATE32_COUNT;
 
-               *count = i386_V86_ASSIST_STATE_COUNT;
+               get_thread_state32(thr_act, (x86_thread_state32_t *)tstate);
                break;
-           }
-
-       case i386_THREAD_STATE: {
-               struct i386_saved_state *saved_state;
-               i386_thread_state_t     *state;
-
-               saved_state = USER_REGS(thr_act);
-               state = (i386_thread_state_t *)tstate;
-
-               state->eax = saved_state->eax;
-               state->ebx = saved_state->ebx;
-               state->ecx = saved_state->ecx;
-               state->edx = saved_state->edx;
-               state->edi = saved_state->edi;
-               state->esi = saved_state->esi;
-               state->ebp = saved_state->ebp;
-               state->esp = saved_state->uesp;
-               state->eflags = saved_state->efl;
-               state->eip = saved_state->eip;
-               state->cs = saved_state->cs;
-               state->ss = saved_state->ss;
-               state->ds = saved_state->ds;
-               state->es = saved_state->es;
-               state->fs = saved_state->fs;
-               state->gs = saved_state->gs;
+       }
+
+       case x86_THREAD_STATE64:
+       {
+               if (*count < x86_THREAD_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               *count = x86_THREAD_STATE64_COUNT;
+
+               get_thread_state64(thr_act, tstate, FALSE);
                break;
        }
 
-           default:
-               return(KERN_INVALID_ARGUMENT);
+       case x86_THREAD_FULL_STATE64:
+       {
+               if (!allow_64bit_proc_LDT_ops) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               if (*count < x86_THREAD_FULL_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               *count = x86_THREAD_FULL_STATE64_COUNT;
+
+               get_thread_state64(thr_act, tstate, TRUE);
+               break;
        }
 
-       return(KERN_SUCCESS);
-}
+       case x86_THREAD_STATE:
+       {
+               x86_thread_state_t      *state;
 
-/*
- * Alter the thread`s state so that a following thread_exception_return
- * will make the thread return 'retval' from a syscall.
- */
-void
-thread_set_syscall_return(
-       thread_t        thread,
-       kern_return_t   retval)
-{
-       thread->top_act->mact.pcb->iss.eax = retval;
-}
+               if (*count < x86_THREAD_STATE_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-/*
- * Initialize the machine-dependent state for a new thread.
- */
-kern_return_t
-thread_machine_create(thread_t thread, thread_act_t thr_act, void (*start_pos)(thread_t))
-{
-        MachineThrAct_t mact = &thr_act->mact;
+               state = (x86_thread_state_t *)tstate;
+
+               bzero((char *)state, sizeof(x86_thread_state_t));
+
+               if (thread_is_64bit_addr(thr_act)) {
+                       state->tsh.flavor = x86_THREAD_STATE64;
+                       state->tsh.count  = x86_THREAD_STATE64_COUNT;
 
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("thread_machine_create(thr=%x,thr_act=%x,st=%x)\n",
-                       thread, thr_act, start_pos);
-#endif /* MACH_ASSERT */
+                       get_thread_state64(thr_act, &state->uts.ts64, FALSE);
+               } else {
+                       state->tsh.flavor = x86_THREAD_STATE32;
+                       state->tsh.count  = x86_THREAD_STATE32_COUNT;
 
-        assert(thread != NULL);
-        assert(thr_act != NULL);
+                       get_thread_state32(thr_act, &state->uts.ts32);
+               }
+               *count = x86_THREAD_STATE_COUNT;
 
-        /*
-         *      Allocate a kernel stack per shuttle
-         */
-        thread->kernel_stack = (int)stack_alloc(thread,start_pos);
-        assert(thread->kernel_stack != 0);
+               break;
+       }
 
-        /*
-         *      Point top of kernel stack to user`s registers.
-         */
-        STACK_IEL(thread->kernel_stack)->saved_state = &mact->pcb->iss;
 
-       /*
-        * Utah code fiddles with pcb here - (we don't need to)
-        */
-       return(KERN_SUCCESS);
-}
+       case x86_EXCEPTION_STATE32:
+       {
+               if (*count < x86_EXCEPTION_STATE32_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-/*
- * Machine-dependent cleanup prior to destroying a thread
- */
-void
-thread_machine_destroy( thread_t thread )
-{
-        spl_t s;
+               if (thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               *count = x86_EXCEPTION_STATE32_COUNT;
 
-        if (thread->kernel_stack != 0) {
-               s = splsched();
-               stack_free(thread);
-               splx(s);
+               get_exception_state32(thr_act, (x86_exception_state32_t *)tstate);
+               /*
+                * Suppress the cpu number for binary compatibility
+                * of this deprecated state.
+                */
+               ((x86_exception_state32_t *)tstate)->cpu = 0;
+               break;
        }
-}
 
-/*
- * This is used to set the current thr_act/thread
- * when starting up a new processor
- */
-void
-thread_machine_set_current( thread_t thread )
-{
-       register int    my_cpu;
+       case x86_EXCEPTION_STATE64:
+       {
+               if (*count < x86_EXCEPTION_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-       mp_disable_preemption();
-       my_cpu = cpu_number();
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-        cpu_data[my_cpu].active_thread = thread;
-       active_kloaded[my_cpu] =
-               thread->top_act->kernel_loaded ? thread->top_act : THR_ACT_NULL;
+               *count = x86_EXCEPTION_STATE64_COUNT;
 
-       mp_enable_preemption();
-}
+               get_exception_state64(thr_act, (x86_exception_state64_t *)tstate);
+               /*
+                * Suppress the cpu number for binary compatibility
+                * of this deprecated state.
+                */
+               ((x86_exception_state64_t *)tstate)->cpu = 0;
+               break;
+       }
 
+       case x86_EXCEPTION_STATE:
+       {
+               x86_exception_state_t   *state;
 
-/*
- * Pool of kernel activations.
- */
+               if (*count < x86_EXCEPTION_STATE_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-void act_machine_init()
-{
-       int i;
-       thread_act_t thr_act;
+               state = (x86_exception_state_t *)tstate;
 
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("act_machine_init()\n");
-#endif /* MACH_ASSERT */
+               bzero((char *)state, sizeof(x86_exception_state_t));
 
-       /* Good to verify this once */
-       assert( THREAD_MACHINE_STATE_MAX <= THREAD_STATE_MAX );
+               if (thread_is_64bit_addr(thr_act)) {
+                       state->esh.flavor = x86_EXCEPTION_STATE64;
+                       state->esh.count  = x86_EXCEPTION_STATE64_COUNT;
 
-       /*
-        * If we start using kernel activations,
-        * would normally create kernel_thread_pool here,
-        * populating it from the act_zone
-        */
-}
+                       get_exception_state64(thr_act, &state->ues.es64);
+               } else {
+                       state->esh.flavor = x86_EXCEPTION_STATE32;
+                       state->esh.count  = x86_EXCEPTION_STATE32_COUNT;
 
-kern_return_t
-act_machine_create(task_t task, thread_act_t thr_act)
-{
-       MachineThrAct_t mact = &thr_act->mact;
-       pcb_t pcb;
+                       get_exception_state32(thr_act, &state->ues.es32);
+               }
+               *count = x86_EXCEPTION_STATE_COUNT;
 
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("act_machine_create(task=%x,thr_act=%x) pcb=%x\n",
-                       task,thr_act, &mact->xxx_pcb);
-#endif /* MACH_ASSERT */
+               break;
+       }
+       case x86_DEBUG_STATE32:
+       {
+               if (*count < x86_DEBUG_STATE32_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-       /*
-        * Clear & Init the pcb  (sets up user-mode s regs)
-        */
-       pcb_init(thr_act);
+               if (thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-       return KERN_SUCCESS;
-}
+               get_debug_state32(thr_act, (x86_debug_state32_t *)tstate);
 
-void
-act_virtual_machine_destroy(thread_act_t thr_act)
-{
-       return;
-}
+               *count = x86_DEBUG_STATE32_COUNT;
 
-void
-act_machine_destroy(thread_act_t thr_act)
-{
+               break;
+       }
+       case x86_DEBUG_STATE64:
+       {
+               if (*count < x86_DEBUG_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               if (!thread_is_64bit_addr(thr_act)) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               get_debug_state64(thr_act, (x86_debug_state64_t *)tstate);
+
+               *count = x86_DEBUG_STATE64_COUNT;
+
+               break;
+       }
+       case x86_DEBUG_STATE:
+       {
+               x86_debug_state_t   *state;
+
+               if (*count < x86_DEBUG_STATE_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("act_machine_destroy(0x%x)\n", thr_act);
-#endif /* MACH_ASSERT */
+               state = (x86_debug_state_t *)tstate;
 
-       pcb_terminate(thr_act);
+               bzero(state, sizeof *state);
+
+               if (thread_is_64bit_addr(thr_act)) {
+                       state->dsh.flavor = x86_DEBUG_STATE64;
+                       state->dsh.count  = x86_DEBUG_STATE64_COUNT;
+
+                       get_debug_state64(thr_act, &state->uds.ds64);
+               } else {
+                       state->dsh.flavor = x86_DEBUG_STATE32;
+                       state->dsh.count  = x86_DEBUG_STATE32_COUNT;
+
+                       get_debug_state32(thr_act, &state->uds.ds32);
+               }
+               *count = x86_DEBUG_STATE_COUNT;
+               break;
+       }
+       default:
+               return KERN_INVALID_ARGUMENT;
+       }
+
+       return KERN_SUCCESS;
 }
 
-void
-act_machine_return(int code)
+kern_return_t
+machine_thread_get_kern_state(
+       thread_t                thread,
+       thread_flavor_t         flavor,
+       thread_state_t          tstate,
+       mach_msg_type_number_t  *count)
 {
-       thread_act_t    thr_act = current_act();
+       x86_saved_state_t       *int_state = current_cpu_datap()->cpu_int_state;
 
-#if    MACH_ASSERT
        /*
-       * We don't go through the locking dance here needed to
-       * acquire thr_act->thread safely.
-       */
+        * This works only for an interrupted kernel thread
+        */
+       if (thread != current_thread() || int_state == NULL) {
+               return KERN_FAILURE;
+       }
 
-       if (watchacts & WA_EXIT)
-               printf("act_machine_return(0x%x) cur_act=%x(%d) thr=%x(%d)\n",
-                       code, thr_act, thr_act->ref_count,
-                      thr_act->thread, thr_act->thread->ref_count);
-#endif /* MACH_ASSERT */
+       switch (flavor) {
+       case x86_THREAD_STATE32: {
+               x86_thread_state32_t *state;
+               x86_saved_state32_t *saved_state;
 
-       /*
-        * This code is called with nothing locked.
-        * It also returns with nothing locked, if it returns.
-        *
-        * This routine terminates the current thread activation.
-        * If this is the only activation associated with its
-        * thread shuttle, then the entire thread (shuttle plus
-        * activation) is terminated.
-        */
-       assert( code == KERN_TERMINATED );
-       assert( thr_act );
+               if (!is_saved_state32(int_state) ||
+                   *count < x86_THREAD_STATE32_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
 
-#ifdef CALLOUT_RPC_MODEL
-       /*
-        * JMM - RPC is not going to be done with a callout/direct-
-        * stack manipulation mechanism.  Instead we will return/
-        * unwind normally as if from a continuation.
-        */
-       act_lock_thread(thr_act);
+               state = (x86_thread_state32_t *) tstate;
 
-       if (thr_act->thread->top_act != thr_act) {
+               saved_state = saved_state32(int_state);
                /*
-                * this is not the top activation;
-                * if possible, we should clone the shuttle so that
-                * both the root RPC-chain and the soon-to-be-orphaned
-                * RPC-chain have shuttles
-                *
-                * JMM - Cloning shuttles isn't the right approach.  We
-                * need to alert the higher up activations to return our
-                * shuttle (because scheduling attributes may TRUELY be
-                * unique and not cloneable.
+                * General registers.
                 */
-               act_unlock_thread(thr_act);
-               panic("act_machine_return: ORPHAN CASE NOT YET IMPLEMENTED");
-       }
-
-       if (thr_act->lower != THR_ACT_NULL) {
-               thread_t        cur_thread = current_thread();
-               thread_act_t    cur_act;
-               struct ipc_port *iplock;
-
-               /* send it an appropriate return code */
-               thr_act->lower->alerts |= SERVER_TERMINATED;
-               install_special_handler(thr_act->lower);
-
-               /* Return to previous act with error code */
-               act_locked_act_reference(thr_act);      /* keep it around */
-               act_switch_swapcheck(cur_thread, (ipc_port_t)0);
-               (void) switch_act(THR_ACT_NULL);
-               /* assert(thr_act->ref_count == 0); */          /* XXX */
-               cur_act = cur_thread->top_act;
-               MACH_RPC_RET(cur_act) = KERN_RPC_SERVER_TERMINATED;         
-            
-               machine_kernel_stack_init(cur_thread, mach_rpc_return_error);
+               state->eax = saved_state->eax;
+               state->ebx = saved_state->ebx;
+               state->ecx = saved_state->ecx;
+               state->edx = saved_state->edx;
+               state->edi = saved_state->edi;
+               state->esi = saved_state->esi;
+               state->ebp = saved_state->ebp;
+               state->esp = saved_state->uesp;
+               state->eflags = saved_state->efl;
+               state->eip = saved_state->eip;
+               state->cs = saved_state->cs;
+               state->ss = saved_state->ss;
+               state->ds = saved_state->ds & 0xffff;
+               state->es = saved_state->es & 0xffff;
+               state->fs = saved_state->fs & 0xffff;
+               state->gs = saved_state->gs & 0xffff;
+
+               *count = x86_THREAD_STATE32_COUNT;
+
+               return KERN_SUCCESS;
+       }
+
+       case x86_THREAD_STATE64: {
+               x86_thread_state64_t    *state;
+               x86_saved_state64_t     *saved_state;
+
+               if (!is_saved_state64(int_state) ||
+                   *count < x86_THREAD_STATE64_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               state = (x86_thread_state64_t *) tstate;
+
+               saved_state = saved_state64(int_state);
                /*
-                * The following unlocks must be done separately since fields 
-                * used by `act_unlock_thread()' have been cleared, meaning
-                * that it would not release all of the appropriate locks.
+                * General registers.
                 */
-               iplock = thr_act->pool_port;    /* remember for unlock call */
-               rpc_unlock(cur_thread);
-               if (iplock) ip_unlock(iplock);  /* must be done separately */
-               act_unlock(thr_act);
-               act_deallocate(thr_act);                /* free it */
-               Load_context(cur_thread);
-               /*NOTREACHED*/
-
-               panic("act_machine_return: TALKING ZOMBIE! (2)");
+               state->rax = saved_state->rax;
+               state->rbx = saved_state->rbx;
+               state->rcx = saved_state->rcx;
+               state->rdx = saved_state->rdx;
+               state->rdi = saved_state->rdi;
+               state->rsi = saved_state->rsi;
+               state->rbp = saved_state->rbp;
+               state->rsp = saved_state->isf.rsp;
+               state->r8 = saved_state->r8;
+               state->r9 = saved_state->r9;
+               state->r10 = saved_state->r10;
+               state->r11 = saved_state->r11;
+               state->r12 = saved_state->r12;
+               state->r13 = saved_state->r13;
+               state->r14 = saved_state->r14;
+               state->r15 = saved_state->r15;
+
+               state->rip = saved_state->isf.rip;
+               state->rflags = saved_state->isf.rflags;
+               state->cs = saved_state->isf.cs;
+               state->fs = saved_state->fs & 0xffff;
+               state->gs = saved_state->gs & 0xffff;
+               *count = x86_THREAD_STATE64_COUNT;
+
+               return KERN_SUCCESS;
        }
-       act_unlock_thread(thr_act);
 
-#endif /* CALLOUT_RPC_MODEL */
+       case x86_THREAD_STATE: {
+               x86_thread_state_t *state = NULL;
+
+               if (*count < x86_THREAD_STATE_COUNT) {
+                       return KERN_INVALID_ARGUMENT;
+               }
+
+               state = (x86_thread_state_t *) tstate;
+
+               if (is_saved_state32(int_state)) {
+                       x86_saved_state32_t *saved_state = saved_state32(int_state);
 
-       /* This is the only activation attached to the shuttle... */
-       /* terminate the entire thread (shuttle plus activation) */
+                       state->tsh.flavor = x86_THREAD_STATE32;
+                       state->tsh.count = x86_THREAD_STATE32_COUNT;
 
-       assert(thr_act->thread->top_act == thr_act);
-       thread_terminate_self();
+                       /*
+                        * General registers.
+                        */
+                       state->uts.ts32.eax = saved_state->eax;
+                       state->uts.ts32.ebx = saved_state->ebx;
+                       state->uts.ts32.ecx = saved_state->ecx;
+                       state->uts.ts32.edx = saved_state->edx;
+                       state->uts.ts32.edi = saved_state->edi;
+                       state->uts.ts32.esi = saved_state->esi;
+                       state->uts.ts32.ebp = saved_state->ebp;
+                       state->uts.ts32.esp = saved_state->uesp;
+                       state->uts.ts32.eflags = saved_state->efl;
+                       state->uts.ts32.eip = saved_state->eip;
+                       state->uts.ts32.cs = saved_state->cs;
+                       state->uts.ts32.ss = saved_state->ss;
+                       state->uts.ts32.ds = saved_state->ds & 0xffff;
+                       state->uts.ts32.es = saved_state->es & 0xffff;
+                       state->uts.ts32.fs = saved_state->fs & 0xffff;
+                       state->uts.ts32.gs = saved_state->gs & 0xffff;
+               } else if (is_saved_state64(int_state)) {
+                       x86_saved_state64_t *saved_state = saved_state64(int_state);
+
+                       state->tsh.flavor = x86_THREAD_STATE64;
+                       state->tsh.count = x86_THREAD_STATE64_COUNT;
 
-       /*NOTREACHED*/
+                       /*
+                        * General registers.
+                        */
+                       state->uts.ts64.rax = saved_state->rax;
+                       state->uts.ts64.rbx = saved_state->rbx;
+                       state->uts.ts64.rcx = saved_state->rcx;
+                       state->uts.ts64.rdx = saved_state->rdx;
+                       state->uts.ts64.rdi = saved_state->rdi;
+                       state->uts.ts64.rsi = saved_state->rsi;
+                       state->uts.ts64.rbp = saved_state->rbp;
+                       state->uts.ts64.rsp = saved_state->isf.rsp;
+                       state->uts.ts64.r8 = saved_state->r8;
+                       state->uts.ts64.r9 = saved_state->r9;
+                       state->uts.ts64.r10 = saved_state->r10;
+                       state->uts.ts64.r11 = saved_state->r11;
+                       state->uts.ts64.r12 = saved_state->r12;
+                       state->uts.ts64.r13 = saved_state->r13;
+                       state->uts.ts64.r14 = saved_state->r14;
+                       state->uts.ts64.r15 = saved_state->r15;
+
+                       state->uts.ts64.rip = saved_state->isf.rip;
+                       state->uts.ts64.rflags = saved_state->isf.rflags;
+                       state->uts.ts64.cs = saved_state->isf.cs;
+                       state->uts.ts64.fs = saved_state->fs & 0xffff;
+                       state->uts.ts64.gs = saved_state->gs & 0xffff;
+               } else {
+                       panic("unknown thread state");
+               }
 
-       panic("act_machine_return: TALKING ZOMBIE! (1)");
+               *count = x86_THREAD_STATE_COUNT;
+               return KERN_SUCCESS;
+       }
+       }
+       return KERN_FAILURE;
 }
 
 
-/*
- * Perform machine-dependent per-thread initializations
- */
 void
-thread_machine_init(void)
+machine_thread_switch_addrmode(thread_t thread)
 {
-       pcb_module_init();
-}
+       /*
+        * We don't want to be preempted until we're done
+        * - particularly if we're switching the current thread
+        */
+       disable_preemption();
 
-/*
- * Some routines for debugging activation code
- */
-static void    dump_handlers(thread_act_t);
-void   dump_regs(thread_act_t);
+       /*
+        * Reset the state saveareas. As we're resetting, we anticipate no
+        * memory allocations in this path.
+        */
+       machine_thread_create(thread, thread->task);
 
-static void
-dump_handlers(thread_act_t thr_act)
-{
-    ReturnHandler *rhp = thr_act->handlers;
-    int        counter = 0;
-
-    printf("\t");
-    while (rhp) {
-       if (rhp == &thr_act->special_handler){
-           if (rhp->next)
-               printf("[NON-Zero next ptr(%x)]", rhp->next);
-           printf("special_handler()->");
-           break;
-       }
-       printf("hdlr_%d(%x)->",counter,rhp->handler);
-       rhp = rhp->next;
-       if (++counter > 32) {
-               printf("Aborting: HUGE handler chain\n");
-               break;
+       /* Adjust FPU state */
+       fpu_switch_addrmode(thread, task_has_64Bit_addr(thread->task));
+
+       /* If we're switching ourselves, reset the pcb addresses etc. */
+       if (thread == current_thread()) {
+               boolean_t istate = ml_set_interrupts_enabled(FALSE);
+               act_machine_switch_pcb(NULL, thread);
+               ml_set_interrupts_enabled(istate);
        }
-    }
-    printf("HLDR_NULL\n");
+       enable_preemption();
 }
 
+
+
+/*
+ * This is used to set the current thr_act/thread
+ * when starting up a new processor
+ */
 void
-dump_regs(thread_act_t thr_act)
+machine_set_current_thread(thread_t thread)
 {
-       if (thr_act->mact.pcb) {
-               register struct i386_saved_state *ssp = USER_REGS(thr_act);
-               /* Print out user register state */
-               printf("\tRegs:\tedi=%x esi=%x ebp=%x ebx=%x edx=%x\n",
-                   ssp->edi, ssp->esi, ssp->ebp, ssp->ebx, ssp->edx);
-               printf("\t\tecx=%x eax=%x eip=%x efl=%x uesp=%x\n",
-                   ssp->ecx, ssp->eax, ssp->eip, ssp->efl, ssp->uesp);
-               printf("\t\tcs=%x ss=%x\n", ssp->cs, ssp->ss);
-       }
+       current_cpu_datap()->cpu_active_thread = thread;
 }
 
-int
-dump_act(thread_act_t thr_act)
-{
-       if (!thr_act)
-               return(0);
-
-       printf("thr_act(0x%x)(%d): thread=%x(%d) task=%x(%d)\n",
-              thr_act, thr_act->ref_count,
-              thr_act->thread, thr_act->thread ? thr_act->thread->ref_count:0,
-              thr_act->task,   thr_act->task   ? thr_act->task->ref_count : 0);
-
-       if (thr_act->pool_port) {
-           thread_pool_t actpp = &thr_act->pool_port->ip_thread_pool;
-           printf("\tpool(acts_p=%x, waiting=%d) pool_next %x\n",
-               actpp->thr_acts, actpp->waiting, thr_act->thread_pool_next);
-       }else
-           printf("\tno thread_pool\n");
-
-       printf("\talerts=%x mask=%x susp=%d user_stop=%d active=%x ast=%x\n",
-                      thr_act->alerts, thr_act->alert_mask,
-                      thr_act->suspend_count, thr_act->user_stop_count,
-                      thr_act->active, thr_act->ast);
-       printf("\thi=%x lo=%x\n", thr_act->higher, thr_act->lower);
-       printf("\tpcb=%x\n", thr_act->mact.pcb);
-
-       if (thr_act->thread && thr_act->thread->kernel_stack) {
-           vm_offset_t stack = thr_act->thread->kernel_stack;
-
-           printf("\tk_stk %x  eip %x ebx %x esp %x iss %x\n",
-               stack, STACK_IKS(stack)->k_eip, STACK_IKS(stack)->k_ebx,
-               STACK_IKS(stack)->k_esp, STACK_IEL(stack)->saved_state);
-       }
-
-       dump_handlers(thr_act);
-       dump_regs(thr_act);
-       return((int)thr_act);
-}
-unsigned int
-get_useraddr()
+
+/*
+ * Perform machine-dependent per-thread initializations
+ */
+void
+machine_thread_init(void)
 {
-  
-        thread_act_t thr_act = current_act();
-       if (thr_act->mact.pcb) 
-               return(thr_act->mact.pcb->iss.eip);
-       else 
-               return(0);
+       iss_zone = zinit(sizeof(x86_saved_state_t),
+           thread_max * sizeof(x86_saved_state_t),
+           THREAD_CHUNK * sizeof(x86_saved_state_t),
+           "x86_64 saved state");
 
+       ids_zone = zinit(sizeof(x86_debug_state64_t),
+           thread_max * sizeof(x86_debug_state64_t),
+           THREAD_CHUNK * sizeof(x86_debug_state64_t),
+           "x86_64 debug state");
+
+       fpu_module_init();
 }
 
-void
-thread_swapin_mach_alloc(thread_t thread)
+
+
+user_addr_t
+get_useraddr(void)
 {
+       thread_t thr_act = current_thread();
+
+       if (thread_is_64bit_addr(thr_act)) {
+               x86_saved_state64_t     *iss64;
 
-  /* 386 does not have saveareas */
+               iss64 = USER_REGS64(thr_act);
 
+               return iss64->isf.rip;
+       } else {
+               x86_saved_state32_t     *iss32;
+
+               iss32 = USER_REGS32(thr_act);
+
+               return iss32->eip;
+       }
 }
+
 /*
  * detach and return a kernel stack from a thread
  */
 
 vm_offset_t
-stack_detach(thread_t thread)
+machine_stack_detach(thread_t thread)
 {
-  vm_offset_t stack;
+       vm_offset_t     stack;
 
-               KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_DETACH),
-                       thread, thread->priority,
-                       thread->sched_pri, 0,
-                       0);
+       KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_DETACH),
+           (uintptr_t)thread_tid(thread), thread->priority,
+           thread->sched_pri, 0,
+           0);
 
-  stack = thread->kernel_stack;
-  thread->kernel_stack = 0;
-  return(stack);
+       stack = thread->kernel_stack;
+       thread->kernel_stack = 0;
+
+       return stack;
 }
 
 /*
@@ -1413,29 +1966,32 @@ stack_detach(thread_t thread)
  */
 
 void
-stack_attach(struct thread_shuttle *thread,
-            vm_offset_t stack,
-            void (*start_pos)(thread_t))
+machine_stack_attach(
+       thread_t                thread,
+       vm_offset_t             stack)
 {
-  struct i386_kernel_state *statep;
-  thread_act_t thr_act;
-
-               KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_ATTACH),
-                       thread, thread->priority,
-                       thread->sched_pri, continuation, 
-                       0);
-
-  assert(stack);
-  statep = STACK_IKS(stack);
-  thread->kernel_stack = stack;
+       struct x86_kernel_state *statep;
 
-  statep->k_eip = (unsigned long) Thread_continue;
-  statep->k_ebx = (unsigned long) start_pos;
-  statep->k_esp = (unsigned long) STACK_IEL(stack);
+       KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_ATTACH),
+           (uintptr_t)thread_tid(thread), thread->priority,
+           thread->sched_pri, 0, 0);
 
-  STACK_IEL(stack)->saved_state = &thr_act->mact.pcb->iss;
+       assert(stack);
+       thread->kernel_stack = stack;
+       thread_initialize_kernel_state(thread);
+
+       statep = STACK_IKS(stack);
+#if defined(__x86_64__)
+       statep->k_rip = (unsigned long) Thread_continue;
+       statep->k_rbx = (unsigned long) thread_continue;
+       statep->k_rsp = (unsigned long) STACK_IKS(stack);
+#else
+       statep->k_eip = (unsigned long) Thread_continue;
+       statep->k_ebx = (unsigned long) thread_continue;
+       statep->k_esp = (unsigned long) STACK_IKS(stack);
+#endif
 
-  return;
+       return;
 }
 
 /*
@@ -1443,31 +1999,233 @@ stack_attach(struct thread_shuttle *thread,
  */
 
 void
-stack_handoff(thread_t old,
-             thread_t new)
+machine_stack_handoff(thread_t old,
+    thread_t new)
+{
+       vm_offset_t     stack;
+
+       assert(new);
+       assert(old);
+
+       kpc_off_cpu(old);
+
+       stack = old->kernel_stack;
+       if (stack == old->reserved_stack) {
+               assert(new->reserved_stack);
+               old->reserved_stack = new->reserved_stack;
+               new->reserved_stack = stack;
+       }
+       old->kernel_stack = 0;
+       /*
+        * A full call to machine_stack_attach() is unnecessry
+        * because old stack is already initialized.
+        */
+       new->kernel_stack = stack;
+
+       fpu_switch_context(old, new);
+
+       old->machine.specFlags &= ~OnProc;
+       new->machine.specFlags |= OnProc;
+
+       pmap_switch_context(old, new, cpu_number());
+       act_machine_switch_pcb(old, new);
+
+#if HYPERVISOR
+       ml_hv_cswitch(old, new);
+#endif
+
+       machine_set_current_thread(new);
+       thread_initialize_kernel_state(new);
+
+       return;
+}
+
+
+
+
+struct x86_act_context32 {
+       x86_saved_state32_t ss;
+       x86_float_state32_t fs;
+       x86_debug_state32_t ds;
+};
+
+struct x86_act_context64 {
+       x86_saved_state64_t ss;
+       x86_float_state64_t fs;
+       x86_debug_state64_t ds;
+};
+
+
+
+void *
+act_thread_csave(void)
+{
+       kern_return_t kret;
+       mach_msg_type_number_t val;
+       thread_t thr_act = current_thread();
+
+       if (thread_is_64bit_addr(thr_act)) {
+               struct x86_act_context64 *ic64;
+
+               ic64 = (struct x86_act_context64 *)kalloc(sizeof(struct x86_act_context64));
+
+               if (ic64 == (struct x86_act_context64 *)NULL) {
+                       return (void *)0;
+               }
+
+               val = x86_SAVED_STATE64_COUNT;
+               kret = machine_thread_get_state(thr_act, x86_SAVED_STATE64,
+                   (thread_state_t) &ic64->ss, &val);
+               if (kret != KERN_SUCCESS) {
+                       kfree(ic64, sizeof(struct x86_act_context64));
+                       return (void *)0;
+               }
+               val = x86_FLOAT_STATE64_COUNT;
+               kret = machine_thread_get_state(thr_act, x86_FLOAT_STATE64,
+                   (thread_state_t) &ic64->fs, &val);
+               if (kret != KERN_SUCCESS) {
+                       kfree(ic64, sizeof(struct x86_act_context64));
+                       return (void *)0;
+               }
+
+               val = x86_DEBUG_STATE64_COUNT;
+               kret = machine_thread_get_state(thr_act,
+                   x86_DEBUG_STATE64,
+                   (thread_state_t)&ic64->ds,
+                   &val);
+               if (kret != KERN_SUCCESS) {
+                       kfree(ic64, sizeof(struct x86_act_context64));
+                       return (void *)0;
+               }
+               return ic64;
+       } else {
+               struct x86_act_context32 *ic32;
+
+               ic32 = (struct x86_act_context32 *)kalloc(sizeof(struct x86_act_context32));
+
+               if (ic32 == (struct x86_act_context32 *)NULL) {
+                       return (void *)0;
+               }
+
+               val = x86_SAVED_STATE32_COUNT;
+               kret = machine_thread_get_state(thr_act, x86_SAVED_STATE32,
+                   (thread_state_t) &ic32->ss, &val);
+               if (kret != KERN_SUCCESS) {
+                       kfree(ic32, sizeof(struct x86_act_context32));
+                       return (void *)0;
+               }
+               val = x86_FLOAT_STATE32_COUNT;
+               kret = machine_thread_get_state(thr_act, x86_FLOAT_STATE32,
+                   (thread_state_t) &ic32->fs, &val);
+               if (kret != KERN_SUCCESS) {
+                       kfree(ic32, sizeof(struct x86_act_context32));
+                       return (void *)0;
+               }
+
+               val = x86_DEBUG_STATE32_COUNT;
+               kret = machine_thread_get_state(thr_act,
+                   x86_DEBUG_STATE32,
+                   (thread_state_t)&ic32->ds,
+                   &val);
+               if (kret != KERN_SUCCESS) {
+                       kfree(ic32, sizeof(struct x86_act_context32));
+                       return (void *)0;
+               }
+               return ic32;
+       }
+}
+
+
+void
+act_thread_catt(void *ctx)
 {
+       thread_t thr_act = current_thread();
+       kern_return_t kret;
+
+       if (ctx == (void *)NULL) {
+               return;
+       }
+
+       if (thread_is_64bit_addr(thr_act)) {
+               struct x86_act_context64 *ic64;
 
-  vm_offset_t stack;
-  pmap_t new_pmap;
+               ic64 = (struct x86_act_context64 *)ctx;
 
-               KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_HANDOFF),
-                       thread, thread->priority,
-                       thread->sched_pri, continuation, 
-                       0);
+               kret = machine_thread_set_state(thr_act, x86_SAVED_STATE64,
+                   (thread_state_t) &ic64->ss, x86_SAVED_STATE64_COUNT);
+               if (kret == KERN_SUCCESS) {
+                       machine_thread_set_state(thr_act, x86_FLOAT_STATE64,
+                           (thread_state_t) &ic64->fs, x86_FLOAT_STATE64_COUNT);
+               }
+               kfree(ic64, sizeof(struct x86_act_context64));
+       } else {
+               struct x86_act_context32 *ic32;
+
+               ic32 = (struct x86_act_context32 *)ctx;
+
+               kret = machine_thread_set_state(thr_act, x86_SAVED_STATE32,
+                   (thread_state_t) &ic32->ss, x86_SAVED_STATE32_COUNT);
+               if (kret == KERN_SUCCESS) {
+                       (void) machine_thread_set_state(thr_act, x86_FLOAT_STATE32,
+                           (thread_state_t) &ic32->fs, x86_FLOAT_STATE32_COUNT);
+               }
+               kfree(ic32, sizeof(struct x86_act_context32));
+       }
+}
 
-  assert(new->top_act);
-  assert(old->top_act);
 
-  stack = stack_detach(old);
-  stack_attach(new, stack, 0);
+void
+act_thread_cfree(__unused void *ctx)
+{
+       /* XXX - Unused */
+}
 
-  new_pmap = new->top_act->task->map->pmap;
-  if (old->top_act->task->map->pmap != new_pmap)
-       PMAP_ACTIVATE_MAP(new->top_act->task->map, cpu_number());
+/*
+ * Duplicate one x86_debug_state32_t to another.  "all" parameter
+ * chooses whether dr4 and dr5 are copied (they are never meant
+ * to be installed when we do machine_task_set_state() or
+ * machine_thread_set_state()).
+ */
+void
+copy_debug_state32(
+       x86_debug_state32_t *src,
+       x86_debug_state32_t *target,
+       boolean_t all)
+{
+       if (all) {
+               target->dr4 = src->dr4;
+               target->dr5 = src->dr5;
+       }
 
-  thread_machine_set_current(new);
+       target->dr0 = src->dr0;
+       target->dr1 = src->dr1;
+       target->dr2 = src->dr2;
+       target->dr3 = src->dr3;
+       target->dr6 = src->dr6;
+       target->dr7 = src->dr7;
+}
 
-  active_stacks[cpu_number()] = new->kernel_stack;
+/*
+ * Duplicate one x86_debug_state64_t to another.  "all" parameter
+ * chooses whether dr4 and dr5 are copied (they are never meant
+ * to be installed when we do machine_task_set_state() or
+ * machine_thread_set_state()).
+ */
+void
+copy_debug_state64(
+       x86_debug_state64_t *src,
+       x86_debug_state64_t *target,
+       boolean_t all)
+{
+       if (all) {
+               target->dr4 = src->dr4;
+               target->dr5 = src->dr5;
+       }
 
-  return;
+       target->dr0 = src->dr0;
+       target->dr1 = src->dr1;
+       target->dr2 = src->dr2;
+       target->dr3 = src->dr3;
+       target->dr6 = src->dr6;
+       target->dr7 = src->dr7;
 }