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