2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990,1991,1992 The University of Utah and
27 * the Center for Software Science (CSS). All rights reserved.
29 * Permission to use, copy, modify and distribute this software is hereby
30 * granted provided that (1) source code retains these copyright, permission,
31 * and disclaimer notices, and (2) redistributions including binaries
32 * reproduce the notices in supporting documentation, and (3) all advertising
33 * materials mentioning features or use of this software display the following
34 * acknowledgement: ``This product includes software developed by the Center
35 * for Software Science at the University of Utah.''
37 * THE UNIVERSITY OF UTAH AND CSS ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
38 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSS DISCLAIM ANY LIABILITY OF
39 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * CSS requests users of this software to return to css-dist@cs.utah.edu any
42 * improvements that they make and grant CSS redistribution rights.
44 * Utah $Hdr: pcb.c 1.23 92/06/27$
51 #include <kern/task.h>
52 #include <kern/thread.h>
53 #include <kern/thread_act.h>
54 #include <kern/thread_swap.h>
55 #include <mach/thread_status.h>
56 #include <vm/vm_kern.h>
57 #include <kern/mach_param.h>
59 #include <kern/misc_protos.h>
60 #include <ppc/misc_protos.h>
61 #include <ppc/exception.h>
62 #include <ppc/proc_reg.h>
66 #include <ppc/mappings.h>
67 #include <ppc/savearea.h>
68 #include <ppc/Firmware.h>
70 #include <ppc/thread_act.h>
71 #include <ppc/vmachmon.h>
72 #include <ppc/low_trace.h>
74 #include <sys/kdebug.h>
76 extern int real_ncpus
; /* Number of actual CPUs */
77 extern struct Saveanchor saveanchor
; /* Aliged savearea anchor */
80 * These constants are dumb. They should not be in asm.h!
83 #define KF_SIZE (FM_SIZE+ARG_SIZE+FM_REDZONE)
86 int fpu_trap_count
= 0;
87 int fpu_switch_count
= 0;
88 int vec_trap_count
= 0;
89 int vec_switch_count
= 0;
92 extern struct thread_shuttle
*Switch_context(
93 struct thread_shuttle
*old
,
95 struct thread_shuttle
*new);
98 #if MACH_LDEBUG || MACH_KDB
99 void log_thread_action (char *, long, long, long);
104 * consider_machine_collect: try to collect machine-dependent pages
107 consider_machine_collect()
110 * none currently available
116 consider_machine_adjust()
118 consider_mapping_adjust();
123 * stack_attach: Attach a kernel stack to a thread.
126 machine_kernel_stack_init(
127 struct thread_shuttle
*thread
,
128 void (*start_pos
)(thread_t
))
134 assert(thread
->top_act
->mact
.pcb
);
135 assert(thread
->kernel_stack
);
136 stack
= thread
->kernel_stack
;
139 if (watchacts
& WA_PCB
)
140 printf("machine_kernel_stack_init(thr=%x,stk=%x,start_pos=%x)\n", thread
,stack
,start_pos
);
141 #endif /* MACH_ASSERT */
143 kss
= (unsigned int *)STACK_IKS(stack
);
144 sv
= thread
->top_act
->mact
.pcb
; /* This for the sake of C */
146 sv
->save_lr
= (unsigned int) start_pos
; /* Set up the execution address */
147 sv
->save_srr0
= (unsigned int) start_pos
; /* Here too */
148 sv
->save_srr1
= MSR_SUPERVISOR_INT_OFF
; /* Set the normal running MSR */
149 sv
->save_r1
= (vm_offset_t
) ((int)kss
- KF_SIZE
); /* Point to the top frame on the stack */
150 sv
->save_fpscr
= 0; /* Clear all floating point exceptions */
151 sv
->save_vrsave
= 0; /* Set the vector save state */
152 sv
->save_vscr
[3] = 0x00010000; /* Supress java mode */
154 *((int *)sv
->save_r1
) = 0; /* Zero the frame backpointer */
155 thread
->top_act
->mact
.ksp
= 0; /* Show that the kernel stack is in use already */
160 * switch_context: Switch from one thread to another, needed for
164 struct thread_shuttle
*
166 struct thread_shuttle
*old
,
167 void (*continuation
)(void),
168 struct thread_shuttle
*new)
170 register thread_act_t old_act
= old
->top_act
, new_act
= new->top_act
;
171 register struct thread_shuttle
* retval
;
173 facility_context
*fowner
;
175 #if MACH_LDEBUG || MACH_KDB
176 log_thread_action("switch",
179 (long)__builtin_return_address(0));
182 per_proc_info
[cpu_number()].old_thread
= (unsigned int)old
;
183 per_proc_info
[cpu_number()].cpu_flags
&= ~traceBE
; /* disable branch tracing if on */
184 assert(old_act
->kernel_loaded
||
185 active_stacks
[cpu_number()] == old_act
->thread
->kernel_stack
);
187 check_simple_locks();
189 /* Our context might wake up on another processor, so we must
190 * not keep hot state in our FPU, it must go back to the pcb
191 * so that it can be found by the other if needed
193 if(real_ncpus
> 1) { /* This is potentially slow, so only do when actually SMP */
194 fowner
= per_proc_info
[cpu_number()].FPU_owner
; /* Cache this because it may change */
195 if(fowner
) { /* Is there any live context? */
196 if(fowner
->facAct
== old
->top_act
) { /* Is it for us? */
197 fpu_save(fowner
); /* Yes, save it */
200 fowner
= per_proc_info
[cpu_number()].VMX_owner
; /* Cache this because it may change */
201 if(fowner
) { /* Is there any live context? */
202 if(fowner
->facAct
== old
->top_act
) { /* Is it for us? */
203 vec_save(fowner
); /* Yes, save it */
209 if (watchacts
& WA_PCB
) {
210 printf("switch_context(0x%08x, 0x%x, 0x%08x)\n",
211 old
,continuation
,new);
216 * We do not have to worry about the PMAP module, so switch.
218 * We must not use top_act->map since this may not be the actual
219 * task map, but the map being used for a klcopyin/out.
222 if(new_act
->mact
.specFlags
& runningVM
) { /* Is the new guy running a VM? */
223 pmap_switch(new_act
->mact
.vmmCEntry
->vmmPmap
); /* Switch to the VM's pmap */
225 else { /* otherwise, we use the task's pmap */
226 new_pmap
= new_act
->task
->map
->pmap
;
227 if ((old_act
->task
->map
->pmap
!= new_pmap
) || (old_act
->mact
.specFlags
& runningVM
)) {
228 pmap_switch(new_pmap
); /* Switch if there is a change */
232 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_SCHED
) | DBG_FUNC_NONE
,
233 (int)old
, (int)new, old
->sched_pri
, new->sched_pri
, 0);
235 retval
= Switch_context(old
, continuation
, new);
236 assert(retval
!= (struct thread_shuttle
*)NULL
);
238 if (branch_tracing_enabled())
239 per_proc_info
[cpu_number()].cpu_flags
|= traceBE
; /* restore branch tracing */
241 /* We've returned from having switched context, so we should be
242 * back in the original context.
249 * Alter the thread's state so that a following thread_exception_return
250 * will make the thread return 'retval' from a syscall.
253 thread_set_syscall_return(
254 struct thread_shuttle
*thread
,
255 kern_return_t retval
)
259 if (watchacts
& WA_PCB
)
260 printf("thread_set_syscall_return(thr=%x,retval=%d)\n", thread
,retval
);
261 #endif /* MACH_ASSERT */
263 thread
->top_act
->mact
.pcb
->save_r3
= retval
;
267 * Initialize the machine-dependent state for a new thread.
270 thread_machine_create(
271 struct thread_shuttle
*thread
,
272 thread_act_t thr_act
,
273 void (*start_pos
)(thread_t
))
276 savearea
*sv
; /* Pointer to newly allocated savearea */
277 unsigned int *CIsTooLimited
, i
;
281 if (watchacts
& WA_PCB
)
282 printf("thread_machine_create(thr=%x,thr_act=%x,st=%x)\n", thread
, thr_act
, start_pos
);
283 #endif /* MACH_ASSERT */
285 hw_atomic_add(&saveanchor
.savetarget
, 4); /* Account for the number of saveareas we think we "need"
286 for this activation */
287 assert(thr_act
->mact
.pcb
== (savearea
*)0); /* Make sure there was no previous savearea */
289 sv
= save_alloc(); /* Go get us a savearea */
291 bzero((char *)((unsigned int)sv
+ sizeof(savearea_comm
)), (sizeof(savearea
) - sizeof(savearea_comm
))); /* Clear it */
293 sv
->save_hdr
.save_prev
= 0; /* Clear the back pointer */
294 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use */
295 sv
->save_hdr
.save_act
= thr_act
; /* Set who owns it */
296 sv
->save_vscr
[3] = 0x00010000; /* Supress java mode */
297 thr_act
->mact
.pcb
= sv
; /* Point to the save area */
298 thr_act
->mact
.curctx
= &thr_act
->mact
.facctx
; /* Initialize facility context */
299 thr_act
->mact
.facctx
.facAct
= thr_act
; /* Initialize facility context pointer to activation */
302 if (watchacts
& WA_PCB
)
303 printf("pcb_init(%x) pcb=%x\n", thr_act
, sv
);
304 #endif /* MACH_ASSERT */
306 * User threads will pull their context from the pcb when first
307 * returning to user mode, so fill in all the necessary values.
308 * Kernel threads are initialized from the save state structure
309 * at the base of the kernel stack (see stack_attach()).
312 sv
->save_srr1
= MSR_EXPORT_MASK_SET
; /* Set the default user MSR */
314 CIsTooLimited
= (unsigned int *)(&sv
->save_sr0
); /* Make a pointer 'cause C can't cast on the left */
315 for(i
=0; i
<16; i
++) { /* Initialize all SRs */
316 CIsTooLimited
[i
] = SEG_REG_PROT
| (i
<< 20) | thr_act
->task
->map
->pmap
->space
; /* Set the SR value */
319 return(KERN_SUCCESS
);
323 * Machine-dependent cleanup prior to destroying a thread
326 thread_machine_destroy( thread_t thread
)
330 if (thread
->kernel_stack
) {
338 * flush out any lazily evaluated HW state in the
339 * owning thread's context, before termination.
342 thread_machine_flush( thread_act_t cur_act
)
347 * Number of times we needed to swap an activation back in before
350 int switch_act_swapins
= 0;
355 * Machine-dependent details of activation switching. Called with
356 * RPC locks held and preemption disabled.
366 facility_context
*fowner
;
368 /* Our context might wake up on another processor, so we must
369 * not keep hot state in our FPU, it must go back to the pcb
370 * so that it can be found by the other if needed
372 if(real_ncpus
> 1) { /* This is potentially slow, so only do when actually SMP */
373 fowner
= per_proc_info
[cpu_number()].FPU_owner
; /* Cache this because it may change */
374 if(fowner
) { /* Is there any live context? */
375 if(fowner
->facAct
== old
) { /* Is it for us? */
376 fpu_save(fowner
); /* Yes, save it */
379 fowner
= per_proc_info
[cpu_number()].VMX_owner
; /* Cache this because it may change */
380 if(fowner
) { /* Is there any live context? */
381 if(fowner
->facAct
== old
) { /* Is it for us? */
382 vec_save(fowner
); /* Yes, save it */
387 active_stacks
[cpu
] = thread
->kernel_stack
;
389 ast_context(new, cpu
);
391 /* Activations might have different pmaps
392 * (process->kernel->server, for example).
393 * Change space if needed
396 if(new->mact
.specFlags
& runningVM
) { /* Is the new guy running a VM? */
397 pmap_switch(new->mact
.vmmCEntry
->vmmPmap
); /* Switch to the VM's pmap */
399 else { /* otherwise, we use the task's pmap */
400 new_pmap
= new->task
->map
->pmap
;
401 if ((old
->task
->map
->pmap
!= new_pmap
) || (old
->mact
.specFlags
& runningVM
)) {
402 pmap_switch(new_pmap
);
409 pcb_user_to_kernel(thread_act_t act
)
412 return; /* Not needed, I hope... */
417 * act_machine_sv_free
418 * release saveareas associated with an act. if flag is true, release
419 * user level savearea(s) too, else don't
421 * this code cannot block so we call the proper save area free routine
424 act_machine_sv_free(thread_act_t act
)
426 register savearea
*pcb
, *userpcb
;
427 register savearea_vec
*vsv
, *vpsv
;
428 register savearea_fpu
*fsv
, *fpsv
;
429 register savearea
*svp
;
433 * This function will release all non-user state context.
438 * Walk through and release all floating point and vector contexts that are not
439 * user state. We will also blow away live context if it belongs to non-user state.
443 if(act
->mact
.curctx
->VMXlevel
) { /* Is the current level user state? */
444 toss_live_vec(act
->mact
.curctx
); /* Dump live vectors if is not user */
445 act
->mact
.curctx
->VMXlevel
= 0; /* Mark as user state */
448 vsv
= act
->mact
.curctx
->VMXsave
; /* Get the top vector savearea */
450 while(vsv
) { /* Any VMX saved state? */
451 vpsv
= vsv
; /* Remember so we can toss this */
452 if (!vsv
->save_hdr
.save_level
) break; /* Done when hit user if any */
453 vsv
= (savearea_vec
*)vsv
->save_hdr
.save_prev
; /* Get one underneath our's */
454 save_ret((savearea
*)vpsv
); /* Release it */
457 act
->mact
.curctx
->VMXsave
= vsv
; /* Queue the user context to the top */
459 if(act
->mact
.curctx
->FPUlevel
) { /* Is the current level user state? */
460 toss_live_fpu(act
->mact
.curctx
); /* Dump live float if is not user */
461 act
->mact
.curctx
->FPUlevel
= 0; /* Mark as user state */
464 fsv
= act
->mact
.curctx
->FPUsave
; /* Get the top float savearea */
466 while(fsv
) { /* Any float saved state? */
467 fpsv
= fsv
; /* Remember so we can toss this */
468 if (!fsv
->save_hdr
.save_level
) break; /* Done when hit user if any */
469 fsv
= (savearea_fpu
*)fsv
->save_hdr
.save_prev
; /* Get one underneath our's */
470 save_ret((savearea
*)fpsv
); /* Release it */
473 act
->mact
.curctx
->FPUsave
= fsv
; /* Queue the user context to the top */
476 * free all regular saveareas except a user savearea, if any
479 pcb
= act
->mact
.pcb
; /* Get the general savearea */
480 userpcb
= 0; /* Assume no user context for now */
482 while(pcb
) { /* Any float saved state? */
483 if (pcb
->save_srr1
& MASK(MSR_PR
)) { /* Is this a user savearea? */
484 userpcb
= pcb
; /* Remember so we can toss this */
487 svp
= pcb
; /* Remember this */
488 pcb
= pcb
->save_hdr
.save_prev
; /* Get one underneath our's */
489 save_ret(svp
); /* Release it */
492 act
->mact
.pcb
= userpcb
; /* Chain in the user if there is one, or 0 if not */
498 * act_virtual_machine_destroy:
499 * Shutdown any virtual machines associated with a thread
502 act_virtual_machine_destroy(thread_act_t act
)
504 if(act
->mact
.bbDescAddr
) { /* Check if the Blue box assist is active */
505 disable_bluebox_internal(act
); /* Kill off bluebox */
508 if(act
->mact
.vmmControl
) { /* Check if VMM is active */
509 vmm_tear_down_all(act
); /* Kill off all VMM contexts */
514 * act_machine_destroy: Shutdown any state associated with a thread pcb.
517 act_machine_destroy(thread_act_t act
)
520 register savearea
*pcb
, *ppsv
;
521 register savearea_vec
*vsv
, *vpsv
;
522 register savearea_fpu
*fsv
, *fpsv
;
523 register savearea
*svp
;
527 if (watchacts
& WA_PCB
)
528 printf("act_machine_destroy(0x%x)\n", act
);
529 #endif /* MACH_ASSERT */
532 * This function will release all context.
535 act_virtual_machine_destroy(act
); /* Make sure all virtual machines are dead first */
539 * Walk through and release all floating point and vector contexts. Also kill live context.
543 toss_live_vec(act
->mact
.curctx
); /* Dump live vectors */
545 vsv
= act
->mact
.curctx
->VMXsave
; /* Get the top vector savearea */
547 while(vsv
) { /* Any VMX saved state? */
548 vpsv
= vsv
; /* Remember so we can toss this */
549 vsv
= (savearea_vec
*)vsv
->save_hdr
.save_prev
; /* Get one underneath our's */
550 save_release((savearea
*)vpsv
); /* Release it */
553 act
->mact
.curctx
->VMXsave
= 0; /* Kill chain */
555 toss_live_fpu(act
->mact
.curctx
); /* Dump live float */
557 fsv
= act
->mact
.curctx
->FPUsave
; /* Get the top float savearea */
559 while(fsv
) { /* Any float saved state? */
560 fpsv
= fsv
; /* Remember so we can toss this */
561 fsv
= (savearea_fpu
*)fsv
->save_hdr
.save_prev
; /* Get one underneath our's */
562 save_release((savearea
*)fpsv
); /* Release it */
565 act
->mact
.curctx
->FPUsave
= 0; /* Kill chain */
568 * free all regular saveareas.
571 pcb
= act
->mact
.pcb
; /* Get the general savearea */
573 while(pcb
) { /* Any float saved state? */
574 ppsv
= pcb
; /* Remember so we can toss this */
575 pcb
= pcb
->save_hdr
.save_prev
; /* Get one underneath our's */
576 save_release(ppsv
); /* Release it */
579 hw_atomic_sub(&saveanchor
.savetarget
, 4); /* Unaccount for the number of saveareas we think we "need" */
585 act_machine_create(task_t task
, thread_act_t thr_act
)
588 * Clear & Init the pcb (sets up user-mode s regs)
589 * We don't use this anymore.
595 void act_machine_init()
598 if (watchacts
& WA_PCB
)
599 printf("act_machine_init()\n");
600 #endif /* MACH_ASSERT */
602 /* Good to verify these once */
603 assert( THREAD_MACHINE_STATE_MAX
<= THREAD_STATE_MAX
);
605 assert( THREAD_STATE_MAX
>= PPC_THREAD_STATE_COUNT
);
606 assert( THREAD_STATE_MAX
>= PPC_EXCEPTION_STATE_COUNT
);
607 assert( THREAD_STATE_MAX
>= PPC_FLOAT_STATE_COUNT
);
610 * If we start using kernel activations,
611 * would normally create kernel_thread_pool here,
612 * populating it from the act_zone
617 act_machine_return(int code
)
619 thread_act_t thr_act
= current_act();
622 if (watchacts
& WA_EXIT
)
623 printf("act_machine_return(0x%x) cur_act=%x(%d) thr=%x(%d)\n",
624 code
, thr_act
, thr_act
->ref_count
,
625 thr_act
->thread
, thr_act
->thread
->ref_count
);
626 #endif /* MACH_ASSERT */
630 * This code is called with nothing locked.
631 * It also returns with nothing locked, if it returns.
633 * This routine terminates the current thread activation.
634 * If this is the only activation associated with its
635 * thread shuttle, then the entire thread (shuttle plus
636 * activation) is terminated.
638 assert( code
== KERN_TERMINATED
);
640 assert(thr_act
->thread
->top_act
== thr_act
);
642 /* This is the only activation attached to the shuttle... */
644 thread_terminate_self();
647 panic("act_machine_return: TALKING ZOMBIE! (1)");
651 thread_machine_set_current(struct thread_shuttle
*thread
)
653 register int my_cpu
= cpu_number();
655 set_machine_current_thread(thread
);
656 set_machine_current_act(thread
->top_act
);
658 active_kloaded
[my_cpu
] = thread
->top_act
->kernel_loaded
? thread
->top_act
: THR_ACT_NULL
;
662 thread_machine_init(void)
665 #if KERNEL_STACK_SIZE > PPC_PGBYTES
666 panic("KERNEL_STACK_SIZE can't be greater than PPC_PGBYTES\n");
674 dump_thread(thread_t th
)
676 printf(" thread @ 0x%x:\n", th
);
680 dump_act(thread_act_t thr_act
)
685 printf("thr_act(0x%x)(%d): thread=%x(%d) task=%x(%d)\n",
686 thr_act
, thr_act
->ref_count
,
687 thr_act
->thread
, thr_act
->thread
? thr_act
->thread
->ref_count
:0,
688 thr_act
->task
, thr_act
->task
? thr_act
->task
->ref_count
: 0);
690 printf("\talerts=%x mask=%x susp=%x active=%x hi=%x lo=%x\n",
691 thr_act
->alerts
, thr_act
->alert_mask
,
692 thr_act
->suspend_count
, thr_act
->active
,
693 thr_act
->higher
, thr_act
->lower
);
695 return((int)thr_act
);
704 thread_act_t thr_act
= current_act();
706 return(thr_act
->mact
.pcb
->save_srr0
);
710 * detach and return a kernel stack from a thread
714 stack_detach(thread_t thread
)
718 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_DETACH
),
719 thread
, thread
->priority
,
720 thread
->sched_pri
, 0, 0);
723 act_machine_sv_free(thread
->top_act
);
725 stack
= thread
->kernel_stack
;
726 thread
->kernel_stack
= 0;
731 * attach a kernel stack to a thread and initialize it
733 * attaches a stack to a thread. if there is no save
734 * area we allocate one. the top save area is then
735 * loaded with the pc (continuation address), the initial
736 * stack pointer, and a std kernel MSR. if the top
737 * save area is the user save area bad things will
743 stack_attach(struct thread_shuttle
*thread
,
745 void (*start_pos
)(thread_t
))
747 thread_act_t thr_act
;
751 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_ATTACH
),
752 thread
, thread
->priority
,
753 thread
->sched_pri
, start_pos
,
757 kss
= (unsigned int *)STACK_IKS(stack
);
758 thread
->kernel_stack
= stack
;
760 /* during initialization we sometimes do not have an
761 activation. in that case do not do anything */
762 if ((thr_act
= thread
->top_act
) != 0) {
763 sv
= save_get(); /* cannot block */
764 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use */
765 sv
->save_hdr
.save_act
= thr_act
;
766 sv
->save_hdr
.save_prev
= thr_act
->mact
.pcb
;
767 thr_act
->mact
.pcb
= sv
;
769 sv
->save_srr0
= (unsigned int) start_pos
;
770 /* sv->save_r3 = ARG ? */
771 sv
->save_r1
= (vm_offset_t
)((int)kss
- KF_SIZE
);
772 sv
->save_srr1
= MSR_SUPERVISOR_INT_OFF
;
773 sv
->save_fpscr
= 0; /* Clear all floating point exceptions */
774 sv
->save_vrsave
= 0; /* Set the vector save state */
775 sv
->save_vscr
[3] = 0x00010000; /* Supress java mode */
776 *((int *)sv
->save_r1
) = 0;
777 thr_act
->mact
.ksp
= 0;
784 * move a stack from old to new thread
788 stack_handoff(thread_t old
,
794 facility_context
*fowner
;
796 assert(new->top_act
);
797 assert(old
->top_act
);
799 stack
= stack_detach(old
);
800 new->kernel_stack
= stack
;
801 if (stack
== old
->stack_privilege
) {
802 assert(new->stack_privilege
);
803 old
->stack_privilege
= new->stack_privilege
;
804 new->stack_privilege
= stack
;
807 per_proc_info
[cpu_number()].cpu_flags
&= ~traceBE
;
809 if(real_ncpus
> 1) { /* This is potentially slow, so only do when actually SMP */
810 fowner
= per_proc_info
[cpu_number()].FPU_owner
; /* Cache this because it may change */
811 if(fowner
) { /* Is there any live context? */
812 if(fowner
->facAct
== old
->top_act
) { /* Is it for us? */
813 fpu_save(fowner
); /* Yes, save it */
816 fowner
= per_proc_info
[cpu_number()].VMX_owner
; /* Cache this because it may change */
817 if(fowner
) { /* Is there any live context? */
818 if(fowner
->facAct
== old
->top_act
) { /* Is it for us? */
819 vec_save(fowner
); /* Yes, save it */
824 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_HANDOFF
) | DBG_FUNC_NONE
,
825 (int)old
, (int)new, old
->sched_pri
, new->sched_pri
, 0);
828 if(new->top_act
->mact
.specFlags
& runningVM
) { /* Is the new guy running a VM? */
829 pmap_switch(new->top_act
->mact
.vmmCEntry
->vmmPmap
); /* Switch to the VM's pmap */
831 else { /* otherwise, we use the task's pmap */
832 new_pmap
= new->top_act
->task
->map
->pmap
;
833 if ((old
->top_act
->task
->map
->pmap
!= new_pmap
) || (old
->top_act
->mact
.specFlags
& runningVM
)) {
834 pmap_switch(new_pmap
);
838 thread_machine_set_current(new);
839 active_stacks
[cpu_number()] = new->kernel_stack
;
840 per_proc_info
[cpu_number()].Uassist
= new->top_act
->mact
.cthread_self
;
842 per_proc_info
[cpu_number()].ppbbTaskEnv
= new->top_act
->mact
.bbTaskEnv
;
843 per_proc_info
[cpu_number()].spcFlags
= new->top_act
->mact
.specFlags
;
845 if (branch_tracing_enabled())
846 per_proc_info
[cpu_number()].cpu_flags
|= traceBE
;
848 if(trcWork
.traceMask
) dbgTrace(0x12345678, (unsigned int)old
->top_act
, (unsigned int)new->top_act
); /* Cut trace entry if tracing */
854 * clean and initialize the current kernel stack and go to
855 * the given continuation routine
859 call_continuation(void (*continuation
)(void) )
865 assert(current_thread()->kernel_stack
);
866 kss
= (unsigned int *)STACK_IKS(current_thread()->kernel_stack
);
867 assert(continuation
);
869 tsp
= (vm_offset_t
)((int)kss
- KF_SIZE
);
873 Call_continuation(continuation
, tsp
);
879 thread_swapin_mach_alloc(thread_t thread
)
883 assert(thread
->top_act
->mact
.pcb
== 0);
887 sv
->save_hdr
.save_prev
= 0; /* Initialize back chain */
888 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use */
889 sv
->save_hdr
.save_act
= thread
->top_act
; /* Initialize owner */
890 thread
->top_act
->mact
.pcb
= sv
;