2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Copyright (c) 1990,1991,1992 The University of Utah and
33 * the Center for Software Science (CSS). All rights reserved.
35 * Permission to use, copy, modify and distribute this software is hereby
36 * granted provided that (1) source code retains these copyright, permission,
37 * and disclaimer notices, and (2) redistributions including binaries
38 * reproduce the notices in supporting documentation, and (3) all advertising
39 * materials mentioning features or use of this software display the following
40 * acknowledgement: ``This product includes software developed by the Center
41 * for Software Science at the University of Utah.''
43 * THE UNIVERSITY OF UTAH AND CSS ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
44 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSS DISCLAIM ANY LIABILITY OF
45 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 * CSS requests users of this software to return to css-dist@cs.utah.edu any
48 * improvements that they make and grant CSS redistribution rights.
50 * Utah $Hdr: pcb.c 1.23 92/06/27$
57 #include <mach/mach_types.h>
58 #include <mach/thread_status.h>
60 #include <kern/kern_types.h>
61 #include <kern/task.h>
62 #include <kern/thread.h>
63 #include <kern/misc_protos.h>
64 #include <kern/mach_param.h>
66 #include <kern/machine.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_protos.h>
72 #include <ppc/misc_protos.h>
73 #include <ppc/cpu_internal.h>
74 #include <ppc/exception.h>
75 #include <ppc/proc_reg.h>
78 #include <ppc/mappings.h>
79 #include <ppc/savearea.h>
80 #include <ppc/Firmware.h>
82 #include <ppc/thread.h>
83 #include <ppc/vmachmon.h>
84 #include <ppc/low_trace.h>
85 #include <ppc/lowglobals.h>
86 #include <ppc/fpu_protos.h>
88 #include <sys/kdebug.h>
90 void machine_act_terminate(thread_t
);
93 * These constants are dumb. They should not be in asm.h!
96 #define KF_SIZE (FM_SIZE+ARG_SIZE+FM_REDZONE)
99 int fpu_trap_count
= 0;
100 int fpu_switch_count
= 0;
101 int vec_trap_count
= 0;
102 int vec_switch_count
= 0;
106 * consider_machine_collect: try to collect machine-dependent pages
109 consider_machine_collect(void)
112 * XXX none currently available
117 consider_machine_adjust(void)
119 consider_mapping_adjust();
123 * switch_context: Switch from one thread to another, needed for
128 machine_switch_context(
130 thread_continue_t continuation
,
133 register thread_t retval
;
135 facility_context
*fowner
;
136 struct per_proc_info
*ppinfo
;
139 panic("machine_switch_context");
141 ppinfo
= getPerProc(); /* Get our processor block */
143 ppinfo
->old_thread
= (unsigned int)old
;
145 /* Our context might wake up on another processor, so we must
146 * not keep hot state in our FPU, it must go back to the pcb
147 * so that it can be found by the other if needed
149 if(real_ncpus
> 1) { /* This is potentially slow, so only do when actually SMP */
150 fowner
= ppinfo
->FPU_owner
; /* Cache this because it may change */
151 if(fowner
) { /* Is there any live context? */
152 if(fowner
->facAct
== old
) { /* Is it for us? */
153 fpu_save(fowner
); /* Yes, save it */
156 fowner
= ppinfo
->VMX_owner
; /* Cache this because it may change */
157 if(fowner
) { /* Is there any live context? */
158 if(fowner
->facAct
== old
) { /* Is it for us? */
159 vec_save(fowner
); /* Yes, save it */
165 * If old thread is running VM, save per proc userProtKey and FamVMmode spcFlags bits in the thread spcFlags
166 * This bits can be modified in the per proc without updating the thread spcFlags
168 if(old
->machine
.specFlags
& runningVM
) {
169 old
->machine
.specFlags
&= ~(userProtKey
|FamVMmode
);
170 old
->machine
.specFlags
|= (ppinfo
->spcFlags
) & (userProtKey
|FamVMmode
);
172 old
->machine
.specFlags
&= ~OnProc
;
173 new->machine
.specFlags
|= OnProc
;
176 * We do not have to worry about the PMAP module, so switch.
178 * We must not use thread->map since this may not be the actual
179 * task map, but the map being used for a klcopyin/out.
182 if(new->machine
.specFlags
& runningVM
) { /* Is the new guy running a VM? */
183 pmap_switch(new->machine
.vmmCEntry
->vmmPmap
); /* Switch to the VM's pmap */
184 ppinfo
->VMMareaPhys
= new->machine
.vmmCEntry
->vmmContextPhys
;
185 ppinfo
->VMMXAFlgs
= new->machine
.vmmCEntry
->vmmXAFlgs
;
186 ppinfo
->FAMintercept
= new->machine
.vmmCEntry
->vmmFAMintercept
;
188 else { /* otherwise, we use the task's pmap */
189 new_pmap
= new->task
->map
->pmap
;
190 if ((old
->task
->map
->pmap
!= new_pmap
) || (old
->machine
.specFlags
& runningVM
)) {
191 pmap_switch(new_pmap
); /* Switch if there is a change */
195 if(old
->machine
.umwSpace
!= invalSpace
) { /* Does our old guy have an active window? */
196 old
->machine
.umwSpace
|= umwSwitchAway
; /* Show we switched away from this guy */
197 hw_blow_seg(lowGlo
.lgUMWvaddr
); /* Blow off the first segment */
198 hw_blow_seg(lowGlo
.lgUMWvaddr
+ 0x10000000ULL
); /* Blow off the second segment */
201 retval
= Switch_context(old
, continuation
, new);
202 assert(retval
!= NULL
);
204 /* We've returned from having switched context, so we should be
205 * back in the original context.
212 * Initialize the machine-dependent state for a new thread.
215 machine_thread_create(
219 struct savearea
*sv
; /* Pointer to newly allocated savearea */
221 (void)hw_atomic_add(&saveanchor
.savetarget
, 4); /* Account for the number of saveareas we think we "need"
222 for this activation */
223 assert(thread
->machine
.pcb
== (struct savearea
*)0); /* Make sure there was no previous savearea */
225 sv
= save_alloc(); /* Go get us a savearea */
227 bzero((char *)((unsigned int)sv
+ sizeof(savearea_comm
)), (sizeof(struct savearea
) - sizeof(savearea_comm
))); /* Clear it */
229 sv
->save_hdr
.save_prev
= 0; /* Clear the back pointer */
230 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use */
231 sv
->save_hdr
.save_act
= thread
; /* Set who owns it */
232 thread
->machine
.pcb
= sv
; /* Point to the save area */
233 thread
->machine
.curctx
= &thread
->machine
.facctx
; /* Initialize facility context */
234 thread
->machine
.facctx
.facAct
= thread
; /* Initialize facility context pointer to activation */
235 thread
->machine
.umwSpace
= invalSpace
; /* Initialize user memory window space to invalid */
236 thread
->machine
.preemption_count
= 0; /* Initialize preemption counter */
239 * User threads will pull their context from the pcb when first
240 * returning to user mode, so fill in all the necessary values.
241 * Kernel threads are initialized from the save state structure
242 * at the base of the kernel stack (see stack_attach()).
245 thread
->machine
.upcb
= sv
; /* Set user pcb */
246 sv
->save_srr1
= (uint64_t)MSR_EXPORT_MASK_SET
; /* Set the default user MSR */
247 if(task_has_64BitAddr(task
)) sv
->save_srr1
|= (uint64_t)MASK32(MSR_SF
) << 32; /* If 64-bit task, force 64-bit mode */
248 sv
->save_fpscr
= 0; /* Clear all floating point exceptions */
249 sv
->save_vrsave
= 0; /* Set the vector save state */
250 sv
->save_vscr
[0] = 0x00000000;
251 sv
->save_vscr
[1] = 0x00000000;
252 sv
->save_vscr
[2] = 0x00000000;
253 sv
->save_vscr
[3] = 0x00010000; /* Disable java mode and clear saturated */
255 return(KERN_SUCCESS
);
259 * Machine-dependent cleanup prior to destroying a thread
262 machine_thread_destroy(
265 struct savearea
*local_pcb
, *ppsv
;
266 savearea_vec
*vsv
, *vpsv
;
267 savearea_fpu
*fsv
, *fpsv
;
271 * This function will release all context.
274 machine_act_terminate(thread
); /* Make sure all virtual machines are dead first */
278 * Walk through and release all floating point and vector contexts. Also kill live context.
282 intr
= ml_set_interrupts_enabled(FALSE
); /* Disable for interruptions */
284 toss_live_vec(thread
->machine
.curctx
); /* Dump live vectors */
286 vsv
= thread
->machine
.curctx
->VMXsave
; /* Get the top vector savearea */
288 while(vsv
) { /* Any VMX saved state? */
289 vpsv
= vsv
; /* Remember so we can toss this */
290 /* XXX save_prev should be a void * 4425537 */
291 vsv
= CAST_DOWN(savearea_vec
*, vsv
->save_hdr
.save_prev
); /* Get one underneath our's */
292 save_release((struct savearea
*)vpsv
); /* Release it */
295 thread
->machine
.curctx
->VMXsave
= NULL
; /* Kill chain */
297 toss_live_fpu(thread
->machine
.curctx
); /* Dump live float */
299 fsv
= thread
->machine
.curctx
->FPUsave
; /* Get the top float savearea */
301 while(fsv
) { /* Any float saved state? */
302 fpsv
= fsv
; /* Remember so we can toss this */
303 /* XXX save_prev should be a void * 4425537 */
304 fsv
= CAST_DOWN(savearea_fpu
*, fsv
->save_hdr
.save_prev
); /* Get one underneath our's */
305 save_release((struct savearea
*)fpsv
); /* Release it */
308 thread
->machine
.curctx
->FPUsave
= NULL
; /* Kill chain */
311 * free all regular saveareas.
314 local_pcb
= thread
->machine
.pcb
; /* Get the general savearea */
316 while(local_pcb
) { /* Any float saved state? */
317 ppsv
= local_pcb
; /* Remember so we can toss this */
318 /* XXX save_prev should be a void * 4425537 */
319 local_pcb
= CAST_DOWN(struct savearea
*, local_pcb
->save_hdr
.save_prev
); /* Get one underneath our's */
320 save_release(ppsv
); /* Release it */
323 (void)hw_atomic_sub(&saveanchor
.savetarget
, 4); /* Unaccount for the number of saveareas we think we "need" */
325 (void) ml_set_interrupts_enabled(intr
); /* Restore interrupts if enabled */
330 * act_machine_sv_free
331 * release saveareas associated with a thread. if flag is true, release
332 * user level savearea(s) too, else don't
334 * This code must run with interruptions disabled because an interrupt handler
335 * could use floating point and/or vectors. If this happens and the thread we
336 * are blowing off owns the facility, we can deadlock.
339 act_machine_sv_free(thread_t act
, __unused
int flag
)
341 struct savearea
*local_pcb
, *userpcb
;
342 register savearea_vec
*vsv
, *vpst
, *vsvt
;
343 register savearea_fpu
*fsv
, *fpst
, *fsvt
;
344 struct savearea
*svp
;
348 * This function will release all non-user state context.
353 * Walk through and release all floating point and vector contexts that are not
354 * user state. We will also blow away live context if it belongs to non-user state.
355 * Note that the level can not change while we are in this code. Nor can another
356 * context be pushed on the stack.
358 * We do nothing here if the current level is user. Otherwise,
359 * the live context is cleared. Then we find the user saved context.
360 * Next, we take the sync lock (to keep us from munging things in *_switch).
361 * The level is set to 0 and all stacked context other than user is dequeued.
362 * Then we unlock. Next, all of the old kernel contexts are released.
366 intr
= ml_set_interrupts_enabled(FALSE
); /* Disable for interruptions */
368 if(act
->machine
.curctx
->VMXlevel
) { /* Is the current level user state? */
370 toss_live_vec(act
->machine
.curctx
); /* Dump live vectors if is not user */
372 if(!hw_lock_to((hw_lock_t
)&act
->machine
.curctx
->VMXsync
, LockTimeOut
)) { /* Get the sync lock */
373 panic("act_machine_sv_free - timeout getting VMX sync lock\n"); /* Tell all and die */
376 vsv
= act
->machine
.curctx
->VMXsave
; /* Get the top vector savearea */
377 while(vsv
&& vsv
->save_hdr
.save_level
) /* Find user context if any */
378 /* XXX save_prev should be a void * 4425537 */
379 vsv
= CAST_DOWN(savearea_vec
*,
380 vsv
->save_hdr
.save_prev
);
382 vsvt
= act
->machine
.curctx
->VMXsave
; /* Get the top of the chain */
383 act
->machine
.curctx
->VMXsave
= vsv
; /* Point to the user context */
384 act
->machine
.curctx
->VMXlevel
= NULL
; /* Set the level to user */
385 hw_lock_unlock((hw_lock_t
)&act
->machine
.curctx
->VMXsync
); /* Unlock */
387 while(vsvt
) { /* Clear any VMX saved state */
388 if (vsvt
== vsv
) break; /* Done when hit user if any */
389 vpst
= vsvt
; /* Remember so we can toss this */
390 /* XXX save_prev should be a void * 4425537 */
391 vsvt
= CAST_DOWN(savearea_vec
*, vsvt
->save_hdr
.save_prev
); /* Get one underneath our's */
392 save_ret((struct savearea
*)vpst
); /* Release it */
397 if(act
->machine
.curctx
->FPUlevel
) { /* Is the current level user state? */
399 toss_live_fpu(act
->machine
.curctx
); /* Dump live floats if is not user */
401 if(!hw_lock_to((hw_lock_t
)&act
->machine
.curctx
->FPUsync
, LockTimeOut
)) { /* Get the sync lock */
402 panic("act_machine_sv_free - timeout getting FPU sync lock\n"); /* Tell all and die */
405 fsv
= act
->machine
.curctx
->FPUsave
; /* Get the top floats savearea */
406 while(fsv
&& fsv
->save_hdr
.save_level
) /* Find user context if any */
407 /* XXX save_prev should be a void * */
408 fsv
= CAST_DOWN(savearea_fpu
*, fsv
->save_hdr
.save_prev
);
410 fsvt
= act
->machine
.curctx
->FPUsave
; /* Get the top of the chain */
411 act
->machine
.curctx
->FPUsave
= fsv
; /* Point to the user context */
412 act
->machine
.curctx
->FPUlevel
= NULL
; /* Set the level to user */
413 hw_lock_unlock((hw_lock_t
)&act
->machine
.curctx
->FPUsync
); /* Unlock */
415 while(fsvt
) { /* Clear any VMX saved state */
416 if (fsvt
== fsv
) break; /* Done when hit user if any */
417 fpst
= fsvt
; /* Remember so we can toss this */
418 /* XXX save_prev should be a void * 4425537 */
419 fsvt
= CAST_DOWN(savearea_fpu
*, fsvt
->save_hdr
.save_prev
); /* Get one underneath our's */
420 save_ret((struct savearea
*)fpst
); /* Release it */
426 * free all regular saveareas except a user savearea, if any
429 local_pcb
= act
->machine
.pcb
; /* Get the general savearea */
430 userpcb
= NULL
; /* Assume no user context for now */
432 while(local_pcb
) { /* Any float saved state? */
433 if (local_pcb
->save_srr1
& MASK(MSR_PR
)) { /* Is this a user savearea? */
434 userpcb
= local_pcb
; /* Remember so we can toss this */
437 svp
= local_pcb
; /* Remember this */
438 /* XXX save_prev should be a void * 4425537 */
439 local_pcb
= CAST_DOWN(struct savearea
*, local_pcb
->save_hdr
.save_prev
); /* Get one underneath our's */
440 save_ret(svp
); /* Release it */
443 act
->machine
.pcb
= userpcb
; /* Chain in the user if there is one, or 0 if not */
444 (void) ml_set_interrupts_enabled(intr
); /* Restore interrupts if enabled */
449 machine_act_terminate(
452 if(act
->machine
.bbDescAddr
) { /* Check if the Blue box assist is active */
453 disable_bluebox_internal(act
); /* Kill off bluebox */
456 if(act
->machine
.vmmControl
) { /* Check if VMM is active */
457 vmm_tear_down_all(act
); /* Kill off all VMM contexts */
462 machine_thread_terminate_self(void)
464 machine_act_terminate(current_thread());
468 machine_thread_init(void)
471 #if KERNEL_STACK_SIZE > PPC_PGBYTES
472 panic("KERNEL_STACK_SIZE can't be greater than PPC_PGBYTES\n");
479 dump_thread(thread_t th
)
481 printf(" thread @ %p:\n", th
);
483 #endif /* MACH_ASSERT */
488 return(current_thread()->machine
.upcb
->save_srr0
);
492 * detach and return a kernel stack from a thread
496 machine_stack_detach(
501 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_DETACH
),
502 thread
, thread
->priority
,
503 thread
->sched_pri
, 0, 0);
505 act_machine_sv_free(thread
, 0); /* XXX flag == 0 OK? */
507 stack
= thread
->kernel_stack
;
508 thread
->kernel_stack
= 0;
513 * attach a kernel stack to a thread and initialize it
515 * attaches a stack to a thread. if there is no save
516 * area we allocate one. the top save area is then
517 * loaded with the pc (continuation address), the initial
518 * stack pointer, and a std kernel MSR. if the top
519 * save area is the user save area bad things will
525 machine_stack_attach(
532 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_ATTACH
),
533 thread
, thread
->priority
,
534 thread
->sched_pri
, 0, 0);
537 kss
= (unsigned int *)STACK_IKS(stack
);
538 thread
->kernel_stack
= stack
;
540 /* during initialization we sometimes do not have an
541 activation. in that case do not do anything */
542 sv
= save_get(); /* cannot block */
543 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use */
544 sv
->save_hdr
.save_act
= thread
;
545 sv
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)thread
->machine
.pcb
);
546 thread
->machine
.pcb
= sv
;
548 sv
->save_srr0
= (unsigned int)thread_continue
;
549 /* sv->save_r3 = ARG ? */
550 sv
->save_r1
= (vm_offset_t
)((int)kss
- KF_SIZE
);
551 sv
->save_srr1
= MSR_SUPERVISOR_INT_OFF
;
552 sv
->save_fpscr
= 0; /* Clear all floating point exceptions */
553 sv
->save_vrsave
= 0; /* Set the vector save state */
554 sv
->save_vscr
[3] = 0x00010000; /* Supress java mode */
555 *(CAST_DOWN(int *, sv
->save_r1
)) = 0;
557 thread
->machine
.ksp
= 0;
561 * move a stack from old to new thread
565 machine_stack_handoff(
572 facility_context
*fowner
;
574 struct per_proc_info
*ppinfo
;
580 panic("machine_stack_handoff");
582 stack
= machine_stack_detach(old
);
583 new->kernel_stack
= stack
;
584 if (stack
== old
->reserved_stack
) {
585 assert(new->reserved_stack
);
586 old
->reserved_stack
= new->reserved_stack
;
587 new->reserved_stack
= stack
;
590 ppinfo
= getPerProc(); /* Get our processor block */
592 if(real_ncpus
> 1) { /* This is potentially slow, so only do when actually SMP */
593 fowner
= ppinfo
->FPU_owner
; /* Cache this because it may change */
594 if(fowner
) { /* Is there any live context? */
595 if(fowner
->facAct
== old
) { /* Is it for us? */
596 fpu_save(fowner
); /* Yes, save it */
599 fowner
= ppinfo
->VMX_owner
; /* Cache this because it may change */
600 if(fowner
) { /* Is there any live context? */
601 if(fowner
->facAct
== old
) { /* Is it for us? */
602 vec_save(fowner
); /* Yes, save it */
608 * If old thread is running VM, save per proc userProtKey and FamVMmode spcFlags bits in the thread spcFlags
609 * This bits can be modified in the per proc without updating the thread spcFlags
611 if(old
->machine
.specFlags
& runningVM
) { /* Is the current thread running a VM? */
612 old
->machine
.specFlags
&= ~(userProtKey
|FamVMmode
);
613 old
->machine
.specFlags
|= (ppinfo
->spcFlags
) & (userProtKey
|FamVMmode
);
615 old
->machine
.specFlags
&= ~OnProc
;
616 new->machine
.specFlags
|= OnProc
;
618 if(new->machine
.specFlags
& runningVM
) { /* Is the new guy running a VM? */
619 pmap_switch(new->machine
.vmmCEntry
->vmmPmap
); /* Switch to the VM's pmap */
620 ppinfo
->VMMareaPhys
= new->machine
.vmmCEntry
->vmmContextPhys
;
621 ppinfo
->VMMXAFlgs
= new->machine
.vmmCEntry
->vmmXAFlgs
;
622 ppinfo
->FAMintercept
= new->machine
.vmmCEntry
->vmmFAMintercept
;
624 else { /* otherwise, we use the task's pmap */
625 new_pmap
= new->task
->map
->pmap
;
626 if ((old
->task
->map
->pmap
!= new_pmap
) || (old
->machine
.specFlags
& runningVM
)) {
627 pmap_switch(new_pmap
);
631 machine_set_current_thread(new);
632 ppinfo
->Uassist
= new->machine
.cthread_self
;
634 ppinfo
->ppbbTaskEnv
= new->machine
.bbTaskEnv
;
635 ppinfo
->spcFlags
= new->machine
.specFlags
;
637 old
->machine
.umwSpace
|= umwSwitchAway
; /* Show we switched away from this guy */
638 mp
= (mapping_t
*)&ppinfo
->ppUMWmp
;
639 mp
->mpSpace
= invalSpace
; /* Since we can't handoff in the middle of copy in/out, just invalidate */
641 if(trcWork
.traceMask
) dbgTrace(0x9903, (unsigned int)old
, (unsigned int)new, 0, 0); /* Cut trace entry if tracing */
646 void Call_continuation(thread_continue_t
, void *, wait_result_t
, vm_offset_t
);
649 * clean and initialize the current kernel stack and go to
650 * the given continuation routine
655 thread_continue_t continuation
,
657 wait_result_t wresult
)
659 thread_t self
= current_thread();
663 assert(self
->kernel_stack
);
664 kss
= (unsigned int *)STACK_IKS(self
->kernel_stack
);
665 assert(continuation
);
667 tsp
= (vm_offset_t
)((int)kss
- KF_SIZE
);
671 Call_continuation(continuation
, parameter
, wresult
, tsp
);