2 * Copyright (c) 2000-2004 Apple Computer, 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@
33 #include <kern/thread.h>
34 #include <kern/misc_protos.h>
35 #include <mach/ppc/thread_status.h>
36 #include <ppc/proc_reg.h>
37 #include <ppc/cpu_internal.h>
38 #include <ppc/exception.h>
39 #include <ppc/misc_protos.h>
40 #include <ppc/savearea.h>
41 #include <ppc/thread.h>
42 #include <ppc/Firmware.h>
44 //#include <sys/time.h>
45 typedef unsigned int fixpt_t
; /* XXX <sys/resource.h> not self contained */
46 #include <ppc/vmparam.h> /* USRSTACK, etc. */
48 #include <vm/vm_map.h>
50 extern unsigned int killprint
;
51 extern double FloatInit
;
52 extern unsigned long QNaNbarbarian
[4];
54 #define USRSTACK 0xc0000000
75 unsigned int get_msr_exportmask(void);
76 unsigned int get_msr_nbits(void);
77 unsigned int get_msr_rbits(void);
78 void ppc_checkthreadstate(void *, int);
79 void thread_set_child(thread_t child
, int pid
);
80 void thread_set_parent(thread_t parent
, int pid
);
81 void save_release(struct savearea
*save
);
84 * Maps state flavor to number of words in the state:
87 unsigned int _MachineStateCount
[] = {
89 PPC_THREAD_STATE_COUNT
,
90 PPC_FLOAT_STATE_COUNT
,
91 PPC_EXCEPTION_STATE_COUNT
,
92 PPC_VECTOR_STATE_COUNT
,
93 PPC_THREAD_STATE64_COUNT
,
94 PPC_EXCEPTION_STATE64_COUNT
,
100 * Get the status of the specified thread.
104 machine_thread_get_state(
106 thread_flavor_t flavor
,
107 thread_state_t tstate
,
108 mach_msg_type_number_t
*count
)
111 register struct savearea
*sv
; /* Pointer to the context savearea */
112 register savearea_fpu
*fsv
;
113 register savearea_vec
*vsv
;
116 unsigned int vrvalidwrk
;
118 register struct ppc_thread_state
*ts
;
119 register struct ppc_thread_state64
*xts
;
120 register struct ppc_exception_state
*es
;
121 register struct ppc_exception_state64
*xes
;
122 register struct ppc_float_state
*fs
;
123 register struct ppc_vector_state
*vs
;
125 genuser
= find_user_regs(thread
);
129 case THREAD_STATE_FLAVOR_LIST
:
132 return (KERN_INVALID_ARGUMENT
);
135 tstate
[0] = PPC_THREAD_STATE
;
136 tstate
[1] = PPC_FLOAT_STATE
;
137 tstate
[2] = PPC_EXCEPTION_STATE
;
138 tstate
[3] = PPC_VECTOR_STATE
;
139 tstate
[4] = PPC_THREAD_STATE64
;
140 tstate
[5] = PPC_EXCEPTION_STATE64
;
145 case PPC_THREAD_STATE
:
147 if (*count
< PPC_THREAD_STATE_COUNT
) { /* Is the count ok? */
148 return KERN_INVALID_ARGUMENT
;
151 ts
= (struct ppc_thread_state
*) tstate
;
153 sv
= genuser
; /* Copy this over */
155 if(sv
) { /* Is there a save area yet? */
156 ts
->r0
= (unsigned int)sv
->save_r0
;
157 ts
->r1
= (unsigned int)sv
->save_r1
;
158 ts
->r2
= (unsigned int)sv
->save_r2
;
159 ts
->r3
= (unsigned int)sv
->save_r3
;
160 ts
->r4
= (unsigned int)sv
->save_r4
;
161 ts
->r5
= (unsigned int)sv
->save_r5
;
162 ts
->r6
= (unsigned int)sv
->save_r6
;
163 ts
->r7
= (unsigned int)sv
->save_r7
;
164 ts
->r8
= (unsigned int)sv
->save_r8
;
165 ts
->r9
= (unsigned int)sv
->save_r9
;
166 ts
->r10
= (unsigned int)sv
->save_r10
;
167 ts
->r11
= (unsigned int)sv
->save_r11
;
168 ts
->r12
= (unsigned int)sv
->save_r12
;
169 ts
->r13
= (unsigned int)sv
->save_r13
;
170 ts
->r14
= (unsigned int)sv
->save_r14
;
171 ts
->r15
= (unsigned int)sv
->save_r15
;
172 ts
->r16
= (unsigned int)sv
->save_r16
;
173 ts
->r17
= (unsigned int)sv
->save_r17
;
174 ts
->r18
= (unsigned int)sv
->save_r18
;
175 ts
->r19
= (unsigned int)sv
->save_r19
;
176 ts
->r20
= (unsigned int)sv
->save_r20
;
177 ts
->r21
= (unsigned int)sv
->save_r21
;
178 ts
->r22
= (unsigned int)sv
->save_r22
;
179 ts
->r23
= (unsigned int)sv
->save_r23
;
180 ts
->r24
= (unsigned int)sv
->save_r24
;
181 ts
->r25
= (unsigned int)sv
->save_r25
;
182 ts
->r26
= (unsigned int)sv
->save_r26
;
183 ts
->r27
= (unsigned int)sv
->save_r27
;
184 ts
->r28
= (unsigned int)sv
->save_r28
;
185 ts
->r29
= (unsigned int)sv
->save_r29
;
186 ts
->r30
= (unsigned int)sv
->save_r30
;
187 ts
->r31
= (unsigned int)sv
->save_r31
;
188 ts
->cr
= (unsigned int)sv
->save_cr
;
189 ts
->xer
= (unsigned int)sv
->save_xer
;
190 ts
->lr
= (unsigned int)sv
->save_lr
;
191 ts
->ctr
= (unsigned int)sv
->save_ctr
;
192 ts
->srr0
= (unsigned int)sv
->save_srr0
;
193 ts
->srr1
= (unsigned int)sv
->save_srr1
;
194 ts
->mq
= 0; /* MQ register (601 only) */
195 ts
->vrsave
= (unsigned int)sv
->save_vrsave
; /* VRSAVE register (Altivec only) */
197 else { /* No user state yet. Save seemingly random values. */
199 for(i
=0; i
< 32; i
+=2) { /* Fill up with defaults */
200 ((unsigned int *)&ts
->r0
)[i
] = ((unsigned int *)&FloatInit
)[0];
201 ((unsigned int *)&ts
->r0
)[i
+1] = ((unsigned int *)&FloatInit
)[1];
205 ts
->lr
= ((unsigned int *)&FloatInit
)[0];
206 ts
->ctr
= ((unsigned int *)&FloatInit
)[1];
207 ts
->srr0
= ((unsigned int *)&FloatInit
)[0];
208 ts
->srr1
= MSR_EXPORT_MASK_SET
;
210 ts
->vrsave
= 0; /* VRSAVE register (Altivec only) */
213 *count
= PPC_THREAD_STATE_COUNT
; /* Pass back the amount we actually copied */
217 case PPC_THREAD_STATE64
:
219 if (*count
< PPC_THREAD_STATE64_COUNT
) { /* Is the count ok? */
220 return KERN_INVALID_ARGUMENT
;
223 xts
= (struct ppc_thread_state64
*) tstate
;
225 sv
= genuser
; /* Copy this over */
227 if(sv
) { /* Is there a save area yet? */
228 xts
->r0
= sv
->save_r0
;
229 xts
->r1
= sv
->save_r1
;
230 xts
->r2
= sv
->save_r2
;
231 xts
->r3
= sv
->save_r3
;
232 xts
->r4
= sv
->save_r4
;
233 xts
->r5
= sv
->save_r5
;
234 xts
->r6
= sv
->save_r6
;
235 xts
->r7
= sv
->save_r7
;
236 xts
->r8
= sv
->save_r8
;
237 xts
->r9
= sv
->save_r9
;
238 xts
->r10
= sv
->save_r10
;
239 xts
->r11
= sv
->save_r11
;
240 xts
->r12
= sv
->save_r12
;
241 xts
->r13
= sv
->save_r13
;
242 xts
->r14
= sv
->save_r14
;
243 xts
->r15
= sv
->save_r15
;
244 xts
->r16
= sv
->save_r16
;
245 xts
->r17
= sv
->save_r17
;
246 xts
->r18
= sv
->save_r18
;
247 xts
->r19
= sv
->save_r19
;
248 xts
->r20
= sv
->save_r20
;
249 xts
->r21
= sv
->save_r21
;
250 xts
->r22
= sv
->save_r22
;
251 xts
->r23
= sv
->save_r23
;
252 xts
->r24
= sv
->save_r24
;
253 xts
->r25
= sv
->save_r25
;
254 xts
->r26
= sv
->save_r26
;
255 xts
->r27
= sv
->save_r27
;
256 xts
->r28
= sv
->save_r28
;
257 xts
->r29
= sv
->save_r29
;
258 xts
->r30
= sv
->save_r30
;
259 xts
->r31
= sv
->save_r31
;
260 xts
->cr
= sv
->save_cr
;
261 xts
->xer
= sv
->save_xer
;
262 xts
->lr
= sv
->save_lr
;
263 xts
->ctr
= sv
->save_ctr
;
264 xts
->srr0
= sv
->save_srr0
;
265 xts
->srr1
= sv
->save_srr1
;
266 xts
->vrsave
= sv
->save_vrsave
; /* VRSAVE register (Altivec only) */
268 else { /* No user state yet. Save seemingly random values. */
270 for(i
=0; i
< 32; i
++) { /* Fill up with defaults */
271 ((unsigned long long *)&xts
->r0
)[i
] = ((unsigned long long *)&FloatInit
)[0];
275 xts
->lr
= ((unsigned long long *)&FloatInit
)[0];
276 xts
->ctr
= ((unsigned long long *)&FloatInit
)[0];
277 xts
->srr0
= ((unsigned long long *)&FloatInit
)[0];
278 xts
->srr1
= MSR_EXPORT_MASK_SET
;
279 if(task_has_64BitAddr(thread
->task
))
280 xts
->srr1
|= (uint64_t)MASK32(MSR_SF
) << 32; /* If 64-bit task, force 64-bit mode */
281 xts
->vrsave
= 0; /* VRSAVE register (Altivec only) */
284 *count
= PPC_THREAD_STATE64_COUNT
; /* Pass back the amount we actually copied */
287 case PPC_EXCEPTION_STATE
:
289 if (*count
< PPC_EXCEPTION_STATE_COUNT
) {
290 return KERN_INVALID_ARGUMENT
;
293 es
= (struct ppc_exception_state
*) tstate
;
294 sv
= genuser
; /* Copy this over */
296 if(sv
) { /* See if valid state yet */
297 es
->dar
= (unsigned int)sv
->save_dar
;
298 es
->dsisr
= sv
->save_dsisr
;
299 es
->exception
= sv
->save_exception
;
301 else { /* Nope, not yet */
304 es
->exception
= ((unsigned int *)&FloatInit
)[0];
307 *count
= PPC_EXCEPTION_STATE_COUNT
;
310 case PPC_EXCEPTION_STATE64
:
312 if (*count
< PPC_EXCEPTION_STATE64_COUNT
) {
313 return KERN_INVALID_ARGUMENT
;
316 xes
= (struct ppc_exception_state64
*) tstate
;
317 sv
= genuser
; /* Copy this over */
319 if(sv
) { /* See if valid state yet */
320 xes
->dar
= sv
->save_dar
;
321 xes
->dsisr
= sv
->save_dsisr
;
322 xes
->exception
= sv
->save_exception
;
324 else { /* Nope, not yet */
327 xes
->exception
= ((unsigned int *)&FloatInit
)[0];
330 *count
= PPC_EXCEPTION_STATE64_COUNT
;
333 case PPC_FLOAT_STATE
:
335 if (*count
< PPC_FLOAT_STATE_COUNT
) {
336 return KERN_INVALID_ARGUMENT
;
339 fpu_save(thread
->machine
.curctx
); /* Just in case it's live, save it */
341 fs
= (struct ppc_float_state
*) tstate
; /* Point to destination */
343 fsv
= find_user_fpu(thread
); /* Get the user's fpu savearea */
345 if(fsv
) { /* See if we have any */
346 bcopy((char *)&fsv
->save_fp0
, (char *)fs
, 32*8); /* 32 registers */
347 fs
->fpscr_pad
= 0; /* Be clean and tidy */
348 if(genuser
) fs
->fpscr
= genuser
->save_fpscr
; /* Set the fpscr value to general */
349 else fs
->fpscr
= 0; /* If no user, initialize this */
351 else { /* No floating point yet */
353 for(i
=0; i
< 32; i
++) { /* Initialize floating points */
354 fs
->fpregs
[i
] = FloatInit
; /* Initial value */
356 fs
->fpscr_pad
= 0; /* Initial value */
357 fs
->fpscr
= 0; /* Initial value */
360 *count
= PPC_FLOAT_STATE_COUNT
;
364 case PPC_VECTOR_STATE
:
366 if (*count
< PPC_VECTOR_STATE_COUNT
) {
367 return KERN_INVALID_ARGUMENT
;
370 vec_save(thread
->machine
.curctx
); /* Just in case it's live, save it */
372 vs
= (struct ppc_vector_state
*) tstate
; /* Point to destination */
374 vsv
= find_user_vec(thread
); /* Find the vector savearea */
376 if(vsv
) { /* See if we have any */
378 vrvalidwrk
= vsv
->save_vrvalid
; /* Get the valid flags */
379 vs
->save_vrvalid
= vsv
->save_vrvalid
; /* Set the valid flags */
380 if(genuser
) for(j
=0; j
< 4; j
++) vs
->save_vscr
[j
] = genuser
->save_vscr
[j
]; /* Set value for vscr */
382 vs
->save_vscr
[0] = 0; /* Set an initial value if no general user yet */
383 vs
->save_vscr
[1] = 0;
384 vs
->save_vscr
[2] = 0;
385 vs
->save_vscr
[3] = 0x00010000; /* Always start with Java mode off */
387 for(i
=0; i
< 32; i
++) { /* Copy the saved registers and invalidate the others */
388 for(j
=0; j
< 4; j
++) {
389 if(vrvalidwrk
& 0x80000000) (vs
->save_vr
)[i
][j
] =
390 ((unsigned int *)&(vsv
->save_vr0
))[(i
* 4) + j
]; /* We have this register saved */
391 else vs
->save_vr
[i
][j
] = QNaNbarbarian
[j
]; /* Set invalid value */
393 vrvalidwrk
= vrvalidwrk
<< 1; /* Shift over to the next */
396 else { /* No vector yet */
398 for(i
=0; i
< 32; i
++) { /* Initialize vector registers */
399 for(j
=0; j
< 4; j
++) vs
->save_vr
[i
][j
] = QNaNbarbarian
[j
]; /* Initial value */
402 if(genuser
) for(j
=0; j
< 4; j
++) vs
->save_vscr
[j
] = genuser
->save_vscr
[j
]; /* Set value for vscr */
404 vs
->save_vscr
[0] = 0; /* Set an initial value if no general user yet */
405 vs
->save_vscr
[1] = 0;
406 vs
->save_vscr
[2] = 0;
407 vs
->save_vscr
[3] = 0x00010000; /* Always start with Java mode off */
409 vs
->save_vrvalid
= 0; /* Clear the valid flags */
412 for (i
=0; i
< 4; i
++) vs
->save_pad5
[i
] = 0; /* Clear cruft */
413 for (i
=0; i
< 7; i
++) vs
->save_pad6
[i
] = 0; /* Clear cruft */
415 *count
= PPC_VECTOR_STATE_COUNT
;
419 return KERN_INVALID_ARGUMENT
;
422 /* Close cousin of machine_thread_get_state().
423 * This function is currently incomplete since we don't really need vector
424 * or FP for the core dump (the save area can be accessed directly if the
425 * user is so inclined). Also the function name is something of a misnomer,
426 * see the comment above find_kern_regs().
430 machine_thread_get_kern_state(
432 thread_flavor_t flavor
,
433 thread_state_t tstate
,
434 mach_msg_type_number_t
*count
)
437 register struct savearea
*sv
; /* Pointer to the context savearea */
441 register struct ppc_thread_state
*ts
;
442 register struct ppc_thread_state64
*xts
;
443 register struct ppc_exception_state
*es
;
444 register struct ppc_exception_state64
*xes
;
446 genkern
= find_kern_regs(thread
);
450 case THREAD_STATE_FLAVOR_LIST
:
453 return (KERN_INVALID_ARGUMENT
);
456 tstate
[0] = PPC_THREAD_STATE
;
457 tstate
[1] = PPC_FLOAT_STATE
;
458 tstate
[2] = PPC_EXCEPTION_STATE
;
459 tstate
[3] = PPC_VECTOR_STATE
;
460 tstate
[4] = PPC_THREAD_STATE64
;
461 tstate
[5] = PPC_EXCEPTION_STATE64
;
466 case PPC_THREAD_STATE
:
468 if (*count
< PPC_THREAD_STATE_COUNT
) { /* Is the count ok? */
469 return KERN_INVALID_ARGUMENT
;
472 ts
= (struct ppc_thread_state
*) tstate
;
474 sv
= genkern
; /* Copy this over */
476 if(sv
) { /* Is there a save area yet? */
477 ts
->r0
= (unsigned int)sv
->save_r0
;
478 ts
->r1
= (unsigned int)sv
->save_r1
;
479 ts
->r2
= (unsigned int)sv
->save_r2
;
480 ts
->r3
= (unsigned int)sv
->save_r3
;
481 ts
->r4
= (unsigned int)sv
->save_r4
;
482 ts
->r5
= (unsigned int)sv
->save_r5
;
483 ts
->r6
= (unsigned int)sv
->save_r6
;
484 ts
->r7
= (unsigned int)sv
->save_r7
;
485 ts
->r8
= (unsigned int)sv
->save_r8
;
486 ts
->r9
= (unsigned int)sv
->save_r9
;
487 ts
->r10
= (unsigned int)sv
->save_r10
;
488 ts
->r11
= (unsigned int)sv
->save_r11
;
489 ts
->r12
= (unsigned int)sv
->save_r12
;
490 ts
->r13
= (unsigned int)sv
->save_r13
;
491 ts
->r14
= (unsigned int)sv
->save_r14
;
492 ts
->r15
= (unsigned int)sv
->save_r15
;
493 ts
->r16
= (unsigned int)sv
->save_r16
;
494 ts
->r17
= (unsigned int)sv
->save_r17
;
495 ts
->r18
= (unsigned int)sv
->save_r18
;
496 ts
->r19
= (unsigned int)sv
->save_r19
;
497 ts
->r20
= (unsigned int)sv
->save_r20
;
498 ts
->r21
= (unsigned int)sv
->save_r21
;
499 ts
->r22
= (unsigned int)sv
->save_r22
;
500 ts
->r23
= (unsigned int)sv
->save_r23
;
501 ts
->r24
= (unsigned int)sv
->save_r24
;
502 ts
->r25
= (unsigned int)sv
->save_r25
;
503 ts
->r26
= (unsigned int)sv
->save_r26
;
504 ts
->r27
= (unsigned int)sv
->save_r27
;
505 ts
->r28
= (unsigned int)sv
->save_r28
;
506 ts
->r29
= (unsigned int)sv
->save_r29
;
507 ts
->r30
= (unsigned int)sv
->save_r30
;
508 ts
->r31
= (unsigned int)sv
->save_r31
;
509 ts
->cr
= (unsigned int)sv
->save_cr
;
510 ts
->xer
= (unsigned int)sv
->save_xer
;
511 ts
->lr
= (unsigned int)sv
->save_lr
;
512 ts
->ctr
= (unsigned int)sv
->save_ctr
;
513 ts
->srr0
= (unsigned int)sv
->save_srr0
;
514 ts
->srr1
= (unsigned int)sv
->save_srr1
;
515 ts
->mq
= 0; /* MQ register (601 only) */
516 ts
->vrsave
= (unsigned int)sv
->save_vrsave
; /* VRSAVE register (Altivec only) */
518 else { /* No state yet. Save seemingly random values. */
520 for(i
=0; i
< 32; i
+=2) { /* Fill up with defaults */
521 ((unsigned int *)&ts
->r0
)[i
] = ((unsigned int *)&FloatInit
)[0];
522 ((unsigned int *)&ts
->r0
)[i
+1] = ((unsigned int *)&FloatInit
)[1];
526 ts
->lr
= ((unsigned int *)&FloatInit
)[0];
527 ts
->ctr
= ((unsigned int *)&FloatInit
)[1];
528 ts
->srr0
= ((unsigned int *)&FloatInit
)[0];
529 ts
->srr1
= MSR_EXPORT_MASK_SET
;
531 ts
->vrsave
= 0; /* VRSAVE register (Altivec only) */
534 *count
= PPC_THREAD_STATE_COUNT
; /* Pass back the amount we actually copied */
538 case PPC_THREAD_STATE64
:
540 if (*count
< PPC_THREAD_STATE64_COUNT
) { /* Is the count ok? */
541 return KERN_INVALID_ARGUMENT
;
544 xts
= (struct ppc_thread_state64
*) tstate
;
546 sv
= genkern
; /* Copy this over */
548 if(sv
) { /* Is there a save area yet? */
549 xts
->r0
= sv
->save_r0
;
550 xts
->r1
= sv
->save_r1
;
551 xts
->r2
= sv
->save_r2
;
552 xts
->r3
= sv
->save_r3
;
553 xts
->r4
= sv
->save_r4
;
554 xts
->r5
= sv
->save_r5
;
555 xts
->r6
= sv
->save_r6
;
556 xts
->r7
= sv
->save_r7
;
557 xts
->r8
= sv
->save_r8
;
558 xts
->r9
= sv
->save_r9
;
559 xts
->r10
= sv
->save_r10
;
560 xts
->r11
= sv
->save_r11
;
561 xts
->r12
= sv
->save_r12
;
562 xts
->r13
= sv
->save_r13
;
563 xts
->r14
= sv
->save_r14
;
564 xts
->r15
= sv
->save_r15
;
565 xts
->r16
= sv
->save_r16
;
566 xts
->r17
= sv
->save_r17
;
567 xts
->r18
= sv
->save_r18
;
568 xts
->r19
= sv
->save_r19
;
569 xts
->r20
= sv
->save_r20
;
570 xts
->r21
= sv
->save_r21
;
571 xts
->r22
= sv
->save_r22
;
572 xts
->r23
= sv
->save_r23
;
573 xts
->r24
= sv
->save_r24
;
574 xts
->r25
= sv
->save_r25
;
575 xts
->r26
= sv
->save_r26
;
576 xts
->r27
= sv
->save_r27
;
577 xts
->r28
= sv
->save_r28
;
578 xts
->r29
= sv
->save_r29
;
579 xts
->r30
= sv
->save_r30
;
580 xts
->r31
= sv
->save_r31
;
581 xts
->cr
= sv
->save_cr
;
582 xts
->xer
= sv
->save_xer
;
583 xts
->lr
= sv
->save_lr
;
584 xts
->ctr
= sv
->save_ctr
;
585 xts
->srr0
= sv
->save_srr0
;
586 xts
->srr1
= sv
->save_srr1
;
587 xts
->vrsave
= sv
->save_vrsave
; /* VRSAVE register (Altivec only) */
589 else { /* No user state yet. Save seemingly random values. */
591 for(i
=0; i
< 32; i
++) { /* Fill up with defaults */
592 ((unsigned long long *)&xts
->r0
)[i
] = ((unsigned long long *)&FloatInit
)[0];
596 xts
->lr
= ((unsigned long long *)&FloatInit
)[0];
597 xts
->ctr
= ((unsigned long long *)&FloatInit
)[0];
598 xts
->srr0
= ((unsigned long long *)&FloatInit
)[0];
599 xts
->srr1
= MSR_EXPORT_MASK_SET
;
600 xts
->vrsave
= 0; /* VRSAVE register (Altivec only) */
603 *count
= PPC_THREAD_STATE64_COUNT
; /* Pass back the amount we actually copied */
606 case PPC_EXCEPTION_STATE
:
608 if (*count
< PPC_EXCEPTION_STATE_COUNT
) {
609 return KERN_INVALID_ARGUMENT
;
612 es
= (struct ppc_exception_state
*) tstate
;
613 sv
= genkern
; /* Copy this over */
615 if(sv
) { /* See if valid state yet */
616 es
->dar
= (unsigned int)sv
->save_dar
;
617 es
->dsisr
= sv
->save_dsisr
;
618 es
->exception
= sv
->save_exception
;
620 else { /* Nope, not yet */
623 es
->exception
= ((unsigned int *)&FloatInit
)[0];
626 *count
= PPC_EXCEPTION_STATE_COUNT
;
629 case PPC_EXCEPTION_STATE64
:
631 if (*count
< PPC_EXCEPTION_STATE64_COUNT
) {
632 return KERN_INVALID_ARGUMENT
;
635 xes
= (struct ppc_exception_state64
*) tstate
;
636 sv
= genkern
; /* Copy this over */
638 if(sv
) { /* See if valid state yet */
639 xes
->dar
= sv
->save_dar
;
640 xes
->dsisr
= sv
->save_dsisr
;
641 xes
->exception
= sv
->save_exception
;
643 else { /* Nope, not yet */
646 xes
->exception
= ((unsigned int *)&FloatInit
)[0];
649 *count
= PPC_EXCEPTION_STATE64_COUNT
;
653 return KERN_INVALID_ARGUMENT
;
661 * Set the status of the specified thread.
664 machine_thread_set_state(
666 thread_flavor_t flavor
,
667 thread_state_t tstate
,
668 mach_msg_type_number_t count
)
672 savearea_fpu
*fsv
, *fsvn
, *fsvo
;
673 savearea_vec
*vsv
, *vsvn
, *vsvo
;
676 register struct ppc_thread_state
*ts
;
677 register struct ppc_thread_state64
*xts
;
678 register struct ppc_exception_state
*es
;
679 register struct ppc_exception_state
*xes
;
680 register struct ppc_float_state
*fs
;
681 register struct ppc_vector_state
*vs
;
683 // dbgTrace((unsigned int)thr_act, (unsigned int)0 /*sv: was never set*/, flavor); /* (TEST/DEBUG) */
685 clgn
= count
; /* Get the count */
687 switch (flavor
) { /* Validate the count before we do anything else */
688 case PPC_THREAD_STATE
:
690 if (clgn
< PPC_THREAD_STATE_COUNT
) { /* Is it too short? */
691 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
695 case PPC_THREAD_STATE64
:
697 if (clgn
< PPC_THREAD_STATE64_COUNT
) { /* Is it too short? */
698 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
702 case PPC_EXCEPTION_STATE
:
704 if (clgn
< PPC_EXCEPTION_STATE_COUNT
) { /* Is it too short? */
705 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
708 case PPC_EXCEPTION_STATE64
:
710 if (clgn
< PPC_EXCEPTION_STATE64_COUNT
) { /* Is it too short? */
711 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
716 case PPC_FLOAT_STATE
:
718 if (clgn
< PPC_FLOAT_STATE_COUNT
) { /* Is it too short? */
719 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
725 case PPC_VECTOR_STATE
:
727 if (clgn
< PPC_VECTOR_STATE_COUNT
) { /* Is it too short? */
728 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
734 return KERN_INVALID_ARGUMENT
;
737 genuser
= get_user_regs(thread
); /* Find or allocate and initialize one */
741 case PPC_THREAD_STATE
:
743 ts
= (struct ppc_thread_state
*)tstate
;
745 genuser
->save_r0
= (uint64_t)ts
->r0
;
746 genuser
->save_r1
= (uint64_t)ts
->r1
;
747 genuser
->save_r2
= (uint64_t)ts
->r2
;
748 genuser
->save_r3
= (uint64_t)ts
->r3
;
749 genuser
->save_r4
= (uint64_t)ts
->r4
;
750 genuser
->save_r5
= (uint64_t)ts
->r5
;
751 genuser
->save_r6
= (uint64_t)ts
->r6
;
752 genuser
->save_r7
= (uint64_t)ts
->r7
;
753 genuser
->save_r8
= (uint64_t)ts
->r8
;
754 genuser
->save_r9
= (uint64_t)ts
->r9
;
755 genuser
->save_r10
= (uint64_t)ts
->r10
;
756 genuser
->save_r11
= (uint64_t)ts
->r11
;
757 genuser
->save_r12
= (uint64_t)ts
->r12
;
758 genuser
->save_r13
= (uint64_t)ts
->r13
;
759 genuser
->save_r14
= (uint64_t)ts
->r14
;
760 genuser
->save_r15
= (uint64_t)ts
->r15
;
761 genuser
->save_r16
= (uint64_t)ts
->r16
;
762 genuser
->save_r17
= (uint64_t)ts
->r17
;
763 genuser
->save_r18
= (uint64_t)ts
->r18
;
764 genuser
->save_r19
= (uint64_t)ts
->r19
;
765 genuser
->save_r20
= (uint64_t)ts
->r20
;
766 genuser
->save_r21
= (uint64_t)ts
->r21
;
767 genuser
->save_r22
= (uint64_t)ts
->r22
;
768 genuser
->save_r23
= (uint64_t)ts
->r23
;
769 genuser
->save_r24
= (uint64_t)ts
->r24
;
770 genuser
->save_r25
= (uint64_t)ts
->r25
;
771 genuser
->save_r26
= (uint64_t)ts
->r26
;
772 genuser
->save_r27
= (uint64_t)ts
->r27
;
773 genuser
->save_r28
= (uint64_t)ts
->r28
;
774 genuser
->save_r29
= (uint64_t)ts
->r29
;
775 genuser
->save_r30
= (uint64_t)ts
->r30
;
776 genuser
->save_r31
= (uint64_t)ts
->r31
;
778 genuser
->save_cr
= ts
->cr
;
779 genuser
->save_xer
= (uint64_t)ts
->xer
;
780 genuser
->save_lr
= (uint64_t)ts
->lr
;
781 genuser
->save_ctr
= (uint64_t)ts
->ctr
;
782 genuser
->save_srr0
= (uint64_t)ts
->srr0
;
783 genuser
->save_vrsave
= ts
->vrsave
; /* VRSAVE register (Altivec only) */
785 genuser
->save_srr1
= MSR_PREPARE_FOR_IMPORT(genuser
->save_srr1
, ts
->srr1
); /* Set the bits we can change */
787 genuser
->save_srr1
|= MSR_EXPORT_MASK_SET
;
789 genuser
->save_srr1
&= ~(MASK(MSR_FP
) | MASK(MSR_VEC
)); /* Make sure we don't enable the floating point unit */
791 if(task_has_64BitAddr(thread
->task
))
792 genuser
->save_srr1
|= (uint64_t)MASK32(MSR_SF
) << 32; /* If 64-bit task, force 64-bit mode */
794 genuser
->save_srr1
&= ~((uint64_t)MASK32(MSR_SF
) << 32); /* else 32-bit mode */
799 case PPC_THREAD_STATE64
:
801 xts
= (struct ppc_thread_state64
*)tstate
;
803 genuser
->save_r0
= xts
->r0
;
804 genuser
->save_r1
= xts
->r1
;
805 genuser
->save_r2
= xts
->r2
;
806 genuser
->save_r3
= xts
->r3
;
807 genuser
->save_r4
= xts
->r4
;
808 genuser
->save_r5
= xts
->r5
;
809 genuser
->save_r6
= xts
->r6
;
810 genuser
->save_r7
= xts
->r7
;
811 genuser
->save_r8
= xts
->r8
;
812 genuser
->save_r9
= xts
->r9
;
813 genuser
->save_r10
= xts
->r10
;
814 genuser
->save_r11
= xts
->r11
;
815 genuser
->save_r12
= xts
->r12
;
816 genuser
->save_r13
= xts
->r13
;
817 genuser
->save_r14
= xts
->r14
;
818 genuser
->save_r15
= xts
->r15
;
819 genuser
->save_r16
= xts
->r16
;
820 genuser
->save_r17
= xts
->r17
;
821 genuser
->save_r18
= xts
->r18
;
822 genuser
->save_r19
= xts
->r19
;
823 genuser
->save_r20
= xts
->r20
;
824 genuser
->save_r21
= xts
->r21
;
825 genuser
->save_r22
= xts
->r22
;
826 genuser
->save_r23
= xts
->r23
;
827 genuser
->save_r24
= xts
->r24
;
828 genuser
->save_r25
= xts
->r25
;
829 genuser
->save_r26
= xts
->r26
;
830 genuser
->save_r27
= xts
->r27
;
831 genuser
->save_r28
= xts
->r28
;
832 genuser
->save_r29
= xts
->r29
;
833 genuser
->save_r30
= xts
->r30
;
834 genuser
->save_r31
= xts
->r31
;
836 genuser
->save_cr
= xts
->cr
;
837 genuser
->save_xer
= xts
->xer
;
838 genuser
->save_lr
= xts
->lr
;
839 genuser
->save_ctr
= xts
->ctr
;
840 genuser
->save_srr0
= xts
->srr0
;
841 genuser
->save_vrsave
= xts
->vrsave
; /* VRSAVE register (Altivec only) */
843 genuser
->save_srr1
= MSR_PREPARE_FOR_IMPORT(genuser
->save_srr1
, xts
->srr1
); /* Set the bits we can change */
845 genuser
->save_srr1
|= MSR_EXPORT_MASK_SET
;
847 genuser
->save_srr1
&= ~(MASK(MSR_FP
) | MASK(MSR_VEC
)); /* Make sure we don't enable the floating point unit */
849 if(task_has_64BitAddr(thread
->task
))
850 genuser
->save_srr1
|= (uint64_t)MASK32(MSR_SF
) << 32; /* If 64-bit task, force 64-bit mode */
852 genuser
->save_srr1
&= ~((uint64_t)MASK32(MSR_SF
) << 32); /* else 32-bit mode */
857 case PPC_EXCEPTION_STATE
:
859 es
= (struct ppc_exception_state
*) tstate
;
861 genuser
->save_dar
= (uint64_t)es
->dar
;
862 genuser
->save_dsisr
= es
->dsisr
;
863 genuser
->save_exception
= es
->exception
;
868 * It's pretty worthless to try to change this stuff, but we'll do it anyway.
871 case PPC_EXCEPTION_STATE64
:
873 xes
= (struct ppc_exception_state
*) tstate
;
875 genuser
->save_dar
= xes
->dar
;
876 genuser
->save_dsisr
= xes
->dsisr
;
877 genuser
->save_exception
= xes
->exception
;
881 case PPC_FLOAT_STATE
:
883 toss_live_fpu(thread
->machine
.curctx
); /* Toss my floating point if live anywhere */
885 fsv
= find_user_fpu(thread
); /* Get the user's floating point context */
887 if(!fsv
) { /* Do we have one yet? */
888 fsv
= (savearea_fpu
*)save_alloc(); /* If we still don't have one, get a new one */
889 fsv
->save_hdr
.save_flags
= (fsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVfloat
<< SAVtypeshft
); /* Mark as in use as float */
890 fsv
->save_hdr
.save_act
= thread
;
891 fsv
->save_hdr
.save_prev
= 0; /* Mark no more */
892 fsv
->save_hdr
.save_level
= 0; /* Mark user state */
894 if(!thread
->machine
.curctx
->FPUsave
) thread
->machine
.curctx
->FPUsave
= fsv
; /* If no floating point, chain us first */
897 fsvn
= fsvo
= thread
->machine
.curctx
->FPUsave
; /* Remember first one */
899 while (fsvn
) { /* Go until we hit the end */
900 fsvo
= fsvn
; /* Remember the previous one */
901 fsvn
= CAST_DOWN(savearea_fpu
*, fsvo
->save_hdr
.save_prev
); /* Skip on to the next */
904 fsvo
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)fsv
); /* Queue us on in */
909 fs
= (struct ppc_float_state
*) tstate
; /* Point to source */
912 bcopy((char *)fs
, (char *)&fsv
->save_fp0
, 32*8); /* Move in the 32 registers */
914 genuser
->save_fpscr
= fs
->fpscr
; /* Copy the fpscr value to normal */
919 case PPC_VECTOR_STATE
:
921 toss_live_vec(thread
->machine
.curctx
); /* Toss my vector if live anywhere */
923 vsv
= find_user_vec(thread
); /* Get the user's vector context */
925 if(!vsv
) { /* Do we have one yet? */
926 vsv
= (savearea_vec
*)save_alloc(); /* If we still don't have one, get a new one */
927 vsv
->save_hdr
.save_flags
= (vsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVvector
<< SAVtypeshft
); /* Mark as in use as vector */
928 vsv
->save_hdr
.save_act
= thread
;
929 vsv
->save_hdr
.save_prev
= 0; /* Mark no more */
930 vsv
->save_hdr
.save_level
= 0; /* Mark user state */
932 if(!thread
->machine
.curctx
->VMXsave
) thread
->machine
.curctx
->VMXsave
= vsv
; /* If no vector, chain us first */
935 vsvn
= vsvo
= thread
->machine
.curctx
->VMXsave
; /* Remember first one */
937 while (vsvn
) { /* Go until we hit the end */
938 vsvo
= vsvn
; /* Remember the previous one */
939 vsvn
= CAST_DOWN(savearea_vec
*, vsvo
->save_hdr
.save_prev
); /* Skip on to the next */
942 vsvo
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)vsv
); /* Queue us on in */
947 vs
= (struct ppc_vector_state
*) tstate
; /* Point to source */
949 bcopy((char *)vs
, (char *)&vsv
->save_vr0
, 32*16); /* 32 registers plus status and validity and pad */
950 vsv
->save_vrvalid
= vs
->save_vrvalid
; /* Set validity bits */
953 for(i
= 0; i
< 4; i
++) genuser
->save_vscr
[i
] = vs
->save_vscr
[i
]; /* Set value for vscr */
959 return KERN_INVALID_ARGUMENT
;
965 * This is where registers that are not normally specified by the mach-o
966 * file on an execve should be nullified, perhaps to avoid a covert channel.
967 * We've never bothered to clear FPRs or VRs, but it is important to clear
968 * the FPSCR, which is kept in the general state but not set by the general
969 * flavor (ie, PPC_THREAD_STATE or PPC_THREAD_STATE64.)
972 machine_thread_state_initialize(
977 sv
= get_user_regs(thread
); /* Find or allocate and initialize one */
979 sv
->save_fpscr
= 0; /* Clear all floating point exceptions */
980 sv
->save_vrsave
= 0; /* Set the vector save state */
981 sv
->save_vscr
[0] = 0x00000000;
982 sv
->save_vscr
[1] = 0x00000000;
983 sv
->save_vscr
[2] = 0x00000000;
984 sv
->save_vscr
[3] = 0x00010000; /* Disable java mode and clear saturated */
991 * Duplicates the context of one thread into a new one.
992 * The new thread is assumed to be new and have no user state contexts except maybe a general one.
993 * We also assume that the old thread can't be running anywhere.
995 * We're only going to be duplicating user context here. That means that we will have to
996 * eliminate any floating point or vector kernel contexts and carry across the user state ones.
1005 savearea_fpu
*fsv
, *fsvn
;
1006 savearea_vec
*vsv
, *vsvn
;
1008 fpu_save(self
->machine
.curctx
); /* Make certain floating point state is all saved */
1009 vec_save(self
->machine
.curctx
); /* Make certain the vector state is all saved */
1011 sv
= get_user_regs(target
); /* Allocate and initialze context in the new activation */
1013 osv
= find_user_regs(self
); /* Find the original context */
1015 return (KERN_FAILURE
);
1017 bcopy((char *)((unsigned int)osv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1018 (char *)((unsigned int)sv
+ sizeof(savearea_comm
)),
1019 sizeof(struct savearea
) - sizeof(savearea_comm
));
1021 sv
->save_srr1
&= (uint64_t)(~(MASK(MSR_FP
) | MASK(MSR_VEC
))); /* Make certain that floating point and vector are turned off */
1023 fsv
= find_user_fpu(self
); /* Get any user floating point */
1025 target
->machine
.curctx
->FPUsave
= 0; /* Assume no floating point */
1027 if(fsv
) { /* Did we find one? */
1028 fsvn
= (savearea_fpu
*)save_alloc(); /* If we still don't have one, get a new one */
1029 fsvn
->save_hdr
.save_flags
= (fsvn
->save_hdr
.save_flags
& ~SAVtype
) | (SAVfloat
<< SAVtypeshft
); /* Mark as in use as float */
1030 fsvn
->save_hdr
.save_act
= target
;
1031 fsvn
->save_hdr
.save_prev
= 0; /* Mark no more */
1032 fsvn
->save_hdr
.save_level
= 0; /* Mark user state */
1034 target
->machine
.curctx
->FPUsave
= fsvn
; /* Chain in the floating point */
1036 bcopy((char *)((unsigned int)fsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1037 (char *)((unsigned int)fsvn
+ sizeof(savearea_comm
)),
1038 sizeof(struct savearea
) - sizeof(savearea_comm
));
1041 vsv
= find_user_vec(self
); /* Get any user vector */
1043 target
->machine
.curctx
->VMXsave
= 0; /* Assume no vector */
1045 if(vsv
) { /* Did we find one? */
1046 vsvn
= (savearea_vec
*)save_alloc(); /* If we still don't have one, get a new one */
1047 vsvn
->save_hdr
.save_flags
= (vsvn
->save_hdr
.save_flags
& ~SAVtype
) | (SAVvector
<< SAVtypeshft
); /* Mark as in use as float */
1048 vsvn
->save_hdr
.save_act
= target
;
1049 vsvn
->save_hdr
.save_prev
= 0; /* Mark no more */
1050 vsvn
->save_hdr
.save_level
= 0; /* Mark user state */
1052 target
->machine
.curctx
->VMXsave
= vsvn
; /* Chain in the floating point */
1054 bcopy((char *)((unsigned int)vsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1055 (char *)((unsigned int)vsvn
+ sizeof(savearea_comm
)),
1056 sizeof(struct savearea
) - sizeof(savearea_comm
));
1059 return (KERN_SUCCESS
);
1063 * Initializes a fresh set of user state values. If there is no user state context,
1064 * one is created. Floats and VMX are not created.
1066 * We only set initial values if there was no context found.
1076 if (thread
->machine
.upcb
)
1077 return thread
->machine
.upcb
;
1079 sv
= thread
->machine
.pcb
; /* Get the top savearea on the stack */
1080 osv
= 0; /* Set no user savearea yet */
1082 while(sv
) { /* Find the user context */
1083 osv
= sv
; /* Save the last one */
1084 sv
= CAST_DOWN(savearea
*, sv
->save_hdr
.save_prev
); /* Get the previous context */
1087 sv
= save_alloc(); /* Get one */
1088 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use as general */
1089 sv
->save_hdr
.save_act
= thread
;
1090 sv
->save_hdr
.save_prev
= 0; /* Mark no more */
1091 sv
->save_hdr
.save_level
= 0; /* Mark user state */
1093 if(osv
) { /* Did we already have one? */
1094 osv
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)sv
); /* Chain us on the end */
1096 else { /* We are the first */
1097 thread
->machine
.pcb
= sv
; /* Put it there */
1099 thread
->machine
.upcb
= sv
; /* Set user pcb */
1101 for(i
=0; i
< 32; i
+=2) { /* Fill up with defaults */
1102 ((unsigned int *)&sv
->save_r0
)[i
] = ((unsigned int *)&FloatInit
)[0];
1103 ((unsigned int *)&sv
->save_r0
)[i
+1] = ((unsigned int *)&FloatInit
)[1];
1107 sv
->save_lr
= (uint64_t)FloatInit
;
1108 sv
->save_ctr
= (uint64_t)FloatInit
;
1109 sv
->save_srr0
= (uint64_t)FloatInit
;
1110 sv
->save_srr1
= (uint64_t)MSR_EXPORT_MASK_SET
;
1111 if(task_has_64BitAddr(thread
->task
))
1112 sv
->save_srr1
|= (uint64_t)MASK32(MSR_SF
) << 32; /* If 64-bit task, force 64-bit mode */
1114 sv
->save_fpscr
= 0; /* Clear all floating point exceptions */
1116 sv
->save_vrsave
= 0; /* Set the vector save state */
1117 sv
->save_vscr
[0] = 0x00000000;
1118 sv
->save_vscr
[1] = 0x00000000;
1119 sv
->save_vscr
[2] = 0x00000000;
1120 sv
->save_vscr
[3] = 0x00010000; /* Disable java mode and clear saturated */
1122 return sv
; /* Bye bye... */
1126 * Find the user state context. If there is no user state context,
1127 * we just return a 0.
1134 return thread
->machine
.upcb
;
1137 /* The name of this call is something of a misnomer since the mact.pcb can
1138 * contain chained saveareas, but it will do for now..
1144 return thread
->machine
.pcb
;
1148 * Find the user state floating point context. If there is no user state context,
1149 * we just return a 0.
1159 intr
= ml_set_interrupts_enabled(FALSE
);
1160 fsv
= thread
->machine
.curctx
->FPUsave
; /* Get the start of the floating point chain */
1162 while(fsv
) { /* Look until the end or we find it */
1163 if(!(fsv
->save_hdr
.save_level
)) break; /* Is the the user state stuff? (the level is 0 if so) */
1164 fsv
= CAST_DOWN(savearea_fpu
*, fsv
->save_hdr
.save_prev
); /* Try the previous one */
1166 (void) ml_set_interrupts_enabled(intr
);
1168 return fsv
; /* Bye bye... */
1172 * Find the user state vector context. If there is no user state context,
1173 * we just return a 0.
1183 intr
= ml_set_interrupts_enabled(FALSE
);
1184 vsv
= thread
->machine
.curctx
->VMXsave
; /* Get the start of the vector chain */
1186 while(vsv
) { /* Look until the end or we find it */
1187 if(!(vsv
->save_hdr
.save_level
)) break; /* Is the the user state stuff? (the level is 0 if so) */
1188 vsv
= CAST_DOWN(savearea_vec
*, vsv
->save_hdr
.save_prev
); /* Try the previous one */
1190 (void) ml_set_interrupts_enabled(intr
);
1192 return vsv
; /* Bye bye... */
1195 * Find the user state vector context for the current thread. If there is no user state context,
1196 * we just return a 0.
1199 savearea_vec
*find_user_vec_curr(void) {
1202 thread_t thread
= current_thread();
1205 vec_save(thread
->machine
.curctx
); /* Force save if live */
1207 intr
= ml_set_interrupts_enabled(FALSE
);
1208 vsv
= thread
->machine
.curctx
->VMXsave
; /* Get the start of the vector chain */
1210 while(vsv
) { /* Look until the end or we find it */
1211 if(!(vsv
->save_hdr
.save_level
)) break; /* Is the the user state stuff? (the level is 0 if so) */
1212 vsv
= CAST_DOWN(savearea_vec
*, vsv
->save_hdr
.save_prev
); /* Try the previous one */
1214 (void) ml_set_interrupts_enabled(intr
);
1216 return vsv
; /* Bye bye... */
1222 * Return the user stack pointer from the machine
1223 * dependent thread state info.
1227 __unused thread_t thread
,
1229 thread_state_t tstate
,
1231 mach_vm_offset_t
*user_stack
,
1240 case PPC_THREAD_STATE
:
1242 struct ppc_thread_state
*state
;
1244 if (count
< PPC_THREAD_STATE_COUNT
)
1245 return (KERN_INVALID_ARGUMENT
);
1247 state
= (struct ppc_thread_state
*) tstate
;
1250 * If a valid user stack is specified, use it.
1253 *user_stack
= CAST_USER_ADDR_T(state
->r1
);
1257 *user_stack
= CAST_USER_ADDR_T(USRSTACK
);
1264 case PPC_THREAD_STATE64
:
1266 struct ppc_thread_state64
*state64
;
1268 if (count
< PPC_THREAD_STATE64_COUNT
)
1269 return (KERN_INVALID_ARGUMENT
);
1271 state64
= (struct ppc_thread_state64
*)tstate
;
1274 * If a valid user stack is specified, use it.
1276 if (state64
->r1
!= MACH_VM_MIN_ADDRESS
) {
1277 *user_stack
= state64
->r1
;
1281 *user_stack
= USRSTACK64
;
1289 return (KERN_INVALID_ARGUMENT
);
1292 return (KERN_SUCCESS
);
1297 * thread_setuserstack:
1299 * Sets the user stack pointer into the machine
1300 * dependent thread state info.
1303 thread_setuserstack(thread_t thread
, mach_vm_address_t user_stack
)
1307 sv
= get_user_regs(thread
); /* Get the user state registers */
1309 sv
->save_r1
= user_stack
;
1315 * thread_adjuserstack:
1317 * Returns the adjusted user stack pointer from the machine
1318 * dependent thread state info. Usef for small (<2G) deltas.
1321 thread_adjuserstack(thread_t thread
, int adjust
)
1325 sv
= get_user_regs(thread
); /* Get the user state registers */
1327 sv
->save_r1
+= adjust
; /* Adjust the stack */
1329 return sv
->save_r1
; /* Return the adjusted stack */
1334 thread_setsinglestep(thread_t thread
, int on
)
1338 sv
= get_user_regs(thread
); /* Get the user state registers */
1341 sv
->save_srr1
|= MASK(MSR_SE
);
1343 sv
->save_srr1
&= ~MASK(MSR_SE
);
1347 * thread_setentrypoint:
1349 * Sets the user PC into the machine
1350 * dependent thread state info.
1354 thread_setentrypoint(thread_t thread
, uint64_t entry
)
1358 sv
= get_user_regs(thread
); /* Get the user state registers */
1360 sv
->save_srr0
= entry
;
1367 __unused thread_t thread
,
1369 thread_state_t tstate
,
1371 mach_vm_offset_t
*entry_point
1375 /* Silly code: "if *entry_point is 0, make it 0" */
1379 if (*entry_point
== 0ULL)
1380 *entry_point
= MACH_VM_MIN_ADDRESS
;
1384 case PPC_THREAD_STATE
:
1386 struct ppc_thread_state
*state
;
1388 if (count
< PPC_THREAD_STATE_COUNT
)
1389 return (KERN_INVALID_ARGUMENT
);
1391 state
= (struct ppc_thread_state
*) tstate
;
1394 * If a valid entry point is specified, use it.
1397 *entry_point
= CAST_USER_ADDR_T(state
->srr0
);
1399 *entry_point
= CAST_USER_ADDR_T(VM_MIN_ADDRESS
);
1404 case PPC_THREAD_STATE64
:
1406 struct ppc_thread_state64
*state64
;
1408 if (count
< PPC_THREAD_STATE_COUNT
)
1409 return (KERN_INVALID_ARGUMENT
);
1411 state64
= (struct ppc_thread_state64
*)tstate
;
1414 * If a valid entry point is specified, use it.
1416 if (state64
->srr0
) {
1417 *entry_point
= state64
->srr0
;
1419 *entry_point
= MACH_VM_MIN_ADDRESS
;
1425 return (KERN_INVALID_ARGUMENT
);
1428 return (KERN_SUCCESS
);
1431 unsigned int get_msr_exportmask(void)
1433 return (MSR_EXPORT_MASK_SET
);
1436 unsigned int get_msr_nbits(void)
1438 return (MASK(MSR_POW
)|MASK(MSR_ILE
)|MASK(MSR_IP
)|MASK(MSR_LE
));
1440 unsigned int get_msr_rbits(void)
1442 return (MASK(MSR_PR
)|MASK(MSR_ME
)|MASK(MSR_IR
)|MASK(MSR_DR
)|MASK(MSR_EE
));
1445 void ppc_checkthreadstate(void * tsptr
, int flavor
)
1447 if (flavor
== PPC_THREAD_STATE64
) {
1448 struct ppc_thread_state64
*ts64
=(struct ppc_thread_state64
*)tsptr
;
1450 /* Make sure naughty bits are off and necessary bits are on */
1451 ts64
->srr1
&= ~(MASK(MSR_POW
)|MASK(MSR_ILE
)|MASK(MSR_IP
)|MASK(MSR_LE
));
1452 ts64
->srr1
|= (MASK(MSR_PR
)|MASK(MSR_ME
)|MASK(MSR_IR
)|MASK(MSR_DR
)|MASK(MSR_EE
));
1454 struct ppc_thread_state
*ts
=(struct ppc_thread_state
*)tsptr
;
1456 /* Make sure naughty bits are off and necessary bits are on */
1457 ts
->srr1
&= ~(MASK(MSR_POW
)|MASK(MSR_ILE
)|MASK(MSR_IP
)|MASK(MSR_LE
));
1458 ts
->srr1
|= (MASK(MSR_PR
)|MASK(MSR_ME
)|MASK(MSR_IR
)|MASK(MSR_DR
)|MASK(MSR_EE
));
1468 struct savearea
*child_state
;
1470 child_state
= get_user_regs(child
);
1472 child_state
->save_r3
= (uint_t
)pid
;
1473 child_state
->save_r4
= 1ULL;
1480 struct savearea
*parent_state
;
1482 parent_state
= get_user_regs(parent
);
1484 parent_state
->save_r3
= (uint64_t)pid
;
1485 parent_state
->save_r4
= 0;
1489 * Saves the complete context (general, floating point, and vector) of the current activation.
1490 * We will collect everything into an opaque block of 1 to 3 saveareas and pass back a
1493 * The savearea is made to look like it belongs to the source activation. This needs to
1494 * be adjusted when these contexts are attached to a new activation.
1498 void *act_thread_csave(void) {
1501 savearea_fpu
*fsv
, *ofsv
;
1502 savearea_vec
*vsv
, *ovsv
;
1506 thread
= current_thread();
1508 fpu_save(thread
->machine
.curctx
); /* Make certain floating point state is all saved */
1509 vec_save(thread
->machine
.curctx
); /* Make certain the vector state is all saved */
1511 osv
= find_user_regs(thread
); /* Get our savearea */
1514 panic("act_thread_csave: attempting to preserve the context of an activation with none (%08X)\n", thread
);
1517 sv
= save_alloc(); /* Get a fresh save area to save into */
1518 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use as general */
1519 sv
->save_hdr
.save_act
= thread
;
1520 sv
->save_hdr
.save_prev
= 0; /* Mark no more */
1521 sv
->save_hdr
.save_level
= 0; /* Mark user state */
1524 bcopy((char *)((unsigned int)osv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1525 (char *)((unsigned int)sv
+ sizeof(savearea_comm
)),
1526 sizeof(struct savearea
) - sizeof(savearea_comm
));
1528 sv
->save_srr1
&= (uint64_t)(~(MASK(MSR_FP
) | MASK(MSR_VEC
))); /* Make certain that floating point and vector are turned off */
1530 sv
->save_hdr
.save_misc2
= 0xDEBB1ED0; /* Eye catcher for debug */
1531 sv
->save_hdr
.save_misc3
= 0xE5DA11A5; /* Eye catcher for debug */
1534 ofsv
= find_user_fpu(thread
); /* Get any user floating point */
1536 sv
->save_hdr
.save_misc0
= 0; /* Assume no floating point */
1538 if(ofsv
) { /* Did we find one? */
1539 fsv
= (savearea_fpu
*)save_alloc(); /* If we still don't have one, get a new one */
1540 fsv
->save_hdr
.save_flags
= (fsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVfloat
<< SAVtypeshft
); /* Mark as in use as float */
1541 fsv
->save_hdr
.save_act
= thread
;
1542 fsv
->save_hdr
.save_prev
= 0; /* Mark no more */
1543 fsv
->save_hdr
.save_level
= 0; /* Mark user state */
1544 fsv
->save_hdr
.save_misc2
= 0xDEBB1ED0; /* Eye catcher for debug */
1545 fsv
->save_hdr
.save_misc3
= 0xE5DA11A5; /* Eye catcher for debug */
1547 sv
->save_hdr
.save_misc0
= (uint64_t)((uintptr_t)fsv
); /* Remember this one */
1549 bcopy((char *)((unsigned int)ofsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1550 (char *)((unsigned int)fsv
+ sizeof(savearea_comm
)),
1551 sizeof(struct savearea
) - sizeof(savearea_comm
));
1554 ovsv
= find_user_vec(thread
); /* Get any user vector */
1556 sv
->save_hdr
.save_misc1
= 0; /* Assume no vector */
1558 if(ovsv
) { /* Did we find one? */
1559 vsv
= (savearea_vec
*)save_alloc(); /* If we still don't have one, get a new one */
1560 vsv
->save_hdr
.save_flags
= (vsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVvector
<< SAVtypeshft
); /* Mark as in use as float */
1561 vsv
->save_hdr
.save_act
= thread
;
1562 vsv
->save_hdr
.save_prev
= 0; /* Mark no more */
1563 vsv
->save_hdr
.save_level
= 0; /* Mark user state */
1564 vsv
->save_hdr
.save_misc2
= 0xDEBB1ED0; /* Eye catcher for debug */
1565 vsv
->save_hdr
.save_misc3
= 0xE5DA11A5; /* Eye catcher for debug */
1567 sv
->save_hdr
.save_misc1
= (uint64_t)((uintptr_t)vsv
); /* Chain in the floating point */
1569 bcopy((char *)((unsigned int)ovsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1570 (char *)((unsigned int)vsv
+ sizeof(savearea_comm
)),
1571 sizeof(struct savearea
) - sizeof(savearea_comm
));
1574 return (void *)sv
; /* Bye bye... */
1580 * Attaches saved user state context to an activation. We will replace any
1581 * user state context with what is passed in. The saved context consists of a
1582 * savearea that was setup by
1583 * We will collect everything into one savearea and pass that back.
1585 * The savearea is made to look like it belongs to the source activation. This needs to
1586 * be adjusted when these contexts are attached to a new activation.
1590 void act_thread_catt(void *ctx
) {
1592 savearea
*sv
, *osv
, *psv
;
1593 savearea_fpu
*fsv
, *ofsv
, *pfsv
;
1594 savearea_vec
*vsv
, *ovsv
, *pvsv
;
1598 sv
= (savearea
*)ctx
; /* Make this easier for C */
1600 fsv
= CAST_DOWN(savearea_fpu
*, sv
->save_hdr
.save_misc0
); /* Get a possible floating point savearea */
1601 vsv
= CAST_DOWN(savearea_vec
*, sv
->save_hdr
.save_misc1
); /* Get a possible vector savearea */
1603 if((sv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (sv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1604 panic("act_thread_catt: attempt to attach invalid general context savearea - %08X\n", sv
); /* Die */
1607 if(fsv
&& ((fsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (fsv
->save_hdr
.save_misc3
!= 0xE5DA11A5))) { /* See if valid savearea */
1608 panic("act_thread_catt: attempt to attach invalid float context savearea - %08X\n", fsv
); /* Die */
1611 if(vsv
&& ((vsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (vsv
->save_hdr
.save_misc3
!= 0xE5DA11A5))) { /* See if valid savearea */
1612 panic("act_thread_catt: attempt to attach invalid vector context savearea - %08X\n", vsv
); /* Die */
1615 thread
= current_thread();
1617 act_machine_sv_free(thread
); /* Blow away any current kernel FP or vector.
1618 We do not support those across a vfork */
1619 toss_live_fpu(thread
->machine
.curctx
); /* Toss my floating point if live anywhere */
1620 toss_live_vec(thread
->machine
.curctx
); /* Toss my vector if live anywhere */
1622 sv
->save_hdr
.save_misc2
= 0; /* Eye catcher for debug */
1623 sv
->save_hdr
.save_misc3
= 0; /* Eye catcher for debug */
1624 sv
->save_hdr
.save_act
= thread
;
1626 spc
= (unsigned int)thread
->map
->pmap
->space
; /* Get the space we're in */
1628 osv
= thread
->machine
.pcb
; /* Get the top general savearea */
1630 while(osv
) { /* Any saved state? */
1631 if(osv
->save_srr1
& MASK(MSR_PR
)) break; /* Leave if this is user state */
1632 psv
= osv
; /* Save previous savearea address */
1633 osv
= CAST_DOWN(savearea
*, osv
->save_hdr
.save_prev
); /* Get one underneath our's */
1636 if(osv
) { /* Did we find one? */
1637 if(psv
) psv
->save_hdr
.save_prev
= 0; /* Yes, clear pointer to it (it should always be last) or */
1638 else thread
->machine
.pcb
= 0; /* to the start if the only one */
1640 save_release(osv
); /* Nope, release it */
1644 if(psv
) psv
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)sv
); /* Chain us to the end or */
1645 else thread
->machine
.pcb
= (pcb_t
)sv
; /* to the start if the only one */
1646 thread
->machine
.upcb
= (pcb_t
)sv
; /* Set the user pcb */
1648 ovsv
= thread
->machine
.curctx
->VMXsave
; /* Get the top vector savearea */
1651 while(ovsv
) { /* Any VMX saved state? */
1652 if(!(ovsv
->save_hdr
.save_level
)) break; /* Leave if this is user state */
1653 pvsv
= ovsv
; /* Save previous savearea address */
1654 ovsv
= CAST_DOWN(savearea_vec
*, ovsv
->save_hdr
.save_prev
); /* Get one underneath our's */
1657 if(ovsv
) { /* Did we find one? */
1658 if(pvsv
) pvsv
->save_hdr
.save_prev
= 0; /* Yes, clear pointer to it (it should always be last) or */
1659 else thread
->machine
.curctx
->VMXsave
= 0; /* to the start if the only one */
1661 save_release((savearea
*)ovsv
); /* Nope, release it */
1664 if(vsv
) { /* Are we sticking any vector on this one? */
1665 if(pvsv
) pvsv
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)vsv
); /* Yes, chain us to the end or */
1667 thread
->machine
.curctx
->VMXsave
= vsv
; /* to the start if the only one */
1668 thread
->machine
.curctx
->VMXlevel
= 0; /* Insure that we don't have a leftover level */
1671 vsv
->save_hdr
.save_misc2
= 0; /* Eye catcher for debug */
1672 vsv
->save_hdr
.save_misc3
= 0; /* Eye catcher for debug */
1673 vsv
->save_hdr
.save_act
= thread
;
1676 ofsv
= thread
->machine
.curctx
->FPUsave
; /* Get the top float savearea */
1679 while(ofsv
) { /* Any float saved state? */
1680 if(!(ofsv
->save_hdr
.save_level
)) break; /* Leave if this is user state */
1681 pfsv
= ofsv
; /* Save previous savearea address */
1682 ofsv
= CAST_DOWN(savearea_fpu
*, ofsv
->save_hdr
.save_prev
); /* Get one underneath our's */
1685 if(ofsv
) { /* Did we find one? */
1686 if(pfsv
) pfsv
->save_hdr
.save_prev
= 0; /* Yes, clear pointer to it (it should always be last) or */
1687 else thread
->machine
.curctx
->FPUsave
= 0; /* to the start if the only one */
1689 save_release((savearea
*)ofsv
); /* Nope, release it */
1692 if(fsv
) { /* Are we sticking any vector on this one? */
1693 if(pfsv
) pfsv
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)fsv
); /* Yes, chain us to the end or */
1695 thread
->machine
.curctx
->FPUsave
= fsv
; /* to the start if the only one */
1696 thread
->machine
.curctx
->FPUlevel
= 0; /* Insure that we don't have a leftover level */
1699 fsv
->save_hdr
.save_misc2
= 0; /* Eye catcher for debug */
1700 fsv
->save_hdr
.save_misc3
= 0; /* Eye catcher for debug */
1701 fsv
->save_hdr
.save_act
= thread
;
1709 * Releases saved context. We need this because the saved context is opague.
1710 * be adjusted when these contexts are attached to a new activation.
1715 act_thread_cfree(void *ctx
)
1722 sv
= (savearea
*)ctx
; /* Make this easier for C */
1724 fsv
= CAST_DOWN(savearea_fpu
*, sv
->save_hdr
.save_misc0
); /* Get a possible floating point savearea */
1725 vsv
= CAST_DOWN(savearea_vec
*, sv
->save_hdr
.save_misc1
); /* Get a possible vector savearea */
1727 if((sv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (sv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1728 panic("act_thread_cfree: attempt to detatch invalid general context savearea - %08X\n", sv
); /* Die */
1731 save_release(sv
); /* Toss the general savearea */
1733 if(fsv
) { /* See if there is any saved floating point */
1734 if((fsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (fsv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1735 panic("act_thread_cfree: attempt to detatch invalid float context savearea - %08X\n", fsv
); /* Die */
1738 save_release((savearea
*)fsv
); /* Toss saved context */
1741 if(vsv
) { /* See if there is any saved floating point */
1742 if((vsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (vsv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1743 panic("act_thread_cfree: attempt to detatch invalid vector context savearea - %08X\n", vsv
); /* Die */
1746 save_release((savearea
*)vsv
); /* Toss saved context */
1753 * thread_enable_fpe:
1755 * enables or disables floating point exceptions for the thread.
1758 int thread_enable_fpe(
1765 sv
= find_user_regs(thread
); /* Find the user registers */
1766 if(!sv
) sv
= get_user_regs(thread
); /* Didn't find any, allocate and initialize one */
1768 oldmsr
= sv
->save_srr1
; /* Get the old msr */
1770 if(onoff
) sv
->save_srr1
= oldmsr
| (uint64_t)(MASK(MSR_FE0
) | MASK(MSR_FE1
)); /* Flip on precise FP exceptions */
1771 else sv
->save_srr1
= oldmsr
& (uint64_t)(~(MASK(MSR_FE0
) | MASK(MSR_FE1
))); /* Flip on precise FP exceptions */
1773 return ((oldmsr
& (MASK(MSR_FE0
) | MASK(MSR_FE1
))) != 0); /* Return if it was enabled or not */