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