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/fpu_protos.h>
62 #include <ppc/exception.h>
63 #include <ppc/proc_reg.h>
67 #include <ppc/mappings.h>
68 #include <ppc/savearea.h>
69 #include <ppc/Firmware.h>
71 #include <ppc/thread_act.h>
72 #include <ppc/vmachmon.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
=(savearea
*)(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_xfpscrpad
= 0; /* Start with a clear fpscr */
151 sv
->save_xfpscr
= 0; /* Start with a clear fpscr */
153 *((int *)sv
->save_r1
) = 0; /* Zero the frame backpointer */
154 thread
->top_act
->mact
.ksp
= 0; /* Show that the kernel stack is in use already */
159 * switch_context: Switch from one thread to another, needed for
163 struct thread_shuttle
*
165 struct thread_shuttle
*old
,
166 void (*continuation
)(void),
167 struct thread_shuttle
*new)
169 register thread_act_t old_act
= old
->top_act
, new_act
= new->top_act
;
170 register struct thread_shuttle
* retval
;
172 #if MACH_LDEBUG || MACH_KDB
173 log_thread_action("switch",
176 (long)__builtin_return_address(0));
178 per_proc_info
[cpu_number()].old_thread
= old
;
179 assert(old_act
->kernel_loaded
||
180 active_stacks
[cpu_number()] == old_act
->thread
->kernel_stack
);
182 if(get_preemption_level() != 1) { /* Make sure we are not at wrong preemption level */
183 panic("switch_context: Invalid preemption level (%d); old = %08X, cont = %08X, new = %08X\n",
184 get_preemption_level(), old
, continuation
, new);
186 check_simple_locks();
188 /* Our context might wake up on another processor, so we must
189 * not keep hot state in our FPU, it must go back to the pcb
190 * so that it can be found by the other if needed
192 if(real_ncpus
> 1) { /* This is potentially slow, so only do when actually SMP */
193 fpu_save(); /* Save floating point if used */
194 vec_save(); /* Save vector if used */
198 if (watchacts
& WA_PCB
) {
199 printf("switch_context(0x%08x, 0x%x, 0x%08x)\n",
200 old
,continuation
,new);
205 * We do not have to worry about the PMAP module, so switch.
207 * We must not use top_act->map since this may not be the actual
208 * task map, but the map being used for a klcopyin/out.
211 if(new_act
->mact
.specFlags
& runningVM
) { /* Is the new guy running a VM? */
212 pmap_switch(new_act
->mact
.vmmCEntry
->vmmPmap
); /* Switch to the VM's pmap */
214 else { /* otherwise, we use the task's pmap */
215 new_pmap
= new_act
->task
->map
->pmap
;
216 if ((old_act
->task
->map
->pmap
!= new_pmap
) || (old_act
->mact
.specFlags
& runningVM
)) {
217 pmap_switch(new_pmap
); /* Switch if there is a change */
221 /* Sanity check - is the stack pointer inside the stack that
222 * we're about to switch to? Is the execution address within
223 * the kernel's VM space??
226 printf("************* stack=%08X; R1=%08X; LR=%08X; old=%08X; cont=%08X; new=%08X\n",
227 new->kernel_stack
, new_act
->mact
.pcb
->ss
.r1
,
228 new_act
->mact
.pcb
->ss
.lr
, old
, continuation
, new); /* (TEST/DEBUG) */
229 assert((new->kernel_stack
< new_act
->mact
.pcb
->ss
.r1
) &&
230 ((unsigned int)STACK_IKS(new->kernel_stack
) >
231 new_act
->mact
.pcb
->ss
.r1
));
232 assert(new_act
->mact
.pcb
->ss
.lr
< VM_MAX_KERNEL_ADDRESS
);
236 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_SCHED
) | DBG_FUNC_NONE
,
237 (int)old
, (int)new, old
->sched_pri
, new->sched_pri
, 0);
240 retval
= Switch_context(old
, continuation
, new);
241 assert(retval
!= (struct thread_shuttle
*)NULL
);
243 /* We've returned from having switched context, so we should be
244 * back in the original context.
251 * Alter the thread's state so that a following thread_exception_return
252 * will make the thread return 'retval' from a syscall.
255 thread_set_syscall_return(
256 struct thread_shuttle
*thread
,
257 kern_return_t retval
)
259 struct ppc_saved_state
*ssp
= &thread
->top_act
->mact
.pcb
->ss
;
262 if (watchacts
& WA_PCB
)
263 printf("thread_set_syscall_return(thr=%x,retval=%d)\n", thread
,retval
);
264 #endif /* MACH_ASSERT */
270 * Initialize the machine-dependent state for a new thread.
273 thread_machine_create(
274 struct thread_shuttle
*thread
,
275 thread_act_t thr_act
,
276 void (*start_pos
)(thread_t
))
279 savearea
*sv
; /* Pointer to newly allocated savearea */
280 unsigned int *CIsTooLimited
, i
;
284 if (watchacts
& WA_PCB
)
285 printf("thread_machine_create(thr=%x,thr_act=%x,st=%x)\n", thread
, thr_act
, start_pos
);
286 #endif /* MACH_ASSERT */
288 hw_atomic_add(&saveanchor
.saveneed
, 4); /* Account for the number of saveareas we think we "need"
289 for this activation */
290 assert(thr_act
->mact
.pcb
== (pcb_t
)0); /* Make sure there was no previous savearea */
292 sv
= save_alloc(); /* Go get us a savearea */
294 bzero((char *) sv
, sizeof(struct pcb
)); /* Clear out the whole shebang */
296 sv
->save_act
= thr_act
; /* Set who owns it */
298 thr_act
->mact
.pcb
= (pcb_t
)sv
; /* Point to the save area */
300 thread
->kernel_stack
= (int)stack_alloc(thread
,start_pos
); /* Allocate our kernel stack */
301 assert(thread
->kernel_stack
); /* Make sure we got it */
304 if (watchacts
& WA_PCB
)
305 printf("pcb_init(%x) pcb=%x\n", thr_act
, sv
);
306 #endif /* MACH_ASSERT */
308 * User threads will pull their context from the pcb when first
309 * returning to user mode, so fill in all the necessary values.
310 * Kernel threads are initialized from the save state structure
311 * at the base of the kernel stack (see stack_attach()).
314 sv
->save_srr1
= MSR_EXPORT_MASK_SET
; /* Set the default user MSR */
316 CIsTooLimited
= (unsigned int *)(&sv
->save_sr0
); /* Make a pointer 'cause C can't cast on the left */
317 for(i
=0; i
<16; i
++) { /* Initialize all SRs */
318 CIsTooLimited
[i
] = SEG_REG_PROT
| (i
<< 20) | thr_act
->task
->map
->pmap
->space
; /* Set the SR value */
320 sv
->save_sr_copyin
= SEG_REG_PROT
| (SR_COPYIN_NUM
<<20) | thr_act
->task
->map
->pmap
->space
; /* Default the copyin */
322 return(KERN_SUCCESS
);
326 * Machine-dependent cleanup prior to destroying a thread
329 thread_machine_destroy( thread_t thread
)
333 if (thread
->kernel_stack
) {
341 * flush out any lazily evaluated HW state in the
342 * owning thread's context, before termination.
345 thread_machine_flush( thread_act_t cur_act
)
350 * Number of times we needed to swap an activation back in before
353 int switch_act_swapins
= 0;
358 * Machine-dependent details of activation switching. Called with
359 * RPC locks held and preemption disabled.
370 /* Our context might wake up on another processor, so we must
371 * not keep hot state in our FPU, it must go back to the pcb
372 * so that it can be found by the other if needed
374 if(real_ncpus
> 1) { /* This is potentially slow, so only do when actually SMP */
375 fpu_save(); /* Save floating point if used */
376 vec_save(); /* Save vector if used */
379 active_stacks
[cpu
] = thread
->kernel_stack
;
381 ast_context(new, cpu
);
383 /* Activations might have different pmaps
384 * (process->kernel->server, for example).
385 * Change space if needed
388 if(new->mact
.specFlags
& runningVM
) { /* Is the new guy running a VM? */
389 pmap_switch(new->mact
.vmmCEntry
->vmmPmap
); /* Switch to the VM's pmap */
391 else { /* otherwise, we use the task's pmap */
392 new_pmap
= new->task
->map
->pmap
;
393 if ((old
->task
->map
->pmap
!= new_pmap
) || (old
->mact
.specFlags
& runningVM
)) {
394 pmap_switch(new_pmap
);
401 pcb_user_to_kernel(thread_act_t act
)
404 return; /* Not needed, I hope... */
409 * act_machine_sv_free
410 * release saveareas associated with an act. if flag is true, release
411 * user level savearea(s) too, else don't
413 * this code cannot block so we call the proper save area free routine
416 act_machine_sv_free(thread_act_t act
)
418 register pcb_t pcb
,userpcb
,npcb
;
419 register savearea
*svp
;
423 * This next bit insures that any live facility context for this thread is discarded on every processor
424 * that may have it. We go through all per-processor blocks and zero the facility owner if
425 * it is the thread being destroyed. This needs to be done via a compare-and-swap because
426 * some other processor could change the owner while we are clearing it. It turns out that
427 * this is the only place where we need the interlock, normal use of the owner field is cpu-local
428 * and doesn't need the interlock. Because we are called during termintation, and a thread
429 * terminates itself, the context on other processors has been saved (because we save it as
430 * part of the context switch), even if it is still considered live. Since the dead thread is
431 * not running elsewhere, and the context is saved, any other processor looking at the owner
432 * field will not attempt to save context again, meaning that it doesn't matter if the owner
433 * changes out from under it.
437 * free VMX and FPU saveareas. do not free user save areas.
438 * user VMX and FPU saveareas, if any, i'm told are last in
439 * the chain so we just stop if we find them
440 * we identify user VMX and FPU saveareas when we find a pcb
441 * with a save level of 0. we identify user regular save
442 * areas when we find one with MSR_PR set
445 pcb
= act
->mact
.VMX_pcb
; /* Get the top vector savearea */
446 while(pcb
) { /* Any VMX saved state? */
447 svp
= (savearea
*)pcb
; /* save lots of casting later */
448 if (svp
->save_level_vec
== 0) break; /* done when hit user if any */
449 pcb
= (pcb_t
)svp
->save_prev_vector
; /* Get one underneath our's */
450 svp
->save_flags
&= ~SAVvmxvalid
; /* Clear the VMX flag */
451 if(!(svp
->save_flags
& SAVinuse
)) { /* Anyone left with this one? */
453 save_ret(svp
); /* release it */
456 act
->mact
.VMX_pcb
= pcb
;
457 if (act
->mact
.VMX_lvl
!= 0) {
458 for(i
=0; i
< real_ncpus
; i
++) { /* Cycle through processors */
459 (void)hw_compare_and_store((unsigned int)act
, 0, &per_proc_info
[i
].VMX_thread
); /* Clear if ours */
463 pcb
= act
->mact
.FPU_pcb
; /* Get the top floating point savearea */
464 while(pcb
) { /* Any floating point saved state? */
465 svp
= (savearea
*)pcb
;
466 if (svp
->save_level_fp
== 0) break; /* done when hit user if any */
467 pcb
= (pcb_t
)svp
->save_prev_float
; /* Get one underneath our's */
468 svp
->save_flags
&= ~SAVfpuvalid
; /* Clear the floating point flag */
469 if(!(svp
->save_flags
& SAVinuse
)) { /* Anyone left with this one? */
470 save_ret(svp
); /* Nope, release it */
473 act
->mact
.FPU_pcb
= pcb
;
474 if (act
->mact
.FPU_lvl
!= 0) {
475 for(i
=0; i
< real_ncpus
; i
++) { /* Cycle through processors */
476 (void)hw_compare_and_store((unsigned int)act
, 0, &per_proc_info
[i
].FPU_thread
); /* Clear if ours */
481 * free all regular saveareas except a user savearea, if any
487 svp
= (savearea
*)pcb
;
488 if ((svp
->save_srr1
& MASK(MSR_PR
))) {
489 assert(userpcb
== (pcb_t
)0);
491 svp
= (savearea
*)userpcb
;
492 npcb
= (pcb_t
)svp
->save_prev
;
493 svp
->save_prev
= (struct savearea
*)0;
495 svp
->save_flags
&= ~SAVattach
; /* Clear the attached flag */
496 npcb
= (pcb_t
)svp
->save_prev
;
497 if(!(svp
->save_flags
& SAVinuse
)) /* Anyone left with this one? */
502 act
->mact
.pcb
= userpcb
;
508 * act_virtual_machine_destroy:
509 * Shutdown any virtual machines associated with a thread
512 act_virtual_machine_destroy(thread_act_t act
)
514 if(act
->mact
.bbDescAddr
) { /* Check if the Blue box assist is active */
515 disable_bluebox_internal(act
); /* Kill off bluebox */
518 if(act
->mact
.vmmControl
) { /* Check if VMM is active */
519 vmm_tear_down_all(act
); /* Kill off all VMM contexts */
524 * act_machine_destroy: Shutdown any state associated with a thread pcb.
527 act_machine_destroy(thread_act_t act
)
529 register pcb_t pcb
, opcb
;
533 if (watchacts
& WA_PCB
)
534 printf("act_machine_destroy(0x%x)\n", act
);
535 #endif /* MACH_ASSERT */
537 act_virtual_machine_destroy(act
);
540 * This next bit insures that any live facility context for this thread is discarded on every processor
541 * that may have it. We go through all per-processor blocks and zero the facility owner if
542 * it is the thread being destroyed. This needs to be done via a compare-and-swap because
543 * some other processor could change the owner while we are clearing it. It turns out that
544 * this is the only place where we need the interlock, normal use of the owner field is cpu-local
545 * and doesn't need the interlock. Because we are called during termintation, and a thread
546 * terminates itself, the context on other processors has been saved (because we save it as
547 * part of the context switch), even if it is still considered live. Since the dead thread is
548 * not running elsewhere, and the context is saved, any other processor looking at the owner
549 * field will not attempt to save context again, meaning that it doesn't matter if the owner
550 * changes out from under it.
553 for(i
=0; i
< real_ncpus
; i
++) { /* Cycle through processors */
554 (void)hw_compare_and_store((unsigned int)act
, 0, &per_proc_info
[i
].FPU_thread
); /* Clear if ours */
555 (void)hw_compare_and_store((unsigned int)act
, 0, &per_proc_info
[i
].VMX_thread
); /* Clear if ours */
558 pcb
= act
->mact
.VMX_pcb
; /* Get the top vector savearea */
559 while(pcb
) { /* Any VMX saved state? */
560 opcb
= pcb
; /* Save current savearea address */
561 pcb
= (pcb_t
)(((savearea
*)pcb
)->save_prev_vector
); /* Get one underneath our's */
562 ((savearea
*)opcb
)->save_flags
&= ~SAVvmxvalid
; /* Clear the VMX flag */
564 if(!(((savearea
*)opcb
)->save_flags
& SAVinuse
)) { /* Anyone left with this one? */
565 save_release((savearea
*)opcb
); /* Nope, release it */
568 act
->mact
.VMX_pcb
= (pcb_t
)0; /* Clear pointer */
570 pcb
= act
->mact
.FPU_pcb
; /* Get the top floating point savearea */
571 while(pcb
) { /* Any floating point saved state? */
572 opcb
= pcb
; /* Save current savearea address */
573 pcb
= (pcb_t
)(((savearea
*)pcb
)->save_prev_float
); /* Get one underneath our's */
574 ((savearea
*)opcb
)->save_flags
&= ~SAVfpuvalid
; /* Clear the floating point flag */
576 if(!(((savearea
*)opcb
)->save_flags
& SAVinuse
)) { /* Anyone left with this one? */
577 save_release((savearea
*)opcb
); /* Nope, release it */
580 act
->mact
.FPU_pcb
= (pcb_t
)0; /* Clear pointer */
582 pcb
= act
->mact
.pcb
; /* Get the top normal savearea */
583 act
->mact
.pcb
= (pcb_t
)0; /* Clear pointer */
585 while(pcb
) { /* Any normal saved state left? */
586 opcb
= pcb
; /* Keep track of what we're working on */
587 pcb
= (pcb_t
)(((savearea
*)pcb
)->save_prev
); /* Get one underneath our's */
589 ((savearea
*)opcb
)->save_flags
= 0; /* Clear all flags since we release this in any case */
590 save_release((savearea
*)opcb
); /* Release this one */
593 hw_atomic_sub(&saveanchor
.saveneed
, 4); /* Unaccount for the number of saveareas we think we "need"
594 for this activation */
598 act_machine_create(task_t task
, thread_act_t thr_act
)
601 * Clear & Init the pcb (sets up user-mode s regs)
602 * We don't use this anymore.
607 unsigned int *CIsTooLimited
;
613 void act_machine_init()
616 if (watchacts
& WA_PCB
)
617 printf("act_machine_init()\n");
618 #endif /* MACH_ASSERT */
620 /* Good to verify these once */
621 assert( THREAD_MACHINE_STATE_MAX
<= THREAD_STATE_MAX
);
623 assert( THREAD_STATE_MAX
>= PPC_THREAD_STATE_COUNT
);
624 assert( THREAD_STATE_MAX
>= PPC_EXCEPTION_STATE_COUNT
);
625 assert( THREAD_STATE_MAX
>= PPC_FLOAT_STATE_COUNT
);
626 assert( THREAD_STATE_MAX
>= sizeof(struct ppc_saved_state
)/sizeof(int));
629 * If we start using kernel activations,
630 * would normally create kernel_thread_pool here,
631 * populating it from the act_zone
636 act_machine_return(int code
)
638 thread_act_t thr_act
= current_act();
641 if (watchacts
& WA_EXIT
)
642 printf("act_machine_return(0x%x) cur_act=%x(%d) thr=%x(%d)\n",
643 code
, thr_act
, thr_act
->ref_count
,
644 thr_act
->thread
, thr_act
->thread
->ref_count
);
645 #endif /* MACH_ASSERT */
649 * This code is called with nothing locked.
650 * It also returns with nothing locked, if it returns.
652 * This routine terminates the current thread activation.
653 * If this is the only activation associated with its
654 * thread shuttle, then the entire thread (shuttle plus
655 * activation) is terminated.
657 assert( code
== KERN_TERMINATED
);
660 act_lock_thread(thr_act
);
662 #ifdef CALLOUT_RPC_MODEL
664 * JMM - This needs to get cleaned up to work under the much simpler
665 * return (instead of callout model).
667 if (thr_act
->thread
->top_act
!= thr_act
) {
669 * this is not the top activation;
670 * if possible, we should clone the shuttle so that
671 * both the root RPC-chain and the soon-to-be-orphaned
672 * RPC-chain have shuttles
674 * JMM - Cloning is a horrible idea! Instead we should alert
675 * the pieces upstream to return the shuttle. We will use
678 act_unlock_thread(thr_act
);
679 panic("act_machine_return: ORPHAN CASE NOT YET IMPLEMENTED");
682 if (thr_act
->lower
!= THR_ACT_NULL
) {
683 thread_t cur_thread
= current_thread();
684 thread_act_t cur_act
;
685 struct ipc_port
*iplock
;
687 /* terminate the entire thread (shuttle plus activation) */
688 /* terminate only this activation, send an appropriate */
689 /* return code back to the activation that invoked us. */
690 iplock
= thr_act
->pool_port
; /* remember for unlock call */
691 thr_act
->lower
->alerts
|= SERVER_TERMINATED
;
692 install_special_handler(thr_act
->lower
);
694 /* Return to previous act with error code */
696 act_locked_act_reference(thr_act
); /* keep it around */
697 act_switch_swapcheck(cur_thread
, (ipc_port_t
)0);
699 (void) switch_act(THR_ACT_NULL
);
700 /* assert(thr_act->ref_count == 0); */ /* XXX */
701 cur_act
= cur_thread
->top_act
;
702 MACH_RPC_RET(cur_act
) = KERN_RPC_SERVER_TERMINATED
;
703 machine_kernel_stack_init(cur_thread
, mach_rpc_return_error
);
705 * The following unlocks must be done separately since fields
706 * used by `act_unlock_thread()' have been cleared, meaning
707 * that it would not release all of the appropriate locks.
709 rpc_unlock(cur_thread
);
710 if (iplock
) ip_unlock(iplock
); /* must be done separately */
712 act_deallocate(thr_act
); /* free it */
713 Load_context(cur_thread
);
716 panic("act_machine_return: TALKING ZOMBIE! (2)");
719 #endif /* CALLOUT_RPC_MODEL */
721 /* This is the only activation attached to the shuttle... */
723 assert(thr_act
->thread
->top_act
== thr_act
);
724 act_unlock_thread(thr_act
);
725 thread_terminate_self();
728 panic("act_machine_return: TALKING ZOMBIE! (1)");
732 thread_machine_set_current(struct thread_shuttle
*thread
)
734 register int my_cpu
= cpu_number();
736 cpu_data
[my_cpu
].active_thread
= thread
;
738 active_kloaded
[my_cpu
] = thread
->top_act
->kernel_loaded
? thread
->top_act
: THR_ACT_NULL
;
742 thread_machine_init(void)
745 #if KERNEL_STACK_SIZE > PPC_PGBYTES
746 panic("KERNEL_STACK_SIZE can't be greater than PPC_PGBYTES\n");
755 printf("pcb @ %8.8x:\n", pcb
);
762 dump_thread(thread_t th
)
764 printf(" thread @ 0x%x:\n", th
);
768 dump_act(thread_act_t thr_act
)
773 printf("thr_act(0x%x)(%d): thread=%x(%d) task=%x(%d)\n",
774 thr_act
, thr_act
->ref_count
,
775 thr_act
->thread
, thr_act
->thread
? thr_act
->thread
->ref_count
:0,
776 thr_act
->task
, thr_act
->task
? thr_act
->task
->ref_count
: 0);
778 printf("\talerts=%x mask=%x susp=%x active=%x hi=%x lo=%x\n",
779 thr_act
->alerts
, thr_act
->alert_mask
,
780 thr_act
->suspend_count
, thr_act
->active
,
781 thr_act
->higher
, thr_act
->lower
);
783 return((int)thr_act
);
792 thread_act_t thr_act
= current_act();
794 return(thr_act
->mact
.pcb
->ss
.srr0
);
798 * detach and return a kernel stack from a thread
802 stack_detach(thread_t thread
)
806 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_DETACH
),
807 thread
, thread
->priority
,
808 thread
->sched_pri
, 0,
811 stack
= thread
->kernel_stack
;
812 thread
->kernel_stack
= 0;
817 * attach a kernel stack to a thread and initialize it
819 * attaches a stack to a thread. if there is no save
820 * area we allocate one. the top save area is then
821 * loaded with the pc (continuation address), the initial
822 * stack pointer, and a std kernel MSR. if the top
823 * save area is the user save area bad things will
829 stack_attach(struct thread_shuttle
*thread
,
831 void (*start_pos
)(thread_t
))
833 thread_act_t thr_act
;
837 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_ATTACH
),
838 thread
, thread
->priority
,
839 thread
->sched_pri
, start_pos
,
843 kss
= (unsigned int *)STACK_IKS(stack
);
844 thread
->kernel_stack
= stack
;
846 /* during initialization we sometimes do not have an
847 activation. in that case do not do anything */
848 if ((thr_act
= thread
->top_act
) != 0) {
849 sv
= save_get(); /* cannot block */
850 // bzero((char *) sv, sizeof(struct pcb));
851 sv
->save_act
= thr_act
;
852 sv
->save_prev
= (struct savearea
*)thr_act
->mact
.pcb
;
853 thr_act
->mact
.pcb
= (pcb_t
)sv
;
855 sv
->save_srr0
= (unsigned int) start_pos
;
856 /* sv->save_r3 = ARG ? */
857 sv
->save_r1
= (vm_offset_t
)((int)kss
- KF_SIZE
);
858 sv
->save_srr1
= MSR_SUPERVISOR_INT_OFF
;
859 sv
->save_xfpscrpad
= 0; /* Start with a clear fpscr */
860 sv
->save_xfpscr
= 0; /* Start with a clear fpscr */
861 *((int *)sv
->save_r1
) = 0;
862 thr_act
->mact
.ksp
= 0;
869 * move a stack from old to new thread
873 stack_handoff(thread_t old
,
880 assert(new->top_act
);
881 assert(old
->top_act
);
883 stack
= stack_detach(old
);
884 new->kernel_stack
= stack
;
887 if (real_ncpus
> 1) {
893 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_HANDOFF
) | DBG_FUNC_NONE
,
894 (int)old
, (int)new, old
->sched_pri
, new->sched_pri
, 0);
897 if(new->top_act
->mact
.specFlags
& runningVM
) { /* Is the new guy running a VM? */
898 pmap_switch(new->top_act
->mact
.vmmCEntry
->vmmPmap
); /* Switch to the VM's pmap */
900 else { /* otherwise, we use the task's pmap */
901 new_pmap
= new->top_act
->task
->map
->pmap
;
902 if ((old
->top_act
->task
->map
->pmap
!= new_pmap
) || (old
->top_act
->mact
.specFlags
& runningVM
)) {
903 pmap_switch(new_pmap
);
907 thread_machine_set_current(new);
908 active_stacks
[cpu_number()] = new->kernel_stack
;
909 per_proc_info
[cpu_number()].Uassist
= new->top_act
->mact
.cthread_self
;
914 * clean and initialize the current kernel stack and go to
915 * the given continuation routine
919 call_continuation(void (*continuation
)(void) )
925 assert(current_thread()->kernel_stack
);
926 kss
= (unsigned int *)STACK_IKS(current_thread()->kernel_stack
);
927 assert(continuation
);
929 tsp
= (vm_offset_t
)((int)kss
- KF_SIZE
);
933 Call_continuation(continuation
, tsp
);
939 thread_swapin_mach_alloc(thread_t thread
)
943 assert(thread
->top_act
->mact
.pcb
== 0);
947 // bzero((char *) sv, sizeof(struct pcb));
948 sv
->save_act
= thread
->top_act
;
949 thread
->top_act
->mact
.pcb
= (pcb_t
)sv
;