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