]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/status.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / osfmk / ppc / status.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 *
31 */
32
33 #include <kern/thread.h>
34 #include <kern/misc_protos.h>
35 #include <mach/ppc/thread_status.h>
36 #include <ppc/proc_reg.h>
37 #include <ppc/cpu_internal.h>
38 #include <ppc/exception.h>
39 #include <ppc/misc_protos.h>
40 #include <ppc/savearea.h>
41 #include <ppc/thread.h>
42 #include <ppc/Firmware.h>
43
44 //#include <sys/time.h>
45 typedef unsigned int fixpt_t; /* XXX <sys/resource.h> not self contained */
46 #include <ppc/vmparam.h> /* USRSTACK, etc. */
47
48 #include <vm/vm_map.h>
49
50 extern unsigned int killprint;
51 extern double FloatInit;
52 extern unsigned long QNaNbarbarian[4];
53
54 #define USRSTACK 0xc0000000
55
56 kern_return_t
57 thread_userstack(
58 thread_t,
59 int,
60 thread_state_t,
61 unsigned int,
62 mach_vm_offset_t *,
63 int *
64 );
65
66 kern_return_t
67 thread_entrypoint(
68 thread_t,
69 int,
70 thread_state_t,
71 unsigned int,
72 mach_vm_offset_t *
73 );
74
75 unsigned int get_msr_exportmask(void);
76 unsigned int get_msr_nbits(void);
77 unsigned int get_msr_rbits(void);
78 void ppc_checkthreadstate(void *, int);
79 void thread_set_child(thread_t child, int pid);
80 void thread_set_parent(thread_t parent, int pid);
81 void save_release(struct savearea *save);
82
83 /*
84 * Maps state flavor to number of words in the state:
85 */
86 __private_extern__
87 unsigned int _MachineStateCount[] = {
88 /* FLAVOR_LIST */ 0,
89 PPC_THREAD_STATE_COUNT,
90 PPC_FLOAT_STATE_COUNT,
91 PPC_EXCEPTION_STATE_COUNT,
92 PPC_VECTOR_STATE_COUNT,
93 PPC_THREAD_STATE64_COUNT,
94 PPC_EXCEPTION_STATE64_COUNT,
95 };
96
97 /*
98 * thread_getstatus:
99 *
100 * Get the status of the specified thread.
101 */
102
103 kern_return_t
104 machine_thread_get_state(
105 thread_t thread,
106 thread_flavor_t flavor,
107 thread_state_t tstate,
108 mach_msg_type_number_t *count)
109 {
110
111 register struct savearea *sv; /* Pointer to the context savearea */
112 register savearea_fpu *fsv;
113 register savearea_vec *vsv;
114 savearea *genuser;
115 int i, j;
116 unsigned int vrvalidwrk;
117
118 register struct ppc_thread_state *ts;
119 register struct ppc_thread_state64 *xts;
120 register struct ppc_exception_state *es;
121 register struct ppc_exception_state64 *xes;
122 register struct ppc_float_state *fs;
123 register struct ppc_vector_state *vs;
124
125 genuser = find_user_regs(thread);
126
127 switch (flavor) {
128
129 case THREAD_STATE_FLAVOR_LIST:
130
131 if (*count < 6) {
132 return (KERN_INVALID_ARGUMENT);
133 }
134
135 tstate[0] = PPC_THREAD_STATE;
136 tstate[1] = PPC_FLOAT_STATE;
137 tstate[2] = PPC_EXCEPTION_STATE;
138 tstate[3] = PPC_VECTOR_STATE;
139 tstate[4] = PPC_THREAD_STATE64;
140 tstate[5] = PPC_EXCEPTION_STATE64;
141 *count = 6;
142
143 return KERN_SUCCESS;
144
145 case PPC_THREAD_STATE:
146
147 if (*count < PPC_THREAD_STATE_COUNT) { /* Is the count ok? */
148 return KERN_INVALID_ARGUMENT;
149 }
150
151 ts = (struct ppc_thread_state *) tstate;
152
153 sv = genuser; /* Copy this over */
154
155 if(sv) { /* Is there a save area yet? */
156 ts->r0 = (unsigned int)sv->save_r0;
157 ts->r1 = (unsigned int)sv->save_r1;
158 ts->r2 = (unsigned int)sv->save_r2;
159 ts->r3 = (unsigned int)sv->save_r3;
160 ts->r4 = (unsigned int)sv->save_r4;
161 ts->r5 = (unsigned int)sv->save_r5;
162 ts->r6 = (unsigned int)sv->save_r6;
163 ts->r7 = (unsigned int)sv->save_r7;
164 ts->r8 = (unsigned int)sv->save_r8;
165 ts->r9 = (unsigned int)sv->save_r9;
166 ts->r10 = (unsigned int)sv->save_r10;
167 ts->r11 = (unsigned int)sv->save_r11;
168 ts->r12 = (unsigned int)sv->save_r12;
169 ts->r13 = (unsigned int)sv->save_r13;
170 ts->r14 = (unsigned int)sv->save_r14;
171 ts->r15 = (unsigned int)sv->save_r15;
172 ts->r16 = (unsigned int)sv->save_r16;
173 ts->r17 = (unsigned int)sv->save_r17;
174 ts->r18 = (unsigned int)sv->save_r18;
175 ts->r19 = (unsigned int)sv->save_r19;
176 ts->r20 = (unsigned int)sv->save_r20;
177 ts->r21 = (unsigned int)sv->save_r21;
178 ts->r22 = (unsigned int)sv->save_r22;
179 ts->r23 = (unsigned int)sv->save_r23;
180 ts->r24 = (unsigned int)sv->save_r24;
181 ts->r25 = (unsigned int)sv->save_r25;
182 ts->r26 = (unsigned int)sv->save_r26;
183 ts->r27 = (unsigned int)sv->save_r27;
184 ts->r28 = (unsigned int)sv->save_r28;
185 ts->r29 = (unsigned int)sv->save_r29;
186 ts->r30 = (unsigned int)sv->save_r30;
187 ts->r31 = (unsigned int)sv->save_r31;
188 ts->cr = (unsigned int)sv->save_cr;
189 ts->xer = (unsigned int)sv->save_xer;
190 ts->lr = (unsigned int)sv->save_lr;
191 ts->ctr = (unsigned int)sv->save_ctr;
192 ts->srr0 = (unsigned int)sv->save_srr0;
193 ts->srr1 = (unsigned int)sv->save_srr1;
194 ts->mq = 0; /* MQ register (601 only) */
195 ts->vrsave = (unsigned int)sv->save_vrsave; /* VRSAVE register (Altivec only) */
196 }
197 else { /* No user state yet. Save seemingly random values. */
198
199 for(i=0; i < 32; i+=2) { /* Fill up with defaults */
200 ((unsigned int *)&ts->r0)[i] = ((unsigned int *)&FloatInit)[0];
201 ((unsigned int *)&ts->r0)[i+1] = ((unsigned int *)&FloatInit)[1];
202 }
203 ts->cr = 0;
204 ts->xer = 0;
205 ts->lr = ((unsigned int *)&FloatInit)[0];
206 ts->ctr = ((unsigned int *)&FloatInit)[1];
207 ts->srr0 = ((unsigned int *)&FloatInit)[0];
208 ts->srr1 = MSR_EXPORT_MASK_SET;
209 ts->mq = 0;
210 ts->vrsave = 0; /* VRSAVE register (Altivec only) */
211 }
212
213 *count = PPC_THREAD_STATE_COUNT; /* Pass back the amount we actually copied */
214 return KERN_SUCCESS;
215
216
217 case PPC_THREAD_STATE64:
218
219 if (*count < PPC_THREAD_STATE64_COUNT) { /* Is the count ok? */
220 return KERN_INVALID_ARGUMENT;
221 }
222
223 xts = (struct ppc_thread_state64 *) tstate;
224
225 sv = genuser; /* Copy this over */
226
227 if(sv) { /* Is there a save area yet? */
228 xts->r0 = sv->save_r0;
229 xts->r1 = sv->save_r1;
230 xts->r2 = sv->save_r2;
231 xts->r3 = sv->save_r3;
232 xts->r4 = sv->save_r4;
233 xts->r5 = sv->save_r5;
234 xts->r6 = sv->save_r6;
235 xts->r7 = sv->save_r7;
236 xts->r8 = sv->save_r8;
237 xts->r9 = sv->save_r9;
238 xts->r10 = sv->save_r10;
239 xts->r11 = sv->save_r11;
240 xts->r12 = sv->save_r12;
241 xts->r13 = sv->save_r13;
242 xts->r14 = sv->save_r14;
243 xts->r15 = sv->save_r15;
244 xts->r16 = sv->save_r16;
245 xts->r17 = sv->save_r17;
246 xts->r18 = sv->save_r18;
247 xts->r19 = sv->save_r19;
248 xts->r20 = sv->save_r20;
249 xts->r21 = sv->save_r21;
250 xts->r22 = sv->save_r22;
251 xts->r23 = sv->save_r23;
252 xts->r24 = sv->save_r24;
253 xts->r25 = sv->save_r25;
254 xts->r26 = sv->save_r26;
255 xts->r27 = sv->save_r27;
256 xts->r28 = sv->save_r28;
257 xts->r29 = sv->save_r29;
258 xts->r30 = sv->save_r30;
259 xts->r31 = sv->save_r31;
260 xts->cr = sv->save_cr;
261 xts->xer = sv->save_xer;
262 xts->lr = sv->save_lr;
263 xts->ctr = sv->save_ctr;
264 xts->srr0 = sv->save_srr0;
265 xts->srr1 = sv->save_srr1;
266 xts->vrsave = sv->save_vrsave; /* VRSAVE register (Altivec only) */
267 }
268 else { /* No user state yet. Save seemingly random values. */
269
270 for(i=0; i < 32; i++) { /* Fill up with defaults */
271 ((unsigned long long *)&xts->r0)[i] = ((unsigned long long *)&FloatInit)[0];
272 }
273 xts->cr = 0;
274 xts->xer = 0;
275 xts->lr = ((unsigned long long *)&FloatInit)[0];
276 xts->ctr = ((unsigned long long *)&FloatInit)[0];
277 xts->srr0 = ((unsigned long long *)&FloatInit)[0];
278 xts->srr1 = MSR_EXPORT_MASK_SET;
279 if(task_has_64BitAddr(thread->task))
280 xts->srr1 |= (uint64_t)MASK32(MSR_SF) << 32; /* If 64-bit task, force 64-bit mode */
281 xts->vrsave = 0; /* VRSAVE register (Altivec only) */
282 }
283
284 *count = PPC_THREAD_STATE64_COUNT; /* Pass back the amount we actually copied */
285 return KERN_SUCCESS;
286
287 case PPC_EXCEPTION_STATE:
288
289 if (*count < PPC_EXCEPTION_STATE_COUNT) {
290 return KERN_INVALID_ARGUMENT;
291 }
292
293 es = (struct ppc_exception_state *) tstate;
294 sv = genuser; /* Copy this over */
295
296 if(sv) { /* See if valid state yet */
297 es->dar = (unsigned int)sv->save_dar;
298 es->dsisr = sv->save_dsisr;
299 es->exception = sv->save_exception;
300 }
301 else { /* Nope, not yet */
302 es->dar = 0;
303 es->dsisr = 0;
304 es->exception = ((unsigned int *)&FloatInit)[0];
305 }
306
307 *count = PPC_EXCEPTION_STATE_COUNT;
308 return KERN_SUCCESS;
309
310 case PPC_EXCEPTION_STATE64:
311
312 if (*count < PPC_EXCEPTION_STATE64_COUNT) {
313 return KERN_INVALID_ARGUMENT;
314 }
315
316 xes = (struct ppc_exception_state64 *) tstate;
317 sv = genuser; /* Copy this over */
318
319 if(sv) { /* See if valid state yet */
320 xes->dar = sv->save_dar;
321 xes->dsisr = sv->save_dsisr;
322 xes->exception = sv->save_exception;
323 }
324 else { /* Nope, not yet */
325 xes->dar = 0;
326 xes->dsisr = 0;
327 xes->exception = ((unsigned int *)&FloatInit)[0];
328 }
329
330 *count = PPC_EXCEPTION_STATE64_COUNT;
331 return KERN_SUCCESS;
332
333 case PPC_FLOAT_STATE:
334
335 if (*count < PPC_FLOAT_STATE_COUNT) {
336 return KERN_INVALID_ARGUMENT;
337 }
338
339 fpu_save(thread->machine.curctx); /* Just in case it's live, save it */
340
341 fs = (struct ppc_float_state *) tstate; /* Point to destination */
342
343 fsv = find_user_fpu(thread); /* Get the user's fpu savearea */
344
345 if(fsv) { /* See if we have any */
346 bcopy((char *)&fsv->save_fp0, (char *)fs, 32*8); /* 32 registers */
347 fs->fpscr_pad = 0; /* Be clean and tidy */
348 if(genuser) fs->fpscr = genuser->save_fpscr; /* Set the fpscr value to general */
349 else fs->fpscr = 0; /* If no user, initialize this */
350 }
351 else { /* No floating point yet */
352
353 for(i=0; i < 32; i++) { /* Initialize floating points */
354 fs->fpregs[i] = FloatInit; /* Initial value */
355 }
356 fs->fpscr_pad = 0; /* Initial value */
357 fs->fpscr = 0; /* Initial value */
358 }
359
360 *count = PPC_FLOAT_STATE_COUNT;
361
362 return KERN_SUCCESS;
363
364 case PPC_VECTOR_STATE:
365
366 if (*count < PPC_VECTOR_STATE_COUNT) {
367 return KERN_INVALID_ARGUMENT;
368 }
369
370 vec_save(thread->machine.curctx); /* Just in case it's live, save it */
371
372 vs = (struct ppc_vector_state *) tstate; /* Point to destination */
373
374 vsv = find_user_vec(thread); /* Find the vector savearea */
375
376 if(vsv) { /* See if we have any */
377
378 vrvalidwrk = vsv->save_vrvalid; /* Get the valid flags */
379 vs->save_vrvalid = vsv->save_vrvalid; /* Set the valid flags */
380 if(genuser) for(j=0; j < 4; j++) vs->save_vscr[j] = genuser->save_vscr[j]; /* Set value for vscr */
381 else {
382 vs->save_vscr[0] = 0; /* Set an initial value if no general user yet */
383 vs->save_vscr[1] = 0;
384 vs->save_vscr[2] = 0;
385 vs->save_vscr[3] = 0x00010000; /* Always start with Java mode off */
386 }
387 for(i=0; i < 32; i++) { /* Copy the saved registers and invalidate the others */
388 for(j=0; j < 4; j++) {
389 if(vrvalidwrk & 0x80000000) (vs->save_vr)[i][j] =
390 ((unsigned int *)&(vsv->save_vr0))[(i * 4) + j]; /* We have this register saved */
391 else vs->save_vr[i][j] = QNaNbarbarian[j]; /* Set invalid value */
392 }
393 vrvalidwrk = vrvalidwrk << 1; /* Shift over to the next */
394 }
395 }
396 else { /* No vector yet */
397
398 for(i=0; i < 32; i++) { /* Initialize vector registers */
399 for(j=0; j < 4; j++) vs->save_vr[i][j] = QNaNbarbarian[j]; /* Initial value */
400 }
401
402 if(genuser) for(j=0; j < 4; j++) vs->save_vscr[j] = genuser->save_vscr[j]; /* Set value for vscr */
403 else {
404 vs->save_vscr[0] = 0; /* Set an initial value if no general user yet */
405 vs->save_vscr[1] = 0;
406 vs->save_vscr[2] = 0;
407 vs->save_vscr[3] = 0x00010000; /* Always start with Java mode off */
408 }
409 vs->save_vrvalid = 0; /* Clear the valid flags */
410 }
411
412 for (i=0; i < 4; i++) vs->save_pad5[i] = 0; /* Clear cruft */
413 for (i=0; i < 7; i++) vs->save_pad6[i] = 0; /* Clear cruft */
414
415 *count = PPC_VECTOR_STATE_COUNT;
416 return KERN_SUCCESS;
417
418 default:
419 return KERN_INVALID_ARGUMENT;
420 }
421 }
422 /* Close cousin of machine_thread_get_state().
423 * This function is currently incomplete since we don't really need vector
424 * or FP for the core dump (the save area can be accessed directly if the
425 * user is so inclined). Also the function name is something of a misnomer,
426 * see the comment above find_kern_regs().
427 */
428
429 kern_return_t
430 machine_thread_get_kern_state(
431 thread_t thread,
432 thread_flavor_t flavor,
433 thread_state_t tstate,
434 mach_msg_type_number_t *count)
435 {
436
437 register struct savearea *sv; /* Pointer to the context savearea */
438 savearea *genkern;
439 int i;
440
441 register struct ppc_thread_state *ts;
442 register struct ppc_thread_state64 *xts;
443 register struct ppc_exception_state *es;
444 register struct ppc_exception_state64 *xes;
445
446 genkern = find_kern_regs(thread);
447
448 switch (flavor) {
449
450 case THREAD_STATE_FLAVOR_LIST:
451
452 if (*count < 6) {
453 return (KERN_INVALID_ARGUMENT);
454 }
455
456 tstate[0] = PPC_THREAD_STATE;
457 tstate[1] = PPC_FLOAT_STATE;
458 tstate[2] = PPC_EXCEPTION_STATE;
459 tstate[3] = PPC_VECTOR_STATE;
460 tstate[4] = PPC_THREAD_STATE64;
461 tstate[5] = PPC_EXCEPTION_STATE64;
462 *count = 6;
463
464 return KERN_SUCCESS;
465
466 case PPC_THREAD_STATE:
467
468 if (*count < PPC_THREAD_STATE_COUNT) { /* Is the count ok? */
469 return KERN_INVALID_ARGUMENT;
470 }
471
472 ts = (struct ppc_thread_state *) tstate;
473
474 sv = genkern; /* Copy this over */
475
476 if(sv) { /* Is there a save area yet? */
477 ts->r0 = (unsigned int)sv->save_r0;
478 ts->r1 = (unsigned int)sv->save_r1;
479 ts->r2 = (unsigned int)sv->save_r2;
480 ts->r3 = (unsigned int)sv->save_r3;
481 ts->r4 = (unsigned int)sv->save_r4;
482 ts->r5 = (unsigned int)sv->save_r5;
483 ts->r6 = (unsigned int)sv->save_r6;
484 ts->r7 = (unsigned int)sv->save_r7;
485 ts->r8 = (unsigned int)sv->save_r8;
486 ts->r9 = (unsigned int)sv->save_r9;
487 ts->r10 = (unsigned int)sv->save_r10;
488 ts->r11 = (unsigned int)sv->save_r11;
489 ts->r12 = (unsigned int)sv->save_r12;
490 ts->r13 = (unsigned int)sv->save_r13;
491 ts->r14 = (unsigned int)sv->save_r14;
492 ts->r15 = (unsigned int)sv->save_r15;
493 ts->r16 = (unsigned int)sv->save_r16;
494 ts->r17 = (unsigned int)sv->save_r17;
495 ts->r18 = (unsigned int)sv->save_r18;
496 ts->r19 = (unsigned int)sv->save_r19;
497 ts->r20 = (unsigned int)sv->save_r20;
498 ts->r21 = (unsigned int)sv->save_r21;
499 ts->r22 = (unsigned int)sv->save_r22;
500 ts->r23 = (unsigned int)sv->save_r23;
501 ts->r24 = (unsigned int)sv->save_r24;
502 ts->r25 = (unsigned int)sv->save_r25;
503 ts->r26 = (unsigned int)sv->save_r26;
504 ts->r27 = (unsigned int)sv->save_r27;
505 ts->r28 = (unsigned int)sv->save_r28;
506 ts->r29 = (unsigned int)sv->save_r29;
507 ts->r30 = (unsigned int)sv->save_r30;
508 ts->r31 = (unsigned int)sv->save_r31;
509 ts->cr = (unsigned int)sv->save_cr;
510 ts->xer = (unsigned int)sv->save_xer;
511 ts->lr = (unsigned int)sv->save_lr;
512 ts->ctr = (unsigned int)sv->save_ctr;
513 ts->srr0 = (unsigned int)sv->save_srr0;
514 ts->srr1 = (unsigned int)sv->save_srr1;
515 ts->mq = 0; /* MQ register (601 only) */
516 ts->vrsave = (unsigned int)sv->save_vrsave; /* VRSAVE register (Altivec only) */
517 }
518 else { /* No state yet. Save seemingly random values. */
519
520 for(i=0; i < 32; i+=2) { /* Fill up with defaults */
521 ((unsigned int *)&ts->r0)[i] = ((unsigned int *)&FloatInit)[0];
522 ((unsigned int *)&ts->r0)[i+1] = ((unsigned int *)&FloatInit)[1];
523 }
524 ts->cr = 0;
525 ts->xer = 0;
526 ts->lr = ((unsigned int *)&FloatInit)[0];
527 ts->ctr = ((unsigned int *)&FloatInit)[1];
528 ts->srr0 = ((unsigned int *)&FloatInit)[0];
529 ts->srr1 = MSR_EXPORT_MASK_SET;
530 ts->mq = 0;
531 ts->vrsave = 0; /* VRSAVE register (Altivec only) */
532 }
533
534 *count = PPC_THREAD_STATE_COUNT; /* Pass back the amount we actually copied */
535 return KERN_SUCCESS;
536
537
538 case PPC_THREAD_STATE64:
539
540 if (*count < PPC_THREAD_STATE64_COUNT) { /* Is the count ok? */
541 return KERN_INVALID_ARGUMENT;
542 }
543
544 xts = (struct ppc_thread_state64 *) tstate;
545
546 sv = genkern; /* Copy this over */
547
548 if(sv) { /* Is there a save area yet? */
549 xts->r0 = sv->save_r0;
550 xts->r1 = sv->save_r1;
551 xts->r2 = sv->save_r2;
552 xts->r3 = sv->save_r3;
553 xts->r4 = sv->save_r4;
554 xts->r5 = sv->save_r5;
555 xts->r6 = sv->save_r6;
556 xts->r7 = sv->save_r7;
557 xts->r8 = sv->save_r8;
558 xts->r9 = sv->save_r9;
559 xts->r10 = sv->save_r10;
560 xts->r11 = sv->save_r11;
561 xts->r12 = sv->save_r12;
562 xts->r13 = sv->save_r13;
563 xts->r14 = sv->save_r14;
564 xts->r15 = sv->save_r15;
565 xts->r16 = sv->save_r16;
566 xts->r17 = sv->save_r17;
567 xts->r18 = sv->save_r18;
568 xts->r19 = sv->save_r19;
569 xts->r20 = sv->save_r20;
570 xts->r21 = sv->save_r21;
571 xts->r22 = sv->save_r22;
572 xts->r23 = sv->save_r23;
573 xts->r24 = sv->save_r24;
574 xts->r25 = sv->save_r25;
575 xts->r26 = sv->save_r26;
576 xts->r27 = sv->save_r27;
577 xts->r28 = sv->save_r28;
578 xts->r29 = sv->save_r29;
579 xts->r30 = sv->save_r30;
580 xts->r31 = sv->save_r31;
581 xts->cr = sv->save_cr;
582 xts->xer = sv->save_xer;
583 xts->lr = sv->save_lr;
584 xts->ctr = sv->save_ctr;
585 xts->srr0 = sv->save_srr0;
586 xts->srr1 = sv->save_srr1;
587 xts->vrsave = sv->save_vrsave; /* VRSAVE register (Altivec only) */
588 }
589 else { /* No user state yet. Save seemingly random values. */
590
591 for(i=0; i < 32; i++) { /* Fill up with defaults */
592 ((unsigned long long *)&xts->r0)[i] = ((unsigned long long *)&FloatInit)[0];
593 }
594 xts->cr = 0;
595 xts->xer = 0;
596 xts->lr = ((unsigned long long *)&FloatInit)[0];
597 xts->ctr = ((unsigned long long *)&FloatInit)[0];
598 xts->srr0 = ((unsigned long long *)&FloatInit)[0];
599 xts->srr1 = MSR_EXPORT_MASK_SET;
600 xts->vrsave = 0; /* VRSAVE register (Altivec only) */
601 }
602
603 *count = PPC_THREAD_STATE64_COUNT; /* Pass back the amount we actually copied */
604 return KERN_SUCCESS;
605
606 case PPC_EXCEPTION_STATE:
607
608 if (*count < PPC_EXCEPTION_STATE_COUNT) {
609 return KERN_INVALID_ARGUMENT;
610 }
611
612 es = (struct ppc_exception_state *) tstate;
613 sv = genkern; /* Copy this over */
614
615 if(sv) { /* See if valid state yet */
616 es->dar = (unsigned int)sv->save_dar;
617 es->dsisr = sv->save_dsisr;
618 es->exception = sv->save_exception;
619 }
620 else { /* Nope, not yet */
621 es->dar = 0;
622 es->dsisr = 0;
623 es->exception = ((unsigned int *)&FloatInit)[0];
624 }
625
626 *count = PPC_EXCEPTION_STATE_COUNT;
627 return KERN_SUCCESS;
628
629 case PPC_EXCEPTION_STATE64:
630
631 if (*count < PPC_EXCEPTION_STATE64_COUNT) {
632 return KERN_INVALID_ARGUMENT;
633 }
634
635 xes = (struct ppc_exception_state64 *) tstate;
636 sv = genkern; /* Copy this over */
637
638 if(sv) { /* See if valid state yet */
639 xes->dar = sv->save_dar;
640 xes->dsisr = sv->save_dsisr;
641 xes->exception = sv->save_exception;
642 }
643 else { /* Nope, not yet */
644 xes->dar = 0;
645 xes->dsisr = 0;
646 xes->exception = ((unsigned int *)&FloatInit)[0];
647 }
648
649 *count = PPC_EXCEPTION_STATE64_COUNT;
650 return KERN_SUCCESS;
651
652 default:
653 return KERN_INVALID_ARGUMENT;
654 }
655 }
656
657
658 /*
659 * thread_setstatus:
660 *
661 * Set the status of the specified thread.
662 */
663 kern_return_t
664 machine_thread_set_state(
665 thread_t thread,
666 thread_flavor_t flavor,
667 thread_state_t tstate,
668 mach_msg_type_number_t count)
669 {
670
671 savearea *genuser;
672 savearea_fpu *fsv, *fsvn, *fsvo;
673 savearea_vec *vsv, *vsvn, *vsvo;
674 unsigned int i;
675 unsigned int clgn;
676 register struct ppc_thread_state *ts;
677 register struct ppc_thread_state64 *xts;
678 register struct ppc_exception_state *es;
679 register struct ppc_exception_state *xes;
680 register struct ppc_float_state *fs;
681 register struct ppc_vector_state *vs;
682
683 // dbgTrace((unsigned int)thr_act, (unsigned int)0 /*sv: was never set*/, flavor); /* (TEST/DEBUG) */
684
685 clgn = count; /* Get the count */
686
687 switch (flavor) { /* Validate the count before we do anything else */
688 case PPC_THREAD_STATE:
689
690 if (clgn < PPC_THREAD_STATE_COUNT) { /* Is it too short? */
691 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
692 }
693 break;
694
695 case PPC_THREAD_STATE64:
696
697 if (clgn < PPC_THREAD_STATE64_COUNT) { /* Is it too short? */
698 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
699 }
700 break;
701
702 case PPC_EXCEPTION_STATE:
703
704 if (clgn < PPC_EXCEPTION_STATE_COUNT) { /* Is it too short? */
705 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
706 }
707
708 case PPC_EXCEPTION_STATE64:
709
710 if (clgn < PPC_EXCEPTION_STATE64_COUNT) { /* Is it too short? */
711 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
712 }
713
714 break;
715
716 case PPC_FLOAT_STATE:
717
718 if (clgn < PPC_FLOAT_STATE_COUNT) { /* Is it too short? */
719 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
720 }
721
722 break;
723
724
725 case PPC_VECTOR_STATE:
726
727 if (clgn < PPC_VECTOR_STATE_COUNT) { /* Is it too short? */
728 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
729 }
730
731 break;
732
733 default:
734 return KERN_INVALID_ARGUMENT;
735 }
736
737 genuser = get_user_regs(thread); /* Find or allocate and initialize one */
738
739 switch (flavor) {
740
741 case PPC_THREAD_STATE:
742
743 ts = (struct ppc_thread_state *)tstate;
744
745 genuser->save_r0 = (uint64_t)ts->r0;
746 genuser->save_r1 = (uint64_t)ts->r1;
747 genuser->save_r2 = (uint64_t)ts->r2;
748 genuser->save_r3 = (uint64_t)ts->r3;
749 genuser->save_r4 = (uint64_t)ts->r4;
750 genuser->save_r5 = (uint64_t)ts->r5;
751 genuser->save_r6 = (uint64_t)ts->r6;
752 genuser->save_r7 = (uint64_t)ts->r7;
753 genuser->save_r8 = (uint64_t)ts->r8;
754 genuser->save_r9 = (uint64_t)ts->r9;
755 genuser->save_r10 = (uint64_t)ts->r10;
756 genuser->save_r11 = (uint64_t)ts->r11;
757 genuser->save_r12 = (uint64_t)ts->r12;
758 genuser->save_r13 = (uint64_t)ts->r13;
759 genuser->save_r14 = (uint64_t)ts->r14;
760 genuser->save_r15 = (uint64_t)ts->r15;
761 genuser->save_r16 = (uint64_t)ts->r16;
762 genuser->save_r17 = (uint64_t)ts->r17;
763 genuser->save_r18 = (uint64_t)ts->r18;
764 genuser->save_r19 = (uint64_t)ts->r19;
765 genuser->save_r20 = (uint64_t)ts->r20;
766 genuser->save_r21 = (uint64_t)ts->r21;
767 genuser->save_r22 = (uint64_t)ts->r22;
768 genuser->save_r23 = (uint64_t)ts->r23;
769 genuser->save_r24 = (uint64_t)ts->r24;
770 genuser->save_r25 = (uint64_t)ts->r25;
771 genuser->save_r26 = (uint64_t)ts->r26;
772 genuser->save_r27 = (uint64_t)ts->r27;
773 genuser->save_r28 = (uint64_t)ts->r28;
774 genuser->save_r29 = (uint64_t)ts->r29;
775 genuser->save_r30 = (uint64_t)ts->r30;
776 genuser->save_r31 = (uint64_t)ts->r31;
777
778 genuser->save_cr = ts->cr;
779 genuser->save_xer = (uint64_t)ts->xer;
780 genuser->save_lr = (uint64_t)ts->lr;
781 genuser->save_ctr = (uint64_t)ts->ctr;
782 genuser->save_srr0 = (uint64_t)ts->srr0;
783 genuser->save_vrsave = ts->vrsave; /* VRSAVE register (Altivec only) */
784
785 genuser->save_srr1 = MSR_PREPARE_FOR_IMPORT(genuser->save_srr1, ts->srr1); /* Set the bits we can change */
786
787 genuser->save_srr1 |= MSR_EXPORT_MASK_SET;
788
789 genuser->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make sure we don't enable the floating point unit */
790
791 if(task_has_64BitAddr(thread->task))
792 genuser->save_srr1 |= (uint64_t)MASK32(MSR_SF) << 32; /* If 64-bit task, force 64-bit mode */
793 else
794 genuser->save_srr1 &= ~((uint64_t)MASK32(MSR_SF) << 32); /* else 32-bit mode */
795
796 return KERN_SUCCESS;
797
798
799 case PPC_THREAD_STATE64:
800
801 xts = (struct ppc_thread_state64 *)tstate;
802
803 genuser->save_r0 = xts->r0;
804 genuser->save_r1 = xts->r1;
805 genuser->save_r2 = xts->r2;
806 genuser->save_r3 = xts->r3;
807 genuser->save_r4 = xts->r4;
808 genuser->save_r5 = xts->r5;
809 genuser->save_r6 = xts->r6;
810 genuser->save_r7 = xts->r7;
811 genuser->save_r8 = xts->r8;
812 genuser->save_r9 = xts->r9;
813 genuser->save_r10 = xts->r10;
814 genuser->save_r11 = xts->r11;
815 genuser->save_r12 = xts->r12;
816 genuser->save_r13 = xts->r13;
817 genuser->save_r14 = xts->r14;
818 genuser->save_r15 = xts->r15;
819 genuser->save_r16 = xts->r16;
820 genuser->save_r17 = xts->r17;
821 genuser->save_r18 = xts->r18;
822 genuser->save_r19 = xts->r19;
823 genuser->save_r20 = xts->r20;
824 genuser->save_r21 = xts->r21;
825 genuser->save_r22 = xts->r22;
826 genuser->save_r23 = xts->r23;
827 genuser->save_r24 = xts->r24;
828 genuser->save_r25 = xts->r25;
829 genuser->save_r26 = xts->r26;
830 genuser->save_r27 = xts->r27;
831 genuser->save_r28 = xts->r28;
832 genuser->save_r29 = xts->r29;
833 genuser->save_r30 = xts->r30;
834 genuser->save_r31 = xts->r31;
835
836 genuser->save_cr = xts->cr;
837 genuser->save_xer = xts->xer;
838 genuser->save_lr = xts->lr;
839 genuser->save_ctr = xts->ctr;
840 genuser->save_srr0 = xts->srr0;
841 genuser->save_vrsave = xts->vrsave; /* VRSAVE register (Altivec only) */
842
843 genuser->save_srr1 = MSR_PREPARE_FOR_IMPORT(genuser->save_srr1, xts->srr1); /* Set the bits we can change */
844
845 genuser->save_srr1 |= MSR_EXPORT_MASK_SET;
846
847 genuser->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make sure we don't enable the floating point unit */
848
849 if(task_has_64BitAddr(thread->task))
850 genuser->save_srr1 |= (uint64_t)MASK32(MSR_SF) << 32; /* If 64-bit task, force 64-bit mode */
851 else
852 genuser->save_srr1 &= ~((uint64_t)MASK32(MSR_SF) << 32); /* else 32-bit mode */
853
854 return KERN_SUCCESS;
855
856
857 case PPC_EXCEPTION_STATE:
858
859 es = (struct ppc_exception_state *) tstate;
860
861 genuser->save_dar = (uint64_t)es->dar;
862 genuser->save_dsisr = es->dsisr;
863 genuser->save_exception = es->exception;
864
865 return KERN_SUCCESS;
866
867 /*
868 * It's pretty worthless to try to change this stuff, but we'll do it anyway.
869 */
870
871 case PPC_EXCEPTION_STATE64:
872
873 xes = (struct ppc_exception_state *) tstate;
874
875 genuser->save_dar = xes->dar;
876 genuser->save_dsisr = xes->dsisr;
877 genuser->save_exception = xes->exception;
878
879 return KERN_SUCCESS;
880
881 case PPC_FLOAT_STATE:
882
883 toss_live_fpu(thread->machine.curctx); /* Toss my floating point if live anywhere */
884
885 fsv = find_user_fpu(thread); /* Get the user's floating point context */
886
887 if(!fsv) { /* Do we have one yet? */
888 fsv = (savearea_fpu *)save_alloc(); /* If we still don't have one, get a new one */
889 fsv->save_hdr.save_flags = (fsv->save_hdr.save_flags & ~SAVtype) | (SAVfloat << SAVtypeshft); /* Mark as in use as float */
890 fsv->save_hdr.save_act = thread;
891 fsv->save_hdr.save_prev = 0; /* Mark no more */
892 fsv->save_hdr.save_level = 0; /* Mark user state */
893
894 if(!thread->machine.curctx->FPUsave) thread->machine.curctx->FPUsave = fsv; /* If no floating point, chain us first */
895 else {
896
897 fsvn = fsvo = thread->machine.curctx->FPUsave; /* Remember first one */
898
899 while (fsvn) { /* Go until we hit the end */
900 fsvo = fsvn; /* Remember the previous one */
901 fsvn = CAST_DOWN(savearea_fpu *, fsvo->save_hdr.save_prev); /* Skip on to the next */
902 }
903
904 fsvo->save_hdr.save_prev = (addr64_t)((uintptr_t)fsv); /* Queue us on in */
905 }
906
907 }
908
909 fs = (struct ppc_float_state *) tstate; /* Point to source */
910
911
912 bcopy((char *)fs, (char *)&fsv->save_fp0, 32*8); /* Move in the 32 registers */
913
914 genuser->save_fpscr = fs->fpscr; /* Copy the fpscr value to normal */
915
916 return KERN_SUCCESS;
917
918
919 case PPC_VECTOR_STATE:
920
921 toss_live_vec(thread->machine.curctx); /* Toss my vector if live anywhere */
922
923 vsv = find_user_vec(thread); /* Get the user's vector context */
924
925 if(!vsv) { /* Do we have one yet? */
926 vsv = (savearea_vec *)save_alloc(); /* If we still don't have one, get a new one */
927 vsv->save_hdr.save_flags = (vsv->save_hdr.save_flags & ~SAVtype) | (SAVvector << SAVtypeshft); /* Mark as in use as vector */
928 vsv->save_hdr.save_act = thread;
929 vsv->save_hdr.save_prev = 0; /* Mark no more */
930 vsv->save_hdr.save_level = 0; /* Mark user state */
931
932 if(!thread->machine.curctx->VMXsave) thread->machine.curctx->VMXsave = vsv; /* If no vector, chain us first */
933 else {
934
935 vsvn = vsvo = thread->machine.curctx->VMXsave; /* Remember first one */
936
937 while (vsvn) { /* Go until we hit the end */
938 vsvo = vsvn; /* Remember the previous one */
939 vsvn = CAST_DOWN(savearea_vec *, vsvo->save_hdr.save_prev); /* Skip on to the next */
940 }
941
942 vsvo->save_hdr.save_prev = (addr64_t)((uintptr_t)vsv); /* Queue us on in */
943 }
944
945 }
946
947 vs = (struct ppc_vector_state *) tstate; /* Point to source */
948
949 bcopy((char *)vs, (char *)&vsv->save_vr0, 32*16); /* 32 registers plus status and validity and pad */
950 vsv->save_vrvalid = vs->save_vrvalid; /* Set validity bits */
951
952
953 for(i = 0; i < 4; i++) genuser->save_vscr[i] = vs->save_vscr[i]; /* Set value for vscr */
954
955 return KERN_SUCCESS;
956
957
958 default:
959 return KERN_INVALID_ARGUMENT;
960 }
961 }
962
963
964 /*
965 * This is where registers that are not normally specified by the mach-o
966 * file on an execve should be nullified, perhaps to avoid a covert channel.
967 * We've never bothered to clear FPRs or VRs, but it is important to clear
968 * the FPSCR, which is kept in the general state but not set by the general
969 * flavor (ie, PPC_THREAD_STATE or PPC_THREAD_STATE64.)
970 */
971 kern_return_t
972 machine_thread_state_initialize(
973 thread_t thread)
974 {
975 savearea *sv;
976
977 sv = get_user_regs(thread); /* Find or allocate and initialize one */
978
979 sv->save_fpscr = 0; /* Clear all floating point exceptions */
980 sv->save_vrsave = 0; /* Set the vector save state */
981 sv->save_vscr[0] = 0x00000000;
982 sv->save_vscr[1] = 0x00000000;
983 sv->save_vscr[2] = 0x00000000;
984 sv->save_vscr[3] = 0x00010000; /* Disable java mode and clear saturated */
985
986 return KERN_SUCCESS;
987 }
988
989
990 /*
991 * Duplicates the context of one thread into a new one.
992 * The new thread is assumed to be new and have no user state contexts except maybe a general one.
993 * We also assume that the old thread can't be running anywhere.
994 *
995 * We're only going to be duplicating user context here. That means that we will have to
996 * eliminate any floating point or vector kernel contexts and carry across the user state ones.
997 */
998
999 kern_return_t
1000 machine_thread_dup(
1001 thread_t self,
1002 thread_t target)
1003 {
1004 savearea *sv, *osv;
1005 savearea_fpu *fsv, *fsvn;
1006 savearea_vec *vsv, *vsvn;
1007
1008 fpu_save(self->machine.curctx); /* Make certain floating point state is all saved */
1009 vec_save(self->machine.curctx); /* Make certain the vector state is all saved */
1010
1011 sv = get_user_regs(target); /* Allocate and initialze context in the new activation */
1012
1013 osv = find_user_regs(self); /* Find the original context */
1014 if(!osv)
1015 return (KERN_FAILURE);
1016
1017 bcopy((char *)((unsigned int)osv + sizeof(savearea_comm)), /* Copy everything but the headers */
1018 (char *)((unsigned int)sv + sizeof(savearea_comm)),
1019 sizeof(struct savearea) - sizeof(savearea_comm));
1020
1021 sv->save_srr1 &= (uint64_t)(~(MASK(MSR_FP) | MASK(MSR_VEC))); /* Make certain that floating point and vector are turned off */
1022
1023 fsv = find_user_fpu(self); /* Get any user floating point */
1024
1025 target->machine.curctx->FPUsave = 0; /* Assume no floating point */
1026
1027 if(fsv) { /* Did we find one? */
1028 fsvn = (savearea_fpu *)save_alloc(); /* If we still don't have one, get a new one */
1029 fsvn->save_hdr.save_flags = (fsvn->save_hdr.save_flags & ~SAVtype) | (SAVfloat << SAVtypeshft); /* Mark as in use as float */
1030 fsvn->save_hdr.save_act = target;
1031 fsvn->save_hdr.save_prev = 0; /* Mark no more */
1032 fsvn->save_hdr.save_level = 0; /* Mark user state */
1033
1034 target->machine.curctx->FPUsave = fsvn; /* Chain in the floating point */
1035
1036 bcopy((char *)((unsigned int)fsv + sizeof(savearea_comm)), /* Copy everything but the headers */
1037 (char *)((unsigned int)fsvn + sizeof(savearea_comm)),
1038 sizeof(struct savearea) - sizeof(savearea_comm));
1039 }
1040
1041 vsv = find_user_vec(self); /* Get any user vector */
1042
1043 target->machine.curctx->VMXsave = 0; /* Assume no vector */
1044
1045 if(vsv) { /* Did we find one? */
1046 vsvn = (savearea_vec *)save_alloc(); /* If we still don't have one, get a new one */
1047 vsvn->save_hdr.save_flags = (vsvn->save_hdr.save_flags & ~SAVtype) | (SAVvector << SAVtypeshft); /* Mark as in use as float */
1048 vsvn->save_hdr.save_act = target;
1049 vsvn->save_hdr.save_prev = 0; /* Mark no more */
1050 vsvn->save_hdr.save_level = 0; /* Mark user state */
1051
1052 target->machine.curctx->VMXsave = vsvn; /* Chain in the floating point */
1053
1054 bcopy((char *)((unsigned int)vsv + sizeof(savearea_comm)), /* Copy everything but the headers */
1055 (char *)((unsigned int)vsvn + sizeof(savearea_comm)),
1056 sizeof(struct savearea) - sizeof(savearea_comm));
1057 }
1058
1059 return (KERN_SUCCESS);
1060 }
1061
1062 /*
1063 * Initializes a fresh set of user state values. If there is no user state context,
1064 * one is created. Floats and VMX are not created.
1065 *
1066 * We only set initial values if there was no context found.
1067 */
1068
1069 savearea *
1070 get_user_regs(
1071 thread_t thread)
1072 {
1073 savearea *sv, *osv;
1074 unsigned int i;
1075
1076 if (thread->machine.upcb)
1077 return thread->machine.upcb;
1078
1079 sv = thread->machine.pcb; /* Get the top savearea on the stack */
1080 osv = 0; /* Set no user savearea yet */
1081
1082 while(sv) { /* Find the user context */
1083 osv = sv; /* Save the last one */
1084 sv = CAST_DOWN(savearea *, sv->save_hdr.save_prev); /* Get the previous context */
1085 }
1086
1087 sv = save_alloc(); /* Get one */
1088 sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use as general */
1089 sv->save_hdr.save_act = thread;
1090 sv->save_hdr.save_prev = 0; /* Mark no more */
1091 sv->save_hdr.save_level = 0; /* Mark user state */
1092
1093 if(osv) { /* Did we already have one? */
1094 osv->save_hdr.save_prev = (addr64_t)((uintptr_t)sv); /* Chain us on the end */
1095 }
1096 else { /* We are the first */
1097 thread->machine.pcb = sv; /* Put it there */
1098 }
1099 thread->machine.upcb = sv; /* Set user pcb */
1100
1101 for(i=0; i < 32; i+=2) { /* Fill up with defaults */
1102 ((unsigned int *)&sv->save_r0)[i] = ((unsigned int *)&FloatInit)[0];
1103 ((unsigned int *)&sv->save_r0)[i+1] = ((unsigned int *)&FloatInit)[1];
1104 }
1105 sv->save_cr = 0;
1106 sv->save_xer = 0;
1107 sv->save_lr = (uint64_t)FloatInit;
1108 sv->save_ctr = (uint64_t)FloatInit;
1109 sv->save_srr0 = (uint64_t)FloatInit;
1110 sv->save_srr1 = (uint64_t)MSR_EXPORT_MASK_SET;
1111 if(task_has_64BitAddr(thread->task))
1112 sv->save_srr1 |= (uint64_t)MASK32(MSR_SF) << 32; /* If 64-bit task, force 64-bit mode */
1113
1114 sv->save_fpscr = 0; /* Clear all floating point exceptions */
1115
1116 sv->save_vrsave = 0; /* Set the vector save state */
1117 sv->save_vscr[0] = 0x00000000;
1118 sv->save_vscr[1] = 0x00000000;
1119 sv->save_vscr[2] = 0x00000000;
1120 sv->save_vscr[3] = 0x00010000; /* Disable java mode and clear saturated */
1121
1122 return sv; /* Bye bye... */
1123 }
1124
1125 /*
1126 * Find the user state context. If there is no user state context,
1127 * we just return a 0.
1128 */
1129
1130 savearea *
1131 find_user_regs(
1132 thread_t thread)
1133 {
1134 return thread->machine.upcb;
1135 }
1136
1137 /* The name of this call is something of a misnomer since the mact.pcb can
1138 * contain chained saveareas, but it will do for now..
1139 */
1140 savearea *
1141 find_kern_regs(
1142 thread_t thread)
1143 {
1144 return thread->machine.pcb;
1145 }
1146
1147 /*
1148 * Find the user state floating point context. If there is no user state context,
1149 * we just return a 0.
1150 */
1151
1152 savearea_fpu *
1153 find_user_fpu(
1154 thread_t thread)
1155 {
1156 savearea_fpu *fsv;
1157 boolean_t intr;
1158
1159 intr = ml_set_interrupts_enabled(FALSE);
1160 fsv = thread->machine.curctx->FPUsave; /* Get the start of the floating point chain */
1161
1162 while(fsv) { /* Look until the end or we find it */
1163 if(!(fsv->save_hdr.save_level)) break; /* Is the the user state stuff? (the level is 0 if so) */
1164 fsv = CAST_DOWN(savearea_fpu *, fsv->save_hdr.save_prev); /* Try the previous one */
1165 }
1166 (void) ml_set_interrupts_enabled(intr);
1167
1168 return fsv; /* Bye bye... */
1169 }
1170
1171 /*
1172 * Find the user state vector context. If there is no user state context,
1173 * we just return a 0.
1174 */
1175
1176 savearea_vec *
1177 find_user_vec(
1178 thread_t thread)
1179 {
1180 savearea_vec *vsv;
1181 boolean_t intr;
1182
1183 intr = ml_set_interrupts_enabled(FALSE);
1184 vsv = thread->machine.curctx->VMXsave; /* Get the start of the vector chain */
1185
1186 while(vsv) { /* Look until the end or we find it */
1187 if(!(vsv->save_hdr.save_level)) break; /* Is the the user state stuff? (the level is 0 if so) */
1188 vsv = CAST_DOWN(savearea_vec *, vsv->save_hdr.save_prev); /* Try the previous one */
1189 }
1190 (void) ml_set_interrupts_enabled(intr);
1191
1192 return vsv; /* Bye bye... */
1193 }
1194 /*
1195 * Find the user state vector context for the current thread. If there is no user state context,
1196 * we just return a 0.
1197 */
1198
1199 savearea_vec *find_user_vec_curr(void) {
1200
1201 savearea_vec *vsv;
1202 thread_t thread = current_thread();
1203 boolean_t intr;
1204
1205 vec_save(thread->machine.curctx); /* Force save if live */
1206
1207 intr = ml_set_interrupts_enabled(FALSE);
1208 vsv = thread->machine.curctx->VMXsave; /* Get the start of the vector chain */
1209
1210 while(vsv) { /* Look until the end or we find it */
1211 if(!(vsv->save_hdr.save_level)) break; /* Is the the user state stuff? (the level is 0 if so) */
1212 vsv = CAST_DOWN(savearea_vec *, vsv->save_hdr.save_prev); /* Try the previous one */
1213 }
1214 (void) ml_set_interrupts_enabled(intr);
1215
1216 return vsv; /* Bye bye... */
1217 }
1218
1219 /*
1220 * thread_userstack:
1221 *
1222 * Return the user stack pointer from the machine
1223 * dependent thread state info.
1224 */
1225 kern_return_t
1226 thread_userstack(
1227 __unused thread_t thread,
1228 int flavor,
1229 thread_state_t tstate,
1230 unsigned int count,
1231 mach_vm_offset_t *user_stack,
1232 int *customstack
1233 )
1234 {
1235 /*
1236 * Set a default.
1237 */
1238
1239 switch (flavor) {
1240 case PPC_THREAD_STATE:
1241 {
1242 struct ppc_thread_state *state;
1243
1244 if (count < PPC_THREAD_STATE_COUNT)
1245 return (KERN_INVALID_ARGUMENT);
1246
1247 state = (struct ppc_thread_state *) tstate;
1248
1249 /*
1250 * If a valid user stack is specified, use it.
1251 */
1252 if (state->r1) {
1253 *user_stack = CAST_USER_ADDR_T(state->r1);
1254 if (customstack)
1255 *customstack = 1;
1256 } else {
1257 *user_stack = CAST_USER_ADDR_T(USRSTACK);
1258 if (customstack)
1259 *customstack = 0;
1260 }
1261 }
1262 break;
1263
1264 case PPC_THREAD_STATE64:
1265 {
1266 struct ppc_thread_state64 *state64;
1267
1268 if (count < PPC_THREAD_STATE64_COUNT)
1269 return (KERN_INVALID_ARGUMENT);
1270
1271 state64 = (struct ppc_thread_state64 *)tstate;
1272
1273 /*
1274 * If a valid user stack is specified, use it.
1275 */
1276 if (state64->r1 != MACH_VM_MIN_ADDRESS) {
1277 *user_stack = state64->r1;
1278 if (customstack)
1279 *customstack = 1;
1280 } else {
1281 *user_stack = USRSTACK64;
1282 if (customstack)
1283 *customstack = 0;
1284 }
1285 }
1286 break;
1287
1288 default :
1289 return (KERN_INVALID_ARGUMENT);
1290 }
1291
1292 return (KERN_SUCCESS);
1293 }
1294
1295
1296 /*
1297 * thread_setuserstack:
1298 *
1299 * Sets the user stack pointer into the machine
1300 * dependent thread state info.
1301 */
1302 void
1303 thread_setuserstack(thread_t thread, mach_vm_address_t user_stack)
1304 {
1305 savearea *sv;
1306
1307 sv = get_user_regs(thread); /* Get the user state registers */
1308
1309 sv->save_r1 = user_stack;
1310
1311 return;
1312 }
1313
1314 /*
1315 * thread_adjuserstack:
1316 *
1317 * Returns the adjusted user stack pointer from the machine
1318 * dependent thread state info. Usef for small (<2G) deltas.
1319 */
1320 uint64_t
1321 thread_adjuserstack(thread_t thread, int adjust)
1322 {
1323 savearea *sv;
1324
1325 sv = get_user_regs(thread); /* Get the user state registers */
1326
1327 sv->save_r1 += adjust; /* Adjust the stack */
1328
1329 return sv->save_r1; /* Return the adjusted stack */
1330
1331 }
1332
1333 void
1334 thread_setsinglestep(thread_t thread, int on)
1335 {
1336 savearea *sv;
1337
1338 sv = get_user_regs(thread); /* Get the user state registers */
1339
1340 if (on)
1341 sv->save_srr1 |= MASK(MSR_SE);
1342 else
1343 sv->save_srr1 &= ~MASK(MSR_SE);
1344 }
1345
1346 /*
1347 * thread_setentrypoint:
1348 *
1349 * Sets the user PC into the machine
1350 * dependent thread state info.
1351 */
1352
1353 void
1354 thread_setentrypoint(thread_t thread, uint64_t entry)
1355 {
1356 savearea *sv;
1357
1358 sv = get_user_regs(thread); /* Get the user state registers */
1359
1360 sv->save_srr0 = entry;
1361
1362 return;
1363 }
1364
1365 kern_return_t
1366 thread_entrypoint(
1367 __unused thread_t thread,
1368 int flavor,
1369 thread_state_t tstate,
1370 unsigned int count,
1371 mach_vm_offset_t *entry_point
1372 )
1373 {
1374 #if 0
1375 /* Silly code: "if *entry_point is 0, make it 0" */
1376 /*
1377 * Set a default.
1378 */
1379 if (*entry_point == 0ULL)
1380 *entry_point = MACH_VM_MIN_ADDRESS;
1381 #endif
1382
1383 switch (flavor) {
1384 case PPC_THREAD_STATE:
1385 {
1386 struct ppc_thread_state *state;
1387
1388 if (count < PPC_THREAD_STATE_COUNT)
1389 return (KERN_INVALID_ARGUMENT);
1390
1391 state = (struct ppc_thread_state *) tstate;
1392
1393 /*
1394 * If a valid entry point is specified, use it.
1395 */
1396 if (state->srr0) {
1397 *entry_point = CAST_USER_ADDR_T(state->srr0);
1398 } else {
1399 *entry_point = CAST_USER_ADDR_T(VM_MIN_ADDRESS);
1400 }
1401 }
1402 break;
1403
1404 case PPC_THREAD_STATE64:
1405 {
1406 struct ppc_thread_state64 *state64;
1407
1408 if (count < PPC_THREAD_STATE_COUNT)
1409 return (KERN_INVALID_ARGUMENT);
1410
1411 state64 = (struct ppc_thread_state64 *)tstate;
1412
1413 /*
1414 * If a valid entry point is specified, use it.
1415 */
1416 if (state64->srr0) {
1417 *entry_point = state64->srr0;
1418 } else {
1419 *entry_point = MACH_VM_MIN_ADDRESS;
1420 }
1421 }
1422 break;
1423
1424 default:
1425 return (KERN_INVALID_ARGUMENT);
1426 }
1427
1428 return (KERN_SUCCESS);
1429 }
1430
1431 unsigned int get_msr_exportmask(void)
1432 {
1433 return (MSR_EXPORT_MASK_SET);
1434 }
1435
1436 unsigned int get_msr_nbits(void)
1437 {
1438 return (MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1439 }
1440 unsigned int get_msr_rbits(void)
1441 {
1442 return (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1443 }
1444
1445 void ppc_checkthreadstate(void * tsptr, int flavor)
1446 {
1447 if (flavor == PPC_THREAD_STATE64) {
1448 struct ppc_thread_state64 *ts64 =(struct ppc_thread_state64 *)tsptr;
1449
1450 /* Make sure naughty bits are off and necessary bits are on */
1451 ts64->srr1 &= ~(MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1452 ts64->srr1 |= (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1453 } else {
1454 struct ppc_thread_state *ts =(struct ppc_thread_state *)tsptr;
1455
1456 /* Make sure naughty bits are off and necessary bits are on */
1457 ts->srr1 &= ~(MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1458 ts->srr1 |= (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1459 }
1460 return;
1461 }
1462
1463 void
1464 thread_set_child(
1465 thread_t child,
1466 int pid)
1467 {
1468 struct savearea *child_state;
1469
1470 child_state = get_user_regs(child);
1471
1472 child_state->save_r3 = (uint_t)pid;
1473 child_state->save_r4 = 1ULL;
1474 }
1475 void
1476 thread_set_parent(
1477 thread_t parent,
1478 int pid)
1479 {
1480 struct savearea *parent_state;
1481
1482 parent_state = get_user_regs(parent);
1483
1484 parent_state->save_r3 = (uint64_t)pid;
1485 parent_state->save_r4 = 0;
1486 }
1487
1488 /*
1489 * Saves the complete context (general, floating point, and vector) of the current activation.
1490 * We will collect everything into an opaque block of 1 to 3 saveareas and pass back a
1491 * pointer to that.
1492 *
1493 * The savearea is made to look like it belongs to the source activation. This needs to
1494 * be adjusted when these contexts are attached to a new activation.
1495 *
1496 */
1497
1498 void *act_thread_csave(void) {
1499
1500 savearea *sv, *osv;
1501 savearea_fpu *fsv, *ofsv;
1502 savearea_vec *vsv, *ovsv;
1503
1504 thread_t thread;
1505
1506 thread = current_thread();
1507
1508 fpu_save(thread->machine.curctx); /* Make certain floating point state is all saved */
1509 vec_save(thread->machine.curctx); /* Make certain the vector state is all saved */
1510
1511 osv = find_user_regs(thread); /* Get our savearea */
1512
1513 if(!osv) {
1514 panic("act_thread_csave: attempting to preserve the context of an activation with none (%08X)\n", thread);
1515 }
1516
1517 sv = save_alloc(); /* Get a fresh save area to save into */
1518 sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use as general */
1519 sv->save_hdr.save_act = thread;
1520 sv->save_hdr.save_prev = 0; /* Mark no more */
1521 sv->save_hdr.save_level = 0; /* Mark user state */
1522
1523
1524 bcopy((char *)((unsigned int)osv + sizeof(savearea_comm)), /* Copy everything but the headers */
1525 (char *)((unsigned int)sv + sizeof(savearea_comm)),
1526 sizeof(struct savearea) - sizeof(savearea_comm));
1527
1528 sv->save_srr1 &= (uint64_t)(~(MASK(MSR_FP) | MASK(MSR_VEC))); /* Make certain that floating point and vector are turned off */
1529
1530 sv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1531 sv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
1532
1533
1534 ofsv = find_user_fpu(thread); /* Get any user floating point */
1535
1536 sv->save_hdr.save_misc0 = 0; /* Assume no floating point */
1537
1538 if(ofsv) { /* Did we find one? */
1539 fsv = (savearea_fpu *)save_alloc(); /* If we still don't have one, get a new one */
1540 fsv->save_hdr.save_flags = (fsv->save_hdr.save_flags & ~SAVtype) | (SAVfloat << SAVtypeshft); /* Mark as in use as float */
1541 fsv->save_hdr.save_act = thread;
1542 fsv->save_hdr.save_prev = 0; /* Mark no more */
1543 fsv->save_hdr.save_level = 0; /* Mark user state */
1544 fsv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1545 fsv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
1546
1547 sv->save_hdr.save_misc0 = (uint64_t)((uintptr_t)fsv); /* Remember this one */
1548
1549 bcopy((char *)((unsigned int)ofsv + sizeof(savearea_comm)), /* Copy everything but the headers */
1550 (char *)((unsigned int)fsv + sizeof(savearea_comm)),
1551 sizeof(struct savearea) - sizeof(savearea_comm));
1552 }
1553
1554 ovsv = find_user_vec(thread); /* Get any user vector */
1555
1556 sv->save_hdr.save_misc1 = 0; /* Assume no vector */
1557
1558 if(ovsv) { /* Did we find one? */
1559 vsv = (savearea_vec *)save_alloc(); /* If we still don't have one, get a new one */
1560 vsv->save_hdr.save_flags = (vsv->save_hdr.save_flags & ~SAVtype) | (SAVvector << SAVtypeshft); /* Mark as in use as float */
1561 vsv->save_hdr.save_act = thread;
1562 vsv->save_hdr.save_prev = 0; /* Mark no more */
1563 vsv->save_hdr.save_level = 0; /* Mark user state */
1564 vsv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1565 vsv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
1566
1567 sv->save_hdr.save_misc1 = (uint64_t)((uintptr_t)vsv); /* Chain in the floating point */
1568
1569 bcopy((char *)((unsigned int)ovsv + sizeof(savearea_comm)), /* Copy everything but the headers */
1570 (char *)((unsigned int)vsv + sizeof(savearea_comm)),
1571 sizeof(struct savearea) - sizeof(savearea_comm));
1572 }
1573
1574 return (void *)sv; /* Bye bye... */
1575 }
1576
1577
1578
1579 /*
1580 * Attaches saved user state context to an activation. We will replace any
1581 * user state context with what is passed in. The saved context consists of a
1582 * savearea that was setup by
1583 * We will collect everything into one savearea and pass that back.
1584 *
1585 * The savearea is made to look like it belongs to the source activation. This needs to
1586 * be adjusted when these contexts are attached to a new activation.
1587 *
1588 */
1589
1590 void act_thread_catt(void *ctx) {
1591
1592 savearea *sv, *osv, *psv;
1593 savearea_fpu *fsv, *ofsv, *pfsv;
1594 savearea_vec *vsv, *ovsv, *pvsv;
1595 unsigned int spc;
1596 thread_t thread;
1597
1598 sv = (savearea *)ctx; /* Make this easier for C */
1599
1600 fsv = CAST_DOWN(savearea_fpu *, sv->save_hdr.save_misc0); /* Get a possible floating point savearea */
1601 vsv = CAST_DOWN(savearea_vec *, sv->save_hdr.save_misc1); /* Get a possible vector savearea */
1602
1603 if((sv->save_hdr.save_misc2 != 0xDEBB1ED0) || (sv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1604 panic("act_thread_catt: attempt to attach invalid general context savearea - %08X\n", sv); /* Die */
1605 }
1606
1607 if(fsv && ((fsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (fsv->save_hdr.save_misc3 != 0xE5DA11A5))) { /* See if valid savearea */
1608 panic("act_thread_catt: attempt to attach invalid float context savearea - %08X\n", fsv); /* Die */
1609 }
1610
1611 if(vsv && ((vsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (vsv->save_hdr.save_misc3 != 0xE5DA11A5))) { /* See if valid savearea */
1612 panic("act_thread_catt: attempt to attach invalid vector context savearea - %08X\n", vsv); /* Die */
1613 }
1614
1615 thread = current_thread();
1616
1617 act_machine_sv_free(thread); /* Blow away any current kernel FP or vector.
1618 We do not support those across a vfork */
1619 toss_live_fpu(thread->machine.curctx); /* Toss my floating point if live anywhere */
1620 toss_live_vec(thread->machine.curctx); /* Toss my vector if live anywhere */
1621
1622 sv->save_hdr.save_misc2 = 0; /* Eye catcher for debug */
1623 sv->save_hdr.save_misc3 = 0; /* Eye catcher for debug */
1624 sv->save_hdr.save_act = thread;
1625
1626 spc = (unsigned int)thread->map->pmap->space; /* Get the space we're in */
1627
1628 osv = thread->machine.pcb; /* Get the top general savearea */
1629 psv = 0;
1630 while(osv) { /* Any saved state? */
1631 if(osv->save_srr1 & MASK(MSR_PR)) break; /* Leave if this is user state */
1632 psv = osv; /* Save previous savearea address */
1633 osv = CAST_DOWN(savearea *, osv->save_hdr.save_prev); /* Get one underneath our's */
1634 }
1635
1636 if(osv) { /* Did we find one? */
1637 if(psv) psv->save_hdr.save_prev = 0; /* Yes, clear pointer to it (it should always be last) or */
1638 else thread->machine.pcb = 0; /* to the start if the only one */
1639
1640 save_release(osv); /* Nope, release it */
1641
1642 }
1643
1644 if(psv) psv->save_hdr.save_prev = (addr64_t)((uintptr_t)sv); /* Chain us to the end or */
1645 else thread->machine.pcb = (pcb_t)sv; /* to the start if the only one */
1646 thread->machine.upcb = (pcb_t)sv; /* Set the user pcb */
1647
1648 ovsv = thread->machine.curctx->VMXsave; /* Get the top vector savearea */
1649
1650 pvsv = 0;
1651 while(ovsv) { /* Any VMX saved state? */
1652 if(!(ovsv->save_hdr.save_level)) break; /* Leave if this is user state */
1653 pvsv = ovsv; /* Save previous savearea address */
1654 ovsv = CAST_DOWN(savearea_vec *, ovsv->save_hdr.save_prev); /* Get one underneath our's */
1655 }
1656
1657 if(ovsv) { /* Did we find one? */
1658 if(pvsv) pvsv->save_hdr.save_prev = 0; /* Yes, clear pointer to it (it should always be last) or */
1659 else thread->machine.curctx->VMXsave = 0; /* to the start if the only one */
1660
1661 save_release((savearea *)ovsv); /* Nope, release it */
1662 }
1663
1664 if(vsv) { /* Are we sticking any vector on this one? */
1665 if(pvsv) pvsv->save_hdr.save_prev = (addr64_t)((uintptr_t)vsv); /* Yes, chain us to the end or */
1666 else {
1667 thread->machine.curctx->VMXsave = vsv; /* to the start if the only one */
1668 thread->machine.curctx->VMXlevel = 0; /* Insure that we don't have a leftover level */
1669 }
1670
1671 vsv->save_hdr.save_misc2 = 0; /* Eye catcher for debug */
1672 vsv->save_hdr.save_misc3 = 0; /* Eye catcher for debug */
1673 vsv->save_hdr.save_act = thread;
1674 }
1675
1676 ofsv = thread->machine.curctx->FPUsave; /* Get the top float savearea */
1677
1678 pfsv = 0;
1679 while(ofsv) { /* Any float saved state? */
1680 if(!(ofsv->save_hdr.save_level)) break; /* Leave if this is user state */
1681 pfsv = ofsv; /* Save previous savearea address */
1682 ofsv = CAST_DOWN(savearea_fpu *, ofsv->save_hdr.save_prev); /* Get one underneath our's */
1683 }
1684
1685 if(ofsv) { /* Did we find one? */
1686 if(pfsv) pfsv->save_hdr.save_prev = 0; /* Yes, clear pointer to it (it should always be last) or */
1687 else thread->machine.curctx->FPUsave = 0; /* to the start if the only one */
1688
1689 save_release((savearea *)ofsv); /* Nope, release it */
1690 }
1691
1692 if(fsv) { /* Are we sticking any vector on this one? */
1693 if(pfsv) pfsv->save_hdr.save_prev = (addr64_t)((uintptr_t)fsv); /* Yes, chain us to the end or */
1694 else {
1695 thread->machine.curctx->FPUsave = fsv; /* to the start if the only one */
1696 thread->machine.curctx->FPUlevel = 0; /* Insure that we don't have a leftover level */
1697 }
1698
1699 fsv->save_hdr.save_misc2 = 0; /* Eye catcher for debug */
1700 fsv->save_hdr.save_misc3 = 0; /* Eye catcher for debug */
1701 fsv->save_hdr.save_act = thread;
1702 }
1703
1704 }
1705
1706
1707
1708 /*
1709 * Releases saved context. We need this because the saved context is opague.
1710 * be adjusted when these contexts are attached to a new activation.
1711 *
1712 */
1713
1714 void
1715 act_thread_cfree(void *ctx)
1716 {
1717
1718 savearea *sv;
1719 savearea_fpu *fsv;
1720 savearea_vec *vsv;
1721
1722 sv = (savearea *)ctx; /* Make this easier for C */
1723
1724 fsv = CAST_DOWN(savearea_fpu *, sv->save_hdr.save_misc0); /* Get a possible floating point savearea */
1725 vsv = CAST_DOWN(savearea_vec *, sv->save_hdr.save_misc1); /* Get a possible vector savearea */
1726
1727 if((sv->save_hdr.save_misc2 != 0xDEBB1ED0) || (sv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1728 panic("act_thread_cfree: attempt to detatch invalid general context savearea - %08X\n", sv); /* Die */
1729 }
1730
1731 save_release(sv); /* Toss the general savearea */
1732
1733 if(fsv) { /* See if there is any saved floating point */
1734 if((fsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (fsv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1735 panic("act_thread_cfree: attempt to detatch invalid float context savearea - %08X\n", fsv); /* Die */
1736 }
1737
1738 save_release((savearea *)fsv); /* Toss saved context */
1739 }
1740
1741 if(vsv) { /* See if there is any saved floating point */
1742 if((vsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (vsv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1743 panic("act_thread_cfree: attempt to detatch invalid vector context savearea - %08X\n", vsv); /* Die */
1744 }
1745
1746 save_release((savearea *)vsv); /* Toss saved context */
1747 }
1748
1749 return;
1750 }
1751
1752 /*
1753 * thread_enable_fpe:
1754 *
1755 * enables or disables floating point exceptions for the thread.
1756 * returns old state
1757 */
1758 int thread_enable_fpe(
1759 thread_t thread,
1760 int onoff)
1761 {
1762 savearea *sv;
1763 uint64_t oldmsr;
1764
1765 sv = find_user_regs(thread); /* Find the user registers */
1766 if(!sv) sv = get_user_regs(thread); /* Didn't find any, allocate and initialize one */
1767
1768 oldmsr = sv->save_srr1; /* Get the old msr */
1769
1770 if(onoff) sv->save_srr1 = oldmsr | (uint64_t)(MASK(MSR_FE0) | MASK(MSR_FE1)); /* Flip on precise FP exceptions */
1771 else sv->save_srr1 = oldmsr & (uint64_t)(~(MASK(MSR_FE0) | MASK(MSR_FE1))); /* Flip on precise FP exceptions */
1772
1773 return ((oldmsr & (MASK(MSR_FE0) | MASK(MSR_FE1))) != 0); /* Return if it was enabled or not */
1774 }