]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/pcb.c
2d4c35bc0ee46092cdb12023f3f0aed8a34b8bc3
[apple/xnu.git] / osfmk / ppc / pcb.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Copyright (c) 1990,1991,1992 The University of Utah and
35 * the Center for Software Science (CSS). All rights reserved.
36 *
37 * Permission to use, copy, modify and distribute this software is hereby
38 * granted provided that (1) source code retains these copyright, permission,
39 * and disclaimer notices, and (2) redistributions including binaries
40 * reproduce the notices in supporting documentation, and (3) all advertising
41 * materials mentioning features or use of this software display the following
42 * acknowledgement: ``This product includes software developed by the Center
43 * for Software Science at the University of Utah.''
44 *
45 * THE UNIVERSITY OF UTAH AND CSS ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
46 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSS DISCLAIM ANY LIABILITY OF
47 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 *
49 * CSS requests users of this software to return to css-dist@cs.utah.edu any
50 * improvements that they make and grant CSS redistribution rights.
51 *
52 * Utah $Hdr: pcb.c 1.23 92/06/27$
53 */
54
55 #include <debug.h>
56
57 #include <types.h>
58
59 #include <mach/mach_types.h>
60 #include <mach/thread_status.h>
61
62 #include <kern/kern_types.h>
63 #include <kern/task.h>
64 #include <kern/thread.h>
65 #include <kern/misc_protos.h>
66 #include <kern/mach_param.h>
67 #include <kern/spl.h>
68
69 #include <vm/vm_map.h>
70 #include <vm/vm_kern.h>
71
72 #include <ppc/misc_protos.h>
73 #include <ppc/cpu_internal.h>
74 #include <ppc/exception.h>
75 #include <ppc/proc_reg.h>
76 #include <ppc/pmap.h>
77 #include <ppc/trap.h>
78 #include <ppc/mappings.h>
79 #include <ppc/savearea.h>
80 #include <ppc/Firmware.h>
81 #include <ppc/asm.h>
82 #include <ppc/thread.h>
83 #include <ppc/vmachmon.h>
84 #include <ppc/low_trace.h>
85 #include <ppc/lowglobals.h>
86
87 #include <sys/kdebug.h>
88
89 void machine_act_terminate(thread_t);
90
91 /*
92 * These constants are dumb. They should not be in asm.h!
93 */
94
95 #define KF_SIZE (FM_SIZE+ARG_SIZE+FM_REDZONE)
96
97 #if DEBUG
98 int fpu_trap_count = 0;
99 int fpu_switch_count = 0;
100 int vec_trap_count = 0;
101 int vec_switch_count = 0;
102 #endif
103
104 /*
105 * consider_machine_collect: try to collect machine-dependent pages
106 */
107 void
108 consider_machine_collect()
109 {
110 /*
111 * none currently available
112 */
113 return;
114 }
115
116 void
117 consider_machine_adjust()
118 {
119 consider_mapping_adjust();
120 }
121
122 /*
123 * switch_context: Switch from one thread to another, needed for
124 * switching of space
125 *
126 */
127 thread_t
128 machine_switch_context(
129 thread_t old,
130 thread_continue_t continuation,
131 thread_t new)
132 {
133 register thread_t retval;
134 pmap_t new_pmap;
135 facility_context *fowner;
136 struct per_proc_info *ppinfo;
137
138 if (old == new)
139 panic("machine_switch_context");
140
141 ppinfo = getPerProc(); /* Get our processor block */
142
143 ppinfo->old_thread = (unsigned int)old;
144 ppinfo->cpu_flags &= ~traceBE; /* disable branch tracing if on */
145
146 /* Our context might wake up on another processor, so we must
147 * not keep hot state in our FPU, it must go back to the pcb
148 * so that it can be found by the other if needed
149 */
150 if(real_ncpus > 1) { /* This is potentially slow, so only do when actually SMP */
151 fowner = ppinfo->FPU_owner; /* Cache this because it may change */
152 if(fowner) { /* Is there any live context? */
153 if(fowner->facAct == old) { /* Is it for us? */
154 fpu_save(fowner); /* Yes, save it */
155 }
156 }
157 fowner = ppinfo->VMX_owner; /* Cache this because it may change */
158 if(fowner) { /* Is there any live context? */
159 if(fowner->facAct == old) { /* Is it for us? */
160 vec_save(fowner); /* Yes, save it */
161 }
162 }
163 }
164
165 /*
166 * If old thread is running VM, save per proc userProtKey and FamVMmode spcFlags bits in the thread spcFlags
167 * This bits can be modified in the per proc without updating the thread spcFlags
168 */
169 if(old->machine.specFlags & runningVM) {
170 old->machine.specFlags &= ~(userProtKey|FamVMmode);
171 old->machine.specFlags |= (ppinfo->spcFlags) & (userProtKey|FamVMmode);
172 }
173 old->machine.specFlags &= ~OnProc;
174 new->machine.specFlags |= OnProc;
175
176 /*
177 * We do not have to worry about the PMAP module, so switch.
178 *
179 * We must not use thread->map since this may not be the actual
180 * task map, but the map being used for a klcopyin/out.
181 */
182
183 if(new->machine.specFlags & runningVM) { /* Is the new guy running a VM? */
184 pmap_switch(new->machine.vmmCEntry->vmmPmap); /* Switch to the VM's pmap */
185 ppinfo->VMMareaPhys = new->machine.vmmCEntry->vmmContextPhys;
186 ppinfo->VMMXAFlgs = new->machine.vmmCEntry->vmmXAFlgs;
187 ppinfo->FAMintercept = new->machine.vmmCEntry->vmmFAMintercept;
188 }
189 else { /* otherwise, we use the task's pmap */
190 new_pmap = new->task->map->pmap;
191 if ((old->task->map->pmap != new_pmap) || (old->machine.specFlags & runningVM)) {
192 pmap_switch(new_pmap); /* Switch if there is a change */
193 }
194 }
195
196 if(old->machine.umwSpace != invalSpace) { /* Does our old guy have an active window? */
197 old->machine.umwSpace |= umwSwitchAway; /* Show we switched away from this guy */
198 hw_blow_seg(lowGlo.lgUMWvaddr); /* Blow off the first segment */
199 hw_blow_seg(lowGlo.lgUMWvaddr + 0x10000000ULL); /* Blow off the second segment */
200 }
201
202 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_SCHED) | DBG_FUNC_NONE,
203 old->reason, (int)new, old->sched_pri, new->sched_pri, 0);
204
205 retval = Switch_context(old, continuation, new);
206 assert(retval != NULL);
207
208 if (branch_tracing_enabled()) {
209 ppinfo = getPerProc(); /* Get our processor block */
210 ppinfo->cpu_flags |= traceBE; /* restore branch tracing */
211 }
212
213 /* We've returned from having switched context, so we should be
214 * back in the original context.
215 */
216
217 return retval;
218 }
219
220 /*
221 * Initialize the machine-dependent state for a new thread.
222 */
223 kern_return_t
224 machine_thread_create(
225 thread_t thread,
226 task_t task)
227 {
228 savearea *sv; /* Pointer to newly allocated savearea */
229 unsigned int *CIsTooLimited, i;
230
231 hw_atomic_add((uint32_t *)&saveanchor.savetarget, 4); /* Account for the number of saveareas we think we "need"
232 for this activation */
233 assert(thread->machine.pcb == (savearea *)0); /* Make sure there was no previous savearea */
234
235 sv = save_alloc(); /* Go get us a savearea */
236
237 bzero((char *)((unsigned int)sv + sizeof(savearea_comm)), (sizeof(savearea) - sizeof(savearea_comm))); /* Clear it */
238
239 sv->save_hdr.save_prev = 0; /* Clear the back pointer */
240 sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use */
241 sv->save_hdr.save_act = thread; /* Set who owns it */
242 thread->machine.pcb = sv; /* Point to the save area */
243 thread->machine.curctx = &thread->machine.facctx; /* Initialize facility context */
244 thread->machine.facctx.facAct = thread; /* Initialize facility context pointer to activation */
245 thread->machine.umwSpace = invalSpace; /* Initialize user memory window space to invalid */
246 thread->machine.preemption_count = 0; /* Initialize preemption counter */
247
248 /*
249 * User threads will pull their context from the pcb when first
250 * returning to user mode, so fill in all the necessary values.
251 * Kernel threads are initialized from the save state structure
252 * at the base of the kernel stack (see stack_attach()).
253 */
254
255 thread->machine.upcb = sv; /* Set user pcb */
256 sv->save_srr1 = (uint64_t)MSR_EXPORT_MASK_SET; /* Set the default user MSR */
257 if(task_has_64BitAddr(task)) sv->save_srr1 |= (uint64_t)MASK32(MSR_SF) << 32; /* If 64-bit task, force 64-bit mode */
258 sv->save_fpscr = 0; /* Clear all floating point exceptions */
259 sv->save_vrsave = 0; /* Set the vector save state */
260 sv->save_vscr[0] = 0x00000000;
261 sv->save_vscr[1] = 0x00000000;
262 sv->save_vscr[2] = 0x00000000;
263 sv->save_vscr[3] = 0x00010000; /* Disable java mode and clear saturated */
264
265 return(KERN_SUCCESS);
266 }
267
268 /*
269 * Machine-dependent cleanup prior to destroying a thread
270 */
271 void
272 machine_thread_destroy(
273 thread_t thread)
274 {
275 register savearea *pcb, *ppsv;
276 register savearea_vec *vsv, *vpsv;
277 register savearea_fpu *fsv, *fpsv;
278 register savearea *svp;
279 register int i;
280 boolean_t intr;
281
282 /*
283 * This function will release all context.
284 */
285
286 machine_act_terminate(thread); /* Make sure all virtual machines are dead first */
287
288 /*
289 *
290 * Walk through and release all floating point and vector contexts. Also kill live context.
291 *
292 */
293
294 intr = ml_set_interrupts_enabled(FALSE); /* Disable for interruptions */
295
296 toss_live_vec(thread->machine.curctx); /* Dump live vectors */
297
298 vsv = thread->machine.curctx->VMXsave; /* Get the top vector savearea */
299
300 while(vsv) { /* Any VMX saved state? */
301 vpsv = vsv; /* Remember so we can toss this */
302 vsv = CAST_DOWN(savearea_vec *, vsv->save_hdr.save_prev); /* Get one underneath our's */
303 save_release((savearea *)vpsv); /* Release it */
304 }
305
306 thread->machine.curctx->VMXsave = 0; /* Kill chain */
307
308 toss_live_fpu(thread->machine.curctx); /* Dump live float */
309
310 fsv = thread->machine.curctx->FPUsave; /* Get the top float savearea */
311
312 while(fsv) { /* Any float saved state? */
313 fpsv = fsv; /* Remember so we can toss this */
314 fsv = CAST_DOWN(savearea_fpu *, fsv->save_hdr.save_prev); /* Get one underneath our's */
315 save_release((savearea *)fpsv); /* Release it */
316 }
317
318 thread->machine.curctx->FPUsave = 0; /* Kill chain */
319
320 /*
321 * free all regular saveareas.
322 */
323
324 pcb = thread->machine.pcb; /* Get the general savearea */
325
326 while(pcb) { /* Any float saved state? */
327 ppsv = pcb; /* Remember so we can toss this */
328 pcb = CAST_DOWN(savearea *, pcb->save_hdr.save_prev); /* Get one underneath our's */
329 save_release(ppsv); /* Release it */
330 }
331
332 hw_atomic_sub((uint32_t *)&saveanchor.savetarget, 4); /* Unaccount for the number of saveareas we think we "need" */
333
334 (void) ml_set_interrupts_enabled(intr); /* Restore interrupts if enabled */
335
336 }
337
338 /*
339 * act_machine_sv_free
340 * release saveareas associated with an act. if flag is true, release
341 * user level savearea(s) too, else don't
342 *
343 * This code must run with interruptions disabled because an interrupt handler could use
344 * floating point and/or vectors. If this happens and the thread we are blowing off owns
345 * the facility, we can deadlock.
346 */
347 void
348 act_machine_sv_free(thread_t act)
349 {
350 register savearea *pcb, *userpcb;
351 register savearea_vec *vsv, *vpst, *vsvt;
352 register savearea_fpu *fsv, *fpst, *fsvt;
353 register savearea *svp;
354 register int i;
355 boolean_t intr;
356
357 /*
358 * This function will release all non-user state context.
359 */
360
361 /*
362 *
363 * Walk through and release all floating point and vector contexts that are not
364 * user state. We will also blow away live context if it belongs to non-user state.
365 * Note that the level can not change while we are in this code. Nor can another
366 * context be pushed on the stack.
367 *
368 * We do nothing here if the current level is user. Otherwise,
369 * the live context is cleared. Then we find the user saved context.
370 * Next, we take the sync lock (to keep us from munging things in *_switch).
371 * The level is set to 0 and all stacked context other than user is dequeued.
372 * Then we unlock. Next, all of the old kernel contexts are released.
373 *
374 */
375
376 intr = ml_set_interrupts_enabled(FALSE); /* Disable for interruptions */
377
378 if(act->machine.curctx->VMXlevel) { /* Is the current level user state? */
379
380 toss_live_vec(act->machine.curctx); /* Dump live vectors if is not user */
381
382 if(!hw_lock_to((hw_lock_t)&act->machine.curctx->VMXsync, LockTimeOut)) { /* Get the sync lock */
383 panic("act_machine_sv_free - timeout getting VMX sync lock\n"); /* Tell all and die */
384 }
385
386 vsv = act->machine.curctx->VMXsave; /* Get the top vector savearea */
387 while(vsv && vsv->save_hdr.save_level) vsv = (savearea_vec *)vsv->save_hdr.save_prev; /* Find user context if any */
388
389 vsvt = act->machine.curctx->VMXsave; /* Get the top of the chain */
390 act->machine.curctx->VMXsave = vsv; /* Point to the user context */
391 act->machine.curctx->VMXlevel = 0; /* Set the level to user */
392 hw_lock_unlock((hw_lock_t)&act->machine.curctx->VMXsync); /* Unlock */
393
394 while(vsvt) { /* Clear any VMX saved state */
395 if (vsvt == vsv) break; /* Done when hit user if any */
396 vpst = vsvt; /* Remember so we can toss this */
397 vsvt = (savearea_vec *)vsvt->save_hdr.save_prev; /* Get one underneath our's */
398 save_ret((savearea *)vpst); /* Release it */
399 }
400
401 }
402
403 if(act->machine.curctx->FPUlevel) { /* Is the current level user state? */
404
405 toss_live_fpu(act->machine.curctx); /* Dump live floats if is not user */
406
407 if(!hw_lock_to((hw_lock_t)&act->machine.curctx->FPUsync, LockTimeOut)) { /* Get the sync lock */
408 panic("act_machine_sv_free - timeout getting FPU sync lock\n"); /* Tell all and die */
409 }
410
411 fsv = act->machine.curctx->FPUsave; /* Get the top floats savearea */
412 while(fsv && fsv->save_hdr.save_level) fsv = (savearea_fpu *)fsv->save_hdr.save_prev; /* Find user context if any */
413
414 fsvt = act->machine.curctx->FPUsave; /* Get the top of the chain */
415 act->machine.curctx->FPUsave = fsv; /* Point to the user context */
416 act->machine.curctx->FPUlevel = 0; /* Set the level to user */
417 hw_lock_unlock((hw_lock_t)&act->machine.curctx->FPUsync); /* Unlock */
418
419 while(fsvt) { /* Clear any VMX saved state */
420 if (fsvt == fsv) break; /* Done when hit user if any */
421 fpst = fsvt; /* Remember so we can toss this */
422 fsvt = (savearea_fpu *)fsvt->save_hdr.save_prev; /* Get one underneath our's */
423 save_ret((savearea *)fpst); /* Release it */
424 }
425
426 }
427
428 /*
429 * free all regular saveareas except a user savearea, if any
430 */
431
432 pcb = act->machine.pcb; /* Get the general savearea */
433 userpcb = 0; /* Assume no user context for now */
434
435 while(pcb) { /* Any float saved state? */
436 if (pcb->save_srr1 & MASK(MSR_PR)) { /* Is this a user savearea? */
437 userpcb = pcb; /* Remember so we can toss this */
438 break;
439 }
440 svp = pcb; /* Remember this */
441 pcb = CAST_DOWN(savearea *, pcb->save_hdr.save_prev); /* Get one underneath our's */
442 save_ret(svp); /* Release it */
443 }
444
445 act->machine.pcb = userpcb; /* Chain in the user if there is one, or 0 if not */
446 (void) ml_set_interrupts_enabled(intr); /* Restore interrupts if enabled */
447
448 }
449
450 void
451 machine_act_terminate(
452 thread_t act)
453 {
454 if(act->machine.bbDescAddr) { /* Check if the Blue box assist is active */
455 disable_bluebox_internal(act); /* Kill off bluebox */
456 }
457
458 if(act->machine.vmmControl) { /* Check if VMM is active */
459 vmm_tear_down_all(act); /* Kill off all VMM contexts */
460 }
461 }
462
463 void
464 machine_thread_terminate_self(void)
465 {
466 machine_act_terminate(current_thread());
467 }
468
469 void
470 machine_thread_init(void)
471 {
472 #ifdef MACHINE_STACK
473 #if KERNEL_STACK_SIZE > PPC_PGBYTES
474 panic("KERNEL_STACK_SIZE can't be greater than PPC_PGBYTES\n");
475 #endif
476 #endif
477 }
478
479 #if MACH_ASSERT
480
481 void
482 dump_thread(thread_t th)
483 {
484 printf(" thread @ 0x%x:\n", th);
485 }
486
487 int
488 dump_act(thread_t thr_act)
489 {
490 if (!thr_act)
491 return(0);
492
493 printf("thread(0x%x)(%d): task=%x(%d)\n",
494 thr_act, thr_act->ref_count,
495 thr_act->task, thr_act->task ? thr_act->task->ref_count : 0);
496
497 printf("\tsusp=%x active=%x\n",
498 thr_act->suspend_count, thr_act->active);
499
500 return((int)thr_act);
501 }
502
503 #endif
504
505 user_addr_t
506 get_useraddr()
507 {
508 return(current_thread()->machine.upcb->save_srr0);
509 }
510
511 /*
512 * detach and return a kernel stack from a thread
513 */
514
515 vm_offset_t
516 machine_stack_detach(
517 thread_t thread)
518 {
519 vm_offset_t stack;
520
521 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_DETACH),
522 thread, thread->priority,
523 thread->sched_pri, 0, 0);
524
525 act_machine_sv_free(thread);
526
527 stack = thread->kernel_stack;
528 thread->kernel_stack = 0;
529 return(stack);
530 }
531
532 /*
533 * attach a kernel stack to a thread and initialize it
534 *
535 * attaches a stack to a thread. if there is no save
536 * area we allocate one. the top save area is then
537 * loaded with the pc (continuation address), the initial
538 * stack pointer, and a std kernel MSR. if the top
539 * save area is the user save area bad things will
540 * happen
541 *
542 */
543
544 void
545 machine_stack_attach(
546 thread_t thread,
547 vm_offset_t stack)
548 {
549 unsigned int *kss;
550 struct savearea *sv;
551
552 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_ATTACH),
553 thread, thread->priority,
554 thread->sched_pri, 0, 0);
555
556 assert(stack);
557 kss = (unsigned int *)STACK_IKS(stack);
558 thread->kernel_stack = stack;
559
560 /* during initialization we sometimes do not have an
561 activation. in that case do not do anything */
562 sv = save_get(); /* cannot block */
563 sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use */
564 sv->save_hdr.save_act = thread;
565 sv->save_hdr.save_prev = (addr64_t)((uintptr_t)thread->machine.pcb);
566 thread->machine.pcb = sv;
567
568 sv->save_srr0 = (unsigned int)thread_continue;
569 /* sv->save_r3 = ARG ? */
570 sv->save_r1 = (vm_offset_t)((int)kss - KF_SIZE);
571 sv->save_srr1 = MSR_SUPERVISOR_INT_OFF;
572 sv->save_fpscr = 0; /* Clear all floating point exceptions */
573 sv->save_vrsave = 0; /* Set the vector save state */
574 sv->save_vscr[3] = 0x00010000; /* Supress java mode */
575 *(CAST_DOWN(int *, sv->save_r1)) = 0;
576
577 thread->machine.ksp = 0;
578 }
579
580 /*
581 * move a stack from old to new thread
582 */
583
584 void
585 machine_stack_handoff(
586 thread_t old,
587 thread_t new)
588 {
589
590 vm_offset_t stack;
591 pmap_t new_pmap;
592 facility_context *fowner;
593 mapping_t *mp;
594 struct per_proc_info *ppinfo;
595
596 assert(new);
597 assert(old);
598
599 if (old == new)
600 panic("machine_stack_handoff");
601
602 stack = machine_stack_detach(old);
603 new->kernel_stack = stack;
604 if (stack == old->reserved_stack) {
605 assert(new->reserved_stack);
606 old->reserved_stack = new->reserved_stack;
607 new->reserved_stack = stack;
608 }
609
610 ppinfo = getPerProc(); /* Get our processor block */
611
612 ppinfo->cpu_flags &= ~traceBE; /* Turn off special branch trace */
613
614 if(real_ncpus > 1) { /* This is potentially slow, so only do when actually SMP */
615 fowner = ppinfo->FPU_owner; /* Cache this because it may change */
616 if(fowner) { /* Is there any live context? */
617 if(fowner->facAct == old) { /* Is it for us? */
618 fpu_save(fowner); /* Yes, save it */
619 }
620 }
621 fowner = ppinfo->VMX_owner; /* Cache this because it may change */
622 if(fowner) { /* Is there any live context? */
623 if(fowner->facAct == old) { /* Is it for us? */
624 vec_save(fowner); /* Yes, save it */
625 }
626 }
627 }
628
629 /*
630 * If old thread is running VM, save per proc userProtKey and FamVMmode spcFlags bits in the thread spcFlags
631 * This bits can be modified in the per proc without updating the thread spcFlags
632 */
633 if(old->machine.specFlags & runningVM) { /* Is the current thread running a VM? */
634 old->machine.specFlags &= ~(userProtKey|FamVMmode);
635 old->machine.specFlags |= (ppinfo->spcFlags) & (userProtKey|FamVMmode);
636 }
637 old->machine.specFlags &= ~OnProc;
638 new->machine.specFlags |= OnProc;
639
640 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_HANDOFF) | DBG_FUNC_NONE,
641 old->reason, (int)new, old->sched_pri, new->sched_pri, 0);
642
643
644 if(new->machine.specFlags & runningVM) { /* Is the new guy running a VM? */
645 pmap_switch(new->machine.vmmCEntry->vmmPmap); /* Switch to the VM's pmap */
646 ppinfo->VMMareaPhys = new->machine.vmmCEntry->vmmContextPhys;
647 ppinfo->VMMXAFlgs = new->machine.vmmCEntry->vmmXAFlgs;
648 ppinfo->FAMintercept = new->machine.vmmCEntry->vmmFAMintercept;
649 }
650 else { /* otherwise, we use the task's pmap */
651 new_pmap = new->task->map->pmap;
652 if ((old->task->map->pmap != new_pmap) || (old->machine.specFlags & runningVM)) {
653 pmap_switch(new_pmap);
654 }
655 }
656
657 machine_set_current_thread(new);
658 ppinfo->Uassist = new->machine.cthread_self;
659
660 ppinfo->ppbbTaskEnv = new->machine.bbTaskEnv;
661 ppinfo->spcFlags = new->machine.specFlags;
662
663 old->machine.umwSpace |= umwSwitchAway; /* Show we switched away from this guy */
664 mp = (mapping_t *)&ppinfo->ppUMWmp;
665 mp->mpSpace = invalSpace; /* Since we can't handoff in the middle of copy in/out, just invalidate */
666
667 if (branch_tracing_enabled())
668 ppinfo->cpu_flags |= traceBE;
669
670 if(trcWork.traceMask) dbgTrace(0x9903, (unsigned int)old, (unsigned int)new, 0, 0); /* Cut trace entry if tracing */
671
672 return;
673 }
674
675 /*
676 * clean and initialize the current kernel stack and go to
677 * the given continuation routine
678 */
679
680 void
681 call_continuation(
682 thread_continue_t continuation,
683 void *parameter,
684 wait_result_t wresult)
685 {
686 thread_t self = current_thread();
687 unsigned int *kss;
688 vm_offset_t tsp;
689
690 assert(self->kernel_stack);
691 kss = (unsigned int *)STACK_IKS(self->kernel_stack);
692 assert(continuation);
693
694 tsp = (vm_offset_t)((int)kss - KF_SIZE);
695 assert(tsp);
696 *((int *)tsp) = 0;
697
698 Call_continuation(continuation, parameter, wresult, tsp);
699 }