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