]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/ppc/pcb.c
xnu-517.tar.gz
[apple/xnu.git] / osfmk / ppc / pcb.c
index 9150455d6e2e5a98190588fd5a5eda486cdd1446..85fbf336c034e99d0d8827b28c71641a38e95e8b 100644 (file)
@@ -3,19 +3,22 @@
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
@@ -76,6 +79,8 @@
 extern int             real_ncpus;                                             /* Number of actual CPUs */
 extern struct  Saveanchor saveanchor;                  /* Aliged savearea anchor */
 
+void   machine_act_terminate(thread_act_t      act);
+
 /*
  * These constants are dumb. They should not be in asm.h!
  */
@@ -89,17 +94,6 @@ int   vec_trap_count = 0;
 int   vec_switch_count = 0;
 #endif
 
-extern struct thread_shuttle   *Switch_context(
-                                       struct thread_shuttle   *old,
-                                       void                    (*cont)(void),
-                                       struct thread_shuttle   *new);
-
-
-#if    MACH_LDEBUG || MACH_KDB
-void           log_thread_action (char *, long, long, long);
-#endif
-
-
 /*
  * consider_machine_collect: try to collect machine-dependent pages
  */
@@ -118,71 +112,30 @@ consider_machine_adjust()
         consider_mapping_adjust();
 }
 
-
-/*
- * stack_attach: Attach a kernel stack to a thread.
- */
-void
-machine_kernel_stack_init(
-       struct thread_shuttle *thread,
-       void            (*start_pos)(thread_t))
-{
-    vm_offset_t        stack;
-    unsigned int                       *kss;
-       struct savearea                 *sv;
-
-    assert(thread->top_act->mact.pcb);
-    assert(thread->kernel_stack);
-    stack = thread->kernel_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);
-#endif /* MACH_ASSERT */
-       
-       kss = (unsigned int *)STACK_IKS(stack);
-       sv = thread->top_act->mact.pcb;                                         /* This for the sake of C */
-
-       sv->save_lr = (unsigned int) start_pos;                         /* Set up the execution address */
-       sv->save_srr0 = (unsigned int) start_pos;                       /* Here too */
-       sv->save_srr1  = MSR_SUPERVISOR_INT_OFF;                        /* Set the normal running MSR */
-       sv->save_r1 = (vm_offset_t) ((int)kss - KF_SIZE);       /* Point to the top frame on the stack */
-       sv->save_fpscr = 0;                                                                     /* Clear all floating point exceptions */
-       sv->save_vrsave = 0;                                                            /* Set the vector save state */
-       sv->save_vscr[3] = 0x00010000;                                          /* Supress java mode */
-
-       *((int *)sv->save_r1) = 0;                                                      /* Zero the frame backpointer */
-       thread->top_act->mact.ksp = 0;                                          /* Show that the kernel stack is in use already */
-
-}
-
 /*
  * switch_context: Switch from one thread to another, needed for
  *                switching of space
  * 
  */
-struct thread_shuttle*
-switch_context(
-       struct thread_shuttle *old,
-       void (*continuation)(void),
-       struct thread_shuttle *new)
+thread_t
+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;
-       register struct thread_shuttle* retval;
+       register thread_t retval;
        pmap_t  new_pmap;
        facility_context *fowner;
-       
-#if    MACH_LDEBUG || MACH_KDB
-       log_thread_action("switch", 
-                         (long)old, 
-                         (long)new, 
-                         (long)__builtin_return_address(0));
-#endif
+       struct per_proc_info *ppinfo;
+
+       if (old == new)
+               panic("machine_switch_context");
+
+       ppinfo = getPerProc();                                                          /* Get our processor block */
 
-       per_proc_info[cpu_number()].old_thread = (unsigned int)old;
-       per_proc_info[cpu_number()].cpu_flags &= ~traceBE;  /* disable branch tracing if on */
-       assert(old_act->kernel_loaded ||
-              active_stacks[cpu_number()] == old_act->thread->kernel_stack);
+       ppinfo->old_thread = (unsigned int)old;
+       ppinfo->cpu_flags &= ~traceBE;                                           /* disable branch tracing if on */
               
        check_simple_locks();
 
