]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/status.c
3f80e92ad88105c35003d49d8b5b5380f83892e3
[apple/xnu.git] / osfmk / ppc / status.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 *
28 */
29
30 #include <kern/thread.h>
31 #include <kern/thread_act.h>
32 #include <kern/misc_protos.h>
33 #include <mach/ppc/thread_status.h>
34 #include <ppc/proc_reg.h>
35 #include <ppc/exception.h>
36 #include <ppc/misc_protos.h>
37 #include <ppc/savearea.h>
38 #include <ppc/thread_act.h>
39 #include <ppc/Firmware.h>
40
41 #include <vm/vm_map.h>
42
43 extern unsigned int killprint;
44 extern double FloatInit;
45 extern unsigned long QNaNbarbarian[4];
46 extern void thread_bootstrap_return(void);
47 extern struct Saveanchor saveanchor;
48 extern int real_ncpus; /* Number of actual CPUs */
49
50 #define USRSTACK 0xc0000000
51
52 kern_return_t
53 thread_userstack(
54 thread_t,
55 int,
56 thread_state_t,
57 unsigned int,
58 vm_offset_t *,
59 int *
60 );
61
62 kern_return_t
63 thread_entrypoint(
64 thread_t,
65 int,
66 thread_state_t,
67 unsigned int,
68 vm_offset_t *
69 );
70
71 unsigned int get_msr_exportmask(void);
72 unsigned int get_msr_nbits(void);
73 unsigned int get_msr_rbits(void);
74 void ppc_checkthreadstate(void *, int);
75 void thread_set_child(thread_act_t child, int pid);
76 void thread_set_parent(thread_act_t parent, int pid);
77
78 /*
79 * Maps state flavor to number of words in the state:
80 */
81 unsigned int state_count[] = {
82 /* FLAVOR_LIST */ 0,
83 PPC_THREAD_STATE_COUNT,
84 PPC_FLOAT_STATE_COUNT,
85 PPC_EXCEPTION_STATE_COUNT,
86 PPC_VECTOR_STATE_COUNT,
87 PPC_THREAD_STATE64_COUNT,
88 PPC_EXCEPTION_STATE64_COUNT,
89 };
90
91 /*
92 * thread_getstatus:
93 *
94 * Get the status of the specified thread.
95 */
96
97 kern_return_t
98 act_machine_get_state(
99 thread_act_t thr_act,
100 thread_flavor_t flavor,
101 thread_state_t tstate,
102 mach_msg_type_number_t *count)
103 {
104
105 register struct savearea *sv; /* Pointer to the context savearea */
106 register savearea_fpu *fsv;
107 register savearea_vec *vsv;
108 savearea *genuser;
109 int i, j;
110 unsigned int vrvalidwrk;
111
112 register struct ppc_thread_state *ts;
113 register struct ppc_thread_state64 *xts;
114 register struct ppc_exception_state *es;
115 register struct ppc_exception_state64 *xes;
116 register struct ppc_float_state *fs;
117 register struct ppc_vector_state *vs;
118
119 genuser = find_user_regs(thr_act); /* Find the current user general context for this activation */
120
121 switch (flavor) {
122
123 case THREAD_STATE_FLAVOR_LIST:
124
125 if (*count < 6) {
126 return (KERN_INVALID_ARGUMENT);
127 }
128
129 tstate[0] = PPC_THREAD_STATE;
130 tstate[1] = PPC_FLOAT_STATE;
131 tstate[2] = PPC_EXCEPTION_STATE;
132 tstate[3] = PPC_VECTOR_STATE;
133 tstate[4] = PPC_THREAD_STATE64;
134 tstate[5] = PPC_EXCEPTION_STATE64;
135 *count = 6;
136
137 return KERN_SUCCESS;
138
139 case PPC_THREAD_STATE:
140
141 if (*count < PPC_THREAD_STATE_COUNT) { /* Is the count ok? */
142 return KERN_INVALID_ARGUMENT;
143 }
144
145 ts = (struct ppc_thread_state *) tstate;
146
147 sv = genuser; /* Copy this over */
148
149 if(sv) { /* Is there a save area yet? */
150 ts->r0 = (unsigned int)sv->save_r0;
151 ts->r1 = (unsigned int)sv->save_r1;
152 ts->r2 = (unsigned int)sv->save_r2;
153 ts->r3 = (unsigned int)sv->save_r3;
154 ts->r4 = (unsigned int)sv->save_r4;
155 ts->r5 = (unsigned int)sv->save_r5;
156 ts->r6 = (unsigned int)sv->save_r6;
157 ts->r7 = (unsigned int)sv->save_r7;
158 ts->r8 = (unsigned int)sv->save_r8;
159 ts->r9 = (unsigned int)sv->save_r9;
160 ts->r10 = (unsigned int)sv->save_r10;
161 ts->r11 = (unsigned int)sv->save_r11;
162 ts->r12 = (unsigned int)sv->save_r12;
163 ts->r13 = (unsigned int)sv->save_r13;
164 ts->r14 = (unsigned int)sv->save_r14;
165 ts->r15 = (unsigned int)sv->save_r15;
166 ts->r16 = (unsigned int)sv->save_r16;
167 ts->r17 = (unsigned int)sv->save_r17;
168 ts->r18 = (unsigned int)sv->save_r18;
169 ts->r19 = (unsigned int)sv->save_r19;
170 ts->r20 = (unsigned int)sv->save_r20;
171 ts->r21 = (unsigned int)sv->save_r21;
172 ts->r22 = (unsigned int)sv->save_r22;
173 ts->r23 = (unsigned int)sv->save_r23;
174 ts->r24 = (unsigned int)sv->save_r24;
175 ts->r25 = (unsigned int)sv->save_r25;
176 ts->r26 = (unsigned int)sv->save_r26;
177 ts->r27 = (unsigned int)sv->save_r27;
178 ts->r28 = (unsigned int)sv->save_r28;
179 ts->r29 = (unsigned int)sv->save_r29;
180 ts->r30 = (unsigned int)sv->save_r30;
181 ts->r31 = (unsigned int)sv->save_r31;
182 ts->cr = (unsigned int)sv->save_cr;
183 ts->xer = (unsigned int)sv->save_xer;
184 ts->lr = (unsigned int)sv->save_lr;
185 ts->ctr = (unsigned int)sv->save_ctr;
186 ts->srr0 = (unsigned int)sv->save_srr0;
187 ts->srr1 = (unsigned int)sv->save_srr1;
188 ts->mq = 0; /* MQ register (601 only) */
189 ts->vrsave = (unsigned int)sv->save_vrsave; /* VRSAVE register (Altivec only) */
190 }
191 else { /* No user state yet. Save seemingly random values. */
192
193 for(i=0; i < 32; i+=2) { /* Fill up with defaults */
194 ((unsigned int *)&ts->r0)[i] = ((unsigned int *)&FloatInit)[0];
195 ((unsigned int *)&ts->r0)[i+1] = ((unsigned int *)&FloatInit)[1];
196 }
197 ts->cr = 0;
198 ts->xer = 0;
199 ts->lr = ((unsigned int *)&FloatInit)[0];
200 ts->ctr = ((unsigned int *)&FloatInit)[1];
201 ts->srr0 = ((unsigned int *)&FloatInit)[0];
202 ts->srr1 = MSR_EXPORT_MASK_SET;
203 ts->mq = 0;
204 ts->vrsave = 0; /* VRSAVE register (Altivec only) */
205 }
206
207 *count = PPC_THREAD_STATE_COUNT; /* Pass back the amount we actually copied */
208 return KERN_SUCCESS;
209
210
211 case PPC_THREAD_STATE64:
212
213 if (*count < PPC_THREAD_STATE64_COUNT) { /* Is the count ok? */
214 return KERN_INVALID_ARGUMENT;
215 }
216
217 xts = (struct ppc_thread_state64 *) tstate;
218
219 sv = genuser; /* Copy this over */
220
221 if(sv) { /* Is there a save area yet? */
222 xts->r0 = sv->save_r0;
223 xts->r1 = sv->save_r1;
224 xts->r2 = sv->save_r2;
225 xts->r3 = sv->save_r3;
226 xts->r4 = sv->save_r4;
227 xts->r5 = sv->save_r5;
228 xts->r6 = sv->save_r6;
229 xts->r7 = sv->save_r7;
230 xts->r8 = sv->save_r8;
231 xts->r9 = sv->save_r9;
232 xts->r10 = sv->save_r10;
233 xts->r11 = sv->save_r11;
234 xts->r12 = sv->save_r12;
235 xts->r13 = sv->save_r13;
236 xts->r14 = sv->save_r14;
237 xts->r15 = sv->save_r15;
238 xts->r16 = sv->save_r16;
239 xts->r17 = sv->save_r17;
240 xts->r18 = sv->save_r18;
241 xts->r19 = sv->save_r19;
242 xts->r20 = sv->save_r20;
243 xts->r21 = sv->save_r21;
244 xts->r22 = sv->save_r22;
245 xts->r23 = sv->save_r23;
246 xts->r24 = sv->save_r24;
247 xts->r25 = sv->save_r25;
248 xts->r26 = sv->save_r26;
249 xts->r27 = sv->save_r27;
250 xts->r28 = sv->save_r28;
251 xts->r29 = sv->save_r29;
252 xts->r30 = sv->save_r30;
253 xts->r31 = sv->save_r31;
254 xts->cr = sv->save_cr;
255 xts->xer = sv->save_xer;
256 xts->lr = sv->save_lr;
257 xts->ctr = sv->save_ctr;
258 xts->srr0 = sv->save_srr0;
259 xts->srr1 = sv->save_srr1;
260 xts->vrsave = sv->save_vrsave; /* VRSAVE register (Altivec only) */
261 }
262 else { /* No user state yet. Save seemingly random values. */
263
264 for(i=0; i < 32; i++) { /* Fill up with defaults */
265 ((unsigned long long *)&xts->r0)[i] = ((unsigned long long *)&FloatInit)[0];
266 }
267 xts->cr = 0;
268 xts->xer = 0;
269 xts->lr = ((unsigned long long *)&FloatInit)[0];
270 xts->ctr = ((unsigned long long *)&FloatInit)[0];
271 xts->srr0 = ((unsigned long long *)&FloatInit)[0];
272 xts->srr1 = MSR_EXPORT_MASK_SET;
273 xts->vrsave = 0; /* VRSAVE register (Altivec only) */
274 }
275
276 *count = PPC_THREAD_STATE64_COUNT; /* Pass back the amount we actually copied */
277 return KERN_SUCCESS;
278
279 case PPC_EXCEPTION_STATE:
280
281 if (*count < PPC_EXCEPTION_STATE_COUNT) {
282 return KERN_INVALID_ARGUMENT;
283 }
284
285 es = (struct ppc_exception_state *) tstate;
286 sv = genuser; /* Copy this over */
287
288 if(sv) { /* See if valid state yet */
289 es->dar = (unsigned int)sv->save_dar;
290 es->dsisr = sv->save_dsisr;
291 es->exception = sv->save_exception;
292 }
293 else { /* Nope, not yet */
294 es->dar = 0;
295 es->dsisr = 0;
296 es->exception = ((unsigned int *)&FloatInit)[0];
297 }
298
299 *count = PPC_EXCEPTION_STATE_COUNT;
300 return KERN_SUCCESS;
301
302 case PPC_EXCEPTION_STATE64:
303
304 if (*count < PPC_EXCEPTION_STATE64_COUNT) {
305 return KERN_INVALID_ARGUMENT;
306 }
307
308 xes = (struct ppc_exception_state64 *) tstate;
309 sv = genuser; /* Copy this over */
310
311 if(sv) { /* See if valid state yet */
312 xes->dar = sv->save_dar;
313 xes->dsisr = sv->save_dsisr;
314 xes->exception = sv->save_exception;
315 }
316 else { /* Nope, not yet */
317 xes->dar = 0;
318 xes->dsisr = 0;
319 xes->exception = ((unsigned int *)&FloatInit)[0];
320 }
321
322 *count = PPC_EXCEPTION_STATE64_COUNT;
323 return KERN_SUCCESS;
324
325 case PPC_FLOAT_STATE:
326
327 if (*count < PPC_FLOAT_STATE_COUNT) {
328 return KERN_INVALID_ARGUMENT;
329 }
330
331 fpu_save(thr_act->mact.curctx); /* Just in case it's live, save it */
332
333 fs = (struct ppc_float_state *) tstate; /* Point to destination */
334
335 fsv = find_user_fpu(thr_act); /* Get the user's fpu savearea */
336
337 if(fsv) { /* See if we have any */
338 bcopy((char *)&fsv->save_fp0, (char *)fs, 32*8); /* 32 registers */
339 fs->fpscr_pad = 0; /* Be clean and tidy */
340 if(genuser) fs->fpscr = genuser->save_fpscr; /* Set the fpscr value to general */
341 else fs->fpscr = 0; /* If no user, initialize this */
342 }
343 else { /* No floating point yet */
344
345 for(i=0; i < 32; i++) { /* Initialize floating points */
346 fs->fpregs[i] = FloatInit; /* Initial value */
347 }
348 fs->fpscr_pad = 0; /* Initial value */
349 fs->fpscr = 0; /* Initial value */
350 }
351
352 *count = PPC_FLOAT_STATE_COUNT;
353
354 return KERN_SUCCESS;
355
356 case PPC_VECTOR_STATE:
357
358 if (*count < PPC_VECTOR_STATE_COUNT) {
359 return KERN_INVALID_ARGUMENT;
360 }
361
362 vec_save(thr_act->mact.curctx); /* Just in case it's live, save it */
363
364 vs = (struct ppc_vector_state *) tstate; /* Point to destination */
365
366 vsv = find_user_vec(thr_act); /* Find the vector savearea */
367
368 if(vsv) { /* See if we have any */
369
370 vrvalidwrk = vsv->save_vrvalid; /* Get the valid flags */
371 vs->save_vrvalid = vsv->save_vrvalid; /* Set the valid flags */
372 if(genuser) for(j=0; j < 4; j++) vs->save_vscr[j] = genuser->save_vscr[j]; /* Set value for vscr */
373 else {
374 vs->save_vscr[0] = 0; /* Set an initial value if no general user yet */
375 vs->save_vscr[1] = 0;
376 vs->save_vscr[2] = 0;
377 vs->save_vscr[3] = 0x00010000; /* Always start with Java mode off */
378 }
379 for(i=0; i < 32; i++) { /* Copy the saved registers and invalidate the others */
380 for(j=0; j < 4; j++) {
381 if(vrvalidwrk & 0x80000000) (vs->save_vr)[i][j] =
382 ((unsigned int *)&(vsv->save_vr0))[(i * 4) + j]; /* We have this register saved */
383 else vs->save_vr[i][j] = QNaNbarbarian[j]; /* Set invalid value */
384 }
385 vrvalidwrk = vrvalidwrk << 1; /* Shift over to the next */
386 }
387 }
388 else { /* No vector yet */
389
390 for(i=0; i < 32; i++) { /* Initialize vector registers */
391 for(j=0; j < 4; j++) vs->save_vr[i][j] = QNaNbarbarian[j]; /* Initial value */
392 }
393
394 if(genuser) for(j=0; j < 4; j++) vs->save_vscr[j] = genuser->save_vscr[j]; /* Set value for vscr */
395 else {
396 vs->save_vscr[0] = 0; /* Set an initial value if no general user yet */
397 vs->save_vscr[1] = 0;
398 vs->save_vscr[2] = 0;
399 vs->save_vscr[3] = 0x00010000; /* Always start with Java mode off */
400 }
401 vs->save_vrvalid = 0; /* Clear the valid flags */
402 }
403
404 for (i=0; i < 4; i++) vs->save_pad5[i] = 0; /* Clear cruft */
405 for (i=0; i < 7; i++) vs->save_pad6[i] = 0; /* Clear cruft */
406
407 *count = PPC_VECTOR_STATE_COUNT;
408 return KERN_SUCCESS;
409
410 default:
411 return KERN_INVALID_ARGUMENT;
412 }
413 }
414
415
416 /*
417 * thread_setstatus:
418 *
419 * Set the status of the specified thread.
420 */
421 kern_return_t
422 act_machine_set_state(
423 thread_act_t thr_act,
424 thread_flavor_t flavor,
425 thread_state_t tstate,
426 mach_msg_type_number_t count)
427 {
428
429 savearea *sv, *genuser;
430 savearea_fpu *fsv, *fsvn, *fsvo;
431 savearea_vec *vsv, *vsvn, *vsvo;
432 unsigned int i;
433 int clgn;
434 register struct ppc_thread_state *ts;
435 register struct ppc_thread_state64 *xts;
436 register struct ppc_exception_state *es;
437 register struct ppc_exception_state *xes;
438 register struct ppc_float_state *fs;
439 register struct ppc_vector_state *vs;
440
441 int kernel_act = thr_act->kernel_loading || thr_act->kernel_loaded;
442
443 // dbgTrace((unsigned int)thr_act, (unsigned int)sv, flavor); /* (TEST/DEBUG) */
444
445 clgn = count; /* Get the count */
446
447 switch (flavor) { /* Validate the count before we do anything else */
448 case PPC_THREAD_STATE:
449
450 if (clgn < PPC_THREAD_STATE_COUNT) { /* Is it too short? */
451 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
452 }
453 break;
454
455 case PPC_THREAD_STATE64:
456
457 if (clgn < PPC_THREAD_STATE64_COUNT) { /* Is it too short? */
458 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
459 }
460 break;
461
462 case PPC_EXCEPTION_STATE:
463
464 if (clgn < PPC_EXCEPTION_STATE_COUNT) { /* Is it too short? */
465 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
466 }
467
468 case PPC_EXCEPTION_STATE64:
469
470 if (clgn < PPC_EXCEPTION_STATE64_COUNT) { /* Is it too short? */
471 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
472 }
473
474 break;
475
476 case PPC_FLOAT_STATE:
477
478 if (clgn < PPC_FLOAT_STATE_COUNT) { /* Is it too short? */
479 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
480 }
481
482 break;
483
484
485 case PPC_VECTOR_STATE:
486
487 if (clgn < PPC_VECTOR_STATE_COUNT) { /* Is it too short? */
488 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
489 }
490
491 break;
492
493 default:
494 return KERN_INVALID_ARGUMENT;
495 }
496
497 genuser = get_user_regs(thr_act); /* Find or allocate and initialize one */
498
499 switch (flavor) {
500
501 case PPC_THREAD_STATE:
502
503 ts = (struct ppc_thread_state *)tstate;
504
505 genuser->save_r0 = (uint64_t)ts->r0;
506 genuser->save_r1 = (uint64_t)ts->r1;
507 genuser->save_r2 = (uint64_t)ts->r2;
508 genuser->save_r3 = (uint64_t)ts->r3;
509 genuser->save_r4 = (uint64_t)ts->r4;
510 genuser->save_r5 = (uint64_t)ts->r5;
511 genuser->save_r6 = (uint64_t)ts->r6;
512 genuser->save_r7 = (uint64_t)ts->r7;
513 genuser->save_r8 = (uint64_t)ts->r8;
514 genuser->save_r9 = (uint64_t)ts->r9;
515 genuser->save_r10 = (uint64_t)ts->r10;
516 genuser->save_r11 = (uint64_t)ts->r11;
517 genuser->save_r12 = (uint64_t)ts->r12;
518 genuser->save_r13 = (uint64_t)ts->r13;
519 genuser->save_r14 = (uint64_t)ts->r14;
520 genuser->save_r15 = (uint64_t)ts->r15;
521 genuser->save_r16 = (uint64_t)ts->r16;
522 genuser->save_r17 = (uint64_t)ts->r17;
523 genuser->save_r18 = (uint64_t)ts->r18;
524 genuser->save_r19 = (uint64_t)ts->r19;
525 genuser->save_r20 = (uint64_t)ts->r20;
526 genuser->save_r21 = (uint64_t)ts->r21;
527 genuser->save_r22 = (uint64_t)ts->r22;
528 genuser->save_r23 = (uint64_t)ts->r23;
529 genuser->save_r24 = (uint64_t)ts->r24;
530 genuser->save_r25 = (uint64_t)ts->r25;
531 genuser->save_r26 = (uint64_t)ts->r26;
532 genuser->save_r27 = (uint64_t)ts->r27;
533 genuser->save_r28 = (uint64_t)ts->r28;
534 genuser->save_r29 = (uint64_t)ts->r29;
535 genuser->save_r30 = (uint64_t)ts->r30;
536 genuser->save_r31 = (uint64_t)ts->r31;
537
538 genuser->save_cr = ts->cr;
539 genuser->save_xer = (uint64_t)ts->xer;
540 genuser->save_lr = (uint64_t)ts->lr;
541 genuser->save_ctr = (uint64_t)ts->ctr;
542 genuser->save_srr0 = (uint64_t)ts->srr0;
543 genuser->save_vrsave = ts->vrsave; /* VRSAVE register (Altivec only) */
544
545 genuser->save_srr1 = MSR_PREPARE_FOR_IMPORT(genuser->save_srr1, ts->srr1); /* Set the bits we can change */
546
547 if(!kernel_act) genuser->save_srr1 |= MSR_EXPORT_MASK_SET; /* If not a kernel guy, force the magic bits on */
548
549 genuser->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make sure we don't enable the floating point unit */
550
551 return KERN_SUCCESS;
552
553
554 case PPC_THREAD_STATE64:
555
556 xts = (struct ppc_thread_state64 *)tstate;
557
558 genuser->save_r0 = xts->r0;
559 genuser->save_r1 = xts->r1;
560 genuser->save_r2 = xts->r2;
561 genuser->save_r3 = xts->r3;
562 genuser->save_r4 = xts->r4;
563 genuser->save_r5 = xts->r5;
564 genuser->save_r6 = xts->r6;
565 genuser->save_r7 = xts->r7;
566 genuser->save_r8 = xts->r8;
567 genuser->save_r9 = xts->r9;
568 genuser->save_r10 = xts->r10;
569 genuser->save_r11 = xts->r11;
570 genuser->save_r12 = xts->r12;
571 genuser->save_r13 = xts->r13;
572 genuser->save_r14 = xts->r14;
573 genuser->save_r15 = xts->r15;
574 genuser->save_r16 = xts->r16;
575 genuser->save_r17 = xts->r17;
576 genuser->save_r18 = xts->r18;
577 genuser->save_r19 = xts->r19;
578 genuser->save_r20 = xts->r20;
579 genuser->save_r21 = xts->r21;
580 genuser->save_r22 = xts->r22;
581 genuser->save_r23 = xts->r23;
582 genuser->save_r24 = xts->r24;
583 genuser->save_r25 = xts->r25;
584 genuser->save_r26 = xts->r26;
585 genuser->save_r27 = xts->r27;
586 genuser->save_r28 = xts->r28;
587 genuser->save_r29 = xts->r29;
588 genuser->save_r30 = xts->r30;
589 genuser->save_r31 = xts->r31;
590
591 genuser->save_cr = xts->cr;
592 genuser->save_xer = xts->xer;
593 genuser->save_lr = xts->lr;
594 genuser->save_ctr = xts->ctr;
595 genuser->save_srr0 = xts->srr0;
596 genuser->save_vrsave = xts->vrsave; /* VRSAVE register (Altivec only) */
597
598 genuser->save_srr1 = MSR_PREPARE_FOR_IMPORT(genuser->save_srr1, xts->srr1); /* Set the bits we can change */
599
600 if(!kernel_act) genuser->save_srr1 |= MSR_EXPORT_MASK_SET; /* If not a kernel guy, force the magic bits on */
601
602 genuser->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make sure we don't enable the floating point unit */
603
604 return KERN_SUCCESS;
605
606
607 case PPC_EXCEPTION_STATE:
608
609 es = (struct ppc_exception_state *) tstate;
610
611 genuser->save_dar = (uint64_t)es->dar;
612 genuser->save_dsisr = es->dsisr;
613 genuser->save_exception = es->exception;
614
615 return KERN_SUCCESS;
616
617 /*
618 * It's pretty worthless to try to change this stuff, but we'll do it anyway.
619 */
620
621 case PPC_EXCEPTION_STATE64:
622
623 xes = (struct ppc_exception_state *) tstate;
624
625 genuser->save_dar = xes->dar;
626 genuser->save_dsisr = xes->dsisr;
627 genuser->save_exception = xes->exception;
628
629 return KERN_SUCCESS;
630
631 case PPC_FLOAT_STATE:
632
633 toss_live_fpu(thr_act->mact.curctx); /* Toss my floating point if live anywhere */
634
635 fsv = find_user_fpu(thr_act); /* Get the user's floating point context */
636
637 if(!fsv) { /* Do we have one yet? */
638 fsv = (savearea_fpu *)save_alloc(); /* If we still don't have one, get a new one */
639 fsv->save_hdr.save_flags = (fsv->save_hdr.save_flags & ~SAVtype) | (SAVfloat << SAVtypeshft); /* Mark as in use as float */
640 fsv->save_hdr.save_act = thr_act; /* Point to the activation */
641 fsv->save_hdr.save_prev = 0; /* Mark no more */
642 fsv->save_hdr.save_level = 0; /* Mark user state */
643
644 if(!thr_act->mact.curctx->FPUsave) thr_act->mact.curctx->FPUsave = fsv; /* If no floating point, chain us first */
645 else {
646
647 fsvn = fsvo = thr_act->mact.curctx->FPUsave; /* Remember first one */
648
649 while (fsvn) { /* Go until we hit the end */
650 fsvo = fsvn; /* Remember the previous one */
651 fsvn = (savearea_fpu *)fsvo->save_hdr.save_prev; /* Skip on to the next */
652 }
653
654 fsvo->save_hdr.save_prev = (addr64_t)fsv; /* Queue us on in */
655 }
656
657 }
658
659 fs = (struct ppc_float_state *) tstate; /* Point to source */
660
661
662 bcopy((char *)fs, (char *)&fsv->save_fp0, 32*8); /* Move in the 32 registers */
663
664 genuser->save_fpscr = fs->fpscr; /* Copy the fpscr value to normal */
665
666 return KERN_SUCCESS;
667
668
669 case PPC_VECTOR_STATE:
670
671 toss_live_vec(thr_act->mact.curctx); /* Toss my vector if live anywhere */
672
673 vsv = find_user_vec(thr_act); /* Get the user's vector context */
674
675 if(!vsv) { /* Do we have one yet? */
676 vsv = (savearea_vec *)save_alloc(); /* If we still don't have one, get a new one */
677 vsv->save_hdr.save_flags = (vsv->save_hdr.save_flags & ~SAVtype) | (SAVvector << SAVtypeshft); /* Mark as in use as vector */
678 vsv->save_hdr.save_act = thr_act; /* Point to the activation */
679 vsv->save_hdr.save_prev = 0; /* Mark no more */
680 vsv->save_hdr.save_level = 0; /* Mark user state */
681
682 if(!thr_act->mact.curctx->VMXsave) thr_act->mact.curctx->VMXsave = vsv; /* If no vector, chain us first */
683 else {
684
685 vsvn = vsvo = thr_act->mact.curctx->VMXsave; /* Remember first one */
686
687 while (vsvn) { /* Go until we hit the end */
688 vsvo = vsvn; /* Remember the previous one */
689 vsvn = (savearea_vec *)vsvo->save_hdr.save_prev; /* Skip on to the next */
690 }
691
692 vsvo->save_hdr.save_prev = (addr64_t)vsv; /* Queue us on in */
693 }
694
695 }
696
697 vs = (struct ppc_vector_state *) tstate; /* Point to source */
698
699 bcopy((char *)vs, (char *)&vsv->save_vr0, 32*16); /* 32 registers plus status and validity and pad */
700 vsv->save_vrvalid = vs->save_vrvalid; /* Set validity bits */
701
702
703 for(i = 0; i < 4; i++) genuser->save_vscr[i] = vs->save_vscr[i]; /* Set value for vscr */
704
705 return KERN_SUCCESS;
706
707
708 default:
709 return KERN_INVALID_ARGUMENT;
710 }
711 }
712
713 /*
714 * Duplicates the context of one thread into a new one.
715 * The new thread is assumed to be new and have no user state contexts except maybe a general one.
716 * We also assume that the old thread can't be running anywhere.
717 *
718 * We're only going to be duplicating user context here. That means that we will have to
719 * eliminate any floating point or vector kernel contexts and carry across the user state ones.
720 */
721
722 void act_thread_dup(thread_act_t old, thread_act_t new) {
723
724 savearea *sv, *osv;
725 savearea_fpu *fsv, *fsvn;
726 savearea_vec *vsv, *vsvn;
727 unsigned int spc, i, *srs;
728
729 fpu_save(old->mact.curctx); /* Make certain floating point state is all saved */
730 vec_save(old->mact.curctx); /* Make certain the vector state is all saved */
731
732 sv = get_user_regs(new); /* Allocate and initialze context in the new activation */
733
734 osv = find_user_regs(old); /* Find the original context */
735 if(!osv) {
736 panic("act_thread_dup: old activation (%08X) has no general user context\n", old);
737 }
738
739 bcopy((char *)((unsigned int)osv + sizeof(savearea_comm)), /* Copy everything but the headers */
740 (char *)((unsigned int)sv + sizeof(savearea_comm)),
741 sizeof(struct savearea) - sizeof(savearea_comm));
742
743 sv->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make certain that floating point and vector are turned off */
744
745 fsv = find_user_fpu(old); /* Get any user floating point */
746
747 new->mact.curctx->FPUsave = 0; /* Assume no floating point */
748
749 if(fsv) { /* Did we find one? */
750 fsvn = (savearea_fpu *)save_alloc(); /* If we still don't have one, get a new one */
751 fsvn->save_hdr.save_flags = (fsvn->save_hdr.save_flags & ~SAVtype) | (SAVfloat << SAVtypeshft); /* Mark as in use as float */
752 fsvn->save_hdr.save_act = new; /* Point to the activation */
753 fsvn->save_hdr.save_prev = 0; /* Mark no more */
754 fsvn->save_hdr.save_level = 0; /* Mark user state */
755
756 new->mact.curctx->FPUsave = fsvn; /* Chain in the floating point */
757
758 bcopy((char *)((unsigned int)fsv + sizeof(savearea_comm)), /* Copy everything but the headers */
759 (char *)((unsigned int)fsvn + sizeof(savearea_comm)),
760 sizeof(struct savearea) - sizeof(savearea_comm));
761 }
762
763 vsv = find_user_vec(old); /* Get any user vector */
764
765 new->mact.curctx->VMXsave = 0; /* Assume no vector */
766
767 if(vsv) { /* Did we find one? */
768 vsvn = (savearea_vec *)save_alloc(); /* If we still don't have one, get a new one */
769 vsvn->save_hdr.save_flags = (vsvn->save_hdr.save_flags & ~SAVtype) | (SAVvector << SAVtypeshft); /* Mark as in use as float */
770 vsvn->save_hdr.save_act = new; /* Point to the activation */
771 vsvn->save_hdr.save_prev = 0; /* Mark no more */
772 vsvn->save_hdr.save_level = 0; /* Mark user state */
773
774 new->mact.curctx->VMXsave = vsvn; /* Chain in the floating point */
775
776 bcopy((char *)((unsigned int)vsv + sizeof(savearea_comm)), /* Copy everything but the headers */
777 (char *)((unsigned int)vsvn + sizeof(savearea_comm)),
778 sizeof(struct savearea) - sizeof(savearea_comm));
779 }
780
781 return; /* Bye bye... */
782 }
783
784 /*
785 * Initializes a fresh set of user state values. If there is no user state context,
786 * one is created. Floats and VMX are not created.
787 *
788 * We only set initial values if there was no context found.
789 */
790
791 savearea *get_user_regs(thread_act_t act) {
792
793 savearea *sv, *osv;
794 unsigned int spc, i, *srs;
795
796 sv = act->mact.pcb; /* Get the top savearea on the stack */
797 osv = 0; /* Set no user savearea yet */
798
799 while(sv) { /* Find the user context */
800 if(sv->save_srr1 & MASK(MSR_PR)) return sv; /* We found a user state context... */
801
802 osv = sv; /* Save the last one */
803 sv = (savearea *)sv->save_hdr.save_prev; /* Get the previous context */
804 }
805
806 sv = save_alloc(); /* Get one */
807 sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use as general */
808 sv->save_hdr.save_act = act; /* Point to the activation */
809 sv->save_hdr.save_prev = 0; /* Mark no more */
810 sv->save_hdr.save_level = 0; /* Mark user state */
811
812 if(osv) { /* Did we already have one? */
813 osv->save_hdr.save_prev = (addr64_t)sv; /* Chain us on the end */
814 }
815 else { /* We are the first */
816 act->mact.pcb = sv; /* Put it there */
817 }
818
819 for(i=0; i < 32; i+=2) { /* Fill up with defaults */
820 ((unsigned int *)&sv->save_r0)[i] = ((unsigned int *)&FloatInit)[0];
821 ((unsigned int *)&sv->save_r0)[i+1] = ((unsigned int *)&FloatInit)[1];
822 }
823 sv->save_cr = 0;
824 sv->save_xer = 0;
825 sv->save_lr = (uint64_t)FloatInit;
826 sv->save_ctr = (uint64_t)FloatInit;
827 sv->save_srr0 = (uint64_t)FloatInit;
828 sv->save_srr1 = (uint64_t)MSR_EXPORT_MASK_SET;
829
830 sv->save_fpscr = 0; /* Clear all floating point exceptions */
831
832 sv->save_vrsave = 0; /* Set the vector save state */
833 sv->save_vscr[0] = 0x00000000;
834 sv->save_vscr[1] = 0x00000000;
835 sv->save_vscr[2] = 0x00000000;
836 sv->save_vscr[3] = 0x00010000; /* Disable java mode and clear saturated */
837
838 return sv; /* Bye bye... */
839 }
840
841 /*
842 * Find the user state context. If there is no user state context,
843 * we just return a 0.
844 */
845
846 savearea *find_user_regs(thread_act_t act) {
847
848 savearea *sv;
849
850 sv = act->mact.pcb; /* Get the top savearea on the stack */
851
852 while(sv) { /* Find the user context */
853 if(sv->save_srr1 & MASK(MSR_PR)) { /* Are we looking at the user context? */
854 break; /* Outta here */
855 }
856 sv = (savearea *)sv->save_hdr.save_prev; /* Get the previous context */
857 }
858
859 return sv; /* Bye bye... */
860 }
861
862 /*
863 * Find the user state floating point context. If there is no user state context,
864 * we just return a 0.
865 */
866
867 savearea_fpu *find_user_fpu(thread_act_t act) {
868
869 savearea_fpu *fsv;
870
871 fsv = act->mact.curctx->FPUsave; /* Get the start of the floating point chain */
872
873 while(fsv) { /* Look until the end or we find it */
874 if(!(fsv->save_hdr.save_level)) break; /* Is the the user state stuff? (the level is 0 if so) */
875 fsv = (savearea_fpu *)fsv->save_hdr.save_prev; /* Try the previous one */
876 }
877
878 return fsv; /* Bye bye... */
879 }
880
881 /*
882 * Find the user state vector context. If there is no user state context,
883 * we just return a 0.
884 */
885
886 savearea_vec *find_user_vec(thread_act_t act) {
887
888 savearea_vec *vsv;
889
890 vsv = act->mact.curctx->VMXsave; /* Get the start of the vector chain */
891
892 while(vsv) { /* Look until the end or we find it */
893 if(!(vsv->save_hdr.save_level)) break; /* Is the the user state stuff? (the level is 0 if so) */
894 vsv = (savearea_vec *)vsv->save_hdr.save_prev; /* Try the previous one */
895 }
896
897 return vsv; /* Bye bye... */
898 }
899
900 /*
901 * thread_userstack:
902 *
903 * Return the user stack pointer from the machine
904 * dependent thread state info.
905 */
906 kern_return_t
907 thread_userstack(
908 thread_t thread,
909 int flavor,
910 thread_state_t tstate,
911 unsigned int count,
912 vm_offset_t *user_stack,
913 int *customstack
914 )
915 {
916 struct ppc_thread_state *state;
917
918 /*
919 * Set a default.
920 */
921 if (*user_stack == 0)
922 *user_stack = USRSTACK;
923 if (customstack)
924 *customstack = 0;
925
926 switch (flavor) {
927 case PPC_THREAD_STATE:
928 if (count < PPC_THREAD_STATE_COUNT)
929 return (KERN_INVALID_ARGUMENT);
930
931 state = (struct ppc_thread_state *) tstate;
932
933 /*
934 * If a valid user stack is specified, use it.
935 */
936 *user_stack = state->r1 ? state->r1: USRSTACK;
937
938 if (customstack && state->r1)
939 *customstack = 1;
940
941 break;
942 default :
943 return (KERN_INVALID_ARGUMENT);
944 }
945
946 return (KERN_SUCCESS);
947 }
948
949
950 /*
951 * thread_setuserstack:
952 *
953 * Sets the user stack pointer into the machine
954 * dependent thread state info.
955 */
956 void thread_setuserstack(struct thread_activation *act, unsigned int user_stack)
957 {
958 savearea *sv;
959
960 sv = get_user_regs(act); /* Get the user state registers */
961
962 sv->save_r1 = (uint64_t)user_stack;
963
964 return;
965 }
966
967 /*
968 * thread_adjuserstack:
969 *
970 * Returns the adjusted user stack pointer from the machine
971 * dependent thread state info.
972 */
973 unsigned int thread_adjuserstack(struct thread_activation *act, int adjust)
974 {
975 savearea *sv;
976
977 sv = get_user_regs(act); /* Get the user state registers */
978
979 sv->save_r1 += adjust; /* Adjust the stack */
980
981 return (unsigned int)sv->save_r1; /* Return the adjusted stack */
982
983 }
984
985 /*
986 * thread_setentrypoint:
987 *
988 * Sets the user PC into the machine
989 * dependent thread state info.
990 */
991
992 void thread_setentrypoint(struct thread_activation *act, unsigned int entry)
993 {
994 savearea *sv;
995
996 sv = get_user_regs(act); /* Get the user state registers */
997
998 sv->save_srr0 = (uint64_t)entry;
999
1000 return;
1001 }
1002
1003 kern_return_t
1004 thread_entrypoint(
1005 thread_t thread,
1006 int flavor,
1007 thread_state_t tstate,
1008 unsigned int count,
1009 vm_offset_t *entry_point
1010 )
1011 {
1012 struct ppc_thread_state *state;
1013
1014 /*
1015 * Set a default.
1016 */
1017 if (*entry_point == 0)
1018 *entry_point = VM_MIN_ADDRESS;
1019
1020 switch (flavor) {
1021
1022 case PPC_THREAD_STATE:
1023 if (count < PPC_THREAD_STATE_COUNT)
1024 return (KERN_INVALID_ARGUMENT);
1025
1026 state = (struct ppc_thread_state *) tstate;
1027
1028 /*
1029 * If a valid entry point is specified, use it.
1030 */
1031 *entry_point = state->srr0 ? state->srr0: VM_MIN_ADDRESS;
1032 break;
1033 default:
1034 return (KERN_INVALID_ARGUMENT);
1035 }
1036
1037 return (KERN_SUCCESS);
1038 }
1039
1040 unsigned int get_msr_exportmask(void)
1041 {
1042 return (MSR_EXPORT_MASK_SET);
1043 }
1044
1045 unsigned int get_msr_nbits(void)
1046 {
1047 return (MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1048 }
1049 unsigned int get_msr_rbits(void)
1050 {
1051 return (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1052 }
1053
1054 void ppc_checkthreadstate(void * tsptr, int flavor)
1055 {
1056 if (flavor == PPC_THREAD_STATE64) {
1057 struct ppc_thread_state64 *ts64 =(struct ppc_thread_state64 *)tsptr;
1058
1059 /* Make sure naughty bits are off and necessary bits are on */
1060 ts64->srr1 &= ~(MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1061 ts64->srr1 |= (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1062 } else {
1063 struct ppc_thread_state *ts =(struct ppc_thread_state *)tsptr;
1064
1065 /* Make sure naughty bits are off and necessary bits are on */
1066 ts->srr1 &= ~(MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1067 ts->srr1 |= (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1068 }
1069 return;
1070 }
1071
1072 void thread_set_child(thread_act_t child, int pid)
1073 {
1074 struct savearea *child_state;
1075
1076 child_state = get_user_regs(child);
1077
1078 child_state->save_r3 = (uint_t)pid;
1079 child_state->save_r4 = 1ULL;
1080 }
1081 void thread_set_parent(thread_act_t parent, int pid)
1082 {
1083 struct savearea *parent_state;
1084
1085 parent_state = get_user_regs(parent);
1086
1087 parent_state->save_r3 = (uint64_t)pid;
1088 parent_state->save_r4 = 0;
1089 }
1090
1091 /*
1092 * Saves the complete context (general, floating point, and vector) of the current activation.
1093 * We will collect everything into an opaque block of 1 to 3 saveareas and pass back a
1094 * pointer to that.
1095 *
1096 * The savearea is made to look like it belongs to the source activation. This needs to
1097 * be adjusted when these contexts are attached to a new activation.
1098 *
1099 */
1100
1101 void *act_thread_csave(void) {
1102
1103 savearea *sv, *osv;
1104 savearea_fpu *fsv, *ofsv;
1105 savearea_vec *vsv, *ovsv;
1106 unsigned int spc, i, *srs;
1107
1108 thread_act_t act;
1109
1110 act = current_act(); /* Find ourselves */
1111
1112 fpu_save(act->mact.curctx); /* Make certain floating point state is all saved */
1113 vec_save(act->mact.curctx); /* Make certain the vector state is all saved */
1114
1115 osv = find_user_regs(act); /* Get our savearea */
1116
1117 if(!osv) {
1118 panic("act_thread_csave: attempting to preserve the context of an activation with none (%08X)\n", act);
1119 }
1120
1121 sv = save_alloc(); /* Get a fresh save area to save into */
1122 sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use as general */
1123 sv->save_hdr.save_act = act; /* Point to the activation */
1124 sv->save_hdr.save_prev = 0; /* Mark no more */
1125 sv->save_hdr.save_level = 0; /* Mark user state */
1126
1127
1128 bcopy((char *)((unsigned int)osv + sizeof(savearea_comm)), /* Copy everything but the headers */
1129 (char *)((unsigned int)sv + sizeof(savearea_comm)),
1130 sizeof(struct savearea) - sizeof(savearea_comm));
1131
1132 sv->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make certain that floating point and vector are turned off */
1133
1134 sv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1135 sv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
1136
1137
1138 ofsv = find_user_fpu(act); /* Get any user floating point */
1139
1140 sv->save_hdr.save_misc0 = 0; /* Assume no floating point */
1141
1142 if(ofsv) { /* Did we find one? */
1143 fsv = (savearea_fpu *)save_alloc(); /* If we still don't have one, get a new one */
1144 fsv->save_hdr.save_flags = (fsv->save_hdr.save_flags & ~SAVtype) | (SAVfloat << SAVtypeshft); /* Mark as in use as float */
1145 fsv->save_hdr.save_act = act; /* Point to the activation */
1146 fsv->save_hdr.save_prev = 0; /* Mark no more */
1147 fsv->save_hdr.save_level = 0; /* Mark user state */
1148 fsv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1149 fsv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
1150
1151 sv->save_hdr.save_misc0 = (uint64_t)fsv; /* Remember this one */
1152
1153 bcopy((char *)((unsigned int)ofsv + sizeof(savearea_comm)), /* Copy everything but the headers */
1154 (char *)((unsigned int)fsv + sizeof(savearea_comm)),
1155 sizeof(struct savearea) - sizeof(savearea_comm));
1156 }
1157
1158 ovsv = find_user_vec(act); /* Get any user vector */
1159
1160 sv->save_hdr.save_misc1 = 0; /* Assume no vector */
1161
1162 if(ovsv) { /* Did we find one? */
1163 vsv = (savearea_vec *)save_alloc(); /* If we still don't have one, get a new one */
1164 vsv->save_hdr.save_flags = (vsv->save_hdr.save_flags & ~SAVtype) | (SAVvector << SAVtypeshft); /* Mark as in use as float */
1165 vsv->save_hdr.save_act = act; /* Point to the activation */
1166 vsv->save_hdr.save_prev = 0; /* Mark no more */
1167 vsv->save_hdr.save_level = 0; /* Mark user state */
1168 vsv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1169 vsv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
1170
1171 sv->save_hdr.save_misc1 = (uint64_t)vsv; /* Chain in the floating point */
1172
1173 bcopy((char *)((unsigned int)ovsv + sizeof(savearea_comm)), /* Copy everything but the headers */
1174 (char *)((unsigned int)vsv + sizeof(savearea_comm)),
1175 sizeof(struct savearea) - sizeof(savearea_comm));
1176 }
1177
1178 return (void *)sv; /* Bye bye... */
1179 }
1180
1181
1182
1183 /*
1184 * Attaches saved user state context to an activation. We will replace any
1185 * user state context with what is passed in. The saved context consists of a
1186 * savearea that was setup by
1187 * We will collect everything into one savearea and pass that back.
1188 *
1189 * The savearea is made to look like it belongs to the source activation. This needs to
1190 * be adjusted when these contexts are attached to a new activation.
1191 *
1192 */
1193
1194 void act_thread_catt(void *ctx) {
1195
1196 savearea *sv, *osv, *psv;
1197 savearea_fpu *fsv, *ofsv, *pfsv;
1198 savearea_vec *vsv, *ovsv, *pvsv;
1199 unsigned int spc, i, *srs;
1200 thread_act_t act;
1201
1202 sv = (savearea *)ctx; /* Make this easier for C */
1203
1204 fsv = (savearea_fpu *)sv->save_hdr.save_misc0; /* Get a possible floating point savearea */
1205 vsv = (savearea_vec *)sv->save_hdr.save_misc1; /* Get a possible vector savearea */
1206
1207 if((sv->save_hdr.save_misc2 != 0xDEBB1ED0) || (sv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1208 panic("act_thread_catt: attempt to attach invalid general context savearea - %08X\n", sv); /* Die */
1209 }
1210
1211 if(fsv && ((fsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (fsv->save_hdr.save_misc3 != 0xE5DA11A5))) { /* See if valid savearea */
1212 panic("act_thread_catt: attempt to attach invalid float context savearea - %08X\n", fsv); /* Die */
1213 }
1214
1215 if(vsv && ((vsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (vsv->save_hdr.save_misc3 != 0xE5DA11A5))) { /* See if valid savearea */
1216 panic("act_thread_catt: attempt to attach invalid vector context savearea - %08X\n", vsv); /* Die */
1217 }
1218
1219 act = current_act(); /* Find ourselves */
1220
1221 toss_live_fpu(act->mact.curctx); /* Toss my floating point if live anywhere */
1222 toss_live_vec(act->mact.curctx); /* Toss my vector if live anywhere */
1223
1224 sv->save_hdr.save_misc2 = 0; /* Eye catcher for debug */
1225 sv->save_hdr.save_misc3 = 0; /* Eye catcher for debug */
1226 sv->save_hdr.save_act = act; /* Set us as owner */
1227
1228 spc = (unsigned int)act->map->pmap->space; /* Get the space we're in */
1229
1230 osv = act->mact.pcb; /* Get the top general savearea */
1231 psv = 0;
1232 while(osv) { /* Any saved state? */
1233 if(osv->save_srr1 & MASK(MSR_PR)) break; /* Leave if this is user state */
1234 psv = osv; /* Save previous savearea address */
1235 osv = (savearea *)osv->save_hdr.save_prev; /* Get one underneath our's */
1236 }
1237
1238 if(osv) { /* Did we find one? */
1239 if(psv) psv->save_hdr.save_prev = 0; /* Yes, clear pointer to it (it should always be last) or */
1240 else act->mact.pcb = 0; /* to the start if the only one */
1241
1242 save_release(osv); /* Nope, release it */
1243
1244 }
1245
1246 if(psv) psv->save_hdr.save_prev = (addr64_t)sv; /* Chain us to the end or */
1247 else act->mact.pcb = (pcb_t)sv; /* to the start if the only one */
1248
1249 ovsv = act->mact.curctx->VMXsave; /* Get the top vector savearea */
1250
1251 pvsv = 0;
1252 while(ovsv) { /* Any VMX saved state? */
1253 if(!(ovsv->save_hdr.save_level)) break; /* Leave if this is user state */
1254 pvsv = ovsv; /* Save previous savearea address */
1255 ovsv = (savearea_vec *)ovsv->save_hdr.save_prev; /* Get one underneath our's */
1256 }
1257
1258 if(ovsv) { /* Did we find one? */
1259 if(pvsv) pvsv->save_hdr.save_prev = 0; /* Yes, clear pointer to it (it should always be last) or */
1260 else act->mact.curctx->VMXsave = 0; /* to the start if the only one */
1261
1262 save_release((savearea *)ovsv); /* Nope, release it */
1263 }
1264
1265 if(vsv) { /* Are we sticking any vector on this one? */
1266 if(pvsv) pvsv->save_hdr.save_prev = (addr64_t)vsv; /* Yes, chain us to the end or */
1267 else act->mact.curctx->VMXsave = vsv; /* to the start if the only one */
1268
1269 vsv->save_hdr.save_misc2 = 0; /* Eye catcher for debug */
1270 vsv->save_hdr.save_misc3 = 0; /* Eye catcher for debug */
1271 vsv->save_hdr.save_act = act; /* Set us as owner */
1272 }
1273
1274 ofsv = act->mact.curctx->FPUsave; /* Get the top float savearea */
1275
1276 pfsv = 0;
1277 while(ofsv) { /* Any float saved state? */
1278 if(!(ofsv->save_hdr.save_level)) break; /* Leave if this is user state */
1279 pfsv = ofsv; /* Save previous savearea address */
1280 ofsv = (savearea_fpu *)ofsv->save_hdr.save_prev; /* Get one underneath our's */
1281 }
1282
1283 if(ofsv) { /* Did we find one? */
1284 if(pfsv) pfsv->save_hdr.save_prev = 0; /* Yes, clear pointer to it (it should always be last) or */
1285 else act->mact.curctx->FPUsave = 0; /* to the start if the only one */
1286
1287 save_release((savearea *)ofsv); /* Nope, release it */
1288 }
1289
1290 if(fsv) { /* Are we sticking any vector on this one? */
1291 if(pfsv) pfsv->save_hdr.save_prev = (addr64_t)fsv; /* Yes, chain us to the end or */
1292 else act->mact.curctx->FPUsave = fsv; /* to the start if the only one */
1293
1294 fsv->save_hdr.save_misc2 = 0; /* Eye catcher for debug */
1295 fsv->save_hdr.save_misc3 = 0; /* Eye catcher for debug */
1296 fsv->save_hdr.save_act = act; /* Set us as owner */
1297 }
1298
1299 }
1300
1301
1302
1303 /*
1304 * Releases saved context. We need this because the saved context is opague.
1305 * be adjusted when these contexts are attached to a new activation.
1306 *
1307 */
1308
1309 void act_thread_cfree(void *ctx) {
1310
1311 savearea *sv, *osv;
1312 savearea_fpu *fsv, *ofsv;
1313 savearea_vec *vsv, *ovsv, *pvsv;
1314
1315 sv = (savearea *)ctx; /* Make this easier for C */
1316
1317 fsv = (savearea_fpu *)sv->save_hdr.save_misc0; /* Get a possible floating point savearea */
1318 vsv = (savearea_vec *)sv->save_hdr.save_misc1; /* Get a possible vector savearea */
1319
1320 if((sv->save_hdr.save_misc2 != 0xDEBB1ED0) || (sv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1321 panic("act_thread_cfree: attempt to detatch invalid general context savearea - %08X\n", sv); /* Die */
1322 }
1323
1324 save_release(sv); /* Toss the general savearea */
1325
1326 if(fsv) { /* See if there is any saved floating point */
1327 if((fsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (fsv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1328 panic("act_thread_cfree: attempt to detatch invalid float context savearea - %08X\n", fsv); /* Die */
1329 }
1330
1331 save_release((savearea *)fsv); /* Toss saved context */
1332 }
1333
1334 if(vsv) { /* See if there is any saved floating point */
1335 if((vsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (vsv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1336 panic("act_thread_cfree: attempt to detatch invalid vector context savearea - %08X\n", vsv); /* Die */
1337 }
1338
1339 save_release((savearea *)vsv); /* Toss saved context */
1340 }
1341
1342 return;
1343 }
1344
1345 /*
1346 * thread_enable_fpe:
1347 *
1348 * enables or disables floating point exceptions for the thread.
1349 * returns old state
1350 */
1351 int thread_enable_fpe(thread_act_t act, int onoff)
1352 {
1353 savearea *sv;
1354 unsigned int oldmsr;
1355
1356 sv = find_user_regs(act); /* Find the user registers */
1357 if(!sv) sv = get_user_regs(act); /* Didn't find any, allocate and initialize o
1358 ne */
1359
1360 oldmsr = sv->save_srr1; /* Get the old msr */
1361
1362 if(onoff) sv->save_srr1 = oldmsr | MASK(MSR_FE0) | MASK(MSR_FE1); /* Flip on precise FP exceptions */
1363 else sv->save_srr1 = oldmsr & ~(MASK(MSR_FE0) | MASK(MSR_FE1)); /* Flip on precise FP exceptions */
1364
1365 return ((oldmsr & (MASK(MSR_FE0) | MASK(MSR_FE1))) != 0); /* Return if it was enabled or not */
1366 }