]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 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 | * |
e5568f75 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, | |
e5568f75 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 | * Copyright (c) 1990,1991,1992 The University of Utah and | |
27 | * the Center for Software Science (CSS). All rights reserved. | |
28 | * | |
29 | * Permission to use, copy, modify and distribute this software is hereby | |
30 | * granted provided that (1) source code retains these copyright, permission, | |
31 | * and disclaimer notices, and (2) redistributions including binaries | |
32 | * reproduce the notices in supporting documentation, and (3) all advertising | |
33 | * materials mentioning features or use of this software display the following | |
34 | * acknowledgement: ``This product includes software developed by the Center | |
35 | * for Software Science at the University of Utah.'' | |
36 | * | |
37 | * THE UNIVERSITY OF UTAH AND CSS ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS | |
38 | * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSS DISCLAIM ANY LIABILITY OF | |
39 | * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
40 | * | |
41 | * CSS requests users of this software to return to css-dist@cs.utah.edu any | |
42 | * improvements that they make and grant CSS redistribution rights. | |
43 | * | |
44 | * Utah $Hdr: pcb.c 1.23 92/06/27$ | |
45 | */ | |
46 | ||
1c79356b A |
47 | #include <debug.h> |
48 | ||
49 | #include <types.h> | |
91447636 A |
50 | |
51 | #include <mach/mach_types.h> | |
52 | #include <mach/thread_status.h> | |
53 | ||
54 | #include <kern/kern_types.h> | |
1c79356b A |
55 | #include <kern/task.h> |
56 | #include <kern/thread.h> | |
91447636 | 57 | #include <kern/misc_protos.h> |
1c79356b | 58 | #include <kern/mach_param.h> |
91447636 A |
59 | #include <kern/spl.h> |
60 | ||
61 | #include <vm/vm_map.h> | |
62 | #include <vm/vm_kern.h> | |
1c79356b | 63 | |
1c79356b | 64 | #include <ppc/misc_protos.h> |
91447636 | 65 | #include <ppc/cpu_internal.h> |
1c79356b A |
66 | #include <ppc/exception.h> |
67 | #include <ppc/proc_reg.h> | |
1c79356b A |
68 | #include <ppc/pmap.h> |
69 | #include <ppc/trap.h> | |
70 | #include <ppc/mappings.h> | |
71 | #include <ppc/savearea.h> | |
72 | #include <ppc/Firmware.h> | |
73 | #include <ppc/asm.h> | |
91447636 | 74 | #include <ppc/thread.h> |
1c79356b | 75 | #include <ppc/vmachmon.h> |
765c9de3 | 76 | #include <ppc/low_trace.h> |
91447636 | 77 | #include <ppc/lowglobals.h> |
1c79356b A |
78 | |
79 | #include <sys/kdebug.h> | |
80 | ||
91447636 | 81 | void machine_act_terminate(thread_t); |
55e303ae | 82 | |
1c79356b A |
83 | /* |
84 | * These constants are dumb. They should not be in asm.h! | |
85 | */ | |
86 | ||
87 | #define KF_SIZE (FM_SIZE+ARG_SIZE+FM_REDZONE) | |
88 | ||
89 | #if DEBUG | |
90 | int fpu_trap_count = 0; | |
91 | int fpu_switch_count = 0; | |
92 | int vec_trap_count = 0; | |
93 | int vec_switch_count = 0; | |
94 | #endif | |
95 | ||
1c79356b A |
96 | /* |
97 | * consider_machine_collect: try to collect machine-dependent pages | |
98 | */ | |
99 | void | |
100 | consider_machine_collect() | |
101 | { | |
102 | /* | |
103 | * none currently available | |
104 | */ | |
105 | return; | |
106 | } | |
107 | ||
108 | void | |
109 | consider_machine_adjust() | |
110 | { | |
111 | consider_mapping_adjust(); | |
112 | } | |
113 | ||
1c79356b A |
114 | /* |
115 | * switch_context: Switch from one thread to another, needed for | |
116 | * switching of space | |
117 | * | |
118 | */ | |
55e303ae A |
119 | thread_t |
120 | machine_switch_context( | |
121 | thread_t old, | |
122 | thread_continue_t continuation, | |
123 | thread_t new) | |
1c79356b | 124 | { |
55e303ae | 125 | register thread_t retval; |
1c79356b | 126 | pmap_t new_pmap; |
9bccf70c | 127 | facility_context *fowner; |
55e303ae A |
128 | struct per_proc_info *ppinfo; |
129 | ||
130 | if (old == new) | |
131 | panic("machine_switch_context"); | |
9bccf70c | 132 | |
55e303ae A |
133 | ppinfo = getPerProc(); /* Get our processor block */ |
134 | ||
135 | ppinfo->old_thread = (unsigned int)old; | |
136 | ppinfo->cpu_flags &= ~traceBE; /* disable branch tracing if on */ | |
1c79356b | 137 | |
1c79356b A |
138 | /* Our context might wake up on another processor, so we must |
139 | * not keep hot state in our FPU, it must go back to the pcb | |
140 | * so that it can be found by the other if needed | |
141 | */ | |
9bccf70c | 142 | if(real_ncpus > 1) { /* This is potentially slow, so only do when actually SMP */ |
55e303ae | 143 | fowner = ppinfo->FPU_owner; /* Cache this because it may change */ |
9bccf70c | 144 | if(fowner) { /* Is there any live context? */ |
91447636 | 145 | if(fowner->facAct == old) { /* Is it for us? */ |
9bccf70c A |
146 | fpu_save(fowner); /* Yes, save it */ |
147 | } | |
148 | } | |
55e303ae | 149 | fowner = ppinfo->VMX_owner; /* Cache this because it may change */ |
9bccf70c | 150 | if(fowner) { /* Is there any live context? */ |
91447636 | 151 | if(fowner->facAct == old) { /* Is it for us? */ |
9bccf70c A |
152 | vec_save(fowner); /* Yes, save it */ |
153 | } | |
154 | } | |
1c79356b A |
155 | } |
156 | ||
d7e50217 A |
157 | /* |
158 | * If old thread is running VM, save per proc userProtKey and FamVMmode spcFlags bits in the thread spcFlags | |
159 | * This bits can be modified in the per proc without updating the thread spcFlags | |
160 | */ | |
91447636 A |
161 | if(old->machine.specFlags & runningVM) { |
162 | old->machine.specFlags &= ~(userProtKey|FamVMmode); | |
163 | old->machine.specFlags |= (ppinfo->spcFlags) & (userProtKey|FamVMmode); | |
1c79356b | 164 | } |
91447636 A |
165 | old->machine.specFlags &= ~OnProc; |
166 | new->machine.specFlags |= OnProc; | |
1c79356b A |
167 | |
168 | /* | |
169 | * We do not have to worry about the PMAP module, so switch. | |
170 | * | |
91447636 | 171 | * We must not use thread->map since this may not be the actual |
1c79356b A |
172 | * task map, but the map being used for a klcopyin/out. |
173 | */ | |
174 | ||
91447636 A |
175 | if(new->machine.specFlags & runningVM) { /* Is the new guy running a VM? */ |
176 | pmap_switch(new->machine.vmmCEntry->vmmPmap); /* Switch to the VM's pmap */ | |
177 | ppinfo->VMMareaPhys = new->machine.vmmCEntry->vmmContextPhys; | |
178 | ppinfo->VMMXAFlgs = new->machine.vmmCEntry->vmmXAFlgs; | |
179 | ppinfo->FAMintercept = new->machine.vmmCEntry->vmmFAMintercept; | |
1c79356b A |
180 | } |
181 | else { /* otherwise, we use the task's pmap */ | |
91447636 A |
182 | new_pmap = new->task->map->pmap; |
183 | if ((old->task->map->pmap != new_pmap) || (old->machine.specFlags & runningVM)) { | |
1c79356b A |
184 | pmap_switch(new_pmap); /* Switch if there is a change */ |
185 | } | |
186 | } | |
187 | ||
91447636 A |
188 | if(old->machine.umwSpace != invalSpace) { /* Does our old guy have an active window? */ |
189 | old->machine.umwSpace |= umwSwitchAway; /* Show we switched away from this guy */ | |
190 | hw_blow_seg(lowGlo.lgUMWvaddr); /* Blow off the first segment */ | |
191 | hw_blow_seg(lowGlo.lgUMWvaddr + 0x10000000ULL); /* Blow off the second segment */ | |
55e303ae A |
192 | } |
193 | ||
1c79356b | 194 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_SCHED) | DBG_FUNC_NONE, |
55e303ae | 195 | old->reason, (int)new, old->sched_pri, new->sched_pri, 0); |
1c79356b | 196 | |
1c79356b | 197 | retval = Switch_context(old, continuation, new); |
91447636 | 198 | assert(retval != NULL); |
1c79356b | 199 | |
55e303ae A |
200 | if (branch_tracing_enabled()) { |
201 | ppinfo = getPerProc(); /* Get our processor block */ | |
202 | ppinfo->cpu_flags |= traceBE; /* restore branch tracing */ | |
203 | } | |
0b4e3aa0 | 204 | |
1c79356b A |
205 | /* We've returned from having switched context, so we should be |
206 | * back in the original context. | |
207 | */ | |
208 | ||
209 | return retval; | |
210 | } | |
211 | ||
1c79356b A |
212 | /* |
213 | * Initialize the machine-dependent state for a new thread. | |
214 | */ | |
215 | kern_return_t | |
55e303ae A |
216 | machine_thread_create( |
217 | thread_t thread, | |
218 | task_t task) | |
1c79356b | 219 | { |
1c79356b A |
220 | savearea *sv; /* Pointer to newly allocated savearea */ |
221 | unsigned int *CIsTooLimited, i; | |
222 | ||
55e303ae | 223 | hw_atomic_add((uint32_t *)&saveanchor.savetarget, 4); /* Account for the number of saveareas we think we "need" |
1c79356b | 224 | for this activation */ |
91447636 | 225 | assert(thread->machine.pcb == (savearea *)0); /* Make sure there was no previous savearea */ |
1c79356b A |
226 | |
227 | sv = save_alloc(); /* Go get us a savearea */ | |
228 | ||
9bccf70c A |
229 | bzero((char *)((unsigned int)sv + sizeof(savearea_comm)), (sizeof(savearea) - sizeof(savearea_comm))); /* Clear it */ |
230 | ||
231 | sv->save_hdr.save_prev = 0; /* Clear the back pointer */ | |
232 | sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use */ | |
91447636 A |
233 | sv->save_hdr.save_act = thread; /* Set who owns it */ |
234 | thread->machine.pcb = sv; /* Point to the save area */ | |
235 | thread->machine.curctx = &thread->machine.facctx; /* Initialize facility context */ | |
236 | thread->machine.facctx.facAct = thread; /* Initialize facility context pointer to activation */ | |
237 | thread->machine.umwSpace = invalSpace; /* Initialize user memory window space to invalid */ | |
238 | thread->machine.preemption_count = 0; /* Initialize preemption counter */ | |
55e303ae | 239 | |
1c79356b A |
240 | /* |
241 | * User threads will pull their context from the pcb when first | |
242 | * returning to user mode, so fill in all the necessary values. | |
243 | * Kernel threads are initialized from the save state structure | |
244 | * at the base of the kernel stack (see stack_attach()). | |
245 | */ | |
246 | ||
91447636 | 247 | thread->machine.upcb = sv; /* Set user pcb */ |
55e303ae | 248 | sv->save_srr1 = (uint64_t)MSR_EXPORT_MASK_SET; /* Set the default user MSR */ |
91447636 | 249 | if(task_has_64BitAddr(task)) sv->save_srr1 |= (uint64_t)MASK32(MSR_SF) << 32; /* If 64-bit task, force 64-bit mode */ |
55e303ae A |
250 | sv->save_fpscr = 0; /* Clear all floating point exceptions */ |
251 | sv->save_vrsave = 0; /* Set the vector save state */ | |
252 | sv->save_vscr[0] = 0x00000000; | |
253 | sv->save_vscr[1] = 0x00000000; | |
254 | sv->save_vscr[2] = 0x00000000; | |
255 | sv->save_vscr[3] = 0x00010000; /* Disable java mode and clear saturated */ | |
1c79356b | 256 | |
1c79356b A |
257 | return(KERN_SUCCESS); |
258 | } | |
259 | ||
260 | /* | |
261 | * Machine-dependent cleanup prior to destroying a thread | |
262 | */ | |
263 | void | |
55e303ae A |
264 | machine_thread_destroy( |
265 | thread_t thread) | |
1c79356b | 266 | { |
55e303ae A |
267 | register savearea *pcb, *ppsv; |
268 | register savearea_vec *vsv, *vpsv; | |
269 | register savearea_fpu *fsv, *fpsv; | |
270 | register savearea *svp; | |
271 | register int i; | |
272 | ||
273 | /* | |
274 | * This function will release all context. | |
275 | */ | |
276 | ||
277 | machine_act_terminate(thread); /* Make sure all virtual machines are dead first */ | |
278 | ||
279 | /* | |
280 | * | |
281 | * Walk through and release all floating point and vector contexts. Also kill live context. | |
282 | * | |
283 | */ | |
284 | ||
91447636 | 285 | toss_live_vec(thread->machine.curctx); /* Dump live vectors */ |
1c79356b | 286 | |
91447636 | 287 | vsv = thread->machine.curctx->VMXsave; /* Get the top vector savearea */ |
55e303ae A |
288 | |
289 | while(vsv) { /* Any VMX saved state? */ | |
290 | vpsv = vsv; /* Remember so we can toss this */ | |
291 | vsv = CAST_DOWN(savearea_vec *, vsv->save_hdr.save_prev); /* Get one underneath our's */ | |
292 | save_release((savearea *)vpsv); /* Release it */ | |
1c79356b | 293 | } |
55e303ae | 294 | |
91447636 | 295 | thread->machine.curctx->VMXsave = 0; /* Kill chain */ |
55e303ae | 296 | |
91447636 | 297 | toss_live_fpu(thread->machine.curctx); /* Dump live float */ |
55e303ae | 298 | |
91447636 | 299 | fsv = thread->machine.curctx->FPUsave; /* Get the top float savearea */ |
55e303ae A |
300 | |
301 | while(fsv) { /* Any float saved state? */ | |
302 | fpsv = fsv; /* Remember so we can toss this */ | |
303 | fsv = CAST_DOWN(savearea_fpu *, fsv->save_hdr.save_prev); /* Get one underneath our's */ | |
304 | save_release((savearea *)fpsv); /* Release it */ | |
305 | } | |
306 | ||
91447636 | 307 | thread->machine.curctx->FPUsave = 0; /* Kill chain */ |
1c79356b A |
308 | |
309 | /* | |
55e303ae | 310 | * free all regular saveareas. |
1c79356b | 311 | */ |
55e303ae | 312 | |
91447636 | 313 | pcb = thread->machine.pcb; /* Get the general savearea */ |
55e303ae A |
314 | |
315 | while(pcb) { /* Any float saved state? */ | |
316 | ppsv = pcb; /* Remember so we can toss this */ | |
317 | pcb = CAST_DOWN(savearea *, pcb->save_hdr.save_prev); /* Get one underneath our's */ | |
318 | save_release(ppsv); /* Release it */ | |
319 | } | |
320 | ||
321 | hw_atomic_sub((uint32_t *)&saveanchor.savetarget, 4); /* Unaccount for the number of saveareas we think we "need" */ | |
1c79356b A |
322 | } |
323 | ||
1c79356b A |
324 | /* |
325 | * act_machine_sv_free | |
326 | * release saveareas associated with an act. if flag is true, release | |
327 | * user level savearea(s) too, else don't | |
328 | * | |
329 | * this code cannot block so we call the proper save area free routine | |
330 | */ | |
331 | void | |
91447636 | 332 | act_machine_sv_free(thread_t act) |
1c79356b | 333 | { |
9bccf70c | 334 | register savearea *pcb, *userpcb; |
55e303ae A |
335 | register savearea_vec *vsv, *vpst, *vsvt; |
336 | register savearea_fpu *fsv, *fpst, *fsvt; | |
1c79356b A |
337 | register savearea *svp; |
338 | register int i; | |
339 | ||
340 | /* | |
9bccf70c | 341 | * This function will release all non-user state context. |
1c79356b A |
342 | */ |
343 | ||
9bccf70c A |
344 | /* |
345 | * | |
346 | * Walk through and release all floating point and vector contexts that are not | |
347 | * user state. We will also blow away live context if it belongs to non-user state. | |
55e303ae A |
348 | * Note that the level can not change while we are in this code. Nor can another |
349 | * context be pushed on the stack. | |
350 | * | |
351 | * We do nothing here if the current level is user. Otherwise, | |
352 | * the live context is cleared. Then we find the user saved context. | |
353 | * Next, we take the sync lock (to keep us from munging things in *_switch). | |
354 | * The level is set to 0 and all stacked context other than user is dequeued. | |
355 | * Then we unlock. Next, all of the old kernel contexts are released. | |
9bccf70c A |
356 | * |
357 | */ | |
358 | ||
91447636 | 359 | if(act->machine.curctx->VMXlevel) { /* Is the current level user state? */ |
55e303ae | 360 | |
91447636 | 361 | toss_live_vec(act->machine.curctx); /* Dump live vectors if is not user */ |
9bccf70c | 362 | |
91447636 | 363 | vsv = act->machine.curctx->VMXsave; /* Get the top vector savearea */ |
55e303ae A |
364 | |
365 | while(vsv && vsv->save_hdr.save_level) vsv = (savearea_vec *)vsv->save_hdr.save_prev; /* Find user context if any */ | |
de355530 | 366 | |
91447636 | 367 | if(!hw_lock_to((hw_lock_t)&act->machine.curctx->VMXsync, LockTimeOut)) { /* Get the sync lock */ |
55e303ae A |
368 | panic("act_machine_sv_free - timeout getting VMX sync lock\n"); /* Tell all and die */ |
369 | } | |
370 | ||
91447636 A |
371 | vsvt = act->machine.curctx->VMXsave; /* Get the top of the chain */ |
372 | act->machine.curctx->VMXsave = vsv; /* Point to the user context */ | |
373 | act->machine.curctx->VMXlevel = 0; /* Set the level to user */ | |
374 | hw_lock_unlock((hw_lock_t)&act->machine.curctx->VMXsync); /* Unlock */ | |
55e303ae A |
375 | |
376 | while(vsvt) { /* Clear any VMX saved state */ | |
377 | if (vsvt == vsv) break; /* Done when hit user if any */ | |
378 | vpst = vsvt; /* Remember so we can toss this */ | |
379 | vsvt = (savearea_vec *)vsvt->save_hdr.save_prev; /* Get one underneath our's */ | |
380 | save_ret((savearea *)vpst); /* Release it */ | |
381 | } | |
382 | ||
383 | } | |
9bccf70c | 384 | |
91447636 | 385 | if(act->machine.curctx->FPUlevel) { /* Is the current level user state? */ |
55e303ae | 386 | |
91447636 | 387 | toss_live_fpu(act->machine.curctx); /* Dump live floats if is not user */ |
1c79356b | 388 | |
91447636 | 389 | fsv = act->machine.curctx->FPUsave; /* Get the top floats savearea */ |
55e303ae A |
390 | |
391 | while(fsv && fsv->save_hdr.save_level) fsv = (savearea_fpu *)fsv->save_hdr.save_prev; /* Find user context if any */ | |
9bccf70c | 392 | |
91447636 | 393 | if(!hw_lock_to((hw_lock_t)&act->machine.curctx->FPUsync, LockTimeOut)) { /* Get the sync lock */ |
55e303ae A |
394 | panic("act_machine_sv_free - timeout getting FPU sync lock\n"); /* Tell all and die */ |
395 | } | |
396 | ||
91447636 A |
397 | fsvt = act->machine.curctx->FPUsave; /* Get the top of the chain */ |
398 | act->machine.curctx->FPUsave = fsv; /* Point to the user context */ | |
399 | act->machine.curctx->FPUlevel = 0; /* Set the level to user */ | |
400 | hw_lock_unlock((hw_lock_t)&act->machine.curctx->FPUsync); /* Unlock */ | |
55e303ae A |
401 | |
402 | while(fsvt) { /* Clear any VMX saved state */ | |
403 | if (fsvt == fsv) break; /* Done when hit user if any */ | |
404 | fpst = fsvt; /* Remember so we can toss this */ | |
405 | fsvt = (savearea_fpu *)fsvt->save_hdr.save_prev; /* Get one underneath our's */ | |
406 | save_ret((savearea *)fpst); /* Release it */ | |
407 | } | |
408 | ||
1c79356b A |
409 | } |
410 | ||
9bccf70c A |
411 | /* |
412 | * free all regular saveareas except a user savearea, if any | |
413 | */ | |
1c79356b | 414 | |
91447636 | 415 | pcb = act->machine.pcb; /* Get the general savearea */ |
9bccf70c A |
416 | userpcb = 0; /* Assume no user context for now */ |
417 | ||
418 | while(pcb) { /* Any float saved state? */ | |
419 | if (pcb->save_srr1 & MASK(MSR_PR)) { /* Is this a user savearea? */ | |
420 | userpcb = pcb; /* Remember so we can toss this */ | |
421 | break; | |
422 | } | |
423 | svp = pcb; /* Remember this */ | |
55e303ae | 424 | pcb = CAST_DOWN(savearea *, pcb->save_hdr.save_prev); /* Get one underneath our's */ |
9bccf70c | 425 | save_ret(svp); /* Release it */ |
1c79356b | 426 | } |
9bccf70c | 427 | |
91447636 | 428 | act->machine.pcb = userpcb; /* Chain in the user if there is one, or 0 if not */ |
9bccf70c | 429 | |
1c79356b A |
430 | } |
431 | ||
1c79356b | 432 | void |
55e303ae | 433 | machine_act_terminate( |
91447636 | 434 | thread_t act) |
1c79356b | 435 | { |
91447636 | 436 | if(act->machine.bbDescAddr) { /* Check if the Blue box assist is active */ |
1c79356b A |
437 | disable_bluebox_internal(act); /* Kill off bluebox */ |
438 | } | |
439 | ||
91447636 | 440 | if(act->machine.vmmControl) { /* Check if VMM is active */ |
1c79356b A |
441 | vmm_tear_down_all(act); /* Kill off all VMM contexts */ |
442 | } | |
443 | } | |
444 | ||
1c79356b | 445 | void |
55e303ae | 446 | machine_thread_terminate_self(void) |
1c79356b | 447 | { |
91447636 | 448 | machine_act_terminate(current_thread()); |
1c79356b A |
449 | } |
450 | ||
451 | void | |
55e303ae | 452 | machine_thread_init(void) |
1c79356b A |
453 | { |
454 | #ifdef MACHINE_STACK | |
455 | #if KERNEL_STACK_SIZE > PPC_PGBYTES | |
456 | panic("KERNEL_STACK_SIZE can't be greater than PPC_PGBYTES\n"); | |
457 | #endif | |
458 | #endif | |
459 | } | |
460 | ||
461 | #if MACH_ASSERT | |
1c79356b A |
462 | |
463 | void | |
464 | dump_thread(thread_t th) | |
465 | { | |
466 | printf(" thread @ 0x%x:\n", th); | |
467 | } | |
468 | ||
469 | int | |
91447636 | 470 | dump_act(thread_t thr_act) |
1c79356b A |
471 | { |
472 | if (!thr_act) | |
473 | return(0); | |
474 | ||
91447636 | 475 | printf("thread(0x%x)(%d): task=%x(%d)\n", |
1c79356b | 476 | thr_act, thr_act->ref_count, |
1c79356b A |
477 | thr_act->task, thr_act->task ? thr_act->task->ref_count : 0); |
478 | ||
91447636 A |
479 | printf("\tsusp=%x active=%x\n", |
480 | thr_act->suspend_count, thr_act->active); | |
1c79356b A |
481 | |
482 | return((int)thr_act); | |
483 | } | |
484 | ||
485 | #endif | |
486 | ||
91447636 | 487 | user_addr_t |
1c79356b A |
488 | get_useraddr() |
489 | { | |
91447636 | 490 | return(current_thread()->machine.upcb->save_srr0); |
1c79356b A |
491 | } |
492 | ||
493 | /* | |
494 | * detach and return a kernel stack from a thread | |
495 | */ | |
496 | ||
497 | vm_offset_t | |
55e303ae A |
498 | machine_stack_detach( |
499 | thread_t thread) | |
1c79356b A |
500 | { |
501 | vm_offset_t stack; | |
502 | ||
9bccf70c A |
503 | KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_DETACH), |
504 | thread, thread->priority, | |
505 | thread->sched_pri, 0, 0); | |
506 | ||
91447636 | 507 | act_machine_sv_free(thread); |
1c79356b A |
508 | |
509 | stack = thread->kernel_stack; | |
510 | thread->kernel_stack = 0; | |
511 | return(stack); | |
512 | } | |
513 | ||
514 | /* | |
515 | * attach a kernel stack to a thread and initialize it | |
516 | * | |
517 | * attaches a stack to a thread. if there is no save | |
518 | * area we allocate one. the top save area is then | |
519 | * loaded with the pc (continuation address), the initial | |
520 | * stack pointer, and a std kernel MSR. if the top | |
521 | * save area is the user save area bad things will | |
522 | * happen | |
523 | * | |
524 | */ | |
525 | ||
526 | void | |
55e303ae A |
527 | machine_stack_attach( |
528 | thread_t thread, | |
91447636 | 529 | vm_offset_t stack) |
1c79356b | 530 | { |
1c79356b A |
531 | unsigned int *kss; |
532 | struct savearea *sv; | |
533 | ||
534 | KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_ATTACH), | |
535 | thread, thread->priority, | |
91447636 | 536 | thread->sched_pri, 0, 0); |
1c79356b A |
537 | |
538 | assert(stack); | |
539 | kss = (unsigned int *)STACK_IKS(stack); | |
540 | thread->kernel_stack = stack; | |
541 | ||
542 | /* during initialization we sometimes do not have an | |
543 | activation. in that case do not do anything */ | |
91447636 A |
544 | sv = save_get(); /* cannot block */ |
545 | sv->save_hdr.save_flags = (sv->save_hdr.save_flags & ~SAVtype) | (SAVgeneral << SAVtypeshft); /* Mark as in use */ | |
546 | sv->save_hdr.save_act = thread; | |
547 | sv->save_hdr.save_prev = (addr64_t)((uintptr_t)thread->machine.pcb); | |
548 | thread->machine.pcb = sv; | |
549 | ||
550 | sv->save_srr0 = (unsigned int)thread_continue; | |
551 | /* sv->save_r3 = ARG ? */ | |
552 | sv->save_r1 = (vm_offset_t)((int)kss - KF_SIZE); | |
553 | sv->save_srr1 = MSR_SUPERVISOR_INT_OFF; | |
554 | sv->save_fpscr = 0; /* Clear all floating point exceptions */ | |
555 | sv->save_vrsave = 0; /* Set the vector save state */ | |
556 | sv->save_vscr[3] = 0x00010000; /* Supress java mode */ | |
557 | *(CAST_DOWN(int *, sv->save_r1)) = 0; | |
558 | ||
559 | thread->machine.ksp = 0; | |
1c79356b A |
560 | } |
561 | ||
562 | /* | |
563 | * move a stack from old to new thread | |
564 | */ | |
565 | ||
566 | void | |
55e303ae A |
567 | machine_stack_handoff( |
568 | thread_t old, | |
569 | thread_t new) | |
1c79356b A |
570 | { |
571 | ||
9bccf70c A |
572 | vm_offset_t stack; |
573 | pmap_t new_pmap; | |
574 | facility_context *fowner; | |
91447636 | 575 | mapping_t *mp; |
55e303ae | 576 | struct per_proc_info *ppinfo; |
9bccf70c | 577 | |
91447636 A |
578 | assert(new); |
579 | assert(old); | |
55e303ae A |
580 | |
581 | if (old == new) | |
582 | panic("machine_stack_handoff"); | |
9bccf70c | 583 | |
55e303ae | 584 | stack = machine_stack_detach(old); |
9bccf70c | 585 | new->kernel_stack = stack; |
55e303ae A |
586 | if (stack == old->reserved_stack) { |
587 | assert(new->reserved_stack); | |
588 | old->reserved_stack = new->reserved_stack; | |
589 | new->reserved_stack = stack; | |
9bccf70c | 590 | } |
1c79356b | 591 | |
55e303ae A |
592 | ppinfo = getPerProc(); /* Get our processor block */ |
593 | ||
594 | ppinfo->cpu_flags &= ~traceBE; /* Turn off special branch trace */ | |
0b4e3aa0 | 595 | |
9bccf70c | 596 | if(real_ncpus > 1) { /* This is potentially slow, so only do when actually SMP */ |
55e303ae | 597 | fowner = ppinfo->FPU_owner; /* Cache this because it may change */ |
9bccf70c | 598 | if(fowner) { /* Is there any live context? */ |
91447636 | 599 | if(fowner->facAct == old) { /* Is it for us? */ |
9bccf70c A |
600 | fpu_save(fowner); /* Yes, save it */ |
601 | } | |
602 | } | |
55e303ae | 603 | fowner = ppinfo->VMX_owner; /* Cache this because it may change */ |
9bccf70c | 604 | if(fowner) { /* Is there any live context? */ |
91447636 | 605 | if(fowner->facAct == old) { /* Is it for us? */ |
9bccf70c A |
606 | vec_save(fowner); /* Yes, save it */ |
607 | } | |
608 | } | |
609 | } | |
55e303ae | 610 | |
d7e50217 A |
611 | /* |
612 | * If old thread is running VM, save per proc userProtKey and FamVMmode spcFlags bits in the thread spcFlags | |
613 | * This bits can be modified in the per proc without updating the thread spcFlags | |
614 | */ | |
91447636 A |
615 | if(old->machine.specFlags & runningVM) { /* Is the current thread running a VM? */ |
616 | old->machine.specFlags &= ~(userProtKey|FamVMmode); | |
617 | old->machine.specFlags |= (ppinfo->spcFlags) & (userProtKey|FamVMmode); | |
d7e50217 | 618 | } |
91447636 A |
619 | old->machine.specFlags &= ~OnProc; |
620 | new->machine.specFlags |= OnProc; | |
1c79356b | 621 | |
9bccf70c | 622 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_HANDOFF) | DBG_FUNC_NONE, |
55e303ae | 623 | old->reason, (int)new, old->sched_pri, new->sched_pri, 0); |
1c79356b A |
624 | |
625 | ||
91447636 A |
626 | if(new->machine.specFlags & runningVM) { /* Is the new guy running a VM? */ |
627 | pmap_switch(new->machine.vmmCEntry->vmmPmap); /* Switch to the VM's pmap */ | |
628 | ppinfo->VMMareaPhys = new->machine.vmmCEntry->vmmContextPhys; | |
629 | ppinfo->VMMXAFlgs = new->machine.vmmCEntry->vmmXAFlgs; | |
630 | ppinfo->FAMintercept = new->machine.vmmCEntry->vmmFAMintercept; | |
1c79356b A |
631 | } |
632 | else { /* otherwise, we use the task's pmap */ | |
91447636 A |
633 | new_pmap = new->task->map->pmap; |
634 | if ((old->task->map->pmap != new_pmap) || (old->machine.specFlags & runningVM)) { | |
1c79356b A |
635 | pmap_switch(new_pmap); |
636 | } | |
637 | } | |
638 | ||
91447636 A |
639 | machine_set_current_thread(new); |
640 | ppinfo->Uassist = new->machine.cthread_self; | |
9bccf70c | 641 | |
91447636 A |
642 | ppinfo->ppbbTaskEnv = new->machine.bbTaskEnv; |
643 | ppinfo->spcFlags = new->machine.specFlags; | |
55e303ae | 644 | |
91447636 A |
645 | old->machine.umwSpace |= umwSwitchAway; /* Show we switched away from this guy */ |
646 | mp = (mapping_t *)&ppinfo->ppUMWmp; | |
55e303ae | 647 | mp->mpSpace = invalSpace; /* Since we can't handoff in the middle of copy in/out, just invalidate */ |
9bccf70c A |
648 | |
649 | if (branch_tracing_enabled()) | |
55e303ae | 650 | ppinfo->cpu_flags |= traceBE; |
765c9de3 | 651 | |
91447636 | 652 | if(trcWork.traceMask) dbgTrace(0x12345678, (unsigned int)old, (unsigned int)new, 0); /* Cut trace entry if tracing */ |
765c9de3 | 653 | |
1c79356b A |
654 | return; |
655 | } | |
656 | ||
657 | /* | |
658 | * clean and initialize the current kernel stack and go to | |
659 | * the given continuation routine | |
660 | */ | |
661 | ||
662 | void | |
91447636 A |
663 | call_continuation( |
664 | thread_continue_t continuation, | |
665 | void *parameter, | |
666 | wait_result_t wresult) | |
1c79356b | 667 | { |
91447636 A |
668 | thread_t self = current_thread(); |
669 | unsigned int *kss; | |
670 | vm_offset_t tsp; | |
1c79356b | 671 | |
91447636 A |
672 | assert(self->kernel_stack); |
673 | kss = (unsigned int *)STACK_IKS(self->kernel_stack); | |
674 | assert(continuation); | |
1c79356b | 675 | |
91447636 A |
676 | tsp = (vm_offset_t)((int)kss - KF_SIZE); |
677 | assert(tsp); | |
678 | *((int *)tsp) = 0; | |
1c79356b | 679 | |
91447636 | 680 | Call_continuation(continuation, parameter, wresult, tsp); |
1c79356b | 681 | } |