@@ -191,13 +144,13 @@ switch_context(
         * so that it can be found by the other if needed
         */
        if(real_ncpus > 1) {                                                            /* This is potentially slow, so only do when actually SMP */
-               fowner = per_proc_info[cpu_number()].FPU_owner; /* Cache this because it may change */
+               fowner = ppinfo->FPU_owner;                                             /* Cache this because it may change */
                if(fowner) {                                                                    /* Is there any live context? */
                        if(fowner->facAct == old->top_act) {            /* Is it for us? */
                                fpu_save(fowner);                                               /* Yes, save it */
                        }
                }
-               fowner = per_proc_info[cpu_number()].VMX_owner; /* Cache this because it may change */
+               fowner = ppinfo->VMX_owner;                                             /* Cache this because it may change */
                if(fowner) {                                                                    /* Is there any live context? */
                        if(fowner->facAct == old->top_act) {            /* Is it for us? */
                                vec_save(fowner);                                               /* Yes, save it */
@@ -205,12 +158,14 @@ switch_context(
                }
        }
 
-#if DEBUG
-       if (watchacts & WA_PCB) {
-               printf("switch_context(0x%08x, 0x%x, 0x%08x)\n",
-                      old,continuation,new);
+       /*
+        * If old thread is running VM, save per proc userProtKey and FamVMmode spcFlags bits in the thread spcFlags
+        * This bits can be modified in the per proc without updating the thread spcFlags
+        */
+       if(old_act->mact.specFlags & runningVM) {
+               old_act->mact.specFlags &=  ~(userProtKey|FamVMmode);
+               old_act->mact.specFlags |= (ppinfo->spcFlags) & (userProtKey|FamVMmode);
        }
-#endif /* DEBUG */
 
        /*
         * We do not have to worry about the PMAP module, so switch.
@@ -221,6 +176,9 @@ switch_context(
 
        if(new_act->mact.specFlags & runningVM) {                       /* Is the new guy running a VM? */
                pmap_switch(new_act->mact.vmmCEntry->vmmPmap);  /* Switch to the VM's pmap */
+               ppinfo->VMMareaPhys = new_act->mact.vmmCEntry->vmmContextPhys;
+               ppinfo->VMMXAFlgs = new_act->mact.vmmCEntry->vmmXAFlgs;
+               ppinfo->FAMintercept = new_act->mact.vmmCEntry->vmmFAMintercept;
        }
        else {                                                                                          /* otherwise, we use the task's pmap */
                new_pmap = new_act->task->map->pmap;
@@ -229,14 +187,22 @@ switch_context(
                }
        }
 
+       if(old_act->mact.cioSpace != invalSpace) {                      /* Does our old guy have an active copyin/out? */
+               old_act->mact.cioSpace |= cioSwitchAway;                /* Show we switched away from this guy */
+               hw_blow_seg(copyIOaddr);                                                /* Blow off the first segment */
+               hw_blow_seg(copyIOaddr + 0x10000000ULL);                /* Blow off the second segment */
+       }
+
        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->reason, (int)new, old->sched_pri, new->sched_pri, 0);
 
        retval = Switch_context(old, continuation, new);
        assert(retval != (struct thread_shuttle*)NULL);
 
-       if (branch_tracing_enabled())
-         per_proc_info[cpu_number()].cpu_flags |= traceBE;  /* restore branch tracing */
+       if (branch_tracing_enabled()) {
+               ppinfo = getPerProc();                                                  /* Get our processor block */
+               ppinfo->cpu_flags |= traceBE;                                   /* restore branch tracing */
+       }
 
        /* We've returned from having switched context, so we should be
         * back in the original context.
@@ -245,46 +211,20 @@ switch_context(
        return retval;
 }
 
-/*
- * 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(
-       struct thread_shuttle *thread,
-       kern_return_t   retval)
-{
-
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("thread_set_syscall_return(thr=%x,retval=%d)\n", thread,retval);
-#endif /* MACH_ASSERT */
-
-        thread->top_act->mact.pcb->save_r3 = retval;
-}
-
 /*
  * Initialize the machine-dependent state for a new thread.
  */
 kern_return_t
-thread_machine_create(
-                     struct thread_shuttle *thread,
-                     thread_act_t thr_act,
-                     void (*start_pos)(thread_t))
+machine_thread_create(
+       thread_t                thread,
+       task_t                  task)
 {
-
        savearea                *sv;                                                                    /* Pointer to newly allocated savearea */
        unsigned int    *CIsTooLimited, i;
 
-
-#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 */
-
-       hw_atomic_add(&saveanchor.savetarget, 4);                               /* Account for the number of saveareas we think we "need"
+       hw_atomic_add((uint32_t *)&saveanchor.savetarget, 4);   /* Account for the number of saveareas we think we "need"
                                                                                                                           for this activation */
-       assert(thr_act->mact.pcb == (savearea *)0);                             /* Make sure there was no previous savearea */
+       assert(thread->mact.pcb == (savearea *)0);                              /* Make sure there was no previous savearea */
        
        sv = save_alloc();                                                                              /* Go get us a savearea */
                
@@ -292,16 +232,13 @@ thread_machine_create(
                
        sv->save_hdr.save_prev = 0;                                                             /* Clear the back pointer */
        sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft);   /* Mark as in use */
-       sv->save_hdr.save_act = thr_act;                                                /* Set who owns it */
-       sv->save_vscr[3] = 0x00010000;                                                  /* Supress java mode */
-       thr_act->mact.pcb = sv;                                                                 /* Point to the save area */
-       thr_act->mact.curctx = &thr_act->mact.facctx;                   /* Initialize facility context */
-       thr_act->mact.facctx.facAct = thr_act;                                  /* Initialize facility context pointer to activation */
-
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("pcb_init(%x) pcb=%x\n", thr_act, sv);
-#endif /* MACH_ASSERT */
+       sv->save_hdr.save_act = (struct thread_activation *)thread;     /* Set who owns it */
+       thread->mact.pcb = sv;                                                                  /* Point to the save area */
+       thread->mact.curctx = &thread->mact.facctx;                             /* Initialize facility context */
+       thread->mact.facctx.facAct = thread;                                    /* Initialize facility context pointer to activation */
+       thread->mact.cioSpace = invalSpace;                                             /* Initialize copyin/out space to invalid */
+       thread->mact.preemption_count = 0;                                              /* Initialize preemption counter */
+
        /*
         * User threads will pull their context from the pcb when first
         * returning to user mode, so fill in all the necessary values.
@@ -309,13 +246,15 @@ thread_machine_create(
         * at the base of the kernel stack (see stack_attach()).
         */
 
-       sv->save_srr1 = MSR_EXPORT_MASK_SET;                                    /* Set the default user MSR */
+       thread->mact.upcb = sv;                                                                 /* Set user pcb */
+       sv->save_srr1 = (uint64_t)MSR_EXPORT_MASK_SET;                  /* Set the default user MSR */
+       sv->save_fpscr = 0;                                                                             /* Clear all floating point exceptions */
+       sv->save_vrsave = 0;                                                                    /* Set the vector save state */
+       sv->save_vscr[0] = 0x00000000;                                  
+       sv->save_vscr[1] = 0x00000000;                                  
+       sv->save_vscr[2] = 0x00000000;                                  
+       sv->save_vscr[3] = 0x00010000;                                                  /* Disable java mode and clear saturated */
        
-       CIsTooLimited = (unsigned int *)(&sv->save_sr0);                /* Make a pointer 'cause C can't cast on the left */
-       for(i=0; i<16; i++) {                                                                   /* Initialize all SRs */
-               CIsTooLimited[i] = SEG_REG_PROT | (i << 20) | thr_act->task->map->pmap->space;  /* Set the SR value */
-       }
-
     return(KERN_SUCCESS);
 }
 
@@ -323,24 +262,64 @@ thread_machine_create(
  * 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 savearea *pcb, *ppsv;
+       register savearea_vec *vsv, *vpsv;
+       register savearea_fpu *fsv, *fpsv;
+       register savearea *svp;
+       register int i;
+
+/*
+ *     This function will release all context.
+ */
+
+       machine_act_terminate(thread);                                                  /* Make sure all virtual machines are dead first */
+/*
+ *
+ *     Walk through and release all floating point and vector contexts. Also kill live context.
+ *
+ */
+       toss_live_vec(thread->mact.curctx);                                             /* Dump live vectors */
 
-       if (thread->kernel_stack) {
-               s = splsched();
-               stack_free(thread);
-               splx(s);
+       vsv = thread->mact.curctx->VMXsave;                                             /* Get the top vector savearea */
+       
+       while(vsv) {                                                                                    /* Any VMX saved state? */
+               vpsv = vsv;                                                                                     /* Remember so we can toss this */
+               vsv = CAST_DOWN(savearea_vec *, vsv->save_hdr.save_prev);  /* Get one underneath our's */
+               save_release((savearea *)vpsv);                                         /* Release it */
        }
-}
+       
+       thread->mact.curctx->VMXsave = 0;                                                       /* Kill chain */
+       toss_live_fpu(thread->mact.curctx);                                             /* Dump live float */
+
+       fsv = thread->mact.curctx->FPUsave;                                             /* Get the top float savearea */
+       
+       while(fsv) {                                                                                    /* Any float saved state? */
+               fpsv = fsv;                                                                                     /* Remember so we can toss this */
+               fsv = CAST_DOWN(savearea_fpu *, fsv->save_hdr.save_prev);   /* Get one underneath our's */
+               save_release((savearea *)fpsv);                                         /* Release it */
+       }
+       
+       thread->mact.curctx->FPUsave = 0;                                                       /* Kill chain */
 
 /*
- * flush out any lazily evaluated HW state in the
- * owning thread's context, before termination.
+ * free all regular saveareas.
  */
-void
-thread_machine_flush( thread_act_t cur_act )
-{
+
+       pcb = thread->mact.pcb;                                                                 /* Get the general savearea */
+       
+       while(pcb) {                                                                                    /* Any float saved state? */
+               ppsv = pcb;                                                                                     /* Remember so we can toss this */
+               pcb = CAST_DOWN(savearea *, pcb->save_hdr.save_prev);  /* Get one underneath our's */ 
+               save_release(ppsv);                                                                     /* Release it */
+       }
+       
+       hw_atomic_sub((uint32_t *)&saveanchor.savetarget, 4);   /* Unaccount for the number of saveareas we think we "need" */
 }
 
 /*
@@ -357,26 +336,28 @@ int switch_act_swapins = 0;
  */
 void
 machine_switch_act( 
-       thread_t        thread,
+       thread_t                thread,
        thread_act_t    old,
-       thread_act_t    new,
-       int                             cpu)
+       thread_act_t    new)
 {
        pmap_t          new_pmap;
        facility_context *fowner;
+       struct per_proc_info *ppinfo;
+       
+       ppinfo = getPerProc();                                                          /* Get our processor block */
 
        /* Our context might wake up on another processor, so we must
         * not keep hot state in our FPU, it must go back to the pcb
         * so that it can be found by the other if needed
         */
        if(real_ncpus > 1) {                                                            /* This is potentially slow, so only do when actually SMP */
-               fowner = per_proc_info[cpu_number()].FPU_owner; /* Cache this because it may change */
+               fowner = ppinfo->FPU_owner;                                             /* Cache this because it may change */
                if(fowner) {                                                                    /* Is there any live context? */
                        if(fowner->facAct == old) {                                     /* Is it for us? */
                                fpu_save(fowner);                                               /* Yes, save it */
                        }
                }
-               fowner = per_proc_info[cpu_number()].VMX_owner; /* Cache this because it may change */
+               fowner = ppinfo->VMX_owner;                                             /* Cache this because it may change */
                if(fowner) {                                                                    /* Is there any live context? */
                        if(fowner->facAct == old) {                                     /* Is it for us? */
                                vec_save(fowner);                                               /* Yes, save it */
@@ -384,9 +365,9 @@ machine_switch_act(
                }
        }
 
-       active_stacks[cpu] = thread->kernel_stack;
+       old->mact.cioSpace |= cioSwitchAway;                            /* Show we switched away from this guy */
 
-       ast_context(new, cpu);
+       ast_context(new, cpu_number());
 
        /* Activations might have different pmaps 
         * (process->kernel->server, for example).
@@ -405,14 +386,6 @@ machine_switch_act(
 
 }
 
-void
-pcb_user_to_kernel(thread_act_t act)
-{
-
-       return;                                                                                                 /* Not needed, I hope... */
-}
-
-
 /*
  * act_machine_sv_free
  * release saveareas associated with an act. if flag is true, release
@@ -424,8 +397,8 @@ void
 act_machine_sv_free(thread_act_t act)
 {
        register savearea *pcb, *userpcb;
-       register savearea_vec *vsv, *vpsv;
-       register savearea_fpu *fsv, *fpsv;
+       register savearea_vec *vsv, *vpst, *vsvt;
+       register savearea_fpu *fsv, *fpst, *fsvt;
        register savearea *svp;
        register int i;
 
@@ -437,40 +410,68 @@ act_machine_sv_free(thread_act_t act)
  *
  *     Walk through and release all floating point and vector contexts that are not
  *     user state.  We will also blow away live context if it belongs to non-user state.
+ *     Note that the level can not change while we are in this code.  Nor can another
+ *     context be pushed on the stack.
+ *
+ *     We do nothing here if the current level is user.  Otherwise,
+ *     the live context is cleared.  Then we find the user saved context.
+ *     Next,  we take the sync lock (to keep us from munging things in *_switch).
+ *     The level is set to 0 and all stacked context other than user is dequeued.
+ *     Then we unlock.  Next, all of the old kernel contexts are released.
  *
  */
  
        if(act->mact.curctx->VMXlevel) {                                                /* Is the current level user state? */
+               
                toss_live_vec(act->mact.curctx);                                        /* Dump live vectors if is not user */
-               act->mact.curctx->VMXlevel = 0;                                         /* Mark as user state */
-       }
 
-       vsv = act->mact.curctx->VMXsave;                                                /* Get the top vector savearea */
-       
-       while(vsv) {                                                                                    /* Any VMX saved state? */
-               vpsv = vsv;                                                                                     /* Remember so we can toss this */
-               if (!vsv->save_hdr.save_level) break;                           /* Done when hit user if any */
-               vsv = (savearea_vec *)vsv->save_hdr.save_prev;          /* Get one underneath our's */          
-               save_ret((savearea *)vpsv);                                                     /* Release it */
-       }
+               vsv = act->mact.curctx->VMXsave;                                        /* Get the top vector savearea */
+               
+               while(vsv && vsv->save_hdr.save_level) vsv = (savearea_vec *)vsv->save_hdr.save_prev;   /* Find user context if any */
        
-       act->mact.curctx->VMXsave = vsv;                                                /* Queue the user context to the top */
+               if(!hw_lock_to((hw_lock_t)&act->mact.curctx->VMXsync, LockTimeOut)) {   /* Get the sync lock */ 
+                       panic("act_machine_sv_free - timeout getting VMX sync lock\n"); /* Tell all and die */
+               }
+               
+               vsvt = act->mact.curctx->VMXsave;                                       /* Get the top of the chain */
+               act->mact.curctx->VMXsave = vsv;                                        /* Point to the user context */
+               act->mact.curctx->VMXlevel = 0;                                         /* Set the level to user */
+               hw_lock_unlock((hw_lock_t)&act->mact.curctx->VMXsync);  /* Unlock */
+               
+               while(vsvt) {                                                                           /* Clear any VMX saved state */
+                       if (vsvt == vsv) break;                                                 /* Done when hit user if any */
+                       vpst = vsvt;                                                                    /* Remember so we can toss this */
+                       vsvt = (savearea_vec *)vsvt->save_hdr.save_prev;        /* Get one underneath our's */          
+                       save_ret((savearea *)vpst);                                             /* Release it */
+               }
+               
+       }
  
        if(act->mact.curctx->FPUlevel) {                                                /* Is the current level user state? */
-               toss_live_fpu(act->mact.curctx);                                        /* Dump live float if is not user */
-               act->mact.curctx->FPUlevel = 0;                                         /* Mark as user state */
-       }
+               
+               toss_live_fpu(act->mact.curctx);                                        /* Dump live floats if is not user */
 
-       fsv = act->mact.curctx->FPUsave;                                                /* Get the top float savearea */
+               fsv = act->mact.curctx->FPUsave;                                        /* Get the top floats savearea */
+               
+               while(fsv && fsv->save_hdr.save_level) fsv = (savearea_fpu *)fsv->save_hdr.save_prev;   /* Find user context if any */
        
-       while(fsv) {                                                                                    /* Any float saved state? */
-               fpsv = fsv;                                                                                     /* Remember so we can toss this */
-               if (!fsv->save_hdr.save_level) break;                           /* Done when hit user if any */
-               fsv = (savearea_fpu *)fsv->save_hdr.save_prev;          /* Get one underneath our's */          
-               save_ret((savearea *)fpsv);                                                     /* Release it */
+               if(!hw_lock_to((hw_lock_t)&act->mact.curctx->FPUsync, LockTimeOut)) {   /* Get the sync lock */ 
+                       panic("act_machine_sv_free - timeout getting FPU sync lock\n"); /* Tell all and die */
+               }
+               
+               fsvt = act->mact.curctx->FPUsave;                                       /* Get the top of the chain */
+               act->mact.curctx->FPUsave = fsv;                                        /* Point to the user context */
+               act->mact.curctx->FPUlevel = 0;                                         /* Set the level to user */
+               hw_lock_unlock((hw_lock_t)&act->mact.curctx->FPUsync);  /* Unlock */
+               
+               while(fsvt) {                                                                           /* Clear any VMX saved state */
+                       if (fsvt == fsv) break;                                                 /* Done when hit user if any */
+                       fpst = fsvt;                                                                    /* Remember so we can toss this */
+                       fsvt = (savearea_fpu *)fsvt->save_hdr.save_prev;        /* Get one underneath our's */          
+                       save_ret((savearea *)fpst);                                             /* Release it */
+               }
+               
        }
-       
-       act->mact.curctx->FPUsave = fsv;                                                /* Queue the user context to the top */
 
 /*
  * free all regular saveareas except a user savearea, if any
@@ -485,7 +486,7 @@ act_machine_sv_free(thread_act_t act)
                        break;
                }
                svp = pcb;                                                                                      /* Remember this */
-               pcb = pcb->save_hdr.save_prev;                                          /* Get one underneath our's */          
+               pcb = CAST_DOWN(savearea *, pcb->save_hdr.save_prev);  /* Get one underneath our's */ 
                save_ret(svp);                                                                          /* Release it */
        }
        
@@ -493,13 +494,15 @@ act_machine_sv_free(thread_act_t act)
        
 }
 
