]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/i386/pcb.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / osfmk / i386 / pcb.c
index c261f42148f8bd3f6f16ba7a4a901b55a3d967e8..27a48c05824f21dfe2b664e78d88a24a7e56afbc 100644 (file)
@@ -1,21 +1,22 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
@@ -48,7 +49,6 @@
  * the rights to redistribute these changes.
  */
 
-#include <cpus.h>
 #include <mach_rt.h>
 #include <mach_debug.h>
 #include <mach_ldebug.h>
 #include <mach/thread_status.h>
 #include <mach/vm_param.h>
 
+#include <i386/cpu_data.h>
+#include <i386/cpu_number.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 <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/eflags.h>
 #include <i386/user_ldt.h>
 #include <i386/fpu.h>
 #include <i386/iopb_entries.h>
+#include <i386/mp_desc.h>
+#include <i386/cpu_data.h>
+
 
 /*
  * Maps state flavor to number of words in the state:
  */
-unsigned int state_count[] = {
+__private_extern__
+unsigned int _MachineStateCount[] = {
        /* FLAVOR_LIST */ 0,
        i386_NEW_THREAD_STATE_COUNT,
        i386_FLOAT_STATE_COUNT,
@@ -99,14 +111,18 @@ unsigned int state_count[] = {
 
 /* Forward */
 
-void act_machine_throughcall(thread_act_t thr_act);
+void           act_machine_throughcall(thread_t thr_act);
+user_addr_t    get_useraddr(void);
+void           act_machine_return(int);
+void           act_machine_sv_free(thread_t, int);
+
 extern thread_t                Switch_context(
-                               thread_t                old,
-                               void                    (*cont)(void),
-                               thread_t                new);
+                               thread_t                        old,
+                               thread_continue_t       cont,
+                               thread_t                        new);
 extern void            Thread_continue(void);
 extern void            Load_context(
-                               thread_t                thread);
+                               thread_t                        thread);
 
 /*
  * consider_machine_collect:
@@ -114,87 +130,32 @@ extern void               Load_context(
  *     Try to collect machine-dependent pages
  */
 void
-consider_machine_collect()
-{
-}
-
-void
-consider_machine_adjust()
+consider_machine_collect(void)
 {
 }
 
-
-/*
- *     machine_kernel_stack_init:
- *
- *     Initialize a kernel stack which has already been
- *     attached to its thread_activation.
- */
-
 void
-machine_kernel_stack_init(
-       thread_t        thread,
-       void            (*start_pos)(thread_t))
+consider_machine_adjust(void)
 {
-       thread_act_t    thr_act = thread->top_act;
-       vm_offset_t     stack;
-
-       assert(thr_act);
-       stack = thread->kernel_stack;
-       assert(stack);
-
-#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));
-       }
-#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.
-        */
-       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);
-
-       /*
-        *      Point top of kernel stack to user`s registers.
-        */
-       STACK_IEL(stack)->saved_state = &thr_act->mact.pcb->iss;
 }
 
 
-#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
-
-#define        gdt_desc_p(mycpu,sel) \
-       ((struct real_descriptor *)&curr_gdt(mycpu)[sel_idx(sel)])
+// DEBUG
+int    DEBUG_kldt = 0;
+int    DEBUG_uldt = 0;
 
