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@
27 #include <kern/thread.h>
28 #include <kern/thread_act.h>
29 #include <kern/misc_protos.h>
30 #include <mach/ppc/thread_status.h>
31 #include <ppc/proc_reg.h>
32 #include <ppc/exception.h>
33 #include <ppc/misc_protos.h>
34 #include <ppc/savearea.h>
35 #include <ppc/thread_act.h>
36 #include <ppc/Firmware.h>
38 #include <vm/vm_map.h>
40 extern unsigned int killprint
;
41 extern double FloatInit
;
42 extern unsigned long QNaNbarbarian
[4];
43 extern void thread_bootstrap_return(void);
44 extern struct Saveanchor saveanchor
;
45 extern int real_ncpus
; /* Number of actual CPUs */
66 unsigned int get_msr_exportmask(void);
67 unsigned int get_msr_nbits(void);
68 unsigned int get_msr_rbits(void);
69 void thread_set_child(thread_act_t child
, int pid
);
70 void thread_set_parent(thread_act_t parent
, int pid
);
73 * Maps state flavor to number of words in the state:
75 unsigned int state_count
[] = {
77 PPC_THREAD_STATE_COUNT
,
78 PPC_FLOAT_STATE_COUNT
,
79 PPC_EXCEPTION_STATE_COUNT
,
85 * Get the status of the specified thread.
89 act_machine_get_state(
91 thread_flavor_t flavor
,
92 thread_state_t tstate
,
93 mach_msg_type_number_t
*count
)
96 register struct savearea
*sv
; /* Pointer to the context savearea */
97 register savearea_fpu
*fsv
;
98 register savearea_vec
*vsv
;
101 unsigned int vrvalidwrk
;
103 register struct ppc_thread_state
*ts
;
104 register struct ppc_exception_state
*es
;
105 register struct ppc_float_state
*fs
;
106 register struct ppc_vector_state
*vs
;
110 if (watchacts
& WA_STATE
)
111 printf("act_%x act_machine_get_state(thr_act=%x,flav=%x,st=%x,cnt@%x=%x)\n",
112 current_act(), thr_act
, flavor
, tstate
,
113 count
, (count
? *count
: 0));
114 #endif /* MACH_ASSERT */
116 genuser
= find_user_regs(thr_act
); /* Find the current user general context for this activation */
120 case THREAD_STATE_FLAVOR_LIST
:
123 return (KERN_INVALID_ARGUMENT
);
126 tstate
[0] = PPC_THREAD_STATE
;
127 tstate
[1] = PPC_FLOAT_STATE
;
128 tstate
[2] = PPC_EXCEPTION_STATE
;
133 case PPC_THREAD_STATE
:
135 if (*count
< PPC_THREAD_STATE_COUNT
) { /* Is the count ok? */
136 return KERN_INVALID_ARGUMENT
;
139 ts
= (struct ppc_thread_state
*) tstate
;
141 sv
= genuser
; /* Copy this over */
143 if(sv
) { /* Is there a save area yet? */
144 ts
->r0
= sv
->save_r0
;
145 ts
->r1
= sv
->save_r1
;
146 ts
->r2
= sv
->save_r2
;
147 ts
->r3
= sv
->save_r3
;
148 ts
->r4
= sv
->save_r4
;
149 ts
->r5
= sv
->save_r5
;
150 ts
->r6
= sv
->save_r6
;
151 ts
->r7
= sv
->save_r7
;
152 ts
->r8
= sv
->save_r8
;
153 ts
->r9
= sv
->save_r9
;
154 ts
->r10
= sv
->save_r10
;
155 ts
->r11
= sv
->save_r11
;
156 ts
->r12
= sv
->save_r12
;
157 ts
->r13
= sv
->save_r13
;
158 ts
->r14
= sv
->save_r14
;
159 ts
->r15
= sv
->save_r15
;
160 ts
->r16
= sv
->save_r16
;
161 ts
->r17
= sv
->save_r17
;
162 ts
->r18
= sv
->save_r18
;
163 ts
->r19
= sv
->save_r19
;
164 ts
->r20
= sv
->save_r20
;
165 ts
->r21
= sv
->save_r21
;
166 ts
->r22
= sv
->save_r22
;
167 ts
->r23
= sv
->save_r23
;
168 ts
->r24
= sv
->save_r24
;
169 ts
->r25
= sv
->save_r25
;
170 ts
->r26
= sv
->save_r26
;
171 ts
->r27
= sv
->save_r27
;
172 ts
->r28
= sv
->save_r28
;
173 ts
->r29
= sv
->save_r29
;
174 ts
->r30
= sv
->save_r30
;
175 ts
->r31
= sv
->save_r31
;
176 ts
->cr
= sv
->save_cr
;
177 ts
->xer
= sv
->save_xer
;
178 ts
->lr
= sv
->save_lr
;
179 ts
->ctr
= sv
->save_ctr
;
180 ts
->srr0
= sv
->save_srr0
;
181 ts
->srr1
= sv
->save_srr1
;
182 ts
->mq
= 0; /* MQ register (601 only) */
183 ts
->vrsave
= sv
->save_vrsave
; /* VRSAVE register (Altivec only) */
185 else { /* No user state yet. Save seemingly random values. */
187 for(i
=0; i
< 32; i
+=2) { /* Fill up with defaults */
188 ((unsigned int *)&ts
->r0
)[i
] = ((unsigned int *)&FloatInit
)[0];
189 ((unsigned int *)&ts
->r0
)[i
+1] = ((unsigned int *)&FloatInit
)[1];
193 ts
->lr
= ((unsigned int *)&FloatInit
)[0];
194 ts
->ctr
= ((unsigned int *)&FloatInit
)[1];
195 ts
->srr0
= ((unsigned int *)&FloatInit
)[0];
196 ts
->srr1
= MSR_EXPORT_MASK_SET
;
198 ts
->vrsave
= 0; /* VRSAVE register (Altivec only) */
201 *count
= PPC_THREAD_STATE_COUNT
; /* Pass back the amount we actually copied */
204 case PPC_EXCEPTION_STATE
:
206 if (*count
< PPC_EXCEPTION_STATE_COUNT
) {
207 return KERN_INVALID_ARGUMENT
;
210 es
= (struct ppc_exception_state
*) tstate
;
212 sv
= thr_act
->mact
.pcb
; /* Start with the normal savearea */
213 while(sv
) { /* Find the user context */
214 if(sv
->save_srr1
& MASK(MSR_PR
)) { /* Are we looking at the user context? */
215 break; /* Outta here */
217 sv
= sv
->save_hdr
.save_prev
; /* Back chain */
220 if(sv
) { /* See if valid state yet */
221 es
->dar
= sv
->save_dar
;
222 es
->dsisr
= sv
->save_dsisr
;
223 es
->exception
= sv
->save_exception
;
225 else { /* Nope, not yet */
228 es
->exception
= ((unsigned int *)&FloatInit
)[0];
231 *count
= PPC_EXCEPTION_STATE_COUNT
;
234 case PPC_FLOAT_STATE
:
236 if (*count
< PPC_FLOAT_STATE_COUNT
) {
237 return KERN_INVALID_ARGUMENT
;
240 fpu_save(thr_act
->mact
.curctx
); /* Just in case it's live, save it */
242 fs
= (struct ppc_float_state
*) tstate
; /* Point to destination */
244 fsv
= (savearea_fpu
*)thr_act
->mact
.curctx
->FPUsave
; /* Start with the top FPU savearea */
246 while(fsv
) { /* Find the user context */
247 if(!fsv
->save_hdr
.save_level
) { /* Are we looking at the user context? */
248 break; /* Outta here */
250 fsv
= (savearea_fpu
*)fsv
->save_hdr
.save_prev
; /* Back chain */
253 if(fsv
) { /* See if we have any */
254 bcopy((char *)&fsv
->save_fp0
, (char *)fs
, 32*8); /* 32 registers */
255 fs
->fpscr_pad
= 0; /* Be clean and tidy */
256 if(genuser
) fs
->fpscr
= genuser
->save_fpscr
; /* Set the fpscr value to general */
257 else fs
->fpscr
= 0; /* If no user, initialize this */
259 else { /* No floating point yet */
261 for(i
=0; i
< 32; i
++) { /* Initialize floating points */
262 fs
->fpregs
[i
] = FloatInit
; /* Initial value */
264 fs
->fpscr_pad
= 0; /* Initial value */
265 fs
->fpscr
= 0; /* Initial value */
268 *count
= PPC_FLOAT_STATE_COUNT
;
272 case PPC_VECTOR_STATE
:
274 if (*count
< PPC_VECTOR_STATE_COUNT
) {
275 return KERN_INVALID_ARGUMENT
;
278 vec_save(thr_act
->mact
.curctx
); /* Just in case it's live, save it */
280 vs
= (struct ppc_vector_state
*) tstate
; /* Point to destination */
282 vsv
= (savearea_vec
*)thr_act
->mact
.curctx
->VMXsave
; /* Start with the top vector savearea */
284 while(vsv
) { /* Find the user context */
285 if(!vsv
->save_hdr
.save_level
) { /* Are we looking at the user context? */
286 break; /* Outta here */
288 vsv
= (savearea_vec
*)vsv
->save_hdr
.save_prev
; /* Back chain */
291 if(vsv
) { /* See if we have any */
293 vrvalidwrk
= vsv
->save_vrvalid
; /* Get the valid flags */
294 vs
->save_vrvalid
= vsv
->save_vrvalid
; /* Set the valid flags */
295 if(genuser
) for(j
=0; j
< 4; j
++) vs
->save_vscr
[j
] = genuser
->save_vscr
[j
]; /* Set value for vscr */
297 vs
->save_vscr
[0] = 0; /* Set an initial value if no general user yet */
298 vs
->save_vscr
[1] = 0;
299 vs
->save_vscr
[2] = 0;
300 vs
->save_vscr
[3] = 0x00010000;
302 for(i
=0; i
< 32; i
++) { /* Copy the saved registers and invalidate the others */
303 for(j
=0; j
< 4; j
++) {
304 if(vrvalidwrk
& 0x80000000) (vs
->save_vr
)[i
][j
] =
305 ((unsigned int *)&(vsv
->save_vr0
))[(i
* 4) + j
]; /* We have this register saved */
306 else vs
->save_vr
[i
][j
] = QNaNbarbarian
[j
]; /* Set invalid value */
308 vrvalidwrk
= vrvalidwrk
<< 1; /* Shift over to the next */
311 else { /* No vector yet */
313 for(i
=0; i
< 32; i
++) { /* Initialize vector registers */
314 for(j
=0; j
< 4; j
++) vs
->save_vr
[i
][j
] = QNaNbarbarian
[j
]; /* Initial value */
317 if(genuser
) for(j
=0; j
< 4; j
++) vs
->save_vscr
[j
] = genuser
->save_vscr
[j
]; /* Set value for vscr */
319 vs
->save_vscr
[0] = 0; /* Set an initial value if no general user yet */
320 vs
->save_vscr
[1] = 0;
321 vs
->save_vscr
[2] = 0;
322 vs
->save_vscr
[3] = 0x00010000;
324 vs
->save_vrvalid
= 0; /* Clear the valid flags */
327 for (i
=0; i
< 4; i
++) vs
->save_pad5
[i
] = 0; /* Clear cruft */
328 for (i
=0; i
< 7; i
++) vs
->save_pad6
[i
] = 0; /* Clear cruft */
330 *count
= PPC_VECTOR_STATE_COUNT
;
334 return KERN_INVALID_ARGUMENT
;
342 * Set the status of the specified thread.
345 act_machine_set_state(
346 thread_act_t thr_act
,
347 thread_flavor_t flavor
,
348 thread_state_t tstate
,
349 mach_msg_type_number_t count
)
352 savearea
*sv
, *genuser
;
353 savearea_fpu
*fsv
, *fsvn
, *fsvo
;
354 savearea_vec
*vsv
, *vsvn
, *vsvo
;
357 register struct ppc_thread_state
*ts
;
358 register struct ppc_exception_state
*es
;
359 register struct ppc_float_state
*fs
;
360 register struct ppc_vector_state
*vs
;
362 int kernel_act
= thr_act
->kernel_loading
|| thr_act
->kernel_loaded
;
365 if (watchacts
& WA_STATE
)
366 printf("act_%x act_machine_set_state(thr_act=%x,flav=%x,st=%x,cnt=%x)\n",
367 current_act(), thr_act
, flavor
, tstate
, count
);
368 #endif /* MACH_ASSERT */
370 // dbgTrace((unsigned int)thr_act, (unsigned int)sv, flavor); /* (TEST/DEBUG) */
372 clgn
= count
; /* Get the count */
374 switch (flavor
) { /* Validate the count before we do anything else */
375 case PPC_THREAD_STATE
:
377 if (clgn
< PPC_THREAD_STATE_COUNT
) { /* Is it too short? */
378 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
382 case PPC_EXCEPTION_STATE
:
384 if (clgn
< PPC_EXCEPTION_STATE_COUNT
) { /* Is it too short? */
385 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
390 case PPC_FLOAT_STATE
:
392 if (clgn
< PPC_FLOAT_STATE_COUNT
) { /* Is it too short? */
393 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
399 case PPC_VECTOR_STATE
:
401 if (clgn
< PPC_VECTOR_STATE_COUNT
) { /* Is it too short? */
402 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
408 return KERN_INVALID_ARGUMENT
;
411 genuser
= get_user_regs(thr_act
); /* Find or allocate and initialize one */
415 case PPC_THREAD_STATE
:
416 case PPC_EXCEPTION_STATE
:
418 ts
= (struct ppc_thread_state
*)tstate
;
420 if(flavor
== PPC_THREAD_STATE
) { /* Are we updating plain state? */
422 genuser
->save_r0
= ts
->r0
;
423 genuser
->save_r1
= ts
->r1
;
424 genuser
->save_r2
= ts
->r2
;
425 genuser
->save_r3
= ts
->r3
;
426 genuser
->save_r4
= ts
->r4
;
427 genuser
->save_r5
= ts
->r5
;
428 genuser
->save_r6
= ts
->r6
;
429 genuser
->save_r7
= ts
->r7
;
430 genuser
->save_r8
= ts
->r8
;
431 genuser
->save_r9
= ts
->r9
;
432 genuser
->save_r10
= ts
->r10
;
433 genuser
->save_r11
= ts
->r11
;
434 genuser
->save_r12
= ts
->r12
;
435 genuser
->save_r13
= ts
->r13
;
436 genuser
->save_r14
= ts
->r14
;
437 genuser
->save_r15
= ts
->r15
;
438 genuser
->save_r16
= ts
->r16
;
439 genuser
->save_r17
= ts
->r17
;
440 genuser
->save_r18
= ts
->r18
;
441 genuser
->save_r19
= ts
->r19
;
442 genuser
->save_r20
= ts
->r20
;
443 genuser
->save_r21
= ts
->r21
;
444 genuser
->save_r22
= ts
->r22
;
445 genuser
->save_r23
= ts
->r23
;
446 genuser
->save_r24
= ts
->r24
;
447 genuser
->save_r25
= ts
->r25
;
448 genuser
->save_r26
= ts
->r26
;
449 genuser
->save_r27
= ts
->r27
;
450 genuser
->save_r28
= ts
->r28
;
451 genuser
->save_r29
= ts
->r29
;
452 genuser
->save_r30
= ts
->r30
;
453 genuser
->save_r31
= ts
->r31
;
455 genuser
->save_cr
= ts
->cr
;
456 genuser
->save_xer
= ts
->xer
;
457 genuser
->save_lr
= ts
->lr
;
458 genuser
->save_ctr
= ts
->ctr
;
459 genuser
->save_srr0
= ts
->srr0
;
460 genuser
->save_vrsave
= ts
->vrsave
; /* VRSAVE register (Altivec only) */
462 genuser
->save_srr1
= MSR_PREPARE_FOR_IMPORT(genuser
->save_srr1
, ts
->srr1
); /* Set the bits we can change */
464 if(!kernel_act
) genuser
->save_srr1
|= MSR_EXPORT_MASK_SET
; /* If not a kernel guy, force the magic bits on */
466 genuser
->save_srr1
&= ~(MASK(MSR_FP
) | MASK(MSR_VEC
)); /* Make sure we don't enable the floating point unit */
472 es
= (struct ppc_exception_state
*) tstate
;
474 genuser
->save_dar
= es
->dar
;
475 genuser
->save_dsisr
= es
->dsisr
;
476 genuser
->save_exception
= es
->exception
;
480 case PPC_FLOAT_STATE
:
482 toss_live_fpu(thr_act
->mact
.curctx
); /* Toss my floating point if live anywhere */
484 fsv
= find_user_fpu(thr_act
); /* Get the user's floating point context */
486 if(!fsv
) { /* Do we have one yet? */
487 fsv
= (savearea_fpu
*)save_alloc(); /* If we still don't have one, get a new one */
488 fsv
->save_hdr
.save_flags
= (fsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVfloat
<< SAVtypeshft
); /* Mark as in use as float */
489 fsv
->save_hdr
.save_act
= thr_act
; /* Point to the activation */
490 fsv
->save_hdr
.save_prev
= 0; /* Mark no more */
491 fsv
->save_hdr
.save_level
= 0; /* Mark user state */
493 if(!thr_act
->mact
.curctx
->FPUsave
) thr_act
->mact
.curctx
->FPUsave
= fsv
; /* If no floating point, chain us first */
496 fsvn
= fsvo
= thr_act
->mact
.curctx
->FPUsave
; /* Remember first one */
498 while (fsvn
) { /* Go until we hit the end */
499 fsvo
= fsvn
; /* Remember the previous one */
500 fsvn
= (savearea_fpu
*)fsvo
->save_hdr
.save_prev
; /* Skip on to the next */
503 fsvo
->save_hdr
.save_prev
= (savearea
*)fsv
; /* Queue us on in */
508 fs
= (struct ppc_float_state
*) tstate
; /* Point to source */
511 bcopy((char *)fs
, (char *)&fsv
->save_fp0
, 32*8); /* Move in the 32 registers */
513 genuser
->save_fpscr
= fs
->fpscr
; /* Copy the fpscr value to normal */
518 case PPC_VECTOR_STATE
:
520 toss_live_vec(thr_act
->mact
.curctx
); /* Toss my vector if live anywhere */
522 vsv
= find_user_vec(thr_act
); /* Get the user's vector context */
524 if(!vsv
) { /* Do we have one yet? */
525 vsv
= (savearea_vec
*)save_alloc(); /* If we still don't have one, get a new one */
526 vsv
->save_hdr
.save_flags
= (vsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVvector
<< SAVtypeshft
); /* Mark as in use as vector */
527 vsv
->save_hdr
.save_act
= thr_act
; /* Point to the activation */
528 vsv
->save_hdr
.save_prev
= 0; /* Mark no more */
529 vsv
->save_hdr
.save_level
= 0; /* Mark user state */
531 if(!thr_act
->mact
.curctx
->VMXsave
) thr_act
->mact
.curctx
->VMXsave
= vsv
; /* If no vector, chain us first */
534 vsvn
= vsvo
= thr_act
->mact
.curctx
->VMXsave
; /* Remember first one */
536 while (vsvn
) { /* Go until we hit the end */
537 vsvo
= vsvn
; /* Remember the previous one */
538 vsvn
= (savearea_vec
*)vsvo
->save_hdr
.save_prev
; /* Skip on to the next */
541 vsvo
->save_hdr
.save_prev
= (savearea
*)vsv
; /* Queue us on in */
546 vs
= (struct ppc_vector_state
*) tstate
; /* Point to source */
548 bcopy((char *)vs
, (char *)&vsv
->save_vr0
, 32*16); /* 32 registers plus status and validity and pad */
549 vsv
->save_vrvalid
= vs
->save_vrvalid
; /* Set validity bits */
552 for(i
= 0; i
< 4; i
++) genuser
->save_vscr
[i
] = vs
->save_vscr
[i
]; /* Set value for vscr */
558 return KERN_INVALID_ARGUMENT
;
563 * Duplicates the context of one thread into a new one.
564 * The new thread is assumed to be new and have no user state contexts except maybe a general one.
565 * We also assume that the old thread can't be running anywhere.
567 * We're only going to be duplicating user context here. That means that we will have to
568 * eliminate any floating point or vector kernel contexts and carry across the user state ones.
571 void act_thread_dup(thread_act_t old
, thread_act_t
new) {
574 savearea_fpu
*fsv
, *fsvn
;
575 savearea_vec
*vsv
, *vsvn
;
576 unsigned int spc
, i
, *srs
;
578 fpu_save(old
->mact
.curctx
); /* Make certain floating point state is all saved */
579 vec_save(old
->mact
.curctx
); /* Make certain the vector state is all saved */
581 sv
= get_user_regs(new); /* Allocate and initialze context in the new activation */
583 osv
= find_user_regs(old
); /* Find the original context */
585 panic("act_thread_dup: old activation (%08X) has no general user context\n", old
);
588 bcopy((char *)((unsigned int)osv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
589 (char *)((unsigned int)sv
+ sizeof(savearea_comm
)),
590 sizeof(struct savearea
) - sizeof(savearea_comm
));
592 sv
->save_srr1
&= ~(MASK(MSR_FP
) | MASK(MSR_VEC
)); /* Make certain that floating point and vector are turned off */
594 fsv
= find_user_fpu(old
); /* Get any user floating point */
596 new->mact
.curctx
->FPUsave
= 0; /* Assume no floating point */
598 if(fsv
) { /* Did we find one? */
599 fsvn
= (savearea_fpu
*)save_alloc(); /* If we still don't have one, get a new one */
600 fsvn
->save_hdr
.save_flags
= (fsvn
->save_hdr
.save_flags
& ~SAVtype
) | (SAVfloat
<< SAVtypeshft
); /* Mark as in use as float */
601 fsvn
->save_hdr
.save_act
= new; /* Point to the activation */
602 fsvn
->save_hdr
.save_prev
= 0; /* Mark no more */
603 fsvn
->save_hdr
.save_level
= 0; /* Mark user state */
605 new->mact
.curctx
->FPUsave
= fsvn
; /* Chain in the floating point */
607 bcopy((char *)((unsigned int)fsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
608 (char *)((unsigned int)fsvn
+ sizeof(savearea_comm
)),
609 sizeof(struct savearea
) - sizeof(savearea_comm
));
612 vsv
= find_user_vec(old
); /* Get any user vector */
614 new->mact
.curctx
->VMXsave
= 0; /* Assume no vector */
616 if(vsv
) { /* Did we find one? */
617 vsvn
= (savearea_vec
*)save_alloc(); /* If we still don't have one, get a new one */
618 vsvn
->save_hdr
.save_flags
= (vsvn
->save_hdr
.save_flags
& ~SAVtype
) | (SAVvector
<< SAVtypeshft
); /* Mark as in use as float */
619 vsvn
->save_hdr
.save_act
= new; /* Point to the activation */
620 vsvn
->save_hdr
.save_prev
= 0; /* Mark no more */
621 vsvn
->save_hdr
.save_level
= 0; /* Mark user state */
623 new->mact
.curctx
->VMXsave
= vsvn
; /* Chain in the floating point */
625 bcopy((char *)((unsigned int)vsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
626 (char *)((unsigned int)vsvn
+ sizeof(savearea_comm
)),
627 sizeof(struct savearea
) - sizeof(savearea_comm
));
630 return; /* Bye bye... */
634 * Initializes a fresh set of user state values. If there is no user state context,
635 * one is created. Floats and VMX are not created.
637 * We only set initial values if there was no context found.
640 savearea
*get_user_regs(thread_act_t act
) {
643 unsigned int spc
, i
, *srs
;
645 sv
= act
->mact
.pcb
; /* Get the top savearea on the stack */
646 osv
= 0; /* Set no user savearea yet */
648 while(sv
) { /* Find the user context */
649 if(sv
->save_srr1
& MASK(MSR_PR
)) return sv
; /* We found a user state context... */
651 osv
= sv
; /* Save the last one */
652 sv
= sv
->save_hdr
.save_prev
; /* Get the previous context */
655 sv
= save_alloc(); /* Get one */
656 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use as general */
657 sv
->save_hdr
.save_act
= act
; /* Point to the activation */
658 sv
->save_hdr
.save_prev
= 0; /* Mark no more */
659 sv
->save_hdr
.save_level
= 0; /* Mark user state */
661 if(osv
) { /* Did we already have one? */
662 osv
->save_hdr
.save_prev
= sv
; /* Chain us on the end */
664 else { /* We are the first */
665 act
->mact
.pcb
= sv
; /* Put it there */
668 for(i
=0; i
< 32; i
+=2) { /* Fill up with defaults */
669 ((unsigned int *)&sv
->save_r0
)[i
] = ((unsigned int *)&FloatInit
)[0];
670 ((unsigned int *)&sv
->save_r0
)[i
+1] = ((unsigned int *)&FloatInit
)[1];
674 sv
->save_lr
= ((unsigned int *)&FloatInit
)[0];
675 sv
->save_ctr
= ((unsigned int *)&FloatInit
)[1];
676 sv
->save_srr0
= ((unsigned int *)&FloatInit
)[0];
677 sv
->save_srr1
= MSR_EXPORT_MASK_SET
;
679 sv
->save_fpscr
= 0; /* Clear all floating point exceptions */
681 sv
->save_vrsave
= 0; /* Set the vector save state */
682 sv
->save_vscr
[0] = 0x00000000;
683 sv
->save_vscr
[1] = 0x00000000;
684 sv
->save_vscr
[2] = 0x00000000;
685 sv
->save_vscr
[3] = 0x00010000; /* Supress java mode and clear saturated */
687 spc
= (unsigned int)act
->map
->pmap
->space
; /* Get the space we're in */
689 srs
= (unsigned int *)&sv
->save_sr0
; /* Point to the SRs */
690 for(i
= 0; i
< 16; i
++) { /* Fill in the SRs for the new context */
691 srs
[i
] = SEG_REG_PROT
| (i
<<20) | spc
; /* Set the SR */
694 return sv
; /* Bye bye... */
698 * Find the user state context. If there is no user state context,
699 * we just return a 0.
702 savearea
*find_user_regs(thread_act_t act
) {
706 sv
= act
->mact
.pcb
; /* Get the top savearea on the stack */
708 while(sv
) { /* Find the user context */
709 if(sv
->save_srr1
& MASK(MSR_PR
)) { /* Are we looking at the user context? */
710 break; /* Outta here */
712 sv
= sv
->save_hdr
.save_prev
; /* Get the previous context */
715 return sv
; /* Bye bye... */
719 * Find the user state floating point context. If there is no user state context,
720 * we just return a 0.
723 savearea_fpu
*find_user_fpu(thread_act_t act
) {
727 fsv
= act
->mact
.curctx
->FPUsave
; /* Get the start of the floating point chain */
729 while(fsv
) { /* Look until the end or we find it */
730 if(!(fsv
->save_hdr
.save_level
)) break; /* Is the the user state stuff? (the level is 0 if so) */
731 fsv
= (savearea_fpu
*)fsv
->save_hdr
.save_prev
; /* Try the previous one */
734 return fsv
; /* Bye bye... */
738 * Find the user state vector context. If there is no user state context,
739 * we just return a 0.
742 savearea_vec
*find_user_vec(thread_act_t act
) {
746 vsv
= act
->mact
.curctx
->VMXsave
; /* Get the start of the vector chain */
748 while(vsv
) { /* Look until the end or we find it */
749 if(!(vsv
->save_hdr
.save_level
)) break; /* Is the the user state stuff? (the level is 0 if so) */
750 vsv
= (savearea_vec
*)vsv
->save_hdr
.save_prev
; /* Try the previous one */
753 return vsv
; /* Bye bye... */
759 * Return the user stack pointer from the machine
760 * dependent thread state info.
766 thread_state_t tstate
,
768 vm_offset_t
*user_stack
,
772 struct ppc_thread_state
*state
;
778 case PPC_THREAD_STATE
:
779 if (count
< PPC_THREAD_STATE_COUNT
)
780 return (KERN_INVALID_ARGUMENT
);
782 state
= (struct ppc_thread_state
*) tstate
;
784 /* If a valid user stack is specified, use it. */
786 *user_stack
= state
->r1
;
788 if (customstack
&& state
->r1
)
793 return (KERN_INVALID_ARGUMENT
);
796 return (KERN_SUCCESS
);
801 * thread_setuserstack:
803 * Sets the user stack pointer into the machine
804 * dependent thread state info.
806 void thread_setuserstack(struct thread_activation
*act
, unsigned int user_stack
)
810 sv
= get_user_regs(act
); /* Get the user state registers */
812 sv
->save_r1
= user_stack
;
818 * thread_adjuserstack:
820 * Returns the adjusted user stack pointer from the machine
821 * dependent thread state info.
823 unsigned int thread_adjuserstack(struct thread_activation
*act
, int adjust
)
827 sv
= get_user_regs(act
); /* Get the user state registers */
829 sv
->save_r1
+= adjust
; /* Adjust the stack */
831 return sv
->save_r1
; /* Return the adjusted stack */
836 * thread_setentrypoint:
838 * Sets the user PC into the machine
839 * dependent thread state info.
842 void thread_setentrypoint(struct thread_activation
*act
, unsigned int entry
)
846 sv
= get_user_regs(act
); /* Get the user state registers */
848 sv
->save_srr0
= entry
;
857 thread_state_t tstate
,
859 vm_offset_t
*entry_point
862 struct ppc_thread_state
*state
;
867 if (*entry_point
== 0)
868 *entry_point
= VM_MIN_ADDRESS
;
872 case PPC_THREAD_STATE
:
873 if (count
< PPC_THREAD_STATE_COUNT
)
874 return (KERN_INVALID_ARGUMENT
);
876 state
= (struct ppc_thread_state
*) tstate
;
879 * If a valid entry point is specified, use it.
881 *entry_point
= state
->srr0
? state
->srr0
: VM_MIN_ADDRESS
;
884 return (KERN_INVALID_ARGUMENT
);
887 return (KERN_SUCCESS
);
890 unsigned int get_msr_exportmask(void)
892 return (MSR_EXPORT_MASK_SET
);
895 unsigned int get_msr_nbits(void)
897 return (MASK(MSR_POW
)|MASK(MSR_ILE
)|MASK(MSR_IP
)|MASK(MSR_LE
));
899 unsigned int get_msr_rbits(void)
901 return (MASK(MSR_PR
)|MASK(MSR_ME
)|MASK(MSR_IR
)|MASK(MSR_DR
)|MASK(MSR_EE
));
904 void thread_set_child(thread_act_t child
, int pid
)
906 struct savearea
*child_state
;
908 child_state
= get_user_regs(child
);
910 child_state
->save_r3
= pid
;
911 child_state
->save_r4
= 1;
913 void thread_set_parent(thread_act_t parent
, int pid
)
915 struct savearea
*parent_state
;
917 parent_state
= get_user_regs(parent
);
919 parent_state
->save_r3
= pid
;
920 parent_state
->save_r4
= 0;
924 * Saves the complete context (general, floating point, and vector) of the current activation.
925 * We will collect everything into an opaque block of 1 to 3 saveareas and pass back a
928 * The savearea is made to look like it belongs to the source activation. This needs to
929 * be adjusted when these contexts are attached to a new activation.
933 void *act_thread_csave(void) {
936 savearea_fpu
*fsv
, *ofsv
;
937 savearea_vec
*vsv
, *ovsv
;
938 unsigned int spc
, i
, *srs
;
942 act
= current_act(); /* Find ourselves */
944 fpu_save(act
->mact
.curctx
); /* Make certain floating point state is all saved */
945 vec_save(act
->mact
.curctx
); /* Make certain the vector state is all saved */
947 osv
= find_user_regs(act
); /* Get our savearea */
950 panic("act_thread_csave: attempting to preserve the context of an activation with none (%08X)\n", act
);
953 sv
= save_alloc(); /* Get a fresh save area to save into */
954 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use as general */
955 sv
->save_hdr
.save_act
= act
; /* Point to the activation */
956 sv
->save_hdr
.save_prev
= 0; /* Mark no more */
957 sv
->save_hdr
.save_level
= 0; /* Mark user state */
960 bcopy((char *)((unsigned int)osv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
961 (char *)((unsigned int)sv
+ sizeof(savearea_comm
)),
962 sizeof(struct savearea
) - sizeof(savearea_comm
));
964 sv
->save_srr1
&= ~(MASK(MSR_FP
) | MASK(MSR_VEC
)); /* Make certain that floating point and vector are turned off */
966 sv
->save_hdr
.save_misc2
= 0xDEBB1ED0; /* Eye catcher for debug */
967 sv
->save_hdr
.save_misc3
= 0xE5DA11A5; /* Eye catcher for debug */
970 ofsv
= find_user_fpu(act
); /* Get any user floating point */
972 sv
->save_hdr
.save_misc0
= 0; /* Assume no floating point */
974 if(ofsv
) { /* Did we find one? */
975 fsv
= (savearea_fpu
*)save_alloc(); /* If we still don't have one, get a new one */
976 fsv
->save_hdr
.save_flags
= (fsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVfloat
<< SAVtypeshft
); /* Mark as in use as float */
977 fsv
->save_hdr
.save_act
= act
; /* Point to the activation */
978 fsv
->save_hdr
.save_prev
= 0; /* Mark no more */
979 fsv
->save_hdr
.save_level
= 0; /* Mark user state */
980 fsv
->save_hdr
.save_misc2
= 0xDEBB1ED0; /* Eye catcher for debug */
981 fsv
->save_hdr
.save_misc3
= 0xE5DA11A5; /* Eye catcher for debug */
983 sv
->save_hdr
.save_misc0
= (unsigned int)fsv
; /* Remember this one */
985 bcopy((char *)((unsigned int)ofsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
986 (char *)((unsigned int)fsv
+ sizeof(savearea_comm
)),
987 sizeof(struct savearea
) - sizeof(savearea_comm
));
990 ovsv
= find_user_vec(act
); /* Get any user vector */
992 sv
->save_hdr
.save_misc1
= 0; /* Assume no vector */
994 if(ovsv
) { /* Did we find one? */
995 vsv
= (savearea_vec
*)save_alloc(); /* If we still don't have one, get a new one */
996 vsv
->save_hdr
.save_flags
= (vsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVvector
<< SAVtypeshft
); /* Mark as in use as float */
997 vsv
->save_hdr
.save_act
= act
; /* Point to the activation */
998 vsv
->save_hdr
.save_prev
= 0; /* Mark no more */
999 vsv
->save_hdr
.save_level
= 0; /* Mark user state */
1000 vsv
->save_hdr
.save_misc2
= 0xDEBB1ED0; /* Eye catcher for debug */
1001 vsv
->save_hdr
.save_misc3
= 0xE5DA11A5; /* Eye catcher for debug */
1003 sv
->save_hdr
.save_misc1
= (unsigned int)vsv
; /* Chain in the floating point */
1005 bcopy((char *)((unsigned int)ovsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1006 (char *)((unsigned int)vsv
+ sizeof(savearea_comm
)),
1007 sizeof(struct savearea
) - sizeof(savearea_comm
));
1010 return (void *)sv
; /* Bye bye... */
1016 * Attaches saved user state context to an activation. We will replace any
1017 * user state context with what is passed in. The saved context consists of a
1018 * savearea that was setup by
1019 * We will collect everything into one savearea and pass that back.
1021 * The savearea is made to look like it belongs to the source activation. This needs to
1022 * be adjusted when these contexts are attached to a new activation.
1026 void act_thread_catt(void *ctx
) {
1028 savearea
*sv
, *osv
, *psv
;
1029 savearea_fpu
*fsv
, *ofsv
, *pfsv
;
1030 savearea_vec
*vsv
, *ovsv
, *pvsv
;
1031 unsigned int spc
, i
, *srs
;
1034 sv
= (savearea
*)ctx
; /* Make this easier for C */
1036 fsv
= (savearea_fpu
*)sv
->save_hdr
.save_misc0
; /* Get a possible floating point savearea */
1037 vsv
= (savearea_vec
*)sv
->save_hdr
.save_misc1
; /* Get a possible vector savearea */
1039 if((sv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (sv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1040 panic("act_thread_catt: attempt to attach invalid general context savearea - %08X\n", sv
); /* Die */
1043 if(fsv
&& ((fsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (fsv
->save_hdr
.save_misc3
!= 0xE5DA11A5))) { /* See if valid savearea */
1044 panic("act_thread_catt: attempt to attach invalid float context savearea - %08X\n", fsv
); /* Die */
1047 if(vsv
&& ((vsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (vsv
->save_hdr
.save_misc3
!= 0xE5DA11A5))) { /* See if valid savearea */
1048 panic("act_thread_catt: attempt to attach invalid vector context savearea - %08X\n", vsv
); /* Die */
1051 act
= current_act(); /* Find ourselves */
1053 toss_live_fpu(act
->mact
.curctx
); /* Toss my floating point if live anywhere */
1054 toss_live_vec(act
->mact
.curctx
); /* Toss my vector if live anywhere */
1056 sv
->save_hdr
.save_misc2
= 0; /* Eye catcher for debug */
1057 sv
->save_hdr
.save_misc3
= 0; /* Eye catcher for debug */
1058 sv
->save_hdr
.save_act
= act
; /* Set us as owner */
1060 spc
= (unsigned int)act
->map
->pmap
->space
; /* Get the space we're in */
1062 srs
= (unsigned int *)&sv
->save_sr0
; /* Point to the SRs */
1063 for(i
= 0; i
< 16; i
++) { /* Fill in the SRs for the new context */
1064 srs
[i
] = SEG_REG_PROT
| (i
<<20) | spc
; /* Set the SRs */
1067 osv
= act
->mact
.pcb
; /* Get the top general savearea */
1069 while(osv
) { /* Any saved state? */
1070 if(osv
->save_srr1
& MASK(MSR_PR
)) break; /* Leave if this is user state */
1071 psv
= osv
; /* Save previous savearea address */
1072 osv
= osv
->save_hdr
.save_prev
; /* Get one underneath our's */
1075 if(osv
) { /* Did we find one? */
1076 if(psv
) psv
->save_hdr
.save_prev
= 0; /* Yes, clear pointer to it (it should always be last) or */
1077 else act
->mact
.pcb
= 0; /* to the start if the only one */
1079 save_release(osv
); /* Nope, release it */
1083 if(psv
) psv
->save_hdr
.save_prev
= sv
; /* Chain us to the end or */
1084 else act
->mact
.pcb
= (pcb_t
)sv
; /* to the start if the only one */
1086 ovsv
= act
->mact
.curctx
->VMXsave
; /* Get the top vector savearea */
1089 while(ovsv
) { /* Any VMX saved state? */
1090 if(!(ovsv
->save_hdr
.save_level
)) break; /* Leave if this is user state */
1091 pvsv
= ovsv
; /* Save previous savearea address */
1092 ovsv
= (savearea_vec
*)ovsv
->save_hdr
.save_prev
; /* Get one underneath our's */
1095 if(ovsv
) { /* Did we find one? */
1096 if(pvsv
) pvsv
->save_hdr
.save_prev
= 0; /* Yes, clear pointer to it (it should always be last) or */
1097 else act
->mact
.curctx
->VMXsave
= 0; /* to the start if the only one */
1099 save_release((savearea
*)ovsv
); /* Nope, release it */
1102 if(vsv
) { /* Are we sticking any vector on this one? */
1103 if(pvsv
) pvsv
->save_hdr
.save_prev
= (savearea
*)vsv
; /* Yes, chain us to the end or */
1104 else act
->mact
.curctx
->VMXsave
= vsv
; /* to the start if the only one */
1106 vsv
->save_hdr
.save_misc2
= 0; /* Eye catcher for debug */
1107 vsv
->save_hdr
.save_misc3
= 0; /* Eye catcher for debug */
1108 vsv
->save_hdr
.save_act
= act
; /* Set us as owner */
1111 ofsv
= act
->mact
.curctx
->FPUsave
; /* Get the top float savearea */
1114 while(ofsv
) { /* Any float saved state? */
1115 if(!(ofsv
->save_hdr
.save_level
)) break; /* Leave if this is user state */
1116 pfsv
= ofsv
; /* Save previous savearea address */
1117 ofsv
= (savearea_fpu
*)ofsv
->save_hdr
.save_prev
; /* Get one underneath our's */
1120 if(ofsv
) { /* Did we find one? */
1121 if(pfsv
) pfsv
->save_hdr
.save_prev
= 0; /* Yes, clear pointer to it (it should always be last) or */
1122 else act
->mact
.curctx
->FPUsave
= 0; /* to the start if the only one */
1124 save_release((savearea
*)ofsv
); /* Nope, release it */
1127 if(fsv
) { /* Are we sticking any vector on this one? */
1128 if(pfsv
) pfsv
->save_hdr
.save_prev
= (savearea
*)fsv
; /* Yes, chain us to the end or */
1129 else act
->mact
.curctx
->FPUsave
= fsv
; /* to the start if the only one */
1131 fsv
->save_hdr
.save_misc2
= 0; /* Eye catcher for debug */
1132 fsv
->save_hdr
.save_misc3
= 0; /* Eye catcher for debug */
1133 fsv
->save_hdr
.save_act
= act
; /* Set us as owner */
1141 * Releases saved context. We need this because the saved context is opague.
1142 * be adjusted when these contexts are attached to a new activation.
1146 void act_thread_cfree(void *ctx
) {
1149 savearea_fpu
*fsv
, *ofsv
;
1150 savearea_vec
*vsv
, *ovsv
, *pvsv
;
1152 sv
= (savearea
*)ctx
; /* Make this easier for C */
1154 fsv
= (savearea_fpu
*)sv
->save_hdr
.save_misc0
; /* Get a possible floating point savearea */
1155 vsv
= (savearea_vec
*)sv
->save_hdr
.save_misc1
; /* Get a possible vector savearea */
1157 if((sv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (sv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1158 panic("act_thread_cfree: attempt to detatch invalid general context savearea - %08X\n", sv
); /* Die */
1161 save_release(sv
); /* Toss the general savearea */
1163 if(fsv
) { /* See if there is any saved floating point */
1164 if((fsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (fsv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1165 panic("act_thread_cfree: attempt to detatch invalid float context savearea - %08X\n", fsv
); /* Die */
1168 save_release((savearea
*)fsv
); /* Toss saved context */
1171 if(vsv
) { /* See if there is any saved floating point */
1172 if((vsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (vsv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1173 panic("act_thread_cfree: attempt to detatch invalid vector context savearea - %08X\n", vsv
); /* Die */
1176 save_release((savearea
*)vsv
); /* Toss saved context */
1183 * thread_enable_fpe:
1185 * enables or disables floating point exceptions for the thread.
1188 int thread_enable_fpe(thread_act_t act
, int onoff
)
1191 unsigned int oldmsr
;
1193 sv
= find_user_regs(act
); /* Find the user registers */
1194 if(!sv
) sv
= get_user_regs(act
); /* Didn't find any, allocate and initialize o
1197 oldmsr
= sv
->save_srr1
; /* Get the old msr */
1199 if(onoff
) sv
->save_srr1
= oldmsr
| MASK(MSR_FE0
) | MASK(MSR_FE1
); /* Flip on precise FP exceptions */
1200 else sv
->save_srr1
= oldmsr
& ~(MASK(MSR_FE0
) | MASK(MSR_FE1
)); /* Flip on precise FP exceptions */
1202 return ((oldmsr
& (MASK(MSR_FE0
) | MASK(MSR_FE1
))) != 0); /* Return if it was enabled or not */