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