2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
35 #include <kern/thread.h>
36 #include <kern/misc_protos.h>
37 #include <mach/ppc/thread_status.h>
38 #include <ppc/proc_reg.h>
39 #include <ppc/cpu_internal.h>
40 #include <ppc/exception.h>
41 #include <ppc/misc_protos.h>
42 #include <ppc/savearea.h>
43 #include <ppc/thread.h>
44 #include <ppc/Firmware.h>
46 //#include <sys/time.h>
47 typedef unsigned int fixpt_t
; /* XXX <sys/resource.h> not self contained */
48 #include <ppc/vmparam.h> /* USRSTACK, etc. */
50 #include <vm/vm_map.h>
52 extern unsigned int killprint
;
53 extern double FloatInit
;
54 extern unsigned long QNaNbarbarian
[4];
56 #define USRSTACK 0xc0000000
77 unsigned int get_msr_exportmask(void);
78 unsigned int get_msr_nbits(void);
79 unsigned int get_msr_rbits(void);
80 void ppc_checkthreadstate(void *, int);
81 void thread_set_child(thread_t child
, int pid
);
82 void thread_set_parent(thread_t parent
, int pid
);
83 void save_release(struct savearea
*save
);
86 * Maps state flavor to number of words in the state:
89 unsigned int _MachineStateCount
[] = {
91 PPC_THREAD_STATE_COUNT
,
92 PPC_FLOAT_STATE_COUNT
,
93 PPC_EXCEPTION_STATE_COUNT
,
94 PPC_VECTOR_STATE_COUNT
,
95 PPC_THREAD_STATE64_COUNT
,
96 PPC_EXCEPTION_STATE64_COUNT
,
102 * Get the status of the specified thread.
106 machine_thread_get_state(
108 thread_flavor_t flavor
,
109 thread_state_t tstate
,
110 mach_msg_type_number_t
*count
)
113 register struct savearea
*sv
; /* Pointer to the context savearea */
114 register savearea_fpu
*fsv
;
115 register savearea_vec
*vsv
;
118 unsigned int vrvalidwrk
;
120 register struct ppc_thread_state
*ts
;
121 register struct ppc_thread_state64
*xts
;
122 register struct ppc_exception_state
*es
;
123 register struct ppc_exception_state64
*xes
;
124 register struct ppc_float_state
*fs
;
125 register struct ppc_vector_state
*vs
;
127 genuser
= find_user_regs(thread
);
131 case THREAD_STATE_FLAVOR_LIST
:
134 return (KERN_INVALID_ARGUMENT
);
137 tstate
[0] = PPC_THREAD_STATE
;
138 tstate
[1] = PPC_FLOAT_STATE
;
139 tstate
[2] = PPC_EXCEPTION_STATE
;
140 tstate
[3] = PPC_VECTOR_STATE
;
141 tstate
[4] = PPC_THREAD_STATE64
;
142 tstate
[5] = PPC_EXCEPTION_STATE64
;
147 case PPC_THREAD_STATE
:
149 if (*count
< PPC_THREAD_STATE_COUNT
) { /* Is the count ok? */
150 return KERN_INVALID_ARGUMENT
;
153 ts
= (struct ppc_thread_state
*) tstate
;
155 sv
= genuser
; /* Copy this over */
157 if(sv
) { /* Is there a save area yet? */
158 ts
->r0
= (unsigned int)sv
->save_r0
;
159 ts
->r1
= (unsigned int)sv
->save_r1
;
160 ts
->r2
= (unsigned int)sv
->save_r2
;
161 ts
->r3
= (unsigned int)sv
->save_r3
;
162 ts
->r4
= (unsigned int)sv
->save_r4
;
163 ts
->r5
= (unsigned int)sv
->save_r5
;
164 ts
->r6
= (unsigned int)sv
->save_r6
;
165 ts
->r7
= (unsigned int)sv
->save_r7
;
166 ts
->r8
= (unsigned int)sv
->save_r8
;
167 ts
->r9
= (unsigned int)sv
->save_r9
;
168 ts
->r10
= (unsigned int)sv
->save_r10
;
169 ts
->r11
= (unsigned int)sv
->save_r11
;
170 ts
->r12
= (unsigned int)sv
->save_r12
;
171 ts
->r13
= (unsigned int)sv
->save_r13
;
172 ts
->r14
= (unsigned int)sv
->save_r14
;
173 ts
->r15
= (unsigned int)sv
->save_r15
;
174 ts
->r16
= (unsigned int)sv
->save_r16
;
175 ts
->r17
= (unsigned int)sv
->save_r17
;
176 ts
->r18
= (unsigned int)sv
->save_r18
;
177 ts
->r19
= (unsigned int)sv
->save_r19
;
178 ts
->r20
= (unsigned int)sv
->save_r20
;
179 ts
->r21
= (unsigned int)sv
->save_r21
;
180 ts
->r22
= (unsigned int)sv
->save_r22
;
181 ts
->r23
= (unsigned int)sv
->save_r23
;
182 ts
->r24
= (unsigned int)sv
->save_r24
;
183 ts
->r25
= (unsigned int)sv
->save_r25
;
184 ts
->r26
= (unsigned int)sv
->save_r26
;
185 ts
->r27
= (unsigned int)sv
->save_r27
;
186 ts
->r28
= (unsigned int)sv
->save_r28
;
187 ts
->r29
= (unsigned int)sv
->save_r29
;
188 ts
->r30
= (unsigned int)sv
->save_r30
;
189 ts
->r31
= (unsigned int)sv
->save_r31
;
190 ts
->cr
= (unsigned int)sv
->save_cr
;
191 ts
->xer
= (unsigned int)sv
->save_xer
;
192 ts
->lr
= (unsigned int)sv
->save_lr
;
193 ts
->ctr
= (unsigned int)sv
->save_ctr
;
194 ts
->srr0
= (unsigned int)sv
->save_srr0
;
195 ts
->srr1
= (unsigned int)sv
->save_srr1
;
196 ts
->mq
= 0; /* MQ register (601 only) */
197 ts
->vrsave
= (unsigned int)sv
->save_vrsave
; /* VRSAVE register (Altivec only) */
199 else { /* No user state yet. Save seemingly random values. */
201 for(i
=0; i
< 32; i
+=2) { /* Fill up with defaults */
202 ((unsigned int *)&ts
->r0
)[i
] = ((unsigned int *)&FloatInit
)[0];
203 ((unsigned int *)&ts
->r0
)[i
+1] = ((unsigned int *)&FloatInit
)[1];
207 ts
->lr
= ((unsigned int *)&FloatInit
)[0];
208 ts
->ctr
= ((unsigned int *)&FloatInit
)[1];
209 ts
->srr0
= ((unsigned int *)&FloatInit
)[0];
210 ts
->srr1
= MSR_EXPORT_MASK_SET
;
212 ts
->vrsave
= 0; /* VRSAVE register (Altivec only) */
215 *count
= PPC_THREAD_STATE_COUNT
; /* Pass back the amount we actually copied */
219 case PPC_THREAD_STATE64
:
221 if (*count
< PPC_THREAD_STATE64_COUNT
) { /* Is the count ok? */
222 return KERN_INVALID_ARGUMENT
;
225 xts
= (struct ppc_thread_state64
*) tstate
;
227 sv
= genuser
; /* Copy this over */
229 if(sv
) { /* Is there a save area yet? */
230 xts
->r0
= sv
->save_r0
;
231 xts
->r1
= sv
->save_r1
;
232 xts
->r2
= sv
->save_r2
;
233 xts
->r3
= sv
->save_r3
;
234 xts
->r4
= sv
->save_r4
;
235 xts
->r5
= sv
->save_r5
;
236 xts
->r6
= sv
->save_r6
;
237 xts
->r7
= sv
->save_r7
;
238 xts
->r8
= sv
->save_r8
;
239 xts
->r9
= sv
->save_r9
;
240 xts
->r10
= sv
->save_r10
;
241 xts
->r11
= sv
->save_r11
;
242 xts
->r12
= sv
->save_r12
;
243 xts
->r13
= sv
->save_r13
;
244 xts
->r14
= sv
->save_r14
;
245 xts
->r15
= sv
->save_r15
;
246 xts
->r16
= sv
->save_r16
;
247 xts
->r17
= sv
->save_r17
;
248 xts
->r18
= sv
->save_r18
;
249 xts
->r19
= sv
->save_r19
;
250 xts
->r20
= sv
->save_r20
;
251 xts
->r21
= sv
->save_r21
;
252 xts
->r22
= sv
->save_r22
;
253 xts
->r23
= sv
->save_r23
;
254 xts
->r24
= sv
->save_r24
;
255 xts
->r25
= sv
->save_r25
;
256 xts
->r26
= sv
->save_r26
;
257 xts
->r27
= sv
->save_r27
;
258 xts
->r28
= sv
->save_r28
;
259 xts
->r29
= sv
->save_r29
;
260 xts
->r30
= sv
->save_r30
;
261 xts
->r31
= sv
->save_r31
;
262 xts
->cr
= sv
->save_cr
;
263 xts
->xer
= sv
->save_xer
;
264 xts
->lr
= sv
->save_lr
;
265 xts
->ctr
= sv
->save_ctr
;
266 xts
->srr0
= sv
->save_srr0
;
267 xts
->srr1
= sv
->save_srr1
;
268 xts
->vrsave
= sv
->save_vrsave
; /* VRSAVE register (Altivec only) */
270 else { /* No user state yet. Save seemingly random values. */
272 for(i
=0; i
< 32; i
++) { /* Fill up with defaults */
273 ((unsigned long long *)&xts
->r0
)[i
] = ((unsigned long long *)&FloatInit
)[0];
277 xts
->lr
= ((unsigned long long *)&FloatInit
)[0];
278 xts
->ctr
= ((unsigned long long *)&FloatInit
)[0];
279 xts
->srr0
= ((unsigned long long *)&FloatInit
)[0];
280 xts
->srr1
= MSR_EXPORT_MASK_SET
;
281 if(task_has_64BitAddr(thread
->task
))
282 xts
->srr1
|= (uint64_t)MASK32(MSR_SF
) << 32; /* If 64-bit task, force 64-bit mode */
283 xts
->vrsave
= 0; /* VRSAVE register (Altivec only) */
286 *count
= PPC_THREAD_STATE64_COUNT
; /* Pass back the amount we actually copied */
289 case PPC_EXCEPTION_STATE
:
291 if (*count
< PPC_EXCEPTION_STATE_COUNT
) {
292 return KERN_INVALID_ARGUMENT
;
295 es
= (struct ppc_exception_state
*) tstate
;
296 sv
= genuser
; /* Copy this over */
298 if(sv
) { /* See if valid state yet */
299 es
->dar
= (unsigned int)sv
->save_dar
;
300 es
->dsisr
= sv
->save_dsisr
;
301 es
->exception
= sv
->save_exception
;
303 else { /* Nope, not yet */
306 es
->exception
= ((unsigned int *)&FloatInit
)[0];
309 *count
= PPC_EXCEPTION_STATE_COUNT
;
312 case PPC_EXCEPTION_STATE64
:
314 if (*count
< PPC_EXCEPTION_STATE64_COUNT
) {
315 return KERN_INVALID_ARGUMENT
;
318 xes
= (struct ppc_exception_state64
*) tstate
;
319 sv
= genuser
; /* Copy this over */
321 if(sv
) { /* See if valid state yet */
322 xes
->dar
= sv
->save_dar
;
323 xes
->dsisr
= sv
->save_dsisr
;
324 xes
->exception
= sv
->save_exception
;
326 else { /* Nope, not yet */
329 xes
->exception
= ((unsigned int *)&FloatInit
)[0];
332 *count
= PPC_EXCEPTION_STATE64_COUNT
;
335 case PPC_FLOAT_STATE
:
337 if (*count
< PPC_FLOAT_STATE_COUNT
) {
338 return KERN_INVALID_ARGUMENT
;
341 fpu_save(thread
->machine
.curctx
); /* Just in case it's live, save it */
343 fs
= (struct ppc_float_state
*) tstate
; /* Point to destination */
345 fsv
= find_user_fpu(thread
); /* Get the user's fpu savearea */
347 if(fsv
) { /* See if we have any */
348 bcopy((char *)&fsv
->save_fp0
, (char *)fs
, 32*8); /* 32 registers */
349 fs
->fpscr_pad
= 0; /* Be clean and tidy */
350 if(genuser
) fs
->fpscr
= genuser
->save_fpscr
; /* Set the fpscr value to general */
351 else fs
->fpscr
= 0; /* If no user, initialize this */
353 else { /* No floating point yet */
355 for(i
=0; i
< 32; i
++) { /* Initialize floating points */
356 fs
->fpregs
[i
] = FloatInit
; /* Initial value */
358 fs
->fpscr_pad
= 0; /* Initial value */
359 fs
->fpscr
= 0; /* Initial value */
362 *count
= PPC_FLOAT_STATE_COUNT
;
366 case PPC_VECTOR_STATE
:
368 if (*count
< PPC_VECTOR_STATE_COUNT
) {
369 return KERN_INVALID_ARGUMENT
;
372 vec_save(thread
->machine
.curctx
); /* Just in case it's live, save it */
374 vs
= (struct ppc_vector_state
*) tstate
; /* Point to destination */
376 vsv
= find_user_vec(thread
); /* Find the vector savearea */
378 if(vsv
) { /* See if we have any */
380 vrvalidwrk
= vsv
->save_vrvalid
; /* Get the valid flags */
381 vs
->save_vrvalid
= vsv
->save_vrvalid
; /* Set the valid flags */
382 if(genuser
) for(j
=0; j
< 4; j
++) vs
->save_vscr
[j
] = genuser
->save_vscr
[j
]; /* Set value for vscr */
384 vs
->save_vscr
[0] = 0; /* Set an initial value if no general user yet */
385 vs
->save_vscr
[1] = 0;
386 vs
->save_vscr
[2] = 0;
387 vs
->save_vscr
[3] = 0x00010000; /* Always start with Java mode off */
389 for(i
=0; i
< 32; i
++) { /* Copy the saved registers and invalidate the others */
390 for(j
=0; j
< 4; j
++) {
391 if(vrvalidwrk
& 0x80000000) (vs
->save_vr
)[i
][j
] =
392 ((unsigned int *)&(vsv
->save_vr0
))[(i
* 4) + j
]; /* We have this register saved */
393 else vs
->save_vr
[i
][j
] = QNaNbarbarian
[j
]; /* Set invalid value */
395 vrvalidwrk
= vrvalidwrk
<< 1; /* Shift over to the next */
398 else { /* No vector yet */
400 for(i
=0; i
< 32; i
++) { /* Initialize vector registers */
401 for(j
=0; j
< 4; j
++) vs
->save_vr
[i
][j
] = QNaNbarbarian
[j
]; /* Initial value */
404 if(genuser
) for(j
=0; j
< 4; j
++) vs
->save_vscr
[j
] = genuser
->save_vscr
[j
]; /* Set value for vscr */
406 vs
->save_vscr
[0] = 0; /* Set an initial value if no general user yet */
407 vs
->save_vscr
[1] = 0;
408 vs
->save_vscr
[2] = 0;
409 vs
->save_vscr
[3] = 0x00010000; /* Always start with Java mode off */
411 vs
->save_vrvalid
= 0; /* Clear the valid flags */
414 for (i
=0; i
< 4; i
++) vs
->save_pad5
[i
] = 0; /* Clear cruft */
415 for (i
=0; i
< 7; i
++) vs
->save_pad6
[i
] = 0; /* Clear cruft */
417 *count
= PPC_VECTOR_STATE_COUNT
;
421 return KERN_INVALID_ARGUMENT
;
424 /* Close cousin of machine_thread_get_state().
425 * This function is currently incomplete since we don't really need vector
426 * or FP for the core dump (the save area can be accessed directly if the
427 * user is so inclined). Also the function name is something of a misnomer,
428 * see the comment above find_kern_regs().
432 machine_thread_get_kern_state(
434 thread_flavor_t flavor
,
435 thread_state_t tstate
,
436 mach_msg_type_number_t
*count
)
439 register struct savearea
*sv
; /* Pointer to the context savearea */
443 register struct ppc_thread_state
*ts
;
444 register struct ppc_thread_state64
*xts
;
445 register struct ppc_exception_state
*es
;
446 register struct ppc_exception_state64
*xes
;
448 genkern
= find_kern_regs(thread
);
452 case THREAD_STATE_FLAVOR_LIST
:
455 return (KERN_INVALID_ARGUMENT
);
458 tstate
[0] = PPC_THREAD_STATE
;
459 tstate
[1] = PPC_FLOAT_STATE
;
460 tstate
[2] = PPC_EXCEPTION_STATE
;
461 tstate
[3] = PPC_VECTOR_STATE
;
462 tstate
[4] = PPC_THREAD_STATE64
;
463 tstate
[5] = PPC_EXCEPTION_STATE64
;
468 case PPC_THREAD_STATE
:
470 if (*count
< PPC_THREAD_STATE_COUNT
) { /* Is the count ok? */
471 return KERN_INVALID_ARGUMENT
;
474 ts
= (struct ppc_thread_state
*) tstate
;
476 sv
= genkern
; /* Copy this over */
478 if(sv
) { /* Is there a save area yet? */
479 ts
->r0
= (unsigned int)sv
->save_r0
;
480 ts
->r1
= (unsigned int)sv
->save_r1
;
481 ts
->r2
= (unsigned int)sv
->save_r2
;
482 ts
->r3
= (unsigned int)sv
->save_r3
;
483 ts
->r4
= (unsigned int)sv
->save_r4
;
484 ts
->r5
= (unsigned int)sv
->save_r5
;
485 ts
->r6
= (unsigned int)sv
->save_r6
;
486 ts
->r7
= (unsigned int)sv
->save_r7
;
487 ts
->r8
= (unsigned int)sv
->save_r8
;
488 ts
->r9
= (unsigned int)sv
->save_r9
;
489 ts
->r10
= (unsigned int)sv
->save_r10
;
490 ts
->r11
= (unsigned int)sv
->save_r11
;
491 ts
->r12
= (unsigned int)sv
->save_r12
;
492 ts
->r13
= (unsigned int)sv
->save_r13
;
493 ts
->r14
= (unsigned int)sv
->save_r14
;
494 ts
->r15
= (unsigned int)sv
->save_r15
;
495 ts
->r16
= (unsigned int)sv
->save_r16
;
496 ts
->r17
= (unsigned int)sv
->save_r17
;
497 ts
->r18
= (unsigned int)sv
->save_r18
;
498 ts
->r19
= (unsigned int)sv
->save_r19
;
499 ts
->r20
= (unsigned int)sv
->save_r20
;
500 ts
->r21
= (unsigned int)sv
->save_r21
;
501 ts
->r22
= (unsigned int)sv
->save_r22
;
502 ts
->r23
= (unsigned int)sv
->save_r23
;
503 ts
->r24
= (unsigned int)sv
->save_r24
;
504 ts
->r25
= (unsigned int)sv
->save_r25
;
505 ts
->r26
= (unsigned int)sv
->save_r26
;
506 ts
->r27
= (unsigned int)sv
->save_r27
;
507 ts
->r28
= (unsigned int)sv
->save_r28
;
508 ts
->r29
= (unsigned int)sv
->save_r29
;
509 ts
->r30
= (unsigned int)sv
->save_r30
;
510 ts
->r31
= (unsigned int)sv
->save_r31
;
511 ts
->cr
= (unsigned int)sv
->save_cr
;
512 ts
->xer
= (unsigned int)sv
->save_xer
;
513 ts
->lr
= (unsigned int)sv
->save_lr
;
514 ts
->ctr
= (unsigned int)sv
->save_ctr
;
515 ts
->srr0
= (unsigned int)sv
->save_srr0
;
516 ts
->srr1
= (unsigned int)sv
->save_srr1
;
517 ts
->mq
= 0; /* MQ register (601 only) */
518 ts
->vrsave
= (unsigned int)sv
->save_vrsave
; /* VRSAVE register (Altivec only) */
520 else { /* No state yet. Save seemingly random values. */
522 for(i
=0; i
< 32; i
+=2) { /* Fill up with defaults */
523 ((unsigned int *)&ts
->r0
)[i
] = ((unsigned int *)&FloatInit
)[0];
524 ((unsigned int *)&ts
->r0
)[i
+1] = ((unsigned int *)&FloatInit
)[1];
528 ts
->lr
= ((unsigned int *)&FloatInit
)[0];
529 ts
->ctr
= ((unsigned int *)&FloatInit
)[1];
530 ts
->srr0
= ((unsigned int *)&FloatInit
)[0];
531 ts
->srr1
= MSR_EXPORT_MASK_SET
;
533 ts
->vrsave
= 0; /* VRSAVE register (Altivec only) */
536 *count
= PPC_THREAD_STATE_COUNT
; /* Pass back the amount we actually copied */
540 case PPC_THREAD_STATE64
:
542 if (*count
< PPC_THREAD_STATE64_COUNT
) { /* Is the count ok? */
543 return KERN_INVALID_ARGUMENT
;
546 xts
= (struct ppc_thread_state64
*) tstate
;
548 sv
= genkern
; /* Copy this over */
550 if(sv
) { /* Is there a save area yet? */
551 xts
->r0
= sv
->save_r0
;
552 xts
->r1
= sv
->save_r1
;
553 xts
->r2
= sv
->save_r2
;
554 xts
->r3
= sv
->save_r3
;
555 xts
->r4
= sv
->save_r4
;
556 xts
->r5
= sv
->save_r5
;
557 xts
->r6
= sv
->save_r6
;
558 xts
->r7
= sv
->save_r7
;
559 xts
->r8
= sv
->save_r8
;
560 xts
->r9
= sv
->save_r9
;
561 xts
->r10
= sv
->save_r10
;
562 xts
->r11
= sv
->save_r11
;
563 xts
->r12
= sv
->save_r12
;
564 xts
->r13
= sv
->save_r13
;
565 xts
->r14
= sv
->save_r14
;
566 xts
->r15
= sv
->save_r15
;
567 xts
->r16
= sv
->save_r16
;
568 xts
->r17
= sv
->save_r17
;
569 xts
->r18
= sv
->save_r18
;
570 xts
->r19
= sv
->save_r19
;
571 xts
->r20
= sv
->save_r20
;
572 xts
->r21
= sv
->save_r21
;
573 xts
->r22
= sv
->save_r22
;
574 xts
->r23
= sv
->save_r23
;
575 xts
->r24
= sv
->save_r24
;
576 xts
->r25
= sv
->save_r25
;
577 xts
->r26
= sv
->save_r26
;
578 xts
->r27
= sv
->save_r27
;
579 xts
->r28
= sv
->save_r28
;
580 xts
->r29
= sv
->save_r29
;
581 xts
->r30
= sv
->save_r30
;
582 xts
->r31
= sv
->save_r31
;
583 xts
->cr
= sv
->save_cr
;
584 xts
->xer
= sv
->save_xer
;
585 xts
->lr
= sv
->save_lr
;
586 xts
->ctr
= sv
->save_ctr
;
587 xts
->srr0
= sv
->save_srr0
;
588 xts
->srr1
= sv
->save_srr1
;
589 xts
->vrsave
= sv
->save_vrsave
; /* VRSAVE register (Altivec only) */
591 else { /* No user state yet. Save seemingly random values. */
593 for(i
=0; i
< 32; i
++) { /* Fill up with defaults */
594 ((unsigned long long *)&xts
->r0
)[i
] = ((unsigned long long *)&FloatInit
)[0];
598 xts
->lr
= ((unsigned long long *)&FloatInit
)[0];
599 xts
->ctr
= ((unsigned long long *)&FloatInit
)[0];
600 xts
->srr0
= ((unsigned long long *)&FloatInit
)[0];
601 xts
->srr1
= MSR_EXPORT_MASK_SET
;
602 xts
->vrsave
= 0; /* VRSAVE register (Altivec only) */
605 *count
= PPC_THREAD_STATE64_COUNT
; /* Pass back the amount we actually copied */
608 case PPC_EXCEPTION_STATE
:
610 if (*count
< PPC_EXCEPTION_STATE_COUNT
) {
611 return KERN_INVALID_ARGUMENT
;
614 es
= (struct ppc_exception_state
*) tstate
;
615 sv
= genkern
; /* Copy this over */
617 if(sv
) { /* See if valid state yet */
618 es
->dar
= (unsigned int)sv
->save_dar
;
619 es
->dsisr
= sv
->save_dsisr
;
620 es
->exception
= sv
->save_exception
;
622 else { /* Nope, not yet */
625 es
->exception
= ((unsigned int *)&FloatInit
)[0];
628 *count
= PPC_EXCEPTION_STATE_COUNT
;
631 case PPC_EXCEPTION_STATE64
:
633 if (*count
< PPC_EXCEPTION_STATE64_COUNT
) {
634 return KERN_INVALID_ARGUMENT
;
637 xes
= (struct ppc_exception_state64
*) tstate
;
638 sv
= genkern
; /* Copy this over */
640 if(sv
) { /* See if valid state yet */
641 xes
->dar
= sv
->save_dar
;
642 xes
->dsisr
= sv
->save_dsisr
;
643 xes
->exception
= sv
->save_exception
;
645 else { /* Nope, not yet */
648 xes
->exception
= ((unsigned int *)&FloatInit
)[0];
651 *count
= PPC_EXCEPTION_STATE64_COUNT
;
655 return KERN_INVALID_ARGUMENT
;
663 * Set the status of the specified thread.
666 machine_thread_set_state(
668 thread_flavor_t flavor
,
669 thread_state_t tstate
,
670 mach_msg_type_number_t count
)
674 savearea_fpu
*fsv
, *fsvn
, *fsvo
;
675 savearea_vec
*vsv
, *vsvn
, *vsvo
;
678 register struct ppc_thread_state
*ts
;
679 register struct ppc_thread_state64
*xts
;
680 register struct ppc_exception_state
*es
;
681 register struct ppc_exception_state
*xes
;
682 register struct ppc_float_state
*fs
;
683 register struct ppc_vector_state
*vs
;
685 // dbgTrace((unsigned int)thr_act, (unsigned int)0 /*sv: was never set*/, flavor); /* (TEST/DEBUG) */
687 clgn
= count
; /* Get the count */
689 switch (flavor
) { /* Validate the count before we do anything else */
690 case PPC_THREAD_STATE
:
692 if (clgn
< PPC_THREAD_STATE_COUNT
) { /* Is it too short? */
693 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
697 case PPC_THREAD_STATE64
:
699 if (clgn
< PPC_THREAD_STATE64_COUNT
) { /* Is it too short? */
700 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
704 case PPC_EXCEPTION_STATE
:
706 if (clgn
< PPC_EXCEPTION_STATE_COUNT
) { /* Is it too short? */
707 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
710 case PPC_EXCEPTION_STATE64
:
712 if (clgn
< PPC_EXCEPTION_STATE64_COUNT
) { /* Is it too short? */
713 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
718 case PPC_FLOAT_STATE
:
720 if (clgn
< PPC_FLOAT_STATE_COUNT
) { /* Is it too short? */
721 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
727 case PPC_VECTOR_STATE
:
729 if (clgn
< PPC_VECTOR_STATE_COUNT
) { /* Is it too short? */
730 return KERN_INVALID_ARGUMENT
; /* Yeah, just leave... */
736 return KERN_INVALID_ARGUMENT
;
739 genuser
= get_user_regs(thread
); /* Find or allocate and initialize one */
743 case PPC_THREAD_STATE
:
745 ts
= (struct ppc_thread_state
*)tstate
;
747 genuser
->save_r0
= (uint64_t)ts
->r0
;
748 genuser
->save_r1
= (uint64_t)ts
->r1
;
749 genuser
->save_r2
= (uint64_t)ts
->r2
;
750 genuser
->save_r3
= (uint64_t)ts
->r3
;
751 genuser
->save_r4
= (uint64_t)ts
->r4
;
752 genuser
->save_r5
= (uint64_t)ts
->r5
;
753 genuser
->save_r6
= (uint64_t)ts
->r6
;
754 genuser
->save_r7
= (uint64_t)ts
->r7
;
755 genuser
->save_r8
= (uint64_t)ts
->r8
;
756 genuser
->save_r9
= (uint64_t)ts
->r9
;
757 genuser
->save_r10
= (uint64_t)ts
->r10
;
758 genuser
->save_r11
= (uint64_t)ts
->r11
;
759 genuser
->save_r12
= (uint64_t)ts
->r12
;
760 genuser
->save_r13
= (uint64_t)ts
->r13
;
761 genuser
->save_r14
= (uint64_t)ts
->r14
;
762 genuser
->save_r15
= (uint64_t)ts
->r15
;
763 genuser
->save_r16
= (uint64_t)ts
->r16
;
764 genuser
->save_r17
= (uint64_t)ts
->r17
;
765 genuser
->save_r18
= (uint64_t)ts
->r18
;
766 genuser
->save_r19
= (uint64_t)ts
->r19
;
767 genuser
->save_r20
= (uint64_t)ts
->r20
;
768 genuser
->save_r21
= (uint64_t)ts
->r21
;
769 genuser
->save_r22
= (uint64_t)ts
->r22
;
770 genuser
->save_r23
= (uint64_t)ts
->r23
;
771 genuser
->save_r24
= (uint64_t)ts
->r24
;
772 genuser
->save_r25
= (uint64_t)ts
->r25
;
773 genuser
->save_r26
= (uint64_t)ts
->r26
;
774 genuser
->save_r27
= (uint64_t)ts
->r27
;
775 genuser
->save_r28
= (uint64_t)ts
->r28
;
776 genuser
->save_r29
= (uint64_t)ts
->r29
;
777 genuser
->save_r30
= (uint64_t)ts
->r30
;
778 genuser
->save_r31
= (uint64_t)ts
->r31
;
780 genuser
->save_cr
= ts
->cr
;
781 genuser
->save_xer
= (uint64_t)ts
->xer
;
782 genuser
->save_lr
= (uint64_t)ts
->lr
;
783 genuser
->save_ctr
= (uint64_t)ts
->ctr
;
784 genuser
->save_srr0
= (uint64_t)ts
->srr0
;
785 genuser
->save_vrsave
= ts
->vrsave
; /* VRSAVE register (Altivec only) */
787 genuser
->save_srr1
= MSR_PREPARE_FOR_IMPORT(genuser
->save_srr1
, ts
->srr1
); /* Set the bits we can change */
789 genuser
->save_srr1
|= MSR_EXPORT_MASK_SET
;
791 genuser
->save_srr1
&= ~(MASK(MSR_FP
) | MASK(MSR_VEC
)); /* Make sure we don't enable the floating point unit */
793 if(task_has_64BitAddr(thread
->task
))
794 genuser
->save_srr1
|= (uint64_t)MASK32(MSR_SF
) << 32; /* If 64-bit task, force 64-bit mode */
796 genuser
->save_srr1
&= ~((uint64_t)MASK32(MSR_SF
) << 32); /* else 32-bit mode */
801 case PPC_THREAD_STATE64
:
803 xts
= (struct ppc_thread_state64
*)tstate
;
805 genuser
->save_r0
= xts
->r0
;
806 genuser
->save_r1
= xts
->r1
;
807 genuser
->save_r2
= xts
->r2
;
808 genuser
->save_r3
= xts
->r3
;
809 genuser
->save_r4
= xts
->r4
;
810 genuser
->save_r5
= xts
->r5
;
811 genuser
->save_r6
= xts
->r6
;
812 genuser
->save_r7
= xts
->r7
;
813 genuser
->save_r8
= xts
->r8
;
814 genuser
->save_r9
= xts
->r9
;
815 genuser
->save_r10
= xts
->r10
;
816 genuser
->save_r11
= xts
->r11
;
817 genuser
->save_r12
= xts
->r12
;
818 genuser
->save_r13
= xts
->r13
;
819 genuser
->save_r14
= xts
->r14
;
820 genuser
->save_r15
= xts
->r15
;
821 genuser
->save_r16
= xts
->r16
;
822 genuser
->save_r17
= xts
->r17
;
823 genuser
->save_r18
= xts
->r18
;
824 genuser
->save_r19
= xts
->r19
;
825 genuser
->save_r20
= xts
->r20
;
826 genuser
->save_r21
= xts
->r21
;
827 genuser
->save_r22
= xts
->r22
;
828 genuser
->save_r23
= xts
->r23
;
829 genuser
->save_r24
= xts
->r24
;
830 genuser
->save_r25
= xts
->r25
;
831 genuser
->save_r26
= xts
->r26
;
832 genuser
->save_r27
= xts
->r27
;
833 genuser
->save_r28
= xts
->r28
;
834 genuser
->save_r29
= xts
->r29
;
835 genuser
->save_r30
= xts
->r30
;
836 genuser
->save_r31
= xts
->r31
;
838 genuser
->save_cr
= xts
->cr
;
839 genuser
->save_xer
= xts
->xer
;
840 genuser
->save_lr
= xts
->lr
;
841 genuser
->save_ctr
= xts
->ctr
;
842 genuser
->save_srr0
= xts
->srr0
;
843 genuser
->save_vrsave
= xts
->vrsave
; /* VRSAVE register (Altivec only) */
845 genuser
->save_srr1
= MSR_PREPARE_FOR_IMPORT(genuser
->save_srr1
, xts
->srr1
); /* Set the bits we can change */
847 genuser
->save_srr1
|= MSR_EXPORT_MASK_SET
;
849 genuser
->save_srr1
&= ~(MASK(MSR_FP
) | MASK(MSR_VEC
)); /* Make sure we don't enable the floating point unit */
851 if(task_has_64BitAddr(thread
->task
))
852 genuser
->save_srr1
|= (uint64_t)MASK32(MSR_SF
) << 32; /* If 64-bit task, force 64-bit mode */
854 genuser
->save_srr1
&= ~((uint64_t)MASK32(MSR_SF
) << 32); /* else 32-bit mode */
859 case PPC_EXCEPTION_STATE
:
861 es
= (struct ppc_exception_state
*) tstate
;
863 genuser
->save_dar
= (uint64_t)es
->dar
;
864 genuser
->save_dsisr
= es
->dsisr
;
865 genuser
->save_exception
= es
->exception
;
870 * It's pretty worthless to try to change this stuff, but we'll do it anyway.
873 case PPC_EXCEPTION_STATE64
:
875 xes
= (struct ppc_exception_state
*) tstate
;
877 genuser
->save_dar
= xes
->dar
;
878 genuser
->save_dsisr
= xes
->dsisr
;
879 genuser
->save_exception
= xes
->exception
;
883 case PPC_FLOAT_STATE
:
885 toss_live_fpu(thread
->machine
.curctx
); /* Toss my floating point if live anywhere */
887 fsv
= find_user_fpu(thread
); /* Get the user's floating point context */
889 if(!fsv
) { /* Do we have one yet? */
890 fsv
= (savearea_fpu
*)save_alloc(); /* If we still don't have one, get a new one */
891 fsv
->save_hdr
.save_flags
= (fsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVfloat
<< SAVtypeshft
); /* Mark as in use as float */
892 fsv
->save_hdr
.save_act
= thread
;
893 fsv
->save_hdr
.save_prev
= 0; /* Mark no more */
894 fsv
->save_hdr
.save_level
= 0; /* Mark user state */
896 if(!thread
->machine
.curctx
->FPUsave
) thread
->machine
.curctx
->FPUsave
= fsv
; /* If no floating point, chain us first */
899 fsvn
= fsvo
= thread
->machine
.curctx
->FPUsave
; /* Remember first one */
901 while (fsvn
) { /* Go until we hit the end */
902 fsvo
= fsvn
; /* Remember the previous one */
903 fsvn
= CAST_DOWN(savearea_fpu
*, fsvo
->save_hdr
.save_prev
); /* Skip on to the next */
906 fsvo
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)fsv
); /* Queue us on in */
911 fs
= (struct ppc_float_state
*) tstate
; /* Point to source */
914 bcopy((char *)fs
, (char *)&fsv
->save_fp0
, 32*8); /* Move in the 32 registers */
916 genuser
->save_fpscr
= fs
->fpscr
; /* Copy the fpscr value to normal */
921 case PPC_VECTOR_STATE
:
923 toss_live_vec(thread
->machine
.curctx
); /* Toss my vector if live anywhere */
925 vsv
= find_user_vec(thread
); /* Get the user's vector context */
927 if(!vsv
) { /* Do we have one yet? */
928 vsv
= (savearea_vec
*)save_alloc(); /* If we still don't have one, get a new one */
929 vsv
->save_hdr
.save_flags
= (vsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVvector
<< SAVtypeshft
); /* Mark as in use as vector */
930 vsv
->save_hdr
.save_act
= thread
;
931 vsv
->save_hdr
.save_prev
= 0; /* Mark no more */
932 vsv
->save_hdr
.save_level
= 0; /* Mark user state */
934 if(!thread
->machine
.curctx
->VMXsave
) thread
->machine
.curctx
->VMXsave
= vsv
; /* If no vector, chain us first */
937 vsvn
= vsvo
= thread
->machine
.curctx
->VMXsave
; /* Remember first one */
939 while (vsvn
) { /* Go until we hit the end */
940 vsvo
= vsvn
; /* Remember the previous one */
941 vsvn
= CAST_DOWN(savearea_vec
*, vsvo
->save_hdr
.save_prev
); /* Skip on to the next */
944 vsvo
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)vsv
); /* Queue us on in */
949 vs
= (struct ppc_vector_state
*) tstate
; /* Point to source */
951 bcopy((char *)vs
, (char *)&vsv
->save_vr0
, 32*16); /* 32 registers plus status and validity and pad */
952 vsv
->save_vrvalid
= vs
->save_vrvalid
; /* Set validity bits */
955 for(i
= 0; i
< 4; i
++) genuser
->save_vscr
[i
] = vs
->save_vscr
[i
]; /* Set value for vscr */
961 return KERN_INVALID_ARGUMENT
;
967 * This is where registers that are not normally specified by the mach-o
968 * file on an execve should be nullified, perhaps to avoid a covert channel.
969 * We've never bothered to clear FPRs or VRs, but it is important to clear
970 * the FPSCR, which is kept in the general state but not set by the general
971 * flavor (ie, PPC_THREAD_STATE or PPC_THREAD_STATE64.)
974 machine_thread_state_initialize(
979 sv
= get_user_regs(thread
); /* Find or allocate and initialize one */
981 sv
->save_fpscr
= 0; /* Clear all floating point exceptions */
982 sv
->save_vrsave
= 0; /* Set the vector save state */
983 sv
->save_vscr
[0] = 0x00000000;
984 sv
->save_vscr
[1] = 0x00000000;
985 sv
->save_vscr
[2] = 0x00000000;
986 sv
->save_vscr
[3] = 0x00010000; /* Disable java mode and clear saturated */
993 * Duplicates the context of one thread into a new one.
994 * The new thread is assumed to be new and have no user state contexts except maybe a general one.
995 * We also assume that the old thread can't be running anywhere.
997 * We're only going to be duplicating user context here. That means that we will have to
998 * eliminate any floating point or vector kernel contexts and carry across the user state ones.
1007 savearea_fpu
*fsv
, *fsvn
;
1008 savearea_vec
*vsv
, *vsvn
;
1010 fpu_save(self
->machine
.curctx
); /* Make certain floating point state is all saved */
1011 vec_save(self
->machine
.curctx
); /* Make certain the vector state is all saved */
1013 sv
= get_user_regs(target
); /* Allocate and initialze context in the new activation */
1015 osv
= find_user_regs(self
); /* Find the original context */
1017 return (KERN_FAILURE
);
1019 bcopy((char *)((unsigned int)osv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1020 (char *)((unsigned int)sv
+ sizeof(savearea_comm
)),
1021 sizeof(struct savearea
) - sizeof(savearea_comm
));
1023 sv
->save_srr1
&= (uint64_t)(~(MASK(MSR_FP
) | MASK(MSR_VEC
))); /* Make certain that floating point and vector are turned off */
1025 fsv
= find_user_fpu(self
); /* Get any user floating point */
1027 target
->machine
.curctx
->FPUsave
= 0; /* Assume no floating point */
1029 if(fsv
) { /* Did we find one? */
1030 fsvn
= (savearea_fpu
*)save_alloc(); /* If we still don't have one, get a new one */
1031 fsvn
->save_hdr
.save_flags
= (fsvn
->save_hdr
.save_flags
& ~SAVtype
) | (SAVfloat
<< SAVtypeshft
); /* Mark as in use as float */
1032 fsvn
->save_hdr
.save_act
= target
;
1033 fsvn
->save_hdr
.save_prev
= 0; /* Mark no more */
1034 fsvn
->save_hdr
.save_level
= 0; /* Mark user state */
1036 target
->machine
.curctx
->FPUsave
= fsvn
; /* Chain in the floating point */
1038 bcopy((char *)((unsigned int)fsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1039 (char *)((unsigned int)fsvn
+ sizeof(savearea_comm
)),
1040 sizeof(struct savearea
) - sizeof(savearea_comm
));
1043 vsv
= find_user_vec(self
); /* Get any user vector */
1045 target
->machine
.curctx
->VMXsave
= 0; /* Assume no vector */
1047 if(vsv
) { /* Did we find one? */
1048 vsvn
= (savearea_vec
*)save_alloc(); /* If we still don't have one, get a new one */
1049 vsvn
->save_hdr
.save_flags
= (vsvn
->save_hdr
.save_flags
& ~SAVtype
) | (SAVvector
<< SAVtypeshft
); /* Mark as in use as float */
1050 vsvn
->save_hdr
.save_act
= target
;
1051 vsvn
->save_hdr
.save_prev
= 0; /* Mark no more */
1052 vsvn
->save_hdr
.save_level
= 0; /* Mark user state */
1054 target
->machine
.curctx
->VMXsave
= vsvn
; /* Chain in the floating point */
1056 bcopy((char *)((unsigned int)vsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1057 (char *)((unsigned int)vsvn
+ sizeof(savearea_comm
)),
1058 sizeof(struct savearea
) - sizeof(savearea_comm
));
1061 return (KERN_SUCCESS
);
1065 * Initializes a fresh set of user state values. If there is no user state context,
1066 * one is created. Floats and VMX are not created.
1068 * We only set initial values if there was no context found.
1078 if (thread
->machine
.upcb
)
1079 return thread
->machine
.upcb
;
1081 sv
= thread
->machine
.pcb
; /* Get the top savearea on the stack */
1082 osv
= 0; /* Set no user savearea yet */
1084 while(sv
) { /* Find the user context */
1085 osv
= sv
; /* Save the last one */
1086 sv
= CAST_DOWN(savearea
*, sv
->save_hdr
.save_prev
); /* Get the previous context */
1089 sv
= save_alloc(); /* Get one */
1090 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use as general */
1091 sv
->save_hdr
.save_act
= thread
;
1092 sv
->save_hdr
.save_prev
= 0; /* Mark no more */
1093 sv
->save_hdr
.save_level
= 0; /* Mark user state */
1095 if(osv
) { /* Did we already have one? */
1096 osv
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)sv
); /* Chain us on the end */
1098 else { /* We are the first */
1099 thread
->machine
.pcb
= sv
; /* Put it there */
1101 thread
->machine
.upcb
= sv
; /* Set user pcb */
1103 for(i
=0; i
< 32; i
+=2) { /* Fill up with defaults */
1104 ((unsigned int *)&sv
->save_r0
)[i
] = ((unsigned int *)&FloatInit
)[0];
1105 ((unsigned int *)&sv
->save_r0
)[i
+1] = ((unsigned int *)&FloatInit
)[1];
1109 sv
->save_lr
= (uint64_t)FloatInit
;
1110 sv
->save_ctr
= (uint64_t)FloatInit
;
1111 sv
->save_srr0
= (uint64_t)FloatInit
;
1112 sv
->save_srr1
= (uint64_t)MSR_EXPORT_MASK_SET
;
1113 if(task_has_64BitAddr(thread
->task
))
1114 sv
->save_srr1
|= (uint64_t)MASK32(MSR_SF
) << 32; /* If 64-bit task, force 64-bit mode */
1116 sv
->save_fpscr
= 0; /* Clear all floating point exceptions */
1118 sv
->save_vrsave
= 0; /* Set the vector save state */
1119 sv
->save_vscr
[0] = 0x00000000;
1120 sv
->save_vscr
[1] = 0x00000000;
1121 sv
->save_vscr
[2] = 0x00000000;
1122 sv
->save_vscr
[3] = 0x00010000; /* Disable java mode and clear saturated */
1124 return sv
; /* Bye bye... */
1128 * Find the user state context. If there is no user state context,
1129 * we just return a 0.
1136 return thread
->machine
.upcb
;
1139 /* The name of this call is something of a misnomer since the mact.pcb can
1140 * contain chained saveareas, but it will do for now..
1146 return thread
->machine
.pcb
;
1150 * Find the user state floating point context. If there is no user state context,
1151 * we just return a 0.
1161 intr
= ml_set_interrupts_enabled(FALSE
);
1162 fsv
= thread
->machine
.curctx
->FPUsave
; /* Get the start of the floating point chain */
1164 while(fsv
) { /* Look until the end or we find it */
1165 if(!(fsv
->save_hdr
.save_level
)) break; /* Is the the user state stuff? (the level is 0 if so) */
1166 fsv
= CAST_DOWN(savearea_fpu
*, fsv
->save_hdr
.save_prev
); /* Try the previous one */
1168 (void) ml_set_interrupts_enabled(intr
);
1170 return fsv
; /* Bye bye... */
1174 * Find the user state vector context. If there is no user state context,
1175 * we just return a 0.
1185 intr
= ml_set_interrupts_enabled(FALSE
);
1186 vsv
= thread
->machine
.curctx
->VMXsave
; /* Get the start of the vector chain */
1188 while(vsv
) { /* Look until the end or we find it */
1189 if(!(vsv
->save_hdr
.save_level
)) break; /* Is the the user state stuff? (the level is 0 if so) */
1190 vsv
= CAST_DOWN(savearea_vec
*, vsv
->save_hdr
.save_prev
); /* Try the previous one */
1192 (void) ml_set_interrupts_enabled(intr
);
1194 return vsv
; /* Bye bye... */
1197 * Find the user state vector context for the current thread. If there is no user state context,
1198 * we just return a 0.
1201 savearea_vec
*find_user_vec_curr(void) {
1204 thread_t thread
= current_thread();
1207 vec_save(thread
->machine
.curctx
); /* Force save if live */
1209 intr
= ml_set_interrupts_enabled(FALSE
);
1210 vsv
= thread
->machine
.curctx
->VMXsave
; /* Get the start of the vector chain */
1212 while(vsv
) { /* Look until the end or we find it */
1213 if(!(vsv
->save_hdr
.save_level
)) break; /* Is the the user state stuff? (the level is 0 if so) */
1214 vsv
= CAST_DOWN(savearea_vec
*, vsv
->save_hdr
.save_prev
); /* Try the previous one */
1216 (void) ml_set_interrupts_enabled(intr
);
1218 return vsv
; /* Bye bye... */
1224 * Return the user stack pointer from the machine
1225 * dependent thread state info.
1229 __unused thread_t thread
,
1231 thread_state_t tstate
,
1233 mach_vm_offset_t
*user_stack
,
1242 case PPC_THREAD_STATE
:
1244 struct ppc_thread_state
*state
;
1246 if (count
< PPC_THREAD_STATE_COUNT
)
1247 return (KERN_INVALID_ARGUMENT
);
1249 state
= (struct ppc_thread_state
*) tstate
;
1252 * If a valid user stack is specified, use it.
1255 *user_stack
= CAST_USER_ADDR_T(state
->r1
);
1259 *user_stack
= CAST_USER_ADDR_T(USRSTACK
);
1266 case PPC_THREAD_STATE64
:
1268 struct ppc_thread_state64
*state64
;
1270 if (count
< PPC_THREAD_STATE64_COUNT
)
1271 return (KERN_INVALID_ARGUMENT
);
1273 state64
= (struct ppc_thread_state64
*)tstate
;
1276 * If a valid user stack is specified, use it.
1278 if (state64
->r1
!= MACH_VM_MIN_ADDRESS
) {
1279 *user_stack
= state64
->r1
;
1283 *user_stack
= USRSTACK64
;
1291 return (KERN_INVALID_ARGUMENT
);
1294 return (KERN_SUCCESS
);
1299 * thread_setuserstack:
1301 * Sets the user stack pointer into the machine
1302 * dependent thread state info.
1305 thread_setuserstack(thread_t thread
, mach_vm_address_t user_stack
)
1309 sv
= get_user_regs(thread
); /* Get the user state registers */
1311 sv
->save_r1
= user_stack
;
1317 * thread_adjuserstack:
1319 * Returns the adjusted user stack pointer from the machine
1320 * dependent thread state info. Usef for small (<2G) deltas.
1323 thread_adjuserstack(thread_t thread
, int adjust
)
1327 sv
= get_user_regs(thread
); /* Get the user state registers */
1329 sv
->save_r1
+= adjust
; /* Adjust the stack */
1331 return sv
->save_r1
; /* Return the adjusted stack */
1336 thread_setsinglestep(thread_t thread
, int on
)
1340 sv
= get_user_regs(thread
); /* Get the user state registers */
1343 sv
->save_srr1
|= MASK(MSR_SE
);
1345 sv
->save_srr1
&= ~MASK(MSR_SE
);
1349 * thread_setentrypoint:
1351 * Sets the user PC into the machine
1352 * dependent thread state info.
1356 thread_setentrypoint(thread_t thread
, uint64_t entry
)
1360 sv
= get_user_regs(thread
); /* Get the user state registers */
1362 sv
->save_srr0
= entry
;
1369 __unused thread_t thread
,
1371 thread_state_t tstate
,
1373 mach_vm_offset_t
*entry_point
1377 /* Silly code: "if *entry_point is 0, make it 0" */
1381 if (*entry_point
== 0ULL)
1382 *entry_point
= MACH_VM_MIN_ADDRESS
;
1386 case PPC_THREAD_STATE
:
1388 struct ppc_thread_state
*state
;
1390 if (count
< PPC_THREAD_STATE_COUNT
)
1391 return (KERN_INVALID_ARGUMENT
);
1393 state
= (struct ppc_thread_state
*) tstate
;
1396 * If a valid entry point is specified, use it.
1399 *entry_point
= CAST_USER_ADDR_T(state
->srr0
);
1401 *entry_point
= CAST_USER_ADDR_T(VM_MIN_ADDRESS
);
1406 case PPC_THREAD_STATE64
:
1408 struct ppc_thread_state64
*state64
;
1410 if (count
< PPC_THREAD_STATE_COUNT
)
1411 return (KERN_INVALID_ARGUMENT
);
1413 state64
= (struct ppc_thread_state64
*)tstate
;
1416 * If a valid entry point is specified, use it.
1418 if (state64
->srr0
) {
1419 *entry_point
= state64
->srr0
;
1421 *entry_point
= MACH_VM_MIN_ADDRESS
;
1427 return (KERN_INVALID_ARGUMENT
);
1430 return (KERN_SUCCESS
);
1433 unsigned int get_msr_exportmask(void)
1435 return (MSR_EXPORT_MASK_SET
);
1438 unsigned int get_msr_nbits(void)
1440 return (MASK(MSR_POW
)|MASK(MSR_ILE
)|MASK(MSR_IP
)|MASK(MSR_LE
));
1442 unsigned int get_msr_rbits(void)
1444 return (MASK(MSR_PR
)|MASK(MSR_ME
)|MASK(MSR_IR
)|MASK(MSR_DR
)|MASK(MSR_EE
));
1447 void ppc_checkthreadstate(void * tsptr
, int flavor
)
1449 if (flavor
== PPC_THREAD_STATE64
) {
1450 struct ppc_thread_state64
*ts64
=(struct ppc_thread_state64
*)tsptr
;
1452 /* Make sure naughty bits are off and necessary bits are on */
1453 ts64
->srr1
&= ~(MASK(MSR_POW
)|MASK(MSR_ILE
)|MASK(MSR_IP
)|MASK(MSR_LE
));
1454 ts64
->srr1
|= (MASK(MSR_PR
)|MASK(MSR_ME
)|MASK(MSR_IR
)|MASK(MSR_DR
)|MASK(MSR_EE
));
1456 struct ppc_thread_state
*ts
=(struct ppc_thread_state
*)tsptr
;
1458 /* Make sure naughty bits are off and necessary bits are on */
1459 ts
->srr1
&= ~(MASK(MSR_POW
)|MASK(MSR_ILE
)|MASK(MSR_IP
)|MASK(MSR_LE
));
1460 ts
->srr1
|= (MASK(MSR_PR
)|MASK(MSR_ME
)|MASK(MSR_IR
)|MASK(MSR_DR
)|MASK(MSR_EE
));
1470 struct savearea
*child_state
;
1472 child_state
= get_user_regs(child
);
1474 child_state
->save_r3
= (uint_t
)pid
;
1475 child_state
->save_r4
= 1ULL;
1482 struct savearea
*parent_state
;
1484 parent_state
= get_user_regs(parent
);
1486 parent_state
->save_r3
= (uint64_t)pid
;
1487 parent_state
->save_r4
= 0;
1491 * Saves the complete context (general, floating point, and vector) of the current activation.
1492 * We will collect everything into an opaque block of 1 to 3 saveareas and pass back a
1495 * The savearea is made to look like it belongs to the source activation. This needs to
1496 * be adjusted when these contexts are attached to a new activation.
1500 void *act_thread_csave(void) {
1503 savearea_fpu
*fsv
, *ofsv
;
1504 savearea_vec
*vsv
, *ovsv
;
1508 thread
= current_thread();
1510 fpu_save(thread
->machine
.curctx
); /* Make certain floating point state is all saved */
1511 vec_save(thread
->machine
.curctx
); /* Make certain the vector state is all saved */
1513 osv
= find_user_regs(thread
); /* Get our savearea */
1516 panic("act_thread_csave: attempting to preserve the context of an activation with none (%08X)\n", thread
);
1519 sv
= save_alloc(); /* Get a fresh save area to save into */
1520 sv
->save_hdr
.save_flags
= (sv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVgeneral
<< SAVtypeshft
); /* Mark as in use as general */
1521 sv
->save_hdr
.save_act
= thread
;
1522 sv
->save_hdr
.save_prev
= 0; /* Mark no more */
1523 sv
->save_hdr
.save_level
= 0; /* Mark user state */
1526 bcopy((char *)((unsigned int)osv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1527 (char *)((unsigned int)sv
+ sizeof(savearea_comm
)),
1528 sizeof(struct savearea
) - sizeof(savearea_comm
));
1530 sv
->save_srr1
&= (uint64_t)(~(MASK(MSR_FP
) | MASK(MSR_VEC
))); /* Make certain that floating point and vector are turned off */
1532 sv
->save_hdr
.save_misc2
= 0xDEBB1ED0; /* Eye catcher for debug */
1533 sv
->save_hdr
.save_misc3
= 0xE5DA11A5; /* Eye catcher for debug */
1536 ofsv
= find_user_fpu(thread
); /* Get any user floating point */
1538 sv
->save_hdr
.save_misc0
= 0; /* Assume no floating point */
1540 if(ofsv
) { /* Did we find one? */
1541 fsv
= (savearea_fpu
*)save_alloc(); /* If we still don't have one, get a new one */
1542 fsv
->save_hdr
.save_flags
= (fsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVfloat
<< SAVtypeshft
); /* Mark as in use as float */
1543 fsv
->save_hdr
.save_act
= thread
;
1544 fsv
->save_hdr
.save_prev
= 0; /* Mark no more */
1545 fsv
->save_hdr
.save_level
= 0; /* Mark user state */
1546 fsv
->save_hdr
.save_misc2
= 0xDEBB1ED0; /* Eye catcher for debug */
1547 fsv
->save_hdr
.save_misc3
= 0xE5DA11A5; /* Eye catcher for debug */
1549 sv
->save_hdr
.save_misc0
= (uint64_t)((uintptr_t)fsv
); /* Remember this one */
1551 bcopy((char *)((unsigned int)ofsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1552 (char *)((unsigned int)fsv
+ sizeof(savearea_comm
)),
1553 sizeof(struct savearea
) - sizeof(savearea_comm
));
1556 ovsv
= find_user_vec(thread
); /* Get any user vector */
1558 sv
->save_hdr
.save_misc1
= 0; /* Assume no vector */
1560 if(ovsv
) { /* Did we find one? */
1561 vsv
= (savearea_vec
*)save_alloc(); /* If we still don't have one, get a new one */
1562 vsv
->save_hdr
.save_flags
= (vsv
->save_hdr
.save_flags
& ~SAVtype
) | (SAVvector
<< SAVtypeshft
); /* Mark as in use as float */
1563 vsv
->save_hdr
.save_act
= thread
;
1564 vsv
->save_hdr
.save_prev
= 0; /* Mark no more */
1565 vsv
->save_hdr
.save_level
= 0; /* Mark user state */
1566 vsv
->save_hdr
.save_misc2
= 0xDEBB1ED0; /* Eye catcher for debug */
1567 vsv
->save_hdr
.save_misc3
= 0xE5DA11A5; /* Eye catcher for debug */
1569 sv
->save_hdr
.save_misc1
= (uint64_t)((uintptr_t)vsv
); /* Chain in the floating point */
1571 bcopy((char *)((unsigned int)ovsv
+ sizeof(savearea_comm
)), /* Copy everything but the headers */
1572 (char *)((unsigned int)vsv
+ sizeof(savearea_comm
)),
1573 sizeof(struct savearea
) - sizeof(savearea_comm
));
1576 return (void *)sv
; /* Bye bye... */
1582 * Attaches saved user state context to an activation. We will replace any
1583 * user state context with what is passed in. The saved context consists of a
1584 * savearea that was setup by
1585 * We will collect everything into one savearea and pass that back.
1587 * The savearea is made to look like it belongs to the source activation. This needs to
1588 * be adjusted when these contexts are attached to a new activation.
1592 void act_thread_catt(void *ctx
) {
1594 savearea
*sv
, *osv
, *psv
;
1595 savearea_fpu
*fsv
, *ofsv
, *pfsv
;
1596 savearea_vec
*vsv
, *ovsv
, *pvsv
;
1600 sv
= (savearea
*)ctx
; /* Make this easier for C */
1602 fsv
= CAST_DOWN(savearea_fpu
*, sv
->save_hdr
.save_misc0
); /* Get a possible floating point savearea */
1603 vsv
= CAST_DOWN(savearea_vec
*, sv
->save_hdr
.save_misc1
); /* Get a possible vector savearea */
1605 if((sv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (sv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1606 panic("act_thread_catt: attempt to attach invalid general context savearea - %08X\n", sv
); /* Die */
1609 if(fsv
&& ((fsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (fsv
->save_hdr
.save_misc3
!= 0xE5DA11A5))) { /* See if valid savearea */
1610 panic("act_thread_catt: attempt to attach invalid float context savearea - %08X\n", fsv
); /* Die */
1613 if(vsv
&& ((vsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (vsv
->save_hdr
.save_misc3
!= 0xE5DA11A5))) { /* See if valid savearea */
1614 panic("act_thread_catt: attempt to attach invalid vector context savearea - %08X\n", vsv
); /* Die */
1617 thread
= current_thread();
1619 act_machine_sv_free(thread
); /* Blow away any current kernel FP or vector.
1620 We do not support those across a vfork */
1621 toss_live_fpu(thread
->machine
.curctx
); /* Toss my floating point if live anywhere */
1622 toss_live_vec(thread
->machine
.curctx
); /* Toss my vector if live anywhere */
1624 sv
->save_hdr
.save_misc2
= 0; /* Eye catcher for debug */
1625 sv
->save_hdr
.save_misc3
= 0; /* Eye catcher for debug */
1626 sv
->save_hdr
.save_act
= thread
;
1628 spc
= (unsigned int)thread
->map
->pmap
->space
; /* Get the space we're in */
1630 osv
= thread
->machine
.pcb
; /* Get the top general savearea */
1632 while(osv
) { /* Any saved state? */
1633 if(osv
->save_srr1
& MASK(MSR_PR
)) break; /* Leave if this is user state */
1634 psv
= osv
; /* Save previous savearea address */
1635 osv
= CAST_DOWN(savearea
*, osv
->save_hdr
.save_prev
); /* Get one underneath our's */
1638 if(osv
) { /* Did we find one? */
1639 if(psv
) psv
->save_hdr
.save_prev
= 0; /* Yes, clear pointer to it (it should always be last) or */
1640 else thread
->machine
.pcb
= 0; /* to the start if the only one */
1642 save_release(osv
); /* Nope, release it */
1646 if(psv
) psv
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)sv
); /* Chain us to the end or */
1647 else thread
->machine
.pcb
= (pcb_t
)sv
; /* to the start if the only one */
1648 thread
->machine
.upcb
= (pcb_t
)sv
; /* Set the user pcb */
1650 ovsv
= thread
->machine
.curctx
->VMXsave
; /* Get the top vector savearea */
1653 while(ovsv
) { /* Any VMX saved state? */
1654 if(!(ovsv
->save_hdr
.save_level
)) break; /* Leave if this is user state */
1655 pvsv
= ovsv
; /* Save previous savearea address */
1656 ovsv
= CAST_DOWN(savearea_vec
*, ovsv
->save_hdr
.save_prev
); /* Get one underneath our's */
1659 if(ovsv
) { /* Did we find one? */
1660 if(pvsv
) pvsv
->save_hdr
.save_prev
= 0; /* Yes, clear pointer to it (it should always be last) or */
1661 else thread
->machine
.curctx
->VMXsave
= 0; /* to the start if the only one */
1663 save_release((savearea
*)ovsv
); /* Nope, release it */
1666 if(vsv
) { /* Are we sticking any vector on this one? */
1667 if(pvsv
) pvsv
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)vsv
); /* Yes, chain us to the end or */
1669 thread
->machine
.curctx
->VMXsave
= vsv
; /* to the start if the only one */
1670 thread
->machine
.curctx
->VMXlevel
= 0; /* Insure that we don't have a leftover level */
1673 vsv
->save_hdr
.save_misc2
= 0; /* Eye catcher for debug */
1674 vsv
->save_hdr
.save_misc3
= 0; /* Eye catcher for debug */
1675 vsv
->save_hdr
.save_act
= thread
;
1678 ofsv
= thread
->machine
.curctx
->FPUsave
; /* Get the top float savearea */
1681 while(ofsv
) { /* Any float saved state? */
1682 if(!(ofsv
->save_hdr
.save_level
)) break; /* Leave if this is user state */
1683 pfsv
= ofsv
; /* Save previous savearea address */
1684 ofsv
= CAST_DOWN(savearea_fpu
*, ofsv
->save_hdr
.save_prev
); /* Get one underneath our's */
1687 if(ofsv
) { /* Did we find one? */
1688 if(pfsv
) pfsv
->save_hdr
.save_prev
= 0; /* Yes, clear pointer to it (it should always be last) or */
1689 else thread
->machine
.curctx
->FPUsave
= 0; /* to the start if the only one */
1691 save_release((savearea
*)ofsv
); /* Nope, release it */
1694 if(fsv
) { /* Are we sticking any vector on this one? */
1695 if(pfsv
) pfsv
->save_hdr
.save_prev
= (addr64_t
)((uintptr_t)fsv
); /* Yes, chain us to the end or */
1697 thread
->machine
.curctx
->FPUsave
= fsv
; /* to the start if the only one */
1698 thread
->machine
.curctx
->FPUlevel
= 0; /* Insure that we don't have a leftover level */
1701 fsv
->save_hdr
.save_misc2
= 0; /* Eye catcher for debug */
1702 fsv
->save_hdr
.save_misc3
= 0; /* Eye catcher for debug */
1703 fsv
->save_hdr
.save_act
= thread
;
1711 * Releases saved context. We need this because the saved context is opague.
1712 * be adjusted when these contexts are attached to a new activation.
1717 act_thread_cfree(void *ctx
)
1724 sv
= (savearea
*)ctx
; /* Make this easier for C */
1726 fsv
= CAST_DOWN(savearea_fpu
*, sv
->save_hdr
.save_misc0
); /* Get a possible floating point savearea */
1727 vsv
= CAST_DOWN(savearea_vec
*, sv
->save_hdr
.save_misc1
); /* Get a possible vector savearea */
1729 if((sv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (sv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1730 panic("act_thread_cfree: attempt to detatch invalid general context savearea - %08X\n", sv
); /* Die */
1733 save_release(sv
); /* Toss the general savearea */
1735 if(fsv
) { /* See if there is any saved floating point */
1736 if((fsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (fsv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1737 panic("act_thread_cfree: attempt to detatch invalid float context savearea - %08X\n", fsv
); /* Die */
1740 save_release((savearea
*)fsv
); /* Toss saved context */
1743 if(vsv
) { /* See if there is any saved floating point */
1744 if((vsv
->save_hdr
.save_misc2
!= 0xDEBB1ED0) || (vsv
->save_hdr
.save_misc3
!= 0xE5DA11A5)) { /* See if valid savearea */
1745 panic("act_thread_cfree: attempt to detatch invalid vector context savearea - %08X\n", vsv
); /* Die */
1748 save_release((savearea
*)vsv
); /* Toss saved context */
1755 * thread_enable_fpe:
1757 * enables or disables floating point exceptions for the thread.
1760 int thread_enable_fpe(
1767 sv
= find_user_regs(thread
); /* Find the user registers */
1768 if(!sv
) sv
= get_user_regs(thread
); /* Didn't find any, allocate and initialize one */
1770 oldmsr
= sv
->save_srr1
; /* Get the old msr */
1772 if(onoff
) sv
->save_srr1
= oldmsr
| (uint64_t)(MASK(MSR_FE0
) | MASK(MSR_FE1
)); /* Flip on precise FP exceptions */
1773 else sv
->save_srr1
= oldmsr
& (uint64_t)(~(MASK(MSR_FE0
) | MASK(MSR_FE1
))); /* Flip on precise FP exceptions */
1775 return ((oldmsr
& (MASK(MSR_FE0
) | MASK(MSR_FE1
))) != 0); /* Return if it was enabled or not */