-void
-act_machine_switch_pcb( thread_act_t new_act )
+static void
+act_machine_switch_pcb( thread_t new )
 {
-       pcb_t                   pcb = new_act->mact.pcb;
+       pcb_t                   pcb = new->machine.pcb;
        int                     mycpu;
-    {
        register iopb_tss_t     tss = pcb->ims.io_tss;
        vm_offset_t             pcb_stack_top;
+       register user_ldt_t     uldt = pcb->ims.ldt;
 
-        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;
+        assert(new->kernel_stack != 0);
+        STACK_IEL(new->kernel_stack)->saved_state =
+                &new->machine.pcb->iss;
 
        /*
         *      Save a pointer to the top of the "kernel" stack -
@@ -217,41 +178,48 @@ act_machine_switch_pcb( thread_act_t new_act )
             *  No per-thread IO permissions.
             *  Use standard kernel TSS.
             */
-           if (!(gdt_desc_p(mycpu,KERNEL_TSS)->access & ACC_TSS_BUSY))
+           if (!(gdt_desc_p(KERNEL_TSS)->access & ACC_TSS_BUSY))
                set_tr(KERNEL_TSS);
-           curr_ktss(mycpu)->esp0 = pcb_stack_top;
+           current_ktss()->esp0 = pcb_stack_top;
        }
        else {
            /*
             * Set the IO permissions.  Use this thread`s TSS.
             */
-           *gdt_desc_p(mycpu,USER_TSS)
+           *gdt_desc_p(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;
+           gdt_desc_p(KERNEL_TSS)->access &= ~ ACC_TSS_BUSY;
        }
-    }
 
-    {
-       register user_ldt_t     ldt = pcb->ims.ldt;
        /*
-        * Set the thread`s LDT.
+        * Set the thread`s LDT or LDT entry.
         */
-       if (ldt == 0) {
+       if (uldt == 0) {
+           struct real_descriptor *ldtp;
            /*
             * Use system LDT.
             */
+            // Set up the tasks specific ldt entries if extant
+           ldtp = (struct real_descriptor *)current_ldt();
+           ldtp[sel_idx(USER_CTHREAD)] = pcb->cthread_desc;
+            if (pcb->uldt_selector != 0)
+                ldtp[sel_idx(pcb->uldt_selector)] = pcb->uldt_desc;
            set_ldt(KERNEL_LDT);
        }
        else {
            /*
-            * Thread has its own LDT.
+            * Thread has its own LDT. // THIS SHOULD BE REMOVED!!!!
             */
-           *gdt_desc_p(mycpu,USER_LDT) = ldt->desc;
+           *gdt_desc_p(USER_LDT) = uldt->desc;
            set_ldt(USER_LDT);
+            /*debug*/
+           if ((DEBUG_uldt++ % 0x7fff) == 0)
+               printf("KERNEL----> setting user ldt");
+       
        }
-    }
+
        mp_enable_preemption();
        /*
         * Load the floating-point context, if necessary.
@@ -260,57 +228,15 @@ act_machine_switch_pcb( thread_act_t new_act )
 
 }
 
-/*
- * flush out any lazily evaluated HW state in the
- * owning thread's context, before termination.
- */
-void
-thread_machine_flush( thread_act_t cur_act )
-{
-    fpflush(cur_act);
-}
-
 /*
  * Switch to the first thread on a CPU.
  */
 void
-load_context(
+machine_load_context(
        thread_t                new)
 {
-       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;
-
-/*
- * machine_switch_act
- *
- * Machine-dependent details of activation switching.  Called with
- * RPC locks held and preemption disabled.
- */
-void
-machine_switch_act( 
-       thread_t        thread,
-       thread_act_t    old,
-       thread_act_t    new,
-       int                             cpu)
-{
-       /*
-        *      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);
-
-       PMAP_SWITCH_CONTEXT(old, new, cpu);
        act_machine_switch_pcb(new);
+       Load_context(new);
 }
 
 /*
@@ -319,32 +245,20 @@ 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;
-
 #if MACH_RT
-        assert(old_act->kernel_loaded ||
-               active_stacks[cpu_number()] == old_act->thread->kernel_stack);
-        assert (get_preemption_level() == 1);
+        assert(current_cpu_datap()->cpu_active_stack == old->kernel_stack);
 #endif
-       check_simple_locks();
 
        /*
         *      Save FP registers if in use.
         */
        fpu_save_context(old);
 
-#if    MACH_ASSERT
-       if (watchacts & WA_SWITCH)
-               printf("\tswitch_context(old=%x con=%x new=%x)\n",
-                                           old, continuation, new);
-#endif /* MACH_ASSERT */
-
        /*
         *      Switch address maps if need be, even if not switching tasks.
         *      (A server activation may be "borrowing" a client map.)
@@ -352,140 +266,58 @@ switch_context(
     {
        int     mycpu = cpu_number();
 
-       PMAP_SWITCH_CONTEXT(old_act, new_act, mycpu)
+       PMAP_SWITCH_CONTEXT(old, new, mycpu)
     }
 
        /*
         *      Load the rest of the user state for the new thread
         */
-       act_machine_switch_pcb(new_act);
+       act_machine_switch_pcb(new);
        KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_SCHED) | DBG_FUNC_NONE,
                     (int)old, (int)new, old->sched_pri, new->sched_pri, 0);
+       old->continuation = NULL;
        return(Switch_context(old, continuation, new));
 }
 
-void
-pcb_module_init(void)
-{
-       fpu_module_init();
-       iopb_init();
-}
-
-void
-pcb_init( register thread_act_t thr_act )
-{
-       register pcb_t pcb;
-
-       assert(thr_act->mact.pcb == (pcb_t)0);
-       pcb = thr_act->mact.pcb = &thr_act->mact.xxx_pcb;
-
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("pcb_init(%x) pcb=%x\n", thr_act, pcb);
-#endif /* MACH_ASSERT */
-
-       /*
-        *      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);
-        */
-       simple_lock_init(&pcb->lock, ETAP_MISC_PCB);
-
-       /*
-        *      Guarantee that the bootstrapped thread will be in user
-        *      mode.
-        */
-       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;
-}
-
 /*
- * Adjust saved register state for thread belonging to task
- * created with kernel_task_create().
+ * act_machine_sv_free
+ * release saveareas associated with an act. if flag is true, release
+ * user level savearea(s) too, else don't
  */
 void
-pcb_user_to_kernel(
-       thread_act_t thr_act)
+act_machine_sv_free(__unused thread_t act, __unused int flag)
 {
-       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;
-}
-
-void
-pcb_terminate(
-       register thread_act_t thr_act)
-{
-       register pcb_t  pcb = thr_act->mact.pcb;
-
-       assert(pcb);
-
-       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;
 }
 
-/*
- *     pcb_collect:
- *
- *     Attempt to free excess pcb memory.
- */
-
-void
-pcb_collect(
-       register thread_act_t  thr_act)
-{
-       /* accomplishes very little */
-}
 
 /*
- * act_machine_sv_free
- * release saveareas associated with an act. if flag is true, release
- * user level savearea(s) too, else don't
+ * 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.
  */
-void
-act_machine_sv_free(thread_act_t act, int flag)
+kern_return_t
+machine_thread_state_initialize(
+       thread_t thread)
 {
+#pragma unused (thread)
 
+    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 */
+       int kernel_act = 0;
 
        switch (flavor) {
            case THREAD_SYSCALL_STATE:
@@ -516,6 +348,16 @@ act_machine_set_state(
 
                state = (struct i386_saved_state *) tstate;
 
+               /* Check segment selectors are safe */
+               if (!kernel_act &&
+                   !valid_user_segment_selectors(state->cs,
+                                                 state->ss,
+                                                 state->ds,
+                                                 state->es,
+                                                 state->fs,
+                                                 state->gs))
+                   return KERN_INVALID_ARGUMENT;
+               
                saved_state = USER_REGS(thr_act);
 
                /*
@@ -558,25 +400,25 @@ act_machine_set_state(
                    saved_state->fs = 0;
                    saved_state->gs = 0;
 
-                   if (thr_act->mact.pcb->ims.v86s.int_table) {
+                   if (thr_act->machine.pcb->ims.v86s.int_table) {
                        /*
                         * Hardware assist on.
                         */
-                       thr_act->mact.pcb->ims.v86s.flags =
+                       thr_act->machine.pcb->ims.v86s.flags =
                            state->efl & (EFL_TF | EFL_IF);
                    }
                }
-               else if (!kernel_act) {
+               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;
+                 saved_state->cs = KERNEL_CS;
+                 saved_state->ss = KERNEL_DS;
+                 saved_state->ds = KERNEL_DS;
+                 saved_state->es = KERNEL_DS;
+                 saved_state->fs = KERNEL_DS;
+                 saved_state->gs = CPU_DATA_GS;
                }
                else {
                    /*
@@ -605,6 +447,8 @@ act_machine_set_state(
                    return(KERN_INVALID_ARGUMENT);
                }
 
+               state = (struct i386_new_thread_state *) tstate;
+
                if (flavor == i386_REGS_SEGS_STATE) {
                    /*
                     * Code and stack selectors must not be null,
@@ -619,13 +463,15 @@ act_machine_set_state(
                    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))
+                       !valid_user_segment_selectors(state->cs,
+                                                     state->ss,
+                                                     state->ds,
+                                                     state->es,
+                                                     state->fs,
+                                                     state->gs))
                        return KERN_INVALID_ARGUMENT;
                }
 
-               state = (struct i386_new_thread_state *) tstate;
-
                saved_state = USER_REGS(thr_act);
 
                /*
@@ -668,25 +514,25 @@ act_machine_set_state(
                    saved_state->fs = 0;
                    saved_state->gs = 0;
 
-                   if (thr_act->mact.pcb->ims.v86s.int_table) {
+                   if (thr_act->machine.pcb->ims.v86s.int_table) {
                        /*
                         * Hardware assist on.
                         */
-                       thr_act->mact.pcb->ims.v86s.flags =
+                       thr_act->machine.pcb->ims.v86s.flags =
                            state->efl & (EFL_TF | EFL_IF);
                    }
                }
-               else if (flavor == i386_NEW_THREAD_STATE && !kernel_act) {
+               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;
+                 saved_state->cs = KERNEL_CS;
+                 saved_state->ss = KERNEL_DS;
+                 saved_state->ds = KERNEL_DS;
+                 saved_state->es = KERNEL_DS;
+                 saved_state->fs = KERNEL_DS;
+                 saved_state->gs = CPU_DATA_GS;
                }
                else {
                    /*
@@ -706,20 +552,17 @@ act_machine_set_state(
            }
 
            case i386_FLOAT_STATE: {
-
-               if (count < i386_FLOAT_STATE_COUNT)
+               if (count < i386_old_FLOAT_STATE_COUNT)
                        return(KERN_INVALID_ARGUMENT);
-
-               return fpu_set_state(thr_act,(struct i386_float_state*)tstate);
+                if (count < i386_FLOAT_STATE_COUNT)
+                    return fpu_set_state(thr_act,(struct i386_float_state*)tstate);
+                else return fpu_set_fxstate(thr_act,(struct i386_float_state*)tstate);
            }
 
            /*
             * 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;
-
                if (count < i386_ISA_PORT_MAP_STATE_COUNT)
                        return(KERN_INVALID_ARGUMENT);
 
@@ -745,10 +588,10 @@ act_machine_set_state(
                            > VM_MAX_ADDRESS)
                    return KERN_INVALID_ARGUMENT;
 
-               thr_act->mact.pcb->ims.v86s.int_table = int_table;
-               thr_act->mact.pcb->ims.v86s.int_count = int_count;
+               thr_act->machine.pcb->ims.v86s.int_table = int_table;
+               thr_act->machine.pcb->ims.v86s.int_count = int_count;
 
-               thr_act->mact.pcb->ims.v86s.flags =
+               thr_act->machine.pcb->ims.v86s.flags =
                        USER_REGS(thr_act)->efl & (EFL_TF | EFL_IF);
                break;
            }
@@ -775,8 +618,8 @@ act_machine_set_state(
                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;
+               saved_state->fs = state25->fs;
+               saved_state->gs = state25->gs;
        }
                break;
 
@@ -795,19 +638,12 @@ act_machine_set_state(
 
 
 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 i386_SAVED_STATE:
@@ -835,11 +671,11 @@ act_machine_get_state(
                    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) {
+                   if (thr_act->machine.pcb->ims.v86s.int_table) {
                        /*
                         * Hardware assist on
                         */
-                       if ((thr_act->mact.pcb->ims.v86s.flags &
+                       if ((thr_act->machine.pcb->ims.v86s.flags &
                                        (EFL_IF|V86_IF_PENDING)) == 0)
                            state->efl &= ~EFL_IF;
                    }
@@ -894,11 +730,11 @@ act_machine_get_state(
                    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) {
+                   if (thr_act->machine.pcb->ims.v86s.int_table) {
                        /*
                         * Hardware assist on
                         */
-                       if ((thr_act->mact.pcb->ims.v86s.flags &
+                       if ((thr_act->machine.pcb->ims.v86s.flags &
                                        (EFL_IF|V86_IF_PENDING)) == 0)
                            state->efl &= ~EFL_IF;
                    }
@@ -943,12 +779,15 @@ act_machine_get_state(
                break;
 
            case i386_FLOAT_STATE: {
-
-               if (*count < i386_FLOAT_STATE_COUNT)
+               if (*count < i386_old_FLOAT_STATE_COUNT)
                        return(KERN_INVALID_ARGUMENT);
-
-               *count = i386_FLOAT_STATE_COUNT;
-               return fpu_get_state(thr_act,(struct i386_float_state *)tstate);
+                if (*count< i386_FLOAT_STATE_COUNT) {
+                    *count = i386_old_FLOAT_STATE_COUNT;
+                    return fpu_get_state(thr_act,(struct i386_float_state *)tstate);
+                } else {
+                    *count = i386_FLOAT_STATE_COUNT;
+                    return fpu_get_fxstate(thr_act,(struct i386_float_state *)tstate);
+                }
            }
 
            /*
@@ -962,10 +801,10 @@ act_machine_get_state(
                        return(KERN_INVALID_ARGUMENT);
 
                state = (struct i386_isa_port_map_state *) tstate;
-               tss = thr_act->mact.pcb->ims.io_tss;
+               tss = thr_act->machine.pcb->ims.io_tss;
 
                if (tss == 0) {
-                   int i;
+                   unsigned int i;
 
                    /*
                     *  The thread has no ktss, so no IO permissions.
@@ -995,8 +834,8 @@ act_machine_get_state(
                    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;
+               state->int_table = thr_act->machine.pcb->ims.v86s.int_table;
+               state->int_count = thr_act->machine.pcb->ims.v86s.int_count;
 
                *count = i386_V86_ASSIST_STATE_COUNT;
                break;
@@ -1035,46 +874,43 @@ act_machine_get_state(
        return(KERN_SUCCESS);
 }
 
-/*
- * 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;
-}
-
 /*
  * 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))
+machine_thread_create(
+       thread_t                thread,
+       __unused task_t         task)
 {
-       MachineThrAct_t mact = &thr_act->mact;
+       pcb_t   pcb = &thread->machine.xxx_pcb;
 
-#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 */
+       thread->machine.pcb = pcb;
 
-       assert(thread != NULL);
-       assert(thr_act != NULL);
+       simple_lock_init(&pcb->lock, 0);
 
        /*
-        *      Allocate a kernel stack per shuttle
+        *      Guarantee that the bootstrapped thread will be in user
+        *      mode.
         */
-       thread->kernel_stack = (int)stack_alloc(thread,start_pos);
-       thread->state &= ~TH_STACK_HANDOFF;
-       assert(thread->kernel_stack != 0);
+       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;
+       {
+         struct real_descriptor *ldtp;
+         ldtp = (struct real_descriptor *)ldt;
+         pcb->cthread_desc = ldtp[sel_idx(USER_DS)];
+          pcb->uldt_desc = ldtp[sel_idx(USER_DS)];
+          pcb->uldt_selector = 0;
+       }
 
        /*
-        *      Point top of kernel stack to user`s registers.
+        *      Allocate a kernel stack per thread.
         */
-       STACK_IEL(thread->kernel_stack)->saved_state = &mact->pcb->iss;
+       stack_alloc(thread);
 
        return(KERN_SUCCESS);
 }
@@ -1083,15 +919,20 @@ thread_machine_create(thread_t thread, thread_act_t thr_act, void (*start_pos)(t
  * Machine-dependent cleanup prior to destroying a thread
  */
 void
-thread_machine_destroy( thread_t thread )
+machine_thread_destroy(
+       thread_t                thread)
 {
-        spl_t s;
+       register pcb_t  pcb = thread->machine.pcb;
 
-        if (thread->kernel_stack != 0) {
-               s = splsched();
-               stack_free(thread);
-               splx(s);
-       }
+       assert(pcb);
+        
+       if (pcb->ims.io_tss != 0)
+               iopb_destroy(pcb->ims.io_tss);
+       if (pcb->ims.ifps != 0)
+               fpu_free(pcb->ims.ifps);
+       if (pcb->ims.ldt != 0)
+               user_ldt_free(pcb->ims.ldt);
+       thread->machine.pcb = (pcb_t)0;
 }
 
 /*
@@ -1099,94 +940,24 @@ thread_machine_destroy( thread_t thread )
  * when starting up a new processor
  */
 void
-thread_machine_set_current( thread_t thread )
+machine_set_current_thread( thread_t thread )
 {
-       register int    my_cpu;
-
        mp_disable_preemption();
-       my_cpu = cpu_number();
 
-        cpu_data[my_cpu].active_thread = thread;
-       active_kloaded[my_cpu] =
-               thread->top_act->kernel_loaded ? thread->top_act : THR_ACT_NULL;
+        current_cpu_datap()->cpu_active_thread = thread;
+       current_cpu_datap()->cpu_active_kloaded = THREAD_NULL;
 
        mp_enable_preemption();
 }
 
-
-/*
- * Pool of kernel activations.
- */
-
-void act_machine_init()
-{
-       int i;
-       thread_act_t thr_act;
-
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("act_machine_init()\n");
-#endif /* MACH_ASSERT */
-
-       /* Good to verify this once */
-       assert( THREAD_MACHINE_STATE_MAX <= THREAD_STATE_MAX );
-}
-
-kern_return_t
-act_machine_create(task_t task, thread_act_t thr_act)
-{
-       MachineThrAct_t mact = &thr_act->mact;
-       pcb_t pcb;
-
-#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 */
-
-       /*
-        * Clear & Init the pcb  (sets up user-mode s regs)
-        */
-       pcb_init(thr_act);
-
-       return KERN_SUCCESS;
-}
-
 void
-act_virtual_machine_destroy(thread_act_t thr_act)
+machine_thread_terminate_self(void)
 {
-       return;
-}
-
-void
-act_machine_destroy(thread_act_t thr_act)
-{
-
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("act_machine_destroy(0x%x)\n", thr_act);
-#endif /* MACH_ASSERT */
-
-       pcb_terminate(thr_act);
 }
 
 void
 act_machine_return(int code)
 {
-       thread_act_t    thr_act = current_act();
-
-#if    MACH_ASSERT
-       /*
-       * We don't go through the locking dance here needed to
-       * acquire thr_act->thread safely.
-       */
-
-       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 */
-
        /*
         * This code is called with nothing locked.
         * It also returns with nothing locked, if it returns.
@@ -1197,17 +968,12 @@ act_machine_return(int code)
         * activation) is terminated.
         */
        assert( code == KERN_TERMINATED );
-       assert( thr_act );
 
-       /* This is the only activation attached to the shuttle... */
-       /* terminate the entire thread (shuttle plus activation) */
-
-       assert(thr_act->thread->top_act == thr_act);
        thread_terminate_self();
 
        /*NOTREACHED*/
 
-       panic("act_machine_return: TALKING ZOMBIE! (1)");
+       panic("act_machine_return(%d): TALKING ZOMBIE! (1)", code);
 }
 
 
@@ -1215,19 +981,21 @@ act_machine_return(int code)
  * Perform machine-dependent per-thread initializations
  */
 void
-thread_machine_init(void)
+machine_thread_init(void)
 {
-       pcb_module_init();
+       fpu_module_init();
+       iopb_init();
 }
 
 /*
  * Some routines for debugging activation code
  */
-static void    dump_handlers(thread_act_t);
-void   dump_regs(thread_act_t);
+static void    dump_handlers(thread_t);
+void           dump_regs(thread_t);
+int            dump_act(thread_t thr_act);
 
 static void
-dump_handlers(thread_act_t thr_act)
+dump_handlers(thread_t thr_act)
 {
     ReturnHandler *rhp = thr_act->handlers;
     int        counter = 0;
@@ -1251,9 +1019,9 @@ dump_handlers(thread_act_t thr_act)
 }
 
 void
-dump_regs(thread_act_t thr_act)
+dump_regs(thread_t thr_act)
 {
-       if (thr_act->mact.pcb) {
+       if (thr_act->machine.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",
@@ -1265,25 +1033,22 @@ dump_regs(thread_act_t thr_act)
 }
 
 int
-dump_act(thread_act_t thr_act)
+dump_act(thread_t thr_act)
 {
        if (!thr_act)
                return(0);
 
-       printf("thr_act(0x%x)(%d): thread=%x(%d) task=%x(%d)\n",
+       printf("thread(0x%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);
 
-       printf("\talerts=%x mask=%x susp=%d user_stop=%d active=%x ast=%x\n",
-                      thr_act->alerts, thr_act->alert_mask,
+       printf("\tsusp=%d user_stop=%d active=%x ast=%x\n",
                       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);
+       printf("\tpcb=%x\n", thr_act->machine.pcb);
 
-       if (thr_act->thread && thr_act->thread->kernel_stack) {
-           vm_offset_t stack = thr_act->thread->kernel_stack;
+       if (thr_act->kernel_stack) {
+           vm_offset_t stack = thr_act->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,
@@ -1294,32 +1059,26 @@ dump_act(thread_act_t thr_act)
        dump_regs(thr_act);
        return((int)thr_act);
 }
-unsigned int
-get_useraddr()
+
+user_addr_t
+get_useraddr(void)
 {
   
-        thread_act_t thr_act = current_act();
+        thread_t thr_act = current_thread();
  
-       if (thr_act->mact.pcb) 
-               return(thr_act->mact.pcb->iss.eip);
+       if (thr_act->machine.pcb) 
+               return(thr_act->machine.pcb->iss.eip);
        else 
                return(0);
 
 }
 
-void
-thread_swapin_mach_alloc(thread_t thread)
-{
-
-  /* 386 does not have saveareas */
-
-}
 /*
  * 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;
 
@@ -1338,26 +1097,25 @@ 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;
 
                KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_ATTACH),
                        thread, thread->priority,
-                       thread->sched_pri, continuation, 
-                       0);
+                       thread->sched_pri, 0, 0);
 
   assert(stack);
   statep = STACK_IKS(stack);
   thread->kernel_stack = stack;
 
   statep->k_eip = (unsigned long) Thread_continue;
-  statep->k_ebx = (unsigned long) start_pos;
+  statep->k_ebx = (unsigned long) thread_continue;
   statep->k_esp = (unsigned long) STACK_IEL(stack);
-  assert(thread->top_act);
-  STACK_IEL(stack)->saved_state = &thread->top_act->mact.pcb->iss;
+
+  STACK_IEL(stack)->saved_state = &thread->machine.pcb->iss;
 
   return;
 }
@@ -1367,34 +1125,29 @@ stack_attach(struct thread_shuttle *thread,
  */
 
 void
-stack_handoff(thread_t old,
+machine_stack_handoff(thread_t old,
              thread_t new)
 {
-
   vm_offset_t stack;
-  pmap_t new_pmap;
 
                KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_HANDOFF),
                        thread, thread->priority,
-                       thread->sched_pri, continuation, 
-                       0);
+                       thread->sched_pri, 0, 0);
 
-  assert(new->top_act);
-  assert(old->top_act);
+  assert(new);
+  assert(old);
 
-  stack = stack_detach(old);
-  stack_attach(new, stack, 0);
+  stack = machine_stack_detach(old);
+  machine_stack_attach(new, stack);
 
-  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());
+  PMAP_SWITCH_CONTEXT(old->task, new->task, cpu_number());
 
   KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_HANDOFF) | DBG_FUNC_NONE,
                     (int)old, (int)new, old->sched_pri, new->sched_pri, 0);
 
-  thread_machine_set_current(new);
+  machine_set_current_thread(new);
 
-  active_stacks[cpu_number()] = new->kernel_stack;
+  current_cpu_datap()->cpu_active_stack = new->kernel_stack;
 
   return;
 }
@@ -1417,15 +1170,21 @@ int val;
                                return((void *)0);
 
                val = i386_SAVED_STATE_COUNT; 
-               kret = act_machine_get_state(current_act(), i386_SAVED_STATE, &ic->ss, &val);
+               kret = machine_thread_get_state(current_thread(),
+                                               i386_SAVED_STATE,
+                                               (thread_state_t) &ic->ss,
+                                               &val);
                if (kret != KERN_SUCCESS) {
-                               kfree((vm_offset_t)ic,sizeof(struct i386_act_context));
+                               kfree(ic,sizeof(struct i386_act_context));
                                return((void *)0);
                }
                val = i386_FLOAT_STATE_COUNT; 
-               kret = act_machine_get_state(current_act(), i386_FLOAT_STATE, &ic->fs, &val);
+               kret = machine_thread_get_state(current_thread(),
+                                               i386_FLOAT_STATE,
+                                               (thread_state_t) &ic->fs,
+                                               &val);
                if (kret != KERN_SUCCESS) {
-                               kfree((vm_offset_t)ic,sizeof(struct i386_act_context));
+                               kfree(ic,sizeof(struct i386_act_context));
                                return((void *)0);
                }
                return(ic);
@@ -1435,26 +1194,31 @@ act_thread_catt(void *ctx)
 {
 struct i386_act_context *ic;
 kern_return_t kret;
-int val;
 
                ic = (struct i386_act_context *)ctx;
 
                if (ic == (struct i386_act_context *)NULL)
                                return;
 
-               kret = act_machine_set_state(current_act(), i386_SAVED_STATE, &ic->ss, i386_SAVED_STATE_COUNT);
+               kret = machine_thread_set_state(current_thread(),
+                                               i386_SAVED_STATE,
+                                               (thread_state_t) &ic->ss,
+                                               i386_SAVED_STATE_COUNT);
                if (kret != KERN_SUCCESS) 
                                goto out;
 
-               kret = act_machine_set_state(current_act(), i386_FLOAT_STATE, &ic->fs, i386_FLOAT_STATE_COUNT);
+               kret = machine_thread_set_state(current_thread(),
+                                               i386_FLOAT_STATE,
+                                               (thread_state_t) &ic->fs,
+                                               i386_FLOAT_STATE_COUNT);
                if (kret != KERN_SUCCESS)
                                goto out;
 out:
-       kfree((vm_offset_t)ic,sizeof(struct i386_act_context));         
+       kfree(ic,sizeof(struct i386_act_context));              
 }
 
 void act_thread_cfree(void *ctx)
 {
-       kfree((vm_offset_t)ctx,sizeof(struct i386_act_context));                
+       kfree(ctx,sizeof(struct i386_act_context));             
 }