]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/status.c
xnu-517.3.15.tar.gz
[apple/xnu.git] / osfmk / ppc / status.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
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
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
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.
1c79356b
A
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>
1c79356b
A
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
43extern unsigned int killprint;
44extern double FloatInit;
45extern unsigned long QNaNbarbarian[4];
46extern void thread_bootstrap_return(void);
0b4e3aa0
A
47extern struct Saveanchor saveanchor;
48extern int real_ncpus; /* Number of actual CPUs */
1c79356b 49
55e303ae
A
50#define USRSTACK 0xc0000000
51
1c79356b
A
52kern_return_t
53thread_userstack(
54 thread_t,
55 int,
56 thread_state_t,
57 unsigned int,
0b4e3aa0
A
58 vm_offset_t *,
59 int *
1c79356b
A
60);
61
62kern_return_t
63thread_entrypoint(
64 thread_t,
65 int,
66 thread_state_t,
67 unsigned int,
68 vm_offset_t *
69);
70
71unsigned int get_msr_exportmask(void);
72unsigned int get_msr_nbits(void);
73unsigned int get_msr_rbits(void);
55e303ae 74void ppc_checkthreadstate(void *, int);
1c79356b 75void thread_set_child(thread_act_t child, int pid);
0b4e3aa0 76void thread_set_parent(thread_act_t parent, int pid);
1c79356b
A
77
78/*
79 * Maps state flavor to number of words in the state:
80 */
81unsigned int state_count[] = {
82 /* FLAVOR_LIST */ 0,
83 PPC_THREAD_STATE_COUNT,
84 PPC_FLOAT_STATE_COUNT,
85 PPC_EXCEPTION_STATE_COUNT,
55e303ae
A
86 PPC_VECTOR_STATE_COUNT,
87 PPC_THREAD_STATE64_COUNT,
88 PPC_EXCEPTION_STATE64_COUNT,
1c79356b
A
89};
90
91/*
92 * thread_getstatus:
93 *
94 * Get the status of the specified thread.
95 */
96
97kern_return_t
55e303ae 98machine_thread_get_state(
1c79356b
A
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 */
9bccf70c
A
106 register savearea_fpu *fsv;
107 register savearea_vec *vsv;
108 savearea *genuser;
1c79356b
A
109 int i, j;
110 unsigned int vrvalidwrk;
111
112 register struct ppc_thread_state *ts;
55e303ae 113 register struct ppc_thread_state64 *xts;
1c79356b 114 register struct ppc_exception_state *es;
55e303ae 115 register struct ppc_exception_state64 *xes;
1c79356b
A
116 register struct ppc_float_state *fs;
117 register struct ppc_vector_state *vs;
118
9bccf70c 119 genuser = find_user_regs(thr_act); /* Find the current user general context for this activation */
1c79356b
A
120
121 switch (flavor) {
122
123 case THREAD_STATE_FLAVOR_LIST:
124
55e303ae 125 if (*count < 6) {
1c79356b
A
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;
55e303ae
A
132 tstate[3] = PPC_VECTOR_STATE;
133 tstate[4] = PPC_THREAD_STATE64;
134 tstate[5] = PPC_EXCEPTION_STATE64;
135 *count = 6;
1c79356b
A
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
9bccf70c
A
147 sv = genuser; /* Copy this over */
148
1c79356b 149 if(sv) { /* Is there a save area yet? */
55e303ae
A
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;
9bccf70c 188 ts->mq = 0; /* MQ register (601 only) */
55e303ae 189 ts->vrsave = (unsigned int)sv->save_vrsave; /* VRSAVE register (Altivec only) */
1c79356b 190 }
9bccf70c 191 else { /* No user state yet. Save seemingly random values. */
1c79356b 192
9bccf70c 193 for(i=0; i < 32; i+=2) { /* Fill up with defaults */
1c79356b
A
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;
9bccf70c 204 ts->vrsave = 0; /* VRSAVE register (Altivec only) */
1c79356b
A
205 }
206
9bccf70c 207 *count = PPC_THREAD_STATE_COUNT; /* Pass back the amount we actually copied */
1c79356b
A
208 return KERN_SUCCESS;
209
55e303ae
A
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
1c79356b
A
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;
55e303ae 286 sv = genuser; /* Copy this over */
1c79356b 287
1c79356b 288 if(sv) { /* See if valid state yet */
55e303ae 289 es->dar = (unsigned int)sv->save_dar;
1c79356b
A
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
55e303ae
A
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
1c79356b
A
325 case PPC_FLOAT_STATE:
326
327 if (*count < PPC_FLOAT_STATE_COUNT) {
328 return KERN_INVALID_ARGUMENT;
329 }
330
9bccf70c 331 fpu_save(thr_act->mact.curctx); /* Just in case it's live, save it */
1c79356b
A
332
333 fs = (struct ppc_float_state *) tstate; /* Point to destination */
334
55e303ae 335 fsv = find_user_fpu(thr_act); /* Get the user's fpu savearea */
1c79356b 336
9bccf70c
A
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 */
1c79356b
A
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
9bccf70c 362 vec_save(thr_act->mact.curctx); /* Just in case it's live, save it */
1c79356b
A
363
364 vs = (struct ppc_vector_state *) tstate; /* Point to destination */
365
55e303ae 366 vsv = find_user_vec(thr_act); /* Find the vector savearea */
1c79356b 367
9bccf70c 368 if(vsv) { /* See if we have any */
1c79356b 369
9bccf70c
A
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;
55e303ae 377 vs->save_vscr[3] = 0x00010000; /* Always start with Java mode off */
9bccf70c 378 }
1c79356b
A
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] =
9bccf70c 382 ((unsigned int *)&(vsv->save_vr0))[(i * 4) + j]; /* We have this register saved */
1c79356b
A
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 }
9bccf70c
A
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;
55e303ae 399 vs->save_vscr[3] = 0x00010000; /* Always start with Java mode off */
9bccf70c 400 }
1c79356b
A
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}
55e303ae
A
414/* Close cousin of machine_thread_get_state().
415 * This function is currently incomplete since we don't really need vector
416 * or FP for the core dump (the save area can be accessed directly if the
417 * user is so inclined). Also the function name is something of a misnomer,
418 * see the comment above find_kern_regs().
419 */
420
421kern_return_t
422machine_thread_get_kern_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 register struct savearea *sv; /* Pointer to the context savearea */
430 savearea *genkern;
431 int i, j;
432 unsigned int vrvalidwrk;
433
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_state64 *xes;
438
439 genkern = find_kern_regs(thr_act);
440
441 switch (flavor) {
442
443 case THREAD_STATE_FLAVOR_LIST:
444
445 if (*count < 6) {
446 return (KERN_INVALID_ARGUMENT);
447 }
448
449 tstate[0] = PPC_THREAD_STATE;
450 tstate[1] = PPC_FLOAT_STATE;
451 tstate[2] = PPC_EXCEPTION_STATE;
452 tstate[3] = PPC_VECTOR_STATE;
453 tstate[4] = PPC_THREAD_STATE64;
454 tstate[5] = PPC_EXCEPTION_STATE64;
455 *count = 6;
456
457 return KERN_SUCCESS;
458
459 case PPC_THREAD_STATE:
460
461 if (*count < PPC_THREAD_STATE_COUNT) { /* Is the count ok? */
462 return KERN_INVALID_ARGUMENT;
463 }
464
465 ts = (struct ppc_thread_state *) tstate;
466
467 sv = genkern; /* Copy this over */
468
469 if(sv) { /* Is there a save area yet? */
470 ts->r0 = (unsigned int)sv->save_r0;
471 ts->r1 = (unsigned int)sv->save_r1;
472 ts->r2 = (unsigned int)sv->save_r2;
473 ts->r3 = (unsigned int)sv->save_r3;
474 ts->r4 = (unsigned int)sv->save_r4;
475 ts->r5 = (unsigned int)sv->save_r5;
476 ts->r6 = (unsigned int)sv->save_r6;
477 ts->r7 = (unsigned int)sv->save_r7;
478 ts->r8 = (unsigned int)sv->save_r8;
479 ts->r9 = (unsigned int)sv->save_r9;
480 ts->r10 = (unsigned int)sv->save_r10;
481 ts->r11 = (unsigned int)sv->save_r11;
482 ts->r12 = (unsigned int)sv->save_r12;
483 ts->r13 = (unsigned int)sv->save_r13;
484 ts->r14 = (unsigned int)sv->save_r14;
485 ts->r15 = (unsigned int)sv->save_r15;
486 ts->r16 = (unsigned int)sv->save_r16;
487 ts->r17 = (unsigned int)sv->save_r17;
488 ts->r18 = (unsigned int)sv->save_r18;
489 ts->r19 = (unsigned int)sv->save_r19;
490 ts->r20 = (unsigned int)sv->save_r20;
491 ts->r21 = (unsigned int)sv->save_r21;
492 ts->r22 = (unsigned int)sv->save_r22;
493 ts->r23 = (unsigned int)sv->save_r23;
494 ts->r24 = (unsigned int)sv->save_r24;
495 ts->r25 = (unsigned int)sv->save_r25;
496 ts->r26 = (unsigned int)sv->save_r26;
497 ts->r27 = (unsigned int)sv->save_r27;
498 ts->r28 = (unsigned int)sv->save_r28;
499 ts->r29 = (unsigned int)sv->save_r29;
500 ts->r30 = (unsigned int)sv->save_r30;
501 ts->r31 = (unsigned int)sv->save_r31;
502 ts->cr = (unsigned int)sv->save_cr;
503 ts->xer = (unsigned int)sv->save_xer;
504 ts->lr = (unsigned int)sv->save_lr;
505 ts->ctr = (unsigned int)sv->save_ctr;
506 ts->srr0 = (unsigned int)sv->save_srr0;
507 ts->srr1 = (unsigned int)sv->save_srr1;
508 ts->mq = 0; /* MQ register (601 only) */
509 ts->vrsave = (unsigned int)sv->save_vrsave; /* VRSAVE register (Altivec only) */
510 }
511 else { /* No state yet. Save seemingly random values. */
512
513 for(i=0; i < 32; i+=2) { /* Fill up with defaults */
514 ((unsigned int *)&ts->r0)[i] = ((unsigned int *)&FloatInit)[0];
515 ((unsigned int *)&ts->r0)[i+1] = ((unsigned int *)&FloatInit)[1];
516 }
517 ts->cr = 0;
518 ts->xer = 0;
519 ts->lr = ((unsigned int *)&FloatInit)[0];
520 ts->ctr = ((unsigned int *)&FloatInit)[1];
521 ts->srr0 = ((unsigned int *)&FloatInit)[0];
522 ts->srr1 = MSR_EXPORT_MASK_SET;
523 ts->mq = 0;
524 ts->vrsave = 0; /* VRSAVE register (Altivec only) */
525 }
526
527 *count = PPC_THREAD_STATE_COUNT; /* Pass back the amount we actually copied */
528 return KERN_SUCCESS;
529
530
531 case PPC_THREAD_STATE64:
532
533 if (*count < PPC_THREAD_STATE64_COUNT) { /* Is the count ok? */
534 return KERN_INVALID_ARGUMENT;
535 }
536
537 xts = (struct ppc_thread_state64 *) tstate;
538
539 sv = genkern; /* Copy this over */
540
541 if(sv) { /* Is there a save area yet? */
542 xts->r0 = sv->save_r0;
543 xts->r1 = sv->save_r1;
544 xts->r2 = sv->save_r2;
545 xts->r3 = sv->save_r3;
546 xts->r4 = sv->save_r4;
547 xts->r5 = sv->save_r5;
548 xts->r6 = sv->save_r6;
549 xts->r7 = sv->save_r7;
550 xts->r8 = sv->save_r8;
551 xts->r9 = sv->save_r9;
552 xts->r10 = sv->save_r10;
553 xts->r11 = sv->save_r11;
554 xts->r12 = sv->save_r12;
555 xts->r13 = sv->save_r13;
556 xts->r14 = sv->save_r14;
557 xts->r15 = sv->save_r15;
558 xts->r16 = sv->save_r16;
559 xts->r17 = sv->save_r17;
560 xts->r18 = sv->save_r18;
561 xts->r19 = sv->save_r19;
562 xts->r20 = sv->save_r20;
563 xts->r21 = sv->save_r21;
564 xts->r22 = sv->save_r22;
565 xts->r23 = sv->save_r23;
566 xts->r24 = sv->save_r24;
567 xts->r25 = sv->save_r25;
568 xts->r26 = sv->save_r26;
569 xts->r27 = sv->save_r27;
570 xts->r28 = sv->save_r28;
571 xts->r29 = sv->save_r29;
572 xts->r30 = sv->save_r30;
573 xts->r31 = sv->save_r31;
574 xts->cr = sv->save_cr;
575 xts->xer = sv->save_xer;
576 xts->lr = sv->save_lr;
577 xts->ctr = sv->save_ctr;
578 xts->srr0 = sv->save_srr0;
579 xts->srr1 = sv->save_srr1;
580 xts->vrsave = sv->save_vrsave; /* VRSAVE register (Altivec only) */
581 }
582 else { /* No user state yet. Save seemingly random values. */
583
584 for(i=0; i < 32; i++) { /* Fill up with defaults */
585 ((unsigned long long *)&xts->r0)[i] = ((unsigned long long *)&FloatInit)[0];
586 }
587 xts->cr = 0;
588 xts->xer = 0;
589 xts->lr = ((unsigned long long *)&FloatInit)[0];
590 xts->ctr = ((unsigned long long *)&FloatInit)[0];
591 xts->srr0 = ((unsigned long long *)&FloatInit)[0];
592 xts->srr1 = MSR_EXPORT_MASK_SET;
593 xts->vrsave = 0; /* VRSAVE register (Altivec only) */
594 }
595
596 *count = PPC_THREAD_STATE64_COUNT; /* Pass back the amount we actually copied */
597 return KERN_SUCCESS;
598
599 case PPC_EXCEPTION_STATE:
600
601 if (*count < PPC_EXCEPTION_STATE_COUNT) {
602 return KERN_INVALID_ARGUMENT;
603 }
604
605 es = (struct ppc_exception_state *) tstate;
606 sv = genkern; /* Copy this over */
607
608 if(sv) { /* See if valid state yet */
609 es->dar = (unsigned int)sv->save_dar;
610 es->dsisr = sv->save_dsisr;
611 es->exception = sv->save_exception;
612 }
613 else { /* Nope, not yet */
614 es->dar = 0;
615 es->dsisr = 0;
616 es->exception = ((unsigned int *)&FloatInit)[0];
617 }
618
619 *count = PPC_EXCEPTION_STATE_COUNT;
620 return KERN_SUCCESS;
621
622 case PPC_EXCEPTION_STATE64:
623
624 if (*count < PPC_EXCEPTION_STATE64_COUNT) {
625 return KERN_INVALID_ARGUMENT;
626 }
627
628 xes = (struct ppc_exception_state64 *) tstate;
629 sv = genkern; /* Copy this over */
630
631 if(sv) { /* See if valid state yet */
632 xes->dar = sv->save_dar;
633 xes->dsisr = sv->save_dsisr;
634 xes->exception = sv->save_exception;
635 }
636 else { /* Nope, not yet */
637 xes->dar = 0;
638 xes->dsisr = 0;
639 xes->exception = ((unsigned int *)&FloatInit)[0];
640 }
641
642 *count = PPC_EXCEPTION_STATE64_COUNT;
643 return KERN_SUCCESS;
644
645 default:
646 return KERN_INVALID_ARGUMENT;
647 }
648}
1c79356b
A
649
650
651/*
652 * thread_setstatus:
653 *
654 * Set the status of the specified thread.
655 */
656kern_return_t
55e303ae 657machine_thread_set_state(
1c79356b
A
658 thread_act_t thr_act,
659 thread_flavor_t flavor,
660 thread_state_t tstate,
661 mach_msg_type_number_t count)
662{
663
9bccf70c
A
664 savearea *sv, *genuser;
665 savearea_fpu *fsv, *fsvn, *fsvo;
666 savearea_vec *vsv, *vsvn, *vsvo;
667 unsigned int i;
668 int clgn;
1c79356b 669 register struct ppc_thread_state *ts;
55e303ae 670 register struct ppc_thread_state64 *xts;
1c79356b 671 register struct ppc_exception_state *es;
55e303ae 672 register struct ppc_exception_state *xes;
1c79356b
A
673 register struct ppc_float_state *fs;
674 register struct ppc_vector_state *vs;
1c79356b 675
1c79356b
A
676// dbgTrace((unsigned int)thr_act, (unsigned int)sv, flavor); /* (TEST/DEBUG) */
677
678 clgn = count; /* Get the count */
679
680 switch (flavor) { /* Validate the count before we do anything else */
681 case PPC_THREAD_STATE:
682
683 if (clgn < PPC_THREAD_STATE_COUNT) { /* Is it too short? */
684 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
685 }
1c79356b 686 break;
55e303ae
A
687
688 case PPC_THREAD_STATE64:
689
690 if (clgn < PPC_THREAD_STATE64_COUNT) { /* Is it too short? */
691 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
692 }
693 break;
1c79356b
A
694
695 case PPC_EXCEPTION_STATE:
696
697 if (clgn < PPC_EXCEPTION_STATE_COUNT) { /* Is it too short? */
698 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
699 }
700
55e303ae
A
701 case PPC_EXCEPTION_STATE64:
702
703 if (clgn < PPC_EXCEPTION_STATE64_COUNT) { /* Is it too short? */
704 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
705 }
706
1c79356b
A
707 break;
708
709 case PPC_FLOAT_STATE:
710
711 if (clgn < PPC_FLOAT_STATE_COUNT) { /* Is it too short? */
712 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
713 }
714
1c79356b
A
715 break;
716
717
718 case PPC_VECTOR_STATE:
719
720 if (clgn < PPC_VECTOR_STATE_COUNT) { /* Is it too short? */
721 return KERN_INVALID_ARGUMENT; /* Yeah, just leave... */
722 }
723
1c79356b
A
724 break;
725
726 default:
727 return KERN_INVALID_ARGUMENT;
728 }
1c79356b 729
9bccf70c
A
730 genuser = get_user_regs(thr_act); /* Find or allocate and initialize one */
731
1c79356b
A
732 switch (flavor) {
733
734 case PPC_THREAD_STATE:
1c79356b
A
735
736 ts = (struct ppc_thread_state *)tstate;
1c79356b 737
55e303ae
A
738 genuser->save_r0 = (uint64_t)ts->r0;
739 genuser->save_r1 = (uint64_t)ts->r1;
740 genuser->save_r2 = (uint64_t)ts->r2;
741 genuser->save_r3 = (uint64_t)ts->r3;
742 genuser->save_r4 = (uint64_t)ts->r4;
743 genuser->save_r5 = (uint64_t)ts->r5;
744 genuser->save_r6 = (uint64_t)ts->r6;
745 genuser->save_r7 = (uint64_t)ts->r7;
746 genuser->save_r8 = (uint64_t)ts->r8;
747 genuser->save_r9 = (uint64_t)ts->r9;
748 genuser->save_r10 = (uint64_t)ts->r10;
749 genuser->save_r11 = (uint64_t)ts->r11;
750 genuser->save_r12 = (uint64_t)ts->r12;
751 genuser->save_r13 = (uint64_t)ts->r13;
752 genuser->save_r14 = (uint64_t)ts->r14;
753 genuser->save_r15 = (uint64_t)ts->r15;
754 genuser->save_r16 = (uint64_t)ts->r16;
755 genuser->save_r17 = (uint64_t)ts->r17;
756 genuser->save_r18 = (uint64_t)ts->r18;
757 genuser->save_r19 = (uint64_t)ts->r19;
758 genuser->save_r20 = (uint64_t)ts->r20;
759 genuser->save_r21 = (uint64_t)ts->r21;
760 genuser->save_r22 = (uint64_t)ts->r22;
761 genuser->save_r23 = (uint64_t)ts->r23;
762 genuser->save_r24 = (uint64_t)ts->r24;
763 genuser->save_r25 = (uint64_t)ts->r25;
764 genuser->save_r26 = (uint64_t)ts->r26;
765 genuser->save_r27 = (uint64_t)ts->r27;
766 genuser->save_r28 = (uint64_t)ts->r28;
767 genuser->save_r29 = (uint64_t)ts->r29;
768 genuser->save_r30 = (uint64_t)ts->r30;
769 genuser->save_r31 = (uint64_t)ts->r31;
770
771 genuser->save_cr = ts->cr;
772 genuser->save_xer = (uint64_t)ts->xer;
773 genuser->save_lr = (uint64_t)ts->lr;
774 genuser->save_ctr = (uint64_t)ts->ctr;
775 genuser->save_srr0 = (uint64_t)ts->srr0;
776 genuser->save_vrsave = ts->vrsave; /* VRSAVE register (Altivec only) */
777
778 genuser->save_srr1 = MSR_PREPARE_FOR_IMPORT(genuser->save_srr1, ts->srr1); /* Set the bits we can change */
779
780 genuser->save_srr1 |= MSR_EXPORT_MASK_SET;
781
782 genuser->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make sure we don't enable the floating point unit */
783
784 return KERN_SUCCESS;
785
786
787 case PPC_THREAD_STATE64:
9bccf70c 788
55e303ae
A
789 xts = (struct ppc_thread_state64 *)tstate;
790
791 genuser->save_r0 = xts->r0;
792 genuser->save_r1 = xts->r1;
793 genuser->save_r2 = xts->r2;
794 genuser->save_r3 = xts->r3;
795 genuser->save_r4 = xts->r4;
796 genuser->save_r5 = xts->r5;
797 genuser->save_r6 = xts->r6;
798 genuser->save_r7 = xts->r7;
799 genuser->save_r8 = xts->r8;
800 genuser->save_r9 = xts->r9;
801 genuser->save_r10 = xts->r10;
802 genuser->save_r11 = xts->r11;
803 genuser->save_r12 = xts->r12;
804 genuser->save_r13 = xts->r13;
805 genuser->save_r14 = xts->r14;
806 genuser->save_r15 = xts->r15;
807 genuser->save_r16 = xts->r16;
808 genuser->save_r17 = xts->r17;
809 genuser->save_r18 = xts->r18;
810 genuser->save_r19 = xts->r19;
811 genuser->save_r20 = xts->r20;
812 genuser->save_r21 = xts->r21;
813 genuser->save_r22 = xts->r22;
814 genuser->save_r23 = xts->r23;
815 genuser->save_r24 = xts->r24;
816 genuser->save_r25 = xts->r25;
817 genuser->save_r26 = xts->r26;
818 genuser->save_r27 = xts->r27;
819 genuser->save_r28 = xts->r28;
820 genuser->save_r29 = xts->r29;
821 genuser->save_r30 = xts->r30;
822 genuser->save_r31 = xts->r31;
823
824 genuser->save_cr = xts->cr;
825 genuser->save_xer = xts->xer;
826 genuser->save_lr = xts->lr;
827 genuser->save_ctr = xts->ctr;
828 genuser->save_srr0 = xts->srr0;
829 genuser->save_vrsave = xts->vrsave; /* VRSAVE register (Altivec only) */
830
831 genuser->save_srr1 = MSR_PREPARE_FOR_IMPORT(genuser->save_srr1, xts->srr1); /* Set the bits we can change */
d7e50217 832
55e303ae
A
833 genuser->save_srr1 |= MSR_EXPORT_MASK_SET;
834
835 genuser->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make sure we don't enable the floating point unit */
836
837 return KERN_SUCCESS;
838
839
840 case PPC_EXCEPTION_STATE:
841
1c79356b
A
842 es = (struct ppc_exception_state *) tstate;
843
55e303ae 844 genuser->save_dar = (uint64_t)es->dar;
9bccf70c
A
845 genuser->save_dsisr = es->dsisr;
846 genuser->save_exception = es->exception;
1c79356b
A
847
848 return KERN_SUCCESS;
849
55e303ae
A
850/*
851 * It's pretty worthless to try to change this stuff, but we'll do it anyway.
852 */
853
854 case PPC_EXCEPTION_STATE64:
855
856 xes = (struct ppc_exception_state *) tstate;
857
858 genuser->save_dar = xes->dar;
859 genuser->save_dsisr = xes->dsisr;
860 genuser->save_exception = xes->exception;
861
862 return KERN_SUCCESS;
863
1c79356b
A
864 case PPC_FLOAT_STATE:
865
9bccf70c 866 toss_live_fpu(thr_act->mact.curctx); /* Toss my floating point if live anywhere */
1c79356b 867
9bccf70c
A
868 fsv = find_user_fpu(thr_act); /* Get the user's floating point context */
869
870 if(!fsv) { /* Do we have one yet? */
871 fsv = (savearea_fpu *)save_alloc(); /* If we still don't have one, get a new one */
872 fsv->save_hdr.save_flags = (fsv->save_hdr.save_flags & ~SAVtype) | (SAVfloat << SAVtypeshft); /* Mark as in use as float */
55e303ae 873 fsv->save_hdr.save_act = (struct thread_activation *)thr_act; /* Point to the activation */
9bccf70c
A
874 fsv->save_hdr.save_prev = 0; /* Mark no more */
875 fsv->save_hdr.save_level = 0; /* Mark user state */
1c79356b 876
9bccf70c
A
877 if(!thr_act->mact.curctx->FPUsave) thr_act->mact.curctx->FPUsave = fsv; /* If no floating point, chain us first */
878 else {
879
880 fsvn = fsvo = thr_act->mact.curctx->FPUsave; /* Remember first one */
1c79356b 881
9bccf70c
A
882 while (fsvn) { /* Go until we hit the end */
883 fsvo = fsvn; /* Remember the previous one */
55e303ae 884 fsvn = CAST_DOWN(savearea_fpu *, fsvo->save_hdr.save_prev); /* Skip on to the next */
1c79356b
A
885 }
886
55e303ae 887 fsvo->save_hdr.save_prev = (addr64_t)((uintptr_t)fsv); /* Queue us on in */
1c79356b 888 }
9bccf70c 889
1c79356b
A
890 }
891
892 fs = (struct ppc_float_state *) tstate; /* Point to source */
893
894
9bccf70c 895 bcopy((char *)fs, (char *)&fsv->save_fp0, 32*8); /* Move in the 32 registers */
1c79356b 896
9bccf70c 897 genuser->save_fpscr = fs->fpscr; /* Copy the fpscr value to normal */
1c79356b 898
1c79356b
A
899 return KERN_SUCCESS;
900
901
902 case PPC_VECTOR_STATE:
903
9bccf70c 904 toss_live_vec(thr_act->mact.curctx); /* Toss my vector if live anywhere */
1c79356b 905
9bccf70c
A
906 vsv = find_user_vec(thr_act); /* Get the user's vector context */
907
908 if(!vsv) { /* Do we have one yet? */
909 vsv = (savearea_vec *)save_alloc(); /* If we still don't have one, get a new one */
910 vsv->save_hdr.save_flags = (vsv->save_hdr.save_flags & ~SAVtype) | (SAVvector << SAVtypeshft); /* Mark as in use as vector */
55e303ae 911 vsv->save_hdr.save_act = (struct thread_activation *)thr_act; /* Point to the activation */
9bccf70c
A
912 vsv->save_hdr.save_prev = 0; /* Mark no more */
913 vsv->save_hdr.save_level = 0; /* Mark user state */
1c79356b 914
9bccf70c
A
915 if(!thr_act->mact.curctx->VMXsave) thr_act->mact.curctx->VMXsave = vsv; /* If no vector, chain us first */
916 else {
917
918 vsvn = vsvo = thr_act->mact.curctx->VMXsave; /* Remember first one */
1c79356b 919
9bccf70c
A
920 while (vsvn) { /* Go until we hit the end */
921 vsvo = vsvn; /* Remember the previous one */
55e303ae 922 vsvn = CAST_DOWN(savearea_vec *, vsvo->save_hdr.save_prev); /* Skip on to the next */
1c79356b
A
923 }
924
55e303ae 925 vsvo->save_hdr.save_prev = (addr64_t)((uintptr_t)vsv); /* Queue us on in */
1c79356b 926 }
9bccf70c 927
1c79356b 928 }
1c79356b
A
929
930 vs = (struct ppc_vector_state *) tstate; /* Point to source */
931
9bccf70c
A
932 bcopy((char *)vs, (char *)&vsv->save_vr0, 32*16); /* 32 registers plus status and validity and pad */
933 vsv->save_vrvalid = vs->save_vrvalid; /* Set validity bits */
934
935
936 for(i = 0; i < 4; i++) genuser->save_vscr[i] = vs->save_vscr[i]; /* Set value for vscr */
1c79356b
A
937
938 return KERN_SUCCESS;
939
940
941 default:
942 return KERN_INVALID_ARGUMENT;
943 }
944}
945
946/*
947 * Duplicates the context of one thread into a new one.
9bccf70c 948 * The new thread is assumed to be new and have no user state contexts except maybe a general one.
1c79356b
A
949 * We also assume that the old thread can't be running anywhere.
950 *
951 * We're only going to be duplicating user context here. That means that we will have to
952 * eliminate any floating point or vector kernel contexts and carry across the user state ones.
1c79356b
A
953 */
954
55e303ae 955kern_return_t machine_thread_dup(thread_act_t self, thread_act_t target) {
1c79356b 956
9bccf70c
A
957 savearea *sv, *osv;
958 savearea_fpu *fsv, *fsvn;
959 savearea_vec *vsv, *vsvn;
1c79356b
A
960 unsigned int spc, i, *srs;
961
55e303ae
A
962 fpu_save(self->mact.curctx); /* Make certain floating point state is all saved */
963 vec_save(self->mact.curctx); /* Make certain the vector state is all saved */
1c79356b 964
55e303ae 965 sv = get_user_regs(target); /* Allocate and initialze context in the new activation */
1c79356b 966
55e303ae
A
967 osv = find_user_regs(self); /* Find the original context */
968 if(!osv)
969 return (KERN_FAILURE);
1c79356b 970
9bccf70c
A
971 bcopy((char *)((unsigned int)osv + sizeof(savearea_comm)), /* Copy everything but the headers */
972 (char *)((unsigned int)sv + sizeof(savearea_comm)),
973 sizeof(struct savearea) - sizeof(savearea_comm));
974
975 sv->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make certain that floating point and vector are turned off */
1c79356b 976
55e303ae 977 fsv = find_user_fpu(self); /* Get any user floating point */
1c79356b 978
55e303ae 979 target->mact.curctx->FPUsave = 0; /* Assume no floating point */
1c79356b 980
9bccf70c
A
981 if(fsv) { /* Did we find one? */
982 fsvn = (savearea_fpu *)save_alloc(); /* If we still don't have one, get a new one */
983 fsvn->save_hdr.save_flags = (fsvn->save_hdr.save_flags & ~SAVtype) | (SAVfloat << SAVtypeshft); /* Mark as in use as float */
55e303ae 984 fsvn->save_hdr.save_act = (struct thread_activation *)target; /* Point to the activation */
9bccf70c
A
985 fsvn->save_hdr.save_prev = 0; /* Mark no more */
986 fsvn->save_hdr.save_level = 0; /* Mark user state */
1c79356b 987
55e303ae 988 target->mact.curctx->FPUsave = fsvn; /* Chain in the floating point */
9bccf70c
A
989
990 bcopy((char *)((unsigned int)fsv + sizeof(savearea_comm)), /* Copy everything but the headers */
991 (char *)((unsigned int)fsvn + sizeof(savearea_comm)),
992 sizeof(struct savearea) - sizeof(savearea_comm));
1c79356b 993 }
9bccf70c 994
55e303ae 995 vsv = find_user_vec(self); /* Get any user vector */
1c79356b 996
55e303ae 997 target->mact.curctx->VMXsave = 0; /* Assume no vector */
9bccf70c
A
998
999 if(vsv) { /* Did we find one? */
1000 vsvn = (savearea_vec *)save_alloc(); /* If we still don't have one, get a new one */
1001 vsvn->save_hdr.save_flags = (vsvn->save_hdr.save_flags & ~SAVtype) | (SAVvector << SAVtypeshft); /* Mark as in use as float */
55e303ae 1002 vsvn->save_hdr.save_act = (struct thread_activation *)target; /* Point to the activation */
9bccf70c
A
1003 vsvn->save_hdr.save_prev = 0; /* Mark no more */
1004 vsvn->save_hdr.save_level = 0; /* Mark user state */
1005
55e303ae 1006 target->mact.curctx->VMXsave = vsvn; /* Chain in the floating point */
9bccf70c
A
1007
1008 bcopy((char *)((unsigned int)vsv + sizeof(savearea_comm)), /* Copy everything but the headers */
1009 (char *)((unsigned int)vsvn + sizeof(savearea_comm)),
1010 sizeof(struct savearea) - sizeof(savearea_comm));
1c79356b
A
1011 }
1012
55e303ae 1013 return (KERN_SUCCESS);
1c79356b
A
1014}
1015
1016/*
1017 * Initializes a fresh set of user state values. If there is no user state context,
9bccf70c
A
1018 * one is created. Floats and VMX are not created.
1019 *
1020 * We only set initial values if there was no context found.
1c79356b
A
1021 */
1022
9bccf70c 1023savearea *get_user_regs(thread_act_t act) {
1c79356b
A
1024
1025 savearea *sv, *osv;
1026 unsigned int spc, i, *srs;
1027
55e303ae
A
1028 if (act->mact.upcb)
1029 return act->mact.upcb;
1030
9bccf70c 1031 sv = act->mact.pcb; /* Get the top savearea on the stack */
1c79356b
A
1032 osv = 0; /* Set no user savearea yet */
1033
1034 while(sv) { /* Find the user context */
1c79356b 1035 osv = sv; /* Save the last one */
55e303ae 1036 sv = CAST_DOWN(savearea *, sv->save_hdr.save_prev); /* Get the previous context */
1c79356b
A
1037 }
1038
9bccf70c
A
1039 sv = save_alloc(); /* Get one */
1040 sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use as general */
55e303ae 1041 sv->save_hdr.save_act = (struct thread_activation *)act; /* Point to the activation */
9bccf70c
A
1042 sv->save_hdr.save_prev = 0; /* Mark no more */
1043 sv->save_hdr.save_level = 0; /* Mark user state */
1044
1045 if(osv) { /* Did we already have one? */
55e303ae 1046 osv->save_hdr.save_prev = (addr64_t)((uintptr_t)sv); /* Chain us on the end */
9bccf70c
A
1047 }
1048 else { /* We are the first */
1049 act->mact.pcb = sv; /* Put it there */
1c79356b 1050 }
55e303ae 1051 act->mact.upcb = sv; /* Set user pcb */
1c79356b
A
1052
1053 for(i=0; i < 32; i+=2) { /* Fill up with defaults */
1054 ((unsigned int *)&sv->save_r0)[i] = ((unsigned int *)&FloatInit)[0];
1055 ((unsigned int *)&sv->save_r0)[i+1] = ((unsigned int *)&FloatInit)[1];
1056 }
1057 sv->save_cr = 0;
1058 sv->save_xer = 0;
55e303ae
A
1059 sv->save_lr = (uint64_t)FloatInit;
1060 sv->save_ctr = (uint64_t)FloatInit;
1061 sv->save_srr0 = (uint64_t)FloatInit;
1062 sv->save_srr1 = (uint64_t)MSR_EXPORT_MASK_SET;
9bccf70c
A
1063
1064 sv->save_fpscr = 0; /* Clear all floating point exceptions */
1065
1066 sv->save_vrsave = 0; /* Set the vector save state */
1067 sv->save_vscr[0] = 0x00000000;
1068 sv->save_vscr[1] = 0x00000000;
1069 sv->save_vscr[2] = 0x00000000;
55e303ae 1070 sv->save_vscr[3] = 0x00010000; /* Disable java mode and clear saturated */
1c79356b 1071
9bccf70c 1072 return sv; /* Bye bye... */
1c79356b
A
1073}
1074
1075/*
1076 * Find the user state context. If there is no user state context,
1077 * we just return a 0.
1078 */
1079
9bccf70c 1080savearea *find_user_regs(thread_act_t act) {
55e303ae
A
1081 return act->mact.upcb;
1082}
1c79356b 1083
55e303ae
A
1084/* The name of this call is something of a misnomer since the mact.pcb can
1085 * contain chained saveareas, but it will do for now..
1086 */
1087savearea *find_kern_regs(thread_act_t act) {
1088 return act->mact.pcb;
1c79356b
A
1089}
1090
1091/*
9bccf70c 1092 * Find the user state floating point context. If there is no user state context,
1c79356b
A
1093 * we just return a 0.
1094 */
1095
9bccf70c 1096savearea_fpu *find_user_fpu(thread_act_t act) {
1c79356b 1097
9bccf70c 1098 savearea_fpu *fsv;
1c79356b 1099
9bccf70c
A
1100 fsv = act->mact.curctx->FPUsave; /* Get the start of the floating point chain */
1101
1c79356b 1102 while(fsv) { /* Look until the end or we find it */
9bccf70c 1103 if(!(fsv->save_hdr.save_level)) break; /* Is the the user state stuff? (the level is 0 if so) */
55e303ae 1104 fsv = CAST_DOWN(savearea_fpu *, fsv->save_hdr.save_prev); /* Try the previous one */
1c79356b
A
1105 }
1106
9bccf70c
A
1107 return fsv; /* Bye bye... */
1108}
1109
1110/*
1111 * Find the user state vector context. If there is no user state context,
1112 * we just return a 0.
1113 */
1114
1115savearea_vec *find_user_vec(thread_act_t act) {
1116
1117 savearea_vec *vsv;
1118
1119 vsv = act->mact.curctx->VMXsave; /* Get the start of the vector chain */
1120
1121 while(vsv) { /* Look until the end or we find it */
1122 if(!(vsv->save_hdr.save_level)) break; /* Is the the user state stuff? (the level is 0 if so) */
55e303ae
A
1123 vsv = CAST_DOWN(savearea_vec *, vsv->save_hdr.save_prev); /* Try the previous one */
1124 }
1125
1126 return vsv; /* Bye bye... */
1127}
1128/*
1129 * Find the user state vector context for the current thread. If there is no user state context,
1130 * we just return a 0.
1131 */
1132
1133savearea_vec *find_user_vec_curr(void) {
1134
1135 savearea_vec *vsv;
1136 thread_act_t act;
1137
1138 act = current_act(); /* Get the current activation */
1139
1140 vec_save(act->mact.curctx); /* Force save if live */
1141
1142 vsv = act->mact.curctx->VMXsave; /* Get the start of the vector chain */
1143
1144 while(vsv) { /* Look until the end or we find it */
1145 if(!(vsv->save_hdr.save_level)) break; /* Is the the user state stuff? (the level is 0 if so) */
1146 vsv = CAST_DOWN(savearea_vec *, vsv->save_hdr.save_prev); /* Try the previous one */
9bccf70c
A
1147 }
1148
1149 return vsv; /* Bye bye... */
1c79356b
A
1150}
1151
1152/*
1153 * thread_userstack:
1154 *
1155 * Return the user stack pointer from the machine
1156 * dependent thread state info.
1157 */
1158kern_return_t
1159thread_userstack(
1160 thread_t thread,
1161 int flavor,
1162 thread_state_t tstate,
1163 unsigned int count,
0b4e3aa0
A
1164 vm_offset_t *user_stack,
1165 int *customstack
1c79356b
A
1166)
1167{
1168 struct ppc_thread_state *state;
1169
55e303ae
A
1170 /*
1171 * Set a default.
1172 */
1173 if (*user_stack == 0)
1174 *user_stack = USRSTACK;
1175 if (customstack)
1176 *customstack = 0;
1c79356b
A
1177
1178 switch (flavor) {
1179 case PPC_THREAD_STATE:
1180 if (count < PPC_THREAD_STATE_COUNT)
1181 return (KERN_INVALID_ARGUMENT);
1182
1183 state = (struct ppc_thread_state *) tstate;
1184
55e303ae
A
1185 /*
1186 * If a valid user stack is specified, use it.
1187 */
1188 *user_stack = state->r1 ? state->r1: USRSTACK;
0b4e3aa0 1189
55e303ae
A
1190 if (customstack && state->r1)
1191 *customstack = 1;
1192
1c79356b
A
1193 break;
1194 default :
1195 return (KERN_INVALID_ARGUMENT);
1196 }
1197
1198 return (KERN_SUCCESS);
1199}
1200
9bccf70c
A
1201
1202/*
1203 * thread_setuserstack:
1204 *
1205 * Sets the user stack pointer into the machine
1206 * dependent thread state info.
1207 */
55e303ae 1208void thread_setuserstack(thread_act_t act, unsigned int user_stack)
9bccf70c
A
1209{
1210 savearea *sv;
1211
1212 sv = get_user_regs(act); /* Get the user state registers */
1213
55e303ae 1214 sv->save_r1 = (uint64_t)user_stack;
9bccf70c
A
1215
1216 return;
1217}
1218
1219/*
1220 * thread_adjuserstack:
1221 *
1222 * Returns the adjusted user stack pointer from the machine
1223 * dependent thread state info.
1224 */
55e303ae 1225unsigned int thread_adjuserstack(thread_act_t act, int adjust)
9bccf70c
A
1226{
1227 savearea *sv;
1228
1229 sv = get_user_regs(act); /* Get the user state registers */
1230
1231 sv->save_r1 += adjust; /* Adjust the stack */
1232
55e303ae 1233 return (unsigned int)sv->save_r1; /* Return the adjusted stack */
9bccf70c
A
1234
1235}
1236
1237/*
1238 * thread_setentrypoint:
1239 *
1240 * Sets the user PC into the machine
1241 * dependent thread state info.
1242 */
1243
55e303ae 1244void thread_setentrypoint(thread_act_t act, unsigned int entry)
9bccf70c
A
1245{
1246 savearea *sv;
1247
1248 sv = get_user_regs(act); /* Get the user state registers */
1249
55e303ae 1250 sv->save_srr0 = (uint64_t)entry;
9bccf70c
A
1251
1252 return;
1253}
1254
1c79356b
A
1255kern_return_t
1256thread_entrypoint(
1257 thread_t thread,
1258 int flavor,
1259 thread_state_t tstate,
1260 unsigned int count,
1261 vm_offset_t *entry_point
1262)
1263{
1264 struct ppc_thread_state *state;
1265
1266 /*
1267 * Set a default.
1268 */
1269 if (*entry_point == 0)
1270 *entry_point = VM_MIN_ADDRESS;
1271
1272 switch (flavor) {
1273
1274 case PPC_THREAD_STATE:
1275 if (count < PPC_THREAD_STATE_COUNT)
1276 return (KERN_INVALID_ARGUMENT);
1277
1278 state = (struct ppc_thread_state *) tstate;
1279
1280 /*
1281 * If a valid entry point is specified, use it.
1282 */
1283 *entry_point = state->srr0 ? state->srr0: VM_MIN_ADDRESS;
1284 break;
1285 default:
1286 return (KERN_INVALID_ARGUMENT);
1287 }
1288
1289 return (KERN_SUCCESS);
1290}
1291
1292unsigned int get_msr_exportmask(void)
1293{
1294 return (MSR_EXPORT_MASK_SET);
1295}
1296
1297unsigned int get_msr_nbits(void)
1298{
1299 return (MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1300}
1301unsigned int get_msr_rbits(void)
1302{
1303 return (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1304}
1305
55e303ae
A
1306void ppc_checkthreadstate(void * tsptr, int flavor)
1307{
1308 if (flavor == PPC_THREAD_STATE64) {
1309 struct ppc_thread_state64 *ts64 =(struct ppc_thread_state64 *)tsptr;
1310
1311 /* Make sure naughty bits are off and necessary bits are on */
1312 ts64->srr1 &= ~(MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1313 ts64->srr1 |= (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1314 } else {
1315 struct ppc_thread_state *ts =(struct ppc_thread_state *)tsptr;
1316
1317 /* Make sure naughty bits are off and necessary bits are on */
1318 ts->srr1 &= ~(MASK(MSR_POW)|MASK(MSR_ILE)|MASK(MSR_IP)|MASK(MSR_LE));
1319 ts->srr1 |= (MASK(MSR_PR)|MASK(MSR_ME)|MASK(MSR_IR)|MASK(MSR_DR)|MASK(MSR_EE));
1320 }
1321 return;
1322}
1323
1c79356b
A
1324void thread_set_child(thread_act_t child, int pid)
1325{
9bccf70c 1326 struct savearea *child_state;
1c79356b 1327
9bccf70c 1328 child_state = get_user_regs(child);
1c79356b 1329
55e303ae
A
1330 child_state->save_r3 = (uint_t)pid;
1331 child_state->save_r4 = 1ULL;
1c79356b 1332}
0b4e3aa0
A
1333void thread_set_parent(thread_act_t parent, int pid)
1334{
9bccf70c 1335 struct savearea *parent_state;
0b4e3aa0 1336
9bccf70c 1337 parent_state = get_user_regs(parent);
0b4e3aa0 1338
55e303ae 1339 parent_state->save_r3 = (uint64_t)pid;
9bccf70c 1340 parent_state->save_r4 = 0;
0b4e3aa0
A
1341}
1342
1343/*
1344 * Saves the complete context (general, floating point, and vector) of the current activation.
9bccf70c
A
1345 * We will collect everything into an opaque block of 1 to 3 saveareas and pass back a
1346 * pointer to that.
0b4e3aa0
A
1347 *
1348 * The savearea is made to look like it belongs to the source activation. This needs to
1349 * be adjusted when these contexts are attached to a new activation.
1350 *
1351 */
1352
1353void *act_thread_csave(void) {
1354
9bccf70c
A
1355 savearea *sv, *osv;
1356 savearea_fpu *fsv, *ofsv;
1357 savearea_vec *vsv, *ovsv;
0b4e3aa0
A
1358 unsigned int spc, i, *srs;
1359
1360 thread_act_t act;
1361
9bccf70c
A
1362 act = current_act(); /* Find ourselves */
1363
1364 fpu_save(act->mact.curctx); /* Make certain floating point state is all saved */
1365 vec_save(act->mact.curctx); /* Make certain the vector state is all saved */
0b4e3aa0 1366
9bccf70c 1367 osv = find_user_regs(act); /* Get our savearea */
0b4e3aa0 1368
9bccf70c
A
1369 if(!osv) {
1370 panic("act_thread_csave: attempting to preserve the context of an activation with none (%08X)\n", act);
1371 }
0b4e3aa0 1372
9bccf70c
A
1373 sv = save_alloc(); /* Get a fresh save area to save into */
1374 sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use as general */
55e303ae 1375 sv->save_hdr.save_act = (struct thread_activation *)act; /* Point to the activation */
9bccf70c
A
1376 sv->save_hdr.save_prev = 0; /* Mark no more */
1377 sv->save_hdr.save_level = 0; /* Mark user state */
0b4e3aa0 1378
0b4e3aa0 1379
9bccf70c
A
1380 bcopy((char *)((unsigned int)osv + sizeof(savearea_comm)), /* Copy everything but the headers */
1381 (char *)((unsigned int)sv + sizeof(savearea_comm)),
1382 sizeof(struct savearea) - sizeof(savearea_comm));
1383
1384 sv->save_srr1 &= ~(MASK(MSR_FP) | MASK(MSR_VEC)); /* Make certain that floating point and vector are turned off */
1385
1386 sv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1387 sv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
0b4e3aa0 1388
0b4e3aa0 1389
9bccf70c 1390 ofsv = find_user_fpu(act); /* Get any user floating point */
0b4e3aa0 1391
9bccf70c 1392 sv->save_hdr.save_misc0 = 0; /* Assume no floating point */
0b4e3aa0 1393
9bccf70c
A
1394 if(ofsv) { /* Did we find one? */
1395 fsv = (savearea_fpu *)save_alloc(); /* If we still don't have one, get a new one */
1396 fsv->save_hdr.save_flags = (fsv->save_hdr.save_flags & ~SAVtype) | (SAVfloat << SAVtypeshft); /* Mark as in use as float */
55e303ae 1397 fsv->save_hdr.save_act = (struct thread_activation *)act; /* Point to the activation */
9bccf70c
A
1398 fsv->save_hdr.save_prev = 0; /* Mark no more */
1399 fsv->save_hdr.save_level = 0; /* Mark user state */
1400 fsv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1401 fsv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
1402
55e303ae 1403 sv->save_hdr.save_misc0 = (uint64_t)((uintptr_t)fsv); /* Remember this one */
9bccf70c
A
1404
1405 bcopy((char *)((unsigned int)ofsv + sizeof(savearea_comm)), /* Copy everything but the headers */
1406 (char *)((unsigned int)fsv + sizeof(savearea_comm)),
1407 sizeof(struct savearea) - sizeof(savearea_comm));
0b4e3aa0 1408 }
9bccf70c
A
1409
1410 ovsv = find_user_vec(act); /* Get any user vector */
0b4e3aa0 1411
9bccf70c
A
1412 sv->save_hdr.save_misc1 = 0; /* Assume no vector */
1413
1414 if(ovsv) { /* Did we find one? */
1415 vsv = (savearea_vec *)save_alloc(); /* If we still don't have one, get a new one */
1416 vsv->save_hdr.save_flags = (vsv->save_hdr.save_flags & ~SAVtype) | (SAVvector << SAVtypeshft); /* Mark as in use as float */
55e303ae 1417 vsv->save_hdr.save_act = (struct thread_activation *)act; /* Point to the activation */
9bccf70c
A
1418 vsv->save_hdr.save_prev = 0; /* Mark no more */
1419 vsv->save_hdr.save_level = 0; /* Mark user state */
1420 vsv->save_hdr.save_misc2 = 0xDEBB1ED0; /* Eye catcher for debug */
1421 vsv->save_hdr.save_misc3 = 0xE5DA11A5; /* Eye catcher for debug */
1422
55e303ae 1423 sv->save_hdr.save_misc1 = (uint64_t)((uintptr_t)vsv); /* Chain in the floating point */
9bccf70c
A
1424
1425 bcopy((char *)((unsigned int)ovsv + sizeof(savearea_comm)), /* Copy everything but the headers */
1426 (char *)((unsigned int)vsv + sizeof(savearea_comm)),
1427 sizeof(struct savearea) - sizeof(savearea_comm));
0b4e3aa0
A
1428 }
1429
1430 return (void *)sv; /* Bye bye... */
1431}
1432
1433
1434
1435/*
1436 * Attaches saved user state context to an activation. We will replace any
1437 * user state context with what is passed in. The saved context consists of a
1438 * savearea that was setup by
1439 * We will collect everything into one savearea and pass that back.
1440 *
1441 * The savearea is made to look like it belongs to the source activation. This needs to
1442 * be adjusted when these contexts are attached to a new activation.
1443 *
1444 */
1445
1446void act_thread_catt(void *ctx) {
1447
9bccf70c
A
1448 savearea *sv, *osv, *psv;
1449 savearea_fpu *fsv, *ofsv, *pfsv;
1450 savearea_vec *vsv, *ovsv, *pvsv;
0b4e3aa0
A
1451 unsigned int spc, i, *srs;
1452 thread_act_t act;
1453
1454 sv = (savearea *)ctx; /* Make this easier for C */
1455
55e303ae
A
1456 fsv = CAST_DOWN(savearea_fpu *, sv->save_hdr.save_misc0); /* Get a possible floating point savearea */
1457 vsv = CAST_DOWN(savearea_vec *, sv->save_hdr.save_misc1); /* Get a possible vector savearea */
9bccf70c
A
1458
1459 if((sv->save_hdr.save_misc2 != 0xDEBB1ED0) || (sv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1460 panic("act_thread_catt: attempt to attach invalid general context savearea - %08X\n", sv); /* Die */
0b4e3aa0
A
1461 }
1462
9bccf70c
A
1463 if(fsv && ((fsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (fsv->save_hdr.save_misc3 != 0xE5DA11A5))) { /* See if valid savearea */
1464 panic("act_thread_catt: attempt to attach invalid float context savearea - %08X\n", fsv); /* Die */
0b4e3aa0
A
1465 }
1466
9bccf70c
A
1467 if(vsv && ((vsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (vsv->save_hdr.save_misc3 != 0xE5DA11A5))) { /* See if valid savearea */
1468 panic("act_thread_catt: attempt to attach invalid vector context savearea - %08X\n", vsv); /* Die */
1469 }
0b4e3aa0 1470
9bccf70c 1471 act = current_act(); /* Find ourselves */
0b4e3aa0 1472
9bccf70c
A
1473 toss_live_fpu(act->mact.curctx); /* Toss my floating point if live anywhere */
1474 toss_live_vec(act->mact.curctx); /* Toss my vector if live anywhere */
1475
1476 sv->save_hdr.save_misc2 = 0; /* Eye catcher for debug */
1477 sv->save_hdr.save_misc3 = 0; /* Eye catcher for debug */
55e303ae 1478 sv->save_hdr.save_act = (struct thread_activation *)act; /* Set us as owner */
0b4e3aa0 1479
9bccf70c 1480 spc = (unsigned int)act->map->pmap->space; /* Get the space we're in */
0b4e3aa0 1481
9bccf70c 1482 osv = act->mact.pcb; /* Get the top general savearea */
0b4e3aa0 1483 psv = 0;
9bccf70c
A
1484 while(osv) { /* Any saved state? */
1485 if(osv->save_srr1 & MASK(MSR_PR)) break; /* Leave if this is user state */
0b4e3aa0 1486 psv = osv; /* Save previous savearea address */
55e303ae 1487 osv = CAST_DOWN(savearea *, osv->save_hdr.save_prev); /* Get one underneath our's */
0b4e3aa0
A
1488 }
1489
1490 if(osv) { /* Did we find one? */
9bccf70c
A
1491 if(psv) psv->save_hdr.save_prev = 0; /* Yes, clear pointer to it (it should always be last) or */
1492 else act->mact.pcb = 0; /* to the start if the only one */
0b4e3aa0 1493
9bccf70c
A
1494 save_release(osv); /* Nope, release it */
1495
0b4e3aa0 1496 }
9bccf70c 1497
55e303ae 1498 if(psv) psv->save_hdr.save_prev = (addr64_t)((uintptr_t)sv); /* Chain us to the end or */
9bccf70c 1499 else act->mact.pcb = (pcb_t)sv; /* to the start if the only one */
55e303ae 1500 act->mact.upcb = (pcb_t)sv; /* Set the user pcb */
0b4e3aa0 1501
9bccf70c 1502 ovsv = act->mact.curctx->VMXsave; /* Get the top vector savearea */
0b4e3aa0 1503
9bccf70c
A
1504 pvsv = 0;
1505 while(ovsv) { /* Any VMX saved state? */
1506 if(!(ovsv->save_hdr.save_level)) break; /* Leave if this is user state */
1507 pvsv = ovsv; /* Save previous savearea address */
55e303ae 1508 ovsv = CAST_DOWN(savearea_vec *, ovsv->save_hdr.save_prev); /* Get one underneath our's */
0b4e3aa0 1509 }
9bccf70c
A
1510
1511 if(ovsv) { /* Did we find one? */
1512 if(pvsv) pvsv->save_hdr.save_prev = 0; /* Yes, clear pointer to it (it should always be last) or */
1513 else act->mact.curctx->VMXsave = 0; /* to the start if the only one */
0b4e3aa0 1514
9bccf70c 1515 save_release((savearea *)ovsv); /* Nope, release it */
0b4e3aa0
A
1516 }
1517
9bccf70c 1518 if(vsv) { /* Are we sticking any vector on this one? */
55e303ae 1519 if(pvsv) pvsv->save_hdr.save_prev = (addr64_t)((uintptr_t)vsv); /* Yes, chain us to the end or */
9bccf70c 1520 else act->mact.curctx->VMXsave = vsv; /* to the start if the only one */
0b4e3aa0 1521
9bccf70c
A
1522 vsv->save_hdr.save_misc2 = 0; /* Eye catcher for debug */
1523 vsv->save_hdr.save_misc3 = 0; /* Eye catcher for debug */
55e303ae 1524 vsv->save_hdr.save_act = (struct thread_activation *)act; /* Set us as owner */
0b4e3aa0
A
1525 }
1526
9bccf70c 1527 ofsv = act->mact.curctx->FPUsave; /* Get the top float savearea */
0b4e3aa0 1528
9bccf70c
A
1529 pfsv = 0;
1530 while(ofsv) { /* Any float saved state? */
1531 if(!(ofsv->save_hdr.save_level)) break; /* Leave if this is user state */
1532 pfsv = ofsv; /* Save previous savearea address */
55e303ae 1533 ofsv = CAST_DOWN(savearea_fpu *, ofsv->save_hdr.save_prev); /* Get one underneath our's */
0b4e3aa0
A
1534 }
1535
9bccf70c
A
1536 if(ofsv) { /* Did we find one? */
1537 if(pfsv) pfsv->save_hdr.save_prev = 0; /* Yes, clear pointer to it (it should always be last) or */
1538 else act->mact.curctx->FPUsave = 0; /* to the start if the only one */
0b4e3aa0 1539
9bccf70c 1540 save_release((savearea *)ofsv); /* Nope, release it */
0b4e3aa0
A
1541 }
1542
9bccf70c 1543 if(fsv) { /* Are we sticking any vector on this one? */
55e303ae 1544 if(pfsv) pfsv->save_hdr.save_prev = (addr64_t)((uintptr_t)fsv); /* Yes, chain us to the end or */
9bccf70c 1545 else act->mact.curctx->FPUsave = fsv; /* to the start if the only one */
0b4e3aa0 1546
9bccf70c
A
1547 fsv->save_hdr.save_misc2 = 0; /* Eye catcher for debug */
1548 fsv->save_hdr.save_misc3 = 0; /* Eye catcher for debug */
55e303ae 1549 fsv->save_hdr.save_act = (struct thread_activation *)act; /* Set us as owner */
9bccf70c
A
1550 }
1551
0b4e3aa0
A
1552}
1553
1554
1555
1556/*
1557 * Releases saved context. We need this because the saved context is opague.
1558 * be adjusted when these contexts are attached to a new activation.
1559 *
1560 */
1561
1562void act_thread_cfree(void *ctx) {
1563
9bccf70c
A
1564 savearea *sv, *osv;
1565 savearea_fpu *fsv, *ofsv;
1566 savearea_vec *vsv, *ovsv, *pvsv;
1567
1568 sv = (savearea *)ctx; /* Make this easier for C */
1569
55e303ae
A
1570 fsv = CAST_DOWN(savearea_fpu *, sv->save_hdr.save_misc0); /* Get a possible floating point savearea */
1571 vsv = CAST_DOWN(savearea_vec *, sv->save_hdr.save_misc1); /* Get a possible vector savearea */
9bccf70c
A
1572
1573 if((sv->save_hdr.save_misc2 != 0xDEBB1ED0) || (sv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1574 panic("act_thread_cfree: attempt to detatch invalid general context savearea - %08X\n", sv); /* Die */
1575 }
1576
1577 save_release(sv); /* Toss the general savearea */
1578
1579 if(fsv) { /* See if there is any saved floating point */
1580 if((fsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (fsv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1581 panic("act_thread_cfree: attempt to detatch invalid float context savearea - %08X\n", fsv); /* Die */
1582 }
1583
1584 save_release((savearea *)fsv); /* Toss saved context */
0b4e3aa0
A
1585 }
1586
9bccf70c
A
1587 if(vsv) { /* See if there is any saved floating point */
1588 if((vsv->save_hdr.save_misc2 != 0xDEBB1ED0) || (vsv->save_hdr.save_misc3 != 0xE5DA11A5)) { /* See if valid savearea */
1589 panic("act_thread_cfree: attempt to detatch invalid vector context savearea - %08X\n", vsv); /* Die */
1590 }
1591
1592 save_release((savearea *)vsv); /* Toss saved context */
1593 }
0b4e3aa0
A
1594
1595 return;
1596}
9bccf70c
A
1597
1598/*
1599 * thread_enable_fpe:
1600 *
1601 * enables or disables floating point exceptions for the thread.
1602 * returns old state
1603 */
1604int thread_enable_fpe(thread_act_t act, int onoff)
1605{
1606 savearea *sv;
1607 unsigned int oldmsr;
1608
1609 sv = find_user_regs(act); /* Find the user registers */
1610 if(!sv) sv = get_user_regs(act); /* Didn't find any, allocate and initialize o
1611ne */
1612
1613 oldmsr = sv->save_srr1; /* Get the old msr */
1614
1615 if(onoff) sv->save_srr1 = oldmsr | MASK(MSR_FE0) | MASK(MSR_FE1); /* Flip on precise FP exceptions */
1616 else sv->save_srr1 = oldmsr & ~(MASK(MSR_FE0) | MASK(MSR_FE1)); /* Flip on precise FP exceptions */
1617
1618 return ((oldmsr & (MASK(MSR_FE0) | MASK(MSR_FE1))) != 0); /* Return if it was enabled or not */
1619}