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