+void
+machine_thread_set_current(thread_t    thread)
+{
+    set_machine_current_act(thread->top_act);
+}
 
-/*
- * act_virtual_machine_destroy:
- * Shutdown any virtual machines associated with a thread
- */
 void
-act_virtual_machine_destroy(thread_act_t act)
+machine_act_terminate(
+       thread_act_t    act)
 {
        if(act->mact.bbDescAddr) {                                                              /* Check if the Blue box assist is active */
                disable_bluebox_internal(act);                                          /* Kill off bluebox */
@@ -510,156 +513,14 @@ act_virtual_machine_destroy(thread_act_t act)
        }
 }
 
-/*
- * act_machine_destroy: Shutdown any state associated with a thread pcb.
- */
 void
-act_machine_destroy(thread_act_t act)
+machine_thread_terminate_self(void)
 {
-
-       register savearea *pcb, *ppsv;
-       register savearea_vec *vsv, *vpsv;
-       register savearea_fpu *fsv, *fpsv;
-       register savearea *svp;
-       register int i;
-
-#if    MACH_ASSERT
-       if (watchacts & WA_PCB)
-               printf("act_machine_destroy(0x%x)\n", act);
-#endif /* MACH_ASSERT */
-
-/*
- *     This function will release all context.
- */
-
-       act_virtual_machine_destroy(act);                                               /* Make sure all virtual machines are dead first */
-/*
- *
- *     Walk through and release all floating point and vector contexts. Also kill live context.
- *
- */
-       toss_live_vec(act->mact.curctx);                                                /* Dump live vectors */
-
-       vsv = act->mact.curctx->VMXsave;                                                /* Get the top vector savearea */
-       
-       while(vsv) {                                                                                    /* Any VMX saved state? */
-               vpsv = vsv;                                                                                     /* Remember so we can toss this */
-               vsv = (savearea_vec *)vsv->save_hdr.save_prev;          /* Get one underneath our's */          
-               save_release((savearea *)vpsv);                                         /* Release it */
-       }
-       
-       act->mact.curctx->VMXsave = 0;                                                  /* Kill chain */
-       toss_live_fpu(act->mact.curctx);                                                /* Dump live float */
-
-       fsv = act->mact.curctx->FPUsave;                                                /* Get the top float savearea */
-       
-       while(fsv) {                                                                                    /* Any float saved state? */
-               fpsv = fsv;                                                                                     /* Remember so we can toss this */
-               fsv = (savearea_fpu *)fsv->save_hdr.save_prev;          /* Get one underneath our's */          
-               save_release((savearea *)fpsv);                                         /* Release it */
-       }
-       
-       act->mact.curctx->FPUsave = 0;                                                  /* Kill chain */
-
-/*
- * free all regular saveareas.
- */
-
-       pcb = act->mact.pcb;                                                                    /* Get the general savearea */
-       
-       while(pcb) {                                                                                    /* Any float saved state? */
-               ppsv = pcb;                                                                                     /* Remember so we can toss this */
-               pcb = pcb->save_hdr.save_prev;                                          /* Get one underneath our's */          
-               save_release(ppsv);                                                                     /* Release it */
-       }
-       
-       hw_atomic_sub(&saveanchor.savetarget, 4);                               /* Unaccount for the number of saveareas we think we "need" */
-
-}
-
-
-kern_return_t
-act_machine_create(task_t task, thread_act_t thr_act)
-{
-       /*
-        * Clear & Init the pcb  (sets up user-mode s regs)
-        * We don't use this anymore.
-        */
-
-       return KERN_SUCCESS;
-}
-
-void act_machine_init()
-{
-#if    MACH_ASSERT
-    if (watchacts & WA_PCB)
-       printf("act_machine_init()\n");
-#endif /* MACH_ASSERT */
-
-    /* Good to verify these once */
-    assert( THREAD_MACHINE_STATE_MAX <= THREAD_STATE_MAX );
-
-    assert( THREAD_STATE_MAX >= PPC_THREAD_STATE_COUNT );
-    assert( THREAD_STATE_MAX >= PPC_EXCEPTION_STATE_COUNT );
-    assert( THREAD_STATE_MAX >= PPC_FLOAT_STATE_COUNT );
-
-    /*
-     * If we start using kernel activations,
-     * would normally create kernel_thread_pool here,
-     * populating it from the act_zone
-     */
+       machine_act_terminate(current_act());
 }
 
 void
-act_machine_return(int code)
-{
-    thread_act_t thr_act = current_act();
-
-#if    MACH_ASSERT
-    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.
-        *
-        * 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 );
-       assert(thr_act->thread->top_act == thr_act);
-
-       /* This is the only activation attached to the shuttle... */
-
-       thread_terminate_self();
-
-       /*NOTREACHED*/
-       panic("act_machine_return: TALKING ZOMBIE! (1)");
-}
-
-void
-thread_machine_set_current(struct thread_shuttle *thread)
-{
-    register int       my_cpu = cpu_number();
-
-    set_machine_current_thread(thread);
-    set_machine_current_act(thread->top_act);
-       
-    active_kloaded[my_cpu] = thread->top_act->kernel_loaded ? thread->top_act : THR_ACT_NULL;
-}
-
-void
-thread_machine_init(void)
+machine_thread_init(void)
 {
 #ifdef MACHINE_STACK
 #if KERNEL_STACK_SIZE > PPC_PGBYTES
@@ -687,8 +548,8 @@ int
           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=%x active=%x hi=%x lo=%x\n",
-          thr_act->alerts, thr_act->alert_mask,
+    printf("\tsusp=%x active=%x hi=%x lo=%x\n",
+          0 /*thr_act->alerts*/, 0 /*thr_act->alert_mask*/,
           thr_act->suspend_count, thr_act->active,
           thr_act->higher, thr_act->lower);
 
@@ -700,10 +561,7 @@ int
 unsigned int 
 get_useraddr()
 {
-
-       thread_act_t thr_act = current_act();
-
-       return(thr_act->mact.pcb->save_srr0);
+       return(current_act()->mact.upcb->save_srr0);
 }
 
 /*
@@ -711,7 +569,8 @@ get_useraddr()
  */
 
 vm_offset_t
-stack_detach(thread_t thread)
+machine_stack_detach(
+       thread_t                thread)
 {
   vm_offset_t stack;
 
@@ -740,9 +599,10 @@ 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,
+       void                     (*start)(thread_t))
 {
   thread_act_t thr_act;
   unsigned int *kss;
@@ -750,7 +610,7 @@ stack_attach(struct thread_shuttle *thread,
 
         KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_ATTACH),
             thread, thread->priority,
-            thread->sched_pri, start_pos,
+            thread->sched_pri, start,
             0);
 
   assert(stack);
@@ -762,18 +622,18 @@ stack_attach(struct thread_shuttle *thread,
   if ((thr_act = thread->top_act) != 0) {
     sv = save_get();  /* cannot block */
        sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft);   /* Mark as in use */
-    sv->save_hdr.save_act = thr_act;
-       sv->save_hdr.save_prev = thr_act->mact.pcb;
+    sv->save_hdr.save_act = (struct thread_activation *)thr_act;
+       sv->save_hdr.save_prev = (addr64_t)((uintptr_t)thr_act->mact.pcb);
     thr_act->mact.pcb = sv;
 
-    sv->save_srr0 = (unsigned int) start_pos;
+    sv->save_srr0 = (unsigned int) start;
     /* sv->save_r3 = ARG ? */
     sv->save_r1 = (vm_offset_t)((int)kss - KF_SIZE);
        sv->save_srr1 = MSR_SUPERVISOR_INT_OFF;
        sv->save_fpscr = 0;                                                                     /* Clear all floating point exceptions */
        sv->save_vrsave = 0;                                                            /* Set the vector save state */
        sv->save_vscr[3] = 0x00010000;                                          /* Supress java mode */
-    *((int *)sv->save_r1) = 0;
+    *(CAST_DOWN(int *, sv->save_r1)) = 0;
     thr_act->mact.ksp = 0;                           
   }
 
@@ -785,35 +645,43 @@ 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;
        pmap_t new_pmap;
        facility_context *fowner;
+       mapping *mp;
+       struct per_proc_info *ppinfo;
        
        assert(new->top_act);
        assert(old->top_act);
+
+       if (old == new)
+               panic("machine_stack_handoff");
        
-       stack = stack_detach(old);
+       stack = machine_stack_detach(old);
        new->kernel_stack = stack;
-       if (stack == old->stack_privilege) {
-               assert(new->stack_privilege);
-               old->stack_privilege = new->stack_privilege;
-               new->stack_privilege = stack;
+       if (stack == old->reserved_stack) {
+               assert(new->reserved_stack);
+               old->reserved_stack = new->reserved_stack;
+               new->reserved_stack = stack;
        }
 
-       per_proc_info[cpu_number()].cpu_flags &= ~traceBE;
+       ppinfo = getPerProc();                                                          /* Get our processor block */
+
+       ppinfo->cpu_flags &= ~traceBE;                                          /* Turn off special branch trace */
 
        if(real_ncpus > 1) {                                                            /* This is potentially slow, so only do when actually SMP */
-               fowner = per_proc_info[cpu_number()].FPU_owner; /* Cache this because it may change */
+               fowner = ppinfo->FPU_owner;                                             /* Cache this because it may change */
                if(fowner) {                                                                    /* Is there any live context? */
                        if(fowner->facAct == old->top_act) {            /* Is it for us? */
                                fpu_save(fowner);                                               /* Yes, save it */
                        }
                }
-               fowner = per_proc_info[cpu_number()].VMX_owner; /* Cache this because it may change */
+               fowner = ppinfo->VMX_owner;                                             /* Cache this because it may change */
                if(fowner) {                                                                    /* Is there any live context? */
                        if(fowner->facAct == old->top_act) {            /* Is it for us? */
                                vec_save(fowner);                                               /* Yes, save it */
@@ -821,12 +689,24 @@ stack_handoff(thread_t old,
                }
        }
 
+       /*
+        * If old thread is running VM, save per proc userProtKey and FamVMmode spcFlags bits in the thread spcFlags
+        * This bits can be modified in the per proc without updating the thread spcFlags
+        */
+       if(old->top_act->mact.specFlags & runningVM) {                  /* Is the current thread running a VM? */
+               old->top_act->mact.specFlags &= ~(userProtKey|FamVMmode);
+               old->top_act->mact.specFlags |= (ppinfo->spcFlags) & (userProtKey|FamVMmode);
+       }
+
        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);
+                    old->reason, (int)new, old->sched_pri, new->sched_pri, 0);
 
 
        if(new->top_act->mact.specFlags & runningVM) {  /* Is the new guy running a VM? */
                pmap_switch(new->top_act->mact.vmmCEntry->vmmPmap);     /* Switch to the VM's pmap */
+               ppinfo->VMMareaPhys = new->top_act->mact.vmmCEntry->vmmContextPhys;
+               ppinfo->VMMXAFlgs = new->top_act->mact.vmmCEntry->vmmXAFlgs;
+               ppinfo->FAMintercept = new->top_act->mact.vmmCEntry->vmmFAMintercept;
        }
        else {                                                                                  /* otherwise, we use the task's pmap */
                new_pmap = new->top_act->task->map->pmap;
@@ -835,17 +715,20 @@ stack_handoff(thread_t old,
                }
        }
 
-       thread_machine_set_current(new);
-       active_stacks[cpu_number()] = new->kernel_stack;
-       per_proc_info[cpu_number()].Uassist = new->top_act->mact.cthread_self;
+       machine_thread_set_current(new);
+       ppinfo->Uassist = new->top_act->mact.cthread_self;
 
-       per_proc_info[cpu_number()].ppbbTaskEnv = new->top_act->mact.bbTaskEnv;
-       per_proc_info[cpu_number()].spcFlags = new->top_act->mact.specFlags;
+       ppinfo->ppbbTaskEnv = new->top_act->mact.bbTaskEnv;
+       ppinfo->spcFlags = new->top_act->mact.specFlags;
+       
+       old->top_act->mact.cioSpace |= cioSwitchAway;   /* Show we switched away from this guy */
+       mp = (mapping *)&ppinfo->ppCIOmp;
+       mp->mpSpace = invalSpace;                                               /* Since we can't handoff in the middle of copy in/out, just invalidate */
 
        if (branch_tracing_enabled()) 
-               per_proc_info[cpu_number()].cpu_flags |= traceBE;
+               ppinfo->cpu_flags |= traceBE;
     
-       if(trcWork.traceMask) dbgTrace(0x12345678, (unsigned int)old->top_act, (unsigned int)new->top_act);     /* Cut trace entry if tracing */    
+       if(trcWork.traceMask) dbgTrace(0x12345678, (unsigned int)old->top_act, (unsigned int)new->top_act, 0);  /* Cut trace entry if tracing */    
     
   return;
 }
@@ -874,19 +757,3 @@ call_continuation(void (*continuation)(void) )
   
   return;
 }
-
-void
-thread_swapin_mach_alloc(thread_t thread)
-{
-    struct savearea *sv;
-
-       assert(thread->top_act->mact.pcb == 0);
-
-    sv = save_alloc();
-       assert(sv);
-    sv->save_hdr.save_prev = 0;                                                /* Initialize back chain */
-       sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft);   /* Mark as in use */
-    sv->save_hdr.save_act = thread->top_act;           /* Initialize owner */
-    thread->top_act->mact.pcb = sv;
-
-}