]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/status.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / ppc / status.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 *
33 */
34
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>
45
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. */
49
50 #include <vm/vm_map.h>
51
52 extern unsigned int killprint;
53 extern double FloatInit;
54 extern unsigned long QNaNbarbarian[4];
55
56 #define USRSTACK 0xc0000000
57
58 kern_return_t
59 thread_userstack(
60 thread_t,
61 int,
62 thread_state_t,
63 unsigned int,
64 mach_vm_offset_t *,
65 int *
66 );
67
68 kern_return_t
69 thread_entrypoint(
70 thread_t,
71 int,
72 thread_state_t,
73 unsigned int,
74 mach_vm_offset_t *
75 );
76
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);
84
85 /*
86 * Maps state flavor to number of words in the state:
87 */
88 __private_extern__
89 unsigned int _MachineStateCount[] = {
90 /* FLAVOR_LIST */ 0,
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,
97 };
98
99 /*
100 * thread_getstatus:
101 *
102 * Get the status of the specified thread.
103 */
104
105 kern_return_t
106 machine_thread_get_state(
107 thread_t thread,
108 thread_flavor_t flavor,
109 thread_state_t tstate,
110 mach_msg_type_number_t *count)
111 {
112
113 register struct savearea *sv; /* Pointer to the context savearea */
114 register savearea_fpu *fsv;
115 register savearea_vec *vsv;
116 savearea *genuser;
117 int i, j;
118 unsigned int vrvalidwrk;
119
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;
126
127 genuser = find_user_regs(thread);
128
129 switch (flavor) {
130
131 case THREAD_STATE_FLAVOR_LIST:
132
133 if (*count < 6) {
134 return (KERN_INVALID_ARGUMENT);
135 }
136
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;
143 *count = 6;
144
145 return KERN_SUCCESS;
146
147 case PPC_THREAD_STATE:
148
149 if (*count < PPC_THREAD_STATE_COUNT) { /* Is the count ok? */
150 return KERN_INVALID_ARGUMENT;
151 }
152
153 ts = (struct ppc_thread_state *) tstate;
154
155 sv = genuser; /* Copy this over */
156
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) */
198 }
199 else { /* No user state yet. Save seemingly random values. */
200
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];
204 }
205 ts->cr = 0;
206 ts->xer = 0;
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;
211 ts->mq = 0;
212 ts->vrsave = 0; /* VRSAVE register (Altivec only) */
213 }
214
215 *count = PPC_THREAD_STATE_COUNT; /* Pass back the amount we actually copied */
216 return KERN_SUCCESS;
217
218
219 case PPC_THREAD_STATE64:
220
221 if (*count < PPC_THREAD_STATE64_COUNT) { /* Is the count ok? */
222 return KERN_INVALID_ARGUMENT;
223 }
224
225 xts = (struct ppc_thread_state64 *) tstate;
226
227 sv = genuser; /* Copy this over */
228
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) */
269 }
270 else { /* No user state yet. Save seemingly random values. */
271
272 for(i=0; i < 32; i++) { /* Fill up with defaults */
273 ((unsigned long long *)&xts->r0)[i] = ((unsigned long long *)&FloatInit)[0];
274 }
275 xts->cr = 0;
276 xts->xer = 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) */
284 }
285
286 *count = PPC_THREAD_STATE64_COUNT; /* Pass back the amount we actually copied */
287 return KERN_SUCCESS;
288
289 case PPC_EXCEPTION_STATE:
290
291 if (*count < PPC_EXCEPTION_STATE_COUNT) {
292 return KERN_INVALID_ARGUMENT;
293 }
294
295 es = (struct ppc_exception_state *) tstate;
296 sv = genuser; /* Copy this over */
297
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;
302 }
303 else { /* Nope, not yet */
304 es->dar = 0;
305 es->dsisr = 0;
306 es->exception = ((unsigned int *)&FloatInit)[0];
307 }
308
309 *count = PPC_EXCEPTION_STATE_COUNT;
310 return KERN_SUCCESS;
311
312 case PPC_EXCEPTION_STATE64:
313
314 if (*count < PPC_EXCEPTION_STATE64_COUNT) {
315 return KERN_INVALID_ARGUMENT;
316 }
317
318 xes = (struct ppc_exception_state64 *) tstate;
319 sv = genuser; /* Copy this over */
320
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;
325 }
326 else { /* Nope, not yet */
327 xes->dar = 0;
328 xes->dsisr = 0;
329 xes->exception = ((unsigned int *)&FloatInit)[0];
330 }
331
332 *count = PPC_EXCEPTION_STATE64_COUNT;
333 return KERN_SUCCESS;
334
335 case PPC_FLOAT_STATE:
336
337 if (*count < PPC_FLOAT_STATE_COUNT) {
338 return KERN_INVALID_ARGUMENT;
339 }
340
341 fpu_save(thread->machine.curctx); /* Just in case it's live, save it */
342
343 fs = (struct ppc_float_state *) tstate; /* Point to destination */
344
345 fsv = find_user_fpu(thread); /* Get the user's fpu savearea */
346
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 */
352 }
353 else { /* No floating point yet */
354
355 for(i=0; i < 32; i++) { /* Initialize floating points */
356 fs->fpregs[i] = FloatInit; /* Initial value */
357 }
358 fs->fpscr_pad = 0; /* Initial value */
359 fs->fpscr = 0; /* Initial value */
360 }
361
362 *count = PPC_FLOAT_STATE_COUNT;
363
364 return KERN_SUCCESS;
365
366 case PPC_VECTOR_STATE:
367
368 if (*count < PPC_VECTOR_STATE_COUNT) {
369 return KERN_INVALID_ARGUMENT;
370 }
371
372 vec_save(thread->machine.curctx); /* Just in case it's live, save it */
373
374 vs = (struct ppc_vector_state *) tstate; /* Point to destination */
375
376 vsv = find_user_vec(thread); /* Find the vector savearea */
377
378 if(vsv) { /* See if we have any */
379
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 */
383 else {
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 */
388 }
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 */
394 }
395 vrvalidwrk = vrvalidwrk << 1; /* Shift over to the next */
396 }
397 }
398 else { /* No vector yet */
399
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 */
402 }
403
404 if(genuser) for(j=0; j < 4; j++) vs->save_vscr[j] = genuser->save_vscr[j]; /* Set value for vscr */
405 else {
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 */
410 }
411 vs->save_vrvalid = 0; /* Clear the valid flags */
412 }
413
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 */
416
417 *count = PPC_VECTOR_STATE_COUNT;
418 return KERN_SUCCESS;
419
420 default:
421 return KERN_INVALID_ARGUMENT;
422 }
423 }
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().
429 */
430
431 kern_return_t
432 machine_thread_get_kern_state(
433 thread_t thread,
434 thread_flavor_t flavor,
435 thread_state_t tstate,
436 mach_msg_type_number_t *count)
437 {
438
439 register struct savearea *sv; /* Pointer to the context savearea */
440 savearea *genkern;
441 int i;
442
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;
447
448 genkern = find_kern_regs(thread);
449
450 switch (flavor) {
451
452 case THREAD_STATE_FLAVOR_LIST:
453
454 if (*count < 6) {
455 return (KERN_INVALID_ARGUMENT);
456 }
457
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;
464 *count = 6;
465
466 return KERN_SUCCESS;
467
468 case PPC_THREAD_STATE:
469
470 if (*count < PPC_THREAD_STATE_COUNT) { /* Is the count ok? */
471 return KERN_INVALID_ARGUMENT;
472 }
473
474 ts = (struct ppc_thread_state *) tstate;
475
476 sv = genkern; /* Copy this over */
477
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) */
519 }
520 else { /* No state yet. Save seemingly random values. */
521
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];
525 }
526 ts->cr = 0;
527 ts->xer = 0;
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;
532 ts->mq = 0;
533 ts->vrsave = 0; /* VRSAVE register (Altivec only) */
534 }
535
536 *count = PPC_THREAD_STATE_COUNT; /* Pass back the amount we actually copied */
537 return KERN_SUCCESS;
538
539
540 case PPC_THREAD_STATE64:
541
542 if (*count < PPC_THREAD_STATE64_COUNT) { /* Is the count ok? */
543 return KERN_INVALID_ARGUMENT;
544 }
545
546 xts = (struct ppc_thread_state64 *) tstate;
547
548 sv = genkern; /* Copy this over */
549
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) */
590 }
591 else { /* No user state yet. Save seemingly random values. */
592
593 for(i=0; i < 32; i++) { /* Fill up with defaults */
594 ((unsigned long long *)&xts->r0)[i] = ((unsigned long long *)&FloatInit)[0];
595 }
596 xts->cr = 0;
597 xts->xer = 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) */
603 }
604
605 *count = PPC_THREAD_STATE64_COUNT; /* Pass back the amount we actually copied */
606 return KERN_SUCCESS;
607
608 case PPC_EXCEPTION_STATE:
609
610 if (*count < PPC_EXCEPTION_STATE_COUNT) {
611 return KERN_INVALID_ARGUMENT;
612 }
613
614 es = (struct ppc_exception_state *) tstate;
615 sv = genkern; /* Copy this over */
616
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;
621 }
622 else { /* Nope, not yet */
623 es->dar = 0;
624 es->dsisr = 0;
625 es->exception = ((unsigned int *)&FloatInit)[0];
626 }
627
628 *count = PPC_EXCEPTION_STATE_COUNT;
629 return KERN_SUCCESS;
630
631 case PPC_EXCEPTION_STATE64:
632
633 if (*count < PPC_EXCEPTION_STATE64_COUNT) {
634 return KERN_INVALID_ARGUMENT;
635 }
636
637 xes = (struct ppc_exception_state64 *) tstate;
638 sv = genkern; /* Copy this over */
639
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;
644 }
645 else { /* Nope, not yet */
646 xes->dar = 0;
647 xes->dsisr = 0;
648 xes->exception = ((unsigned int *)&FloatInit)[0];
649 }
650
651 *count = PPC_EXCEPTION_STATE64_COUNT;
652 return KERN_SUCCESS;
653
654 default:
655 return KERN_INVALID_ARGUMENT;
656 }
657 }
658
659
660 /*
661 * thread_setstatus:
662 *
663 * Set the status of the specified thread.
664 */
665 kern_return_t
666 machine_thread_set_state(
667 thread_t thread,
668 thread_flavor_t flavor,
669 thread_state_t tstate,
670 mach_msg_type_number_t count)
671 {
672
673 savearea *genuser;
674 savearea_fpu *fsv, *fsvn, *fsvo;
675 savearea_vec *vsv, *vsvn, *vsvo;
676 unsigned int i;
677 unsigned int clgn;
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;
684
685 // dbgTrace((unsigned int)thr_act, (unsigned int)0 /*sv: was never set*/, flavor); /* (TEST/DEBUG) */
686
687 clgn = count; /* Get the count */
688
689 switch (flavor) { /* Validate the count before we do anything else */
690 case PPC_THREAD_STATE:
691
692 if (clgn < PPC_THREAD_STATE_COUNT) { /* Is it too short? */
693 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
694 }
695 break;
696
697 case PPC_THREAD_STATE64:
698
699 if (clgn < PPC_THREAD_STATE64_COUNT) { /* Is it too short? */
700 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
701 }
702 break;
703
704 case PPC_EXCEPTION_STATE:
705
706 if (clgn < PPC_EXCEPTION_STATE_COUNT) { /* Is it too short? */
707 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
708 }
709
710 case PPC_EXCEPTION_STATE64:
711
712 if (clgn < PPC_EXCEPTION_STATE64_COUNT) { /* Is it too short? */
713 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
714 }
715
716 break;
717
718 case PPC_FLOAT_STATE:
719
720 if (clgn < PPC_FLOAT_STATE_COUNT) { /* Is it too short? */
721 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
722 }
723
724 break;
725
726
727 case PPC_VECTOR_STATE:
728
729 if (clgn < PPC_VECTOR_STATE_COUNT) { /* Is it too short? */
730 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
731 }
732
733 break;
734
735 default:
736 return KERN_INVALID_ARGUMENT;
737 }
738
739 genuser = get_user_regs(thread); /* Find or allocate and initialize one */
740
741 switch (flavor) {
742
743 case PPC_THREAD_STATE:
744
745 ts = (struct ppc_thread_state *)tstate;
746
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;
779
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) */
786
787 genuser->save_srr1 = MSR_PREPARE_FOR_IMPORT(genuser->save_srr1, ts->srr1); /* Set the bits we can change */
788
789 genuser->save_srr1 |= MSR_EXPORT_MASK_SET;
790
791 genuser->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make sure we don't enable the floating point unit */
792
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 */
795 else
796 genuser->save_srr1 &= ~((uint64_t)MASK32(MSR_SF) << 32); /* else 32-bit mode */
797
798 return KERN_SUCCESS;
799
800
801 case PPC_THREAD_STATE64:
802
803 xts = (struct ppc_thread_state64 *)tstate;
804
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;
837
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) */
844
845 genuser->save_srr1 = MSR_PREPARE_FOR_IMPORT(genuser->save_srr1, xts->srr1); /* Set the bits we can change */
846
847 genuser->save_srr1 |= MSR_EXPORT_MASK_SET;
848
849 genuser->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make sure we don't enable the floating point unit */
850
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 */
853 else
854 genuser->save_srr1 &= ~((uint64_t)MASK32(MSR_SF) << 32); /* else 32-bit mode */
855
856 return KERN_SUCCESS;
857
858
859 case PPC_EXCEPTION_STATE:
860
861 es = (struct ppc_exception_state *) tstate;
862
863 genuser->save_dar = (uint64_t)es->dar;
864 genuser->save_dsisr = es->dsisr;
865 genuser->save_exception = es->exception;
866
867 return KERN_SUCCESS;
868
869 /*
870 * It's pretty worthless to try to change this stuff, but we'll do it anyway.
871 */
872
873 case PPC_EXCEPTION_STATE64:
874
875 xes = (struct ppc_exception_state *) tstate;
876
877 genuser->save_dar = xes->dar;
878 genuser->save_dsisr = xes->dsisr;
879 genuser->save_exception = xes->exception;
880
881 return KERN_SUCCESS;
882
883 case PPC_FLOAT_STATE:
884
885 toss_live_fpu(thread->machine.curctx); /* Toss my floating point if live anywhere */
886
887 fsv = find_user_fpu(thread); /* Get the user's floating point context */
888
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 */
895
896 if(!thread->machine.curctx->FPUsave) thread->machine.curctx->FPUsave = fsv; /* If no floating point, chain us first */
897 else {
898
899 fsvn = fsvo = thread->machine.curctx->FPUsave; /* Remember first one */
900
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 */
904 }
905
906 fsvo->save_hdr.save_prev = (addr64_t)((uintptr_t)fsv); /* Queue us on in */
907 }
908
909 }
910
911 fs = (struct ppc_float_state *) tstate; /* Point to source */
912
913
914 bcopy((char *)fs, (char *)&fsv->save_fp0, 32*8); /* Move in the 32 registers */
915
916 genuser->save_fpscr = fs->fpscr; /* Copy the fpscr value to normal */
917
918 return KERN_SUCCESS;
919
920
921 case PPC_VECTOR_STATE:
922
923 toss_live_vec(thread->machine.curctx); /* Toss my vector if live anywhere */
924
925 vsv = find_user_vec(thread); /* Get the user's vector context */
926
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 */
933
934 if(!thread->machine.curctx->VMXsave) thread->machine.curctx->VMXsave = vsv; /* If no vector, chain us first */
935 else {
936
937 vsvn = vsvo = thread->machine.curctx->VMXsave; /* Remember first one */
938
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 */
942 }
943
944 vsvo->save_hdr.save_prev = (addr64_t)((uintptr_t)vsv); /* Queue us on in */
945 }
946
947 }
948
949 vs = (struct ppc_vector_state *) tstate; /* Point to source */
950
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 */
953
954
955 for(i = 0; i < 4; i++) genuser->save_vscr[i] = vs->save_vscr[i]; /* Set value for vscr */
956
957 return KERN_SUCCESS;
958
959
960 default:
961 return KERN_INVALID_ARGUMENT;
962 }
963 }
964
965
966 /*
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.)
972 */
973 kern_return_t
974 machine_thread_state_initialize(
975 thread_t thread)
976 {
977 savearea *sv;
978
979 sv = get_user_regs(thread); /* Find or allocate and initialize one */
980
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 */
987
988 return KERN_SUCCESS;
989 }
990
991
992 /*
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.
996 *
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.
999 */
1000
1001 kern_return_t
1002 machine_thread_dup(
1003 thread_t self,
1004 thread_t target)
1005 {
1006 savearea *sv, *osv;
1007 savearea_fpu *fsv, *fsvn;
1008 savearea_vec *vsv, *vsvn;
1009
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 */
1012
1013 sv = get_user_regs(target); /* Allocate and initialze context in the new activation */
1014
1015 osv = find_user_regs(self); /* Find the original context */
1016 if(!osv)
1017 return (KERN_FAILURE);
1018
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));
1022
1023 sv->save_srr1 &= (uint64_t)(~(MASK(MSR_FP) | MASK(MSR_VEC))); /* Make certain that floating point and vector are turned off */
1024
1025 fsv = find_user_fpu(self); /* Get any user floating point */
1026
1027 target->machine.curctx->FPUsave = 0; /* Assume no floating point */
1028
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 */
1035
1036 target->machine.curctx->FPUsave = fsvn; /* Chain in the floating point */
1037
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));
1041 }
1042
1043 vsv = find_user_vec(self); /* Get any user vector */
1044
1045 target->machine.curctx->VMXsave = 0; /* Assume no vector */
1046
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 */
1053
1054 target->machine.curctx->VMXsave = vsvn; /* Chain in the floating point */
1055
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));
1059 }
1060
1061 return (KERN_SUCCESS);
1062 }
1063
1064 /*
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.
1067 *
1068 * We only set initial values if there was no context found.
1069 */
1070
1071 savearea *
1072 get_user_regs(
1073 thread_t thread)
1074 {
1075 savearea *sv, *osv;
1076 unsigned int i;
1077
1078 if (thread->machine.upcb)
1079 return thread->machine.upcb;
1080
1081 sv = thread->machine.pcb; /* Get the top savearea on the stack */
1082 osv = 0; /* Set no user savearea yet */
1083
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 */
1087 }
1088
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 */
1094
1095 if(osv) { /* Did we already have one? */
1096 osv->save_hdr.save_prev = (addr64_t)((uintptr_t)sv); /* Chain us on the end */
1097 }
1098 else { /* We are the first */
1099 thread->machine.pcb = sv; /* Put it there */
1100 }
1101 thread->machine.upcb = sv; /* Set user pcb */
1102
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];
1106 }
1107 sv->save_cr = 0;
1108 sv->save_xer = 0;
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 */
1115
1116 sv->save_fpscr = 0; /* Clear all floating point exceptions */
1117
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 */
1123
1124 return sv; /* Bye bye... */
1125 }
1126
1127 /*
1128 * Find the user state context. If there is no user state context,
1129 * we just return a 0.
1130 */
1131
1132 savearea *
1133 find_user_regs(
1134 thread_t thread)
1135 {
1136 return thread->machine.upcb;
1137 }
1138
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..
1141 */
1142 savearea *
1143 find_kern_regs(
1144 thread_t thread)
1145 {
1146 return thread->machine.pcb;
1147 }
1148
1149 /*
1150 * Find the user state floating point context. If there is no user state context,
1151 * we just return a 0.
1152 */
1153
1154 savearea_fpu *
1155 find_user_fpu(
1156 thread_t thread)
1157 {
1158 savearea_fpu *fsv;
1159 boolean_t intr;
1160
1161 intr = ml_set_interrupts_enabled(FALSE);
1162 fsv = thread->machine.curctx->FPUsave; /* Get the start of the floating point chain */
1163
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 */
1167 }
1168 (void) ml_set_interrupts_enabled(intr);
1169
1170 return fsv; /* Bye bye... */
1171 }
1172
1173 /*
1174 * Find the user state vector context. If there is no user state context,
1175 * we just return a 0.
1176 */
1177
1178 savearea_vec *
1179 find_user_vec(
1180 thread_t thread)
1181 {
1182 savearea_vec *vsv;
1183 boolean_t intr;
1184
1185 intr = ml_set_interrupts_enabled(FALSE);
1186 vsv = thread->machine.curctx->VMXsave; /* Get the start of the vector chain */
1187
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 */
1191 }
1192 (void) ml_set_interrupts_enabled(intr);
1193
1194 return vsv; /* Bye bye... */
1195 }
1196 /*
1197 * Find the user state vector context for the current thread. If there is no user state context,
1198 * we just return a 0.
1199 */
1200
1201 savearea_vec *find_user_vec_curr(void) {
1202
1203 savearea_vec *vsv;
1204 thread_t thread = current_thread();
1205 boolean_t intr;
1206
1207 vec_save(thread->machine.curctx); /* Force save if live */
1208
1209 intr = ml_set_interrupts_enabled(FALSE);
1210 vsv = thread->machine.curctx->VMXsave; /* Get the start of the vector chain */
1211
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 */
1215 }
1216 (void) ml_set_interrupts_enabled(intr);
1217
1218 return vsv; /* Bye bye... */
1219 }
1220
1221 /*
1222 * thread_userstack:
1223 *
1224 * Return the user stack pointer from the machine
1225 * dependent thread state info.
1226 */
1227 kern_return_t
1228 thread_userstack(
1229 __unused thread_t thread,
1230 int flavor,
1231 thread_state_t tstate,
1232 unsigned int count,
1233 mach_vm_offset_t *user_stack,
1234 int *customstack
1235 )
1236 {
1237 /*
1238 * Set a default.
1239 */
1240
1241 switch (flavor) {
1242 case PPC_THREAD_STATE:
1243 {
1244 struct ppc_thread_state *state;
1245
1246 if (count < PPC_THREAD_STATE_COUNT)
1247 return (KERN_INVALID_ARGUMENT);
1248
1249 state = (struct ppc_thread_state *) tstate;
1250
1251 /*
1252 * If a valid user stack is specified, use it.
1253 */
1254 if (state->r1) {
1255 *user_stack = CAST_USER_ADDR_T(state->r1);
1256 if (customstack)
1257 *customstack = 1;
1258 } else {
1259 *user_stack = CAST_USER_ADDR_T(USRSTACK);
1260 if (customstack)
1261 *customstack = 0;
1262 }
1263 }
1264 break;
1265
1266 case PPC_THREAD_STATE64:
1267 {
1268 struct ppc_thread_state64 *state64;
1269
1270 if (count < PPC_THREAD_STATE64_COUNT)
1271 return (KERN_INVALID_ARGUMENT);
1272
1273 state64 = (struct ppc_thread_state64 *)tstate;
1274
1275 /*
1276 * If a valid user stack is specified, use it.
1277 */
1278 if (state64->r1 != MACH_VM_MIN_ADDRESS) {
1279 *user_stack = state64->r1;
1280 if (customstack)
1281 *customstack = 1;
1282 } else {
1283 *user_stack = USRSTACK64;
1284 if (customstack)
1285 *customstack = 0;
1286 }
1287 }
1288 break;
1289
1290 default :
1291 return (KERN_INVALID_ARGUMENT);
1292 }
1293
1294 return (KERN_SUCCESS);
1295 }
1296
1297
1298 /*
1299 * thread_setuserstack:
1300 *
1301 * Sets the user stack pointer into the machine
1302 * dependent thread state info.
1303 */
1304 void
1305 thread_setuserstack(thread_t thread, mach_vm_address_t user_stack)
1306 {
1307 savearea *sv;
1308
1309 sv = get_user_regs(thread); /* Get the user state registers */
1310
1311 sv->save_r1 = user_stack;
1312
1313 return;
1314 }
1315
1316 /*
1317 * thread_adjuserstack:
1318 *
1319 * Returns the adjusted user stack pointer from the machine
1320 * dependent thread state info. Usef for small (<2G) deltas.
1321 */
1322 uint64_t
1323 thread_adjuserstack(thread_t thread, int adjust)
1324 {
1325 savearea *sv;
1326
1327 sv = get_user_regs(thread); /* Get the user state registers */
1328
1329 sv->save_r1 += adjust; /* Adjust the stack */
1330
1331 return sv->save_r1; /* Return the adjusted stack */
1332
1333 }
1334
1335 void
1336 thread_setsinglestep(thread_t thread, int on)
1337 {
1338 savearea *sv;
1339
1340 sv = get_user_regs(thread); /* Get the user state registers */
1341
1342 if (on)
1343 sv->save_srr1 |= MASK(MSR_SE);
1344 else
1345 sv->save_srr1 &= ~MASK(MSR_SE);
1346 }
1347
1348 /*
1349 * thread_setentrypoint:
1350 *
1351 * Sets the user PC into the machine
1352 * dependent thread state info.
1353 */
1354
1355 void
1356 thread_setentrypoint(thread_t thread, uint64_t entry)
1357 {
1358 savearea *sv;
1359
1360 sv = get_user_regs(thread); /* Get the user state registers */
1361
1362 sv->save_srr0 = entry;
1363
1364 return;
1365 }
1366
1367 kern_return_t
1368 thread_entrypoint(
1369 __unused thread_t thread,
1370 int flavor,
1371 thread_state_t tstate,
1372 unsigned int count,
1373 mach_vm_offset_t *entry_point
1374 )
1375 {
1376 #if 0
1377 /* Silly code: "if *entry_point is 0, make it 0" */
1378 /*
1379 * Set a default.
1380 */
1381 if (*entry_point == 0ULL)
1382 *entry_point = MACH_VM_MIN_ADDRESS;
1383 #endif
1384
1385 switch (flavor) {
1386 case PPC_THREAD_STATE:
1387 {
1388 struct ppc_thread_state *state;
1389
1390 if (count < PPC_THREAD_STATE_COUNT)
1391 return (KERN_INVALID_ARGUMENT);
1392
1393 state = (struct ppc_thread_state *) tstate;
1394
1395 /*
1396 * If a valid entry point is specified, use it.
1397 */
1398 if (state->srr0) {
1399 *entry_point = CAST_USER_ADDR_T(state->srr0);
1400 } else {
1401 *entry_point = CAST_USER_ADDR_T(VM_MIN_ADDRESS);
1402 }
1403 }
1404 break;
1405
1406 case PPC_THREAD_STATE64:
1407 {
1408 struct ppc_thread_state64 *state64;
1409
1410 if (count < PPC_THREAD_STATE_COUNT)
1411 return (KERN_INVALID_ARGUMENT);
1412
1413 state64 = (struct ppc_thread_state64 *)tstate;
1414
1415 /*
1416 * If a valid entry point is specified, use it.
1417 */
1418 if (state64->srr0) {
1419 *entry_point = state64->srr0;
1420 } else {
1421 *entry_point = MACH_VM_MIN_ADDRESS;
1422 }
1423 }
1424 break;
1425
1426 default:
1427 return (KERN_INVALID_ARGUMENT);
1428 }
1429
1430 return (KERN_SUCCESS);
1431 }
1432
1433 unsigned int get_msr_exportmask(void)
1434 {
1435 return (MSR_EXPORT_MASK_SET);
1436 }
1437
1438 unsigned int get_msr_nbits(void)
1439 {
1440 return (MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1441 }
1442 unsigned int get_msr_rbits(void)
1443 {
1444 return (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1445 }
1446
1447 void ppc_checkthreadstate(void * tsptr, int flavor)
1448 {
1449 if (flavor == PPC_THREAD_STATE64) {
1450 struct ppc_thread_state64 *ts64 =(struct ppc_thread_state64 *)tsptr;
1451
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));
1455 } else {
1456 struct ppc_thread_state *ts =(struct ppc_thread_state *)tsptr;
1457
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));
1461 }
1462 return;
1463 }
1464
1465 void
1466 thread_set_child(
1467 thread_t child,
1468 int pid)
1469 {
1470 struct savearea *child_state;
1471
1472 child_state = get_user_regs(child);
1473
1474 child_state->save_r3 = (uint_t)pid;
1475 child_state->save_r4 = 1ULL;
1476 }
1477 void
1478 thread_set_parent(
1479 thread_t parent,
1480 int pid)
1481 {
1482 struct savearea *parent_state;
1483
1484 parent_state = get_user_regs(parent);
1485
1486 parent_state->save_r3 = (uint64_t)pid;
1487 parent_state->save_r4 = 0;
1488 }
1489
1490 /*
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
1493 * pointer to that.
1494 *
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.
1497 *
1498 */
1499
1500 void *act_thread_csave(void) {
1501
1502 savearea *sv, *osv;
1503 savearea_fpu *fsv, *ofsv;
1504 savearea_vec *vsv, *ovsv;
1505
1506 thread_t thread;
1507
1508 thread = current_thread();
1509
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 */
1512
1513 osv = find_user_regs(thread); /* Get our savearea */
1514
1515 if(!osv) {
1516 panic("act_thread_csave: attempting to preserve the context of an activation with none (%08X)\n", thread);
1517 }
1518
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 */
1524
1525
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));
1529
1530 sv->save_srr1 &= (uint64_t)(~(MASK(MSR_FP) | MASK(MSR_VEC))); /* Make certain that floating point and vector are turned off */
1531
1532 sv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1533 sv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
1534
1535
1536 ofsv = find_user_fpu(thread); /* Get any user floating point */
1537
1538 sv->save_hdr.save_misc0 = 0; /* Assume no floating point */
1539
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 */
1548
1549 sv->save_hdr.save_misc0 = (uint64_t)((uintptr_t)fsv); /* Remember this one */
1550
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));
1554 }
1555
1556 ovsv = find_user_vec(thread); /* Get any user vector */
1557
1558 sv->save_hdr.save_misc1 = 0; /* Assume no vector */
1559
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 */
1568
1569 sv->save_hdr.save_misc1 = (uint64_t)((uintptr_t)vsv); /* Chain in the floating point */
1570
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));
1574 }
1575
1576 return (void *)sv; /* Bye bye... */
1577 }
1578
1579
1580
1581 /*
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.
1586 *
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.
1589 *
1590 */
1591
1592 void act_thread_catt(void *ctx) {
1593
1594 savearea *sv, *osv, *psv;
1595 savearea_fpu *fsv, *ofsv, *pfsv;
1596 savearea_vec *vsv, *ovsv, *pvsv;
1597 unsigned int spc;
1598 thread_t thread;
1599
1600 sv = (savearea *)ctx; /* Make this easier for C */
1601
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 */
1604
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 */
1607 }
1608
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 */
1611 }
1612
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 */
1615 }
1616
1617 thread = current_thread();
1618
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 */
1623
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;
1627
1628 spc = (unsigned int)thread->map->pmap->space; /* Get the space we're in */
1629
1630 osv = thread->machine.pcb; /* Get the top general savearea */
1631 psv = 0;
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 */
1636 }
1637
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 */
1641
1642 save_release(osv); /* Nope, release it */
1643
1644 }
1645
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 */
1649
1650 ovsv = thread->machine.curctx->VMXsave; /* Get the top vector savearea */
1651
1652 pvsv = 0;
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 */
1657 }
1658
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 */
1662
1663 save_release((savearea *)ovsv); /* Nope, release it */
1664 }
1665
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 */
1668 else {
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 */
1671 }
1672
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;
1676 }
1677
1678 ofsv = thread->machine.curctx->FPUsave; /* Get the top float savearea */
1679
1680 pfsv = 0;
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 */
1685 }
1686
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 */
1690
1691 save_release((savearea *)ofsv); /* Nope, release it */
1692 }
1693
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 */
1696 else {
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 */
1699 }
1700
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;
1704 }
1705
1706 }
1707
1708
1709
1710 /*
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.
1713 *
1714 */
1715
1716 void
1717 act_thread_cfree(void *ctx)
1718 {
1719
1720 savearea *sv;
1721 savearea_fpu *fsv;
1722 savearea_vec *vsv;
1723
1724 sv = (savearea *)ctx; /* Make this easier for C */
1725
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 */
1728
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 */
1731 }
1732
1733 save_release(sv); /* Toss the general savearea */
1734
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 */
1738 }
1739
1740 save_release((savearea *)fsv); /* Toss saved context */
1741 }
1742
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 */
1746 }
1747
1748 save_release((savearea *)vsv); /* Toss saved context */
1749 }
1750
1751 return;
1752 }
1753
1754 /*
1755 * thread_enable_fpe:
1756 *
1757 * enables or disables floating point exceptions for the thread.
1758 * returns old state
1759 */
1760 int thread_enable_fpe(
1761 thread_t thread,
1762 int onoff)
1763 {
1764 savearea *sv;
1765 uint64_t oldmsr;
1766
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 */
1769
1770 oldmsr = sv->save_srr1; /* Get the old msr */
1771
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 */
1774
1775 return ((oldmsr & (MASK(MSR_FE0) | MASK(MSR_FE1))) != 0); /* Return if it was enabled or not */
1776 }