2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /*-----------------------------------------------------------------------
25 ** C routines that we are adding to the MacOS X kernel.
27 -----------------------------------------------------------------------*/
29 #include <mach/mach_types.h>
30 #include <mach/kern_return.h>
31 #include <mach/host_info.h>
32 #include <kern/kern_types.h>
33 #include <kern/kalloc.h>
34 #include <kern/host.h>
35 #include <kern/task.h>
36 #include <kern/thread.h>
37 #include <ppc/exception.h>
38 #include <ppc/mappings.h>
39 #include <ppc/thread.h>
40 #include <vm/vm_kern.h>
41 #include <vm/vm_fault.h>
43 #include <ppc/vmachmon.h>
44 #include <ppc/lowglobals.h>
46 extern double FloatInit
;
47 extern unsigned long QNaNbarbarian
[4];
49 /*************************************************************************************
50 Virtual Machine Monitor Internal Routines
51 **************************************************************************************/
53 /*-----------------------------------------------------------------------
56 ** This function verifies and return a vmm context entry index
59 ** act - pointer to current thread activation
60 ** index - index into vmm control table (this is a "one based" value)
63 ** address of a vmmCntrlEntry or 0 if not found
64 -----------------------------------------------------------------------*/
66 static vmmCntrlEntry
*vmm_get_entry(
68 vmm_thread_index_t index
)
70 vmmCntrlTable
*CTable
;
71 vmmCntrlEntry
*CEntry
;
73 index
= index
& vmmTInum
; /* Clean up the index */
75 if (act
->machine
.vmmControl
== 0) return NULL
; /* No control table means no vmm */
76 if ((index
- 1) >= kVmmMaxContexts
) return NULL
; /* Index not in range */
78 CTable
= act
->machine
.vmmControl
; /* Make the address a bit more convienient */
79 CEntry
= &CTable
->vmmc
[index
- 1]; /* Point to the entry */
81 if (!(CEntry
->vmmFlags
& vmmInUse
)) return NULL
; /* See if the slot is actually in use */
86 /*-----------------------------------------------------------------------
89 ** This function verifies and returns the pmap for an address space.
90 ** If there is none and the request is valid, a pmap will be created.
93 ** act - pointer to current thread activation
94 ** index - index into vmm control table (this is a "one based" value)
97 ** address of a pmap or 0 if not found or could no be created
98 ** Note that if there is no pmap for the address space it will be created.
99 -----------------------------------------------------------------------*/
101 static pmap_t
vmm_get_adsp(thread_t act
, vmm_thread_index_t index
)
105 if (act
->machine
.vmmControl
== 0) return NULL
; /* No control table means no vmm */
106 if ((index
- 1) >= kVmmMaxContexts
) return NULL
; /* Index not in range */
108 pmap
= act
->machine
.vmmControl
->vmmAdsp
[index
- 1]; /* Get the pmap */
109 return (pmap
); /* and return it. */
112 /*-----------------------------------------------------------------------
113 ** vmm_build_shadow_hash
115 ** Allocate and initialize a shadow hash table.
117 ** This function assumes that PAGE_SIZE is 4k-bytes.
119 -----------------------------------------------------------------------*/
120 static pmap_vmm_ext
*vmm_build_shadow_hash(pmap_t pmap
)
122 pmap_vmm_ext
*ext
; /* VMM pmap extension we're building */
123 ppnum_t extPP
; /* VMM pmap extension physical page number */
124 kern_return_t ret
; /* Return code from various calls */
125 uint32_t pages
= GV_HPAGES
; /* Number of pages in the hash table */
126 vm_offset_t free
= VMX_HPIDX_OFFSET
; /* Offset into extension page of free area (128-byte aligned) */
127 uint32_t freeSize
= PAGE_SIZE
- free
; /* Number of free bytes in the extension page */
129 if ((pages
* sizeof(addr64_t
)) + (pages
* sizeof(vm_offset_t
)) > freeSize
) {
130 panic("vmm_build_shadow_hash: too little pmap_vmm_ext free space\n");
133 ret
= kmem_alloc_wired(kernel_map
, (vm_offset_t
*)&ext
, PAGE_SIZE
);
134 /* Allocate a page-sized extension block */
135 if (ret
!= KERN_SUCCESS
) return (NULL
); /* Return NULL for failed allocate */
136 bzero((char *)ext
, PAGE_SIZE
); /* Zero the entire extension block page */
138 extPP
= pmap_find_phys(kernel_pmap
, (vm_offset_t
)ext
);
139 /* Get extension block's physical page number */
140 if (!extPP
) { /* This should not fail, but then again... */
141 panic("vmm_build_shadow_hash: could not translate pmap_vmm_ext vaddr %08X\n", ext
);
144 ext
->vmxSalt
= (addr64_t
)(vm_offset_t
)ext
^ ptoa_64(extPP
);
145 /* Set effective<->physical conversion salt */
146 ext
->vmxHostPmapPhys
= (addr64_t
)(vm_offset_t
)pmap
^ pmap
->pmapvr
;
147 /* Set host pmap's physical address */
148 ext
->vmxHostPmap
= pmap
; /* Set host pmap's effective address */
149 ext
->vmxHashPgIdx
= (addr64_t
*)((vm_offset_t
)ext
+ VMX_HPIDX_OFFSET
);
150 /* Allocate physical index */
151 ext
->vmxHashPgList
= (vm_offset_t
*)((vm_offset_t
)ext
+ VMX_HPLIST_OFFSET
);
152 /* Allocate page list */
153 ext
->vmxActiveBitmap
= (vm_offset_t
*)((vm_offset_t
)ext
+ VMX_ACTMAP_OFFSET
);
154 /* Allocate active mapping bitmap */
156 /* The hash table is typically larger than a single page, but we don't require it to be in a
157 contiguous virtual or physical chunk. So, we allocate it page by page, noting the effective and
158 physical address of each page in vmxHashPgList and vmxHashPgIdx, respectively. */
160 for (idx
= 0; idx
< pages
; idx
++) {
161 ret
= kmem_alloc_wired(kernel_map
, &ext
->vmxHashPgList
[idx
], PAGE_SIZE
);
162 /* Allocate a hash-table page */
163 if (ret
!= KERN_SUCCESS
) goto fail
; /* Allocation failed, exit through cleanup */
164 bzero((char *)ext
->vmxHashPgList
[idx
], PAGE_SIZE
); /* Zero the page */
165 ext
->vmxHashPgIdx
[idx
] = ptoa_64(pmap_find_phys(kernel_pmap
, (addr64_t
)ext
->vmxHashPgList
[idx
]));
166 /* Put page's physical address into index */
167 if (!ext
->vmxHashPgIdx
[idx
]) { /* Hash-table page's LRA failed */
168 panic("vmm_build_shadow_hash: could not translate hash-table vaddr %08X\n", ext
->vmxHashPgList
[idx
]);
170 mapping_t
*map
= (mapping_t
*)ext
->vmxHashPgList
[idx
];
172 for (mapIdx
= 0; mapIdx
< GV_SLTS_PPG
; mapIdx
++) { /* Iterate over mappings in this page */
173 map
->mpFlags
= (mpGuest
| mpgFree
); /* Mark guest type and free */
174 map
= (mapping_t
*)((char *)map
+ GV_SLOT_SZ
); /* Next slot-sized mapping */
178 return (ext
); /* Return newly-minted VMM pmap extension */
181 for (idx
= 0; idx
< pages
; idx
++) { /* De-allocate any pages we managed to allocate */
182 if (ext
->vmxHashPgList
[idx
]) {
183 kmem_free(kernel_map
, ext
->vmxHashPgList
[idx
], PAGE_SIZE
);
186 kmem_free(kernel_map
, (vm_offset_t
)ext
, PAGE_SIZE
); /* Release the VMM pmap extension page */
187 return (NULL
); /* Return NULL for failure */
191 /*-----------------------------------------------------------------------
192 ** vmm_release_shadow_hash
194 ** Release shadow hash table and VMM extension block
196 -----------------------------------------------------------------------*/
197 static void vmm_release_shadow_hash(pmap_vmm_ext
*ext
)
201 for (idx
= 0; idx
< GV_HPAGES
; idx
++) { /* Release the hash table page by page */
202 kmem_free(kernel_map
, ext
->vmxHashPgList
[idx
], PAGE_SIZE
);
205 kmem_free(kernel_map
, (vm_offset_t
)ext
, PAGE_SIZE
); /* Release the VMM pmap extension page */
208 /*-----------------------------------------------------------------------
211 ** Activate guest shadow assist
213 -----------------------------------------------------------------------*/
214 static kern_return_t
vmm_activate_gsa(
216 vmm_thread_index_t index
)
218 vmmCntrlTable
*CTable
= act
->machine
.vmmControl
; /* Get VMM control table */
219 if (!CTable
) { /* Caller guarantees that this will work */
220 panic("vmm_activate_gsa: VMM control table not present; act = %08X, idx = %d\n",
224 vmmCntrlEntry
*CEntry
= vmm_get_entry(act
, index
); /* Get context from index */
225 if (!CEntry
) { /* Caller guarantees that this will work */
226 panic("vmm_activate_gsa: Unexpected failure of vmm_get_entry; act = %08X, idx = %d\n",
231 pmap_t hpmap
= act
->map
->pmap
; /* Get host pmap */
232 pmap_t gpmap
= vmm_get_adsp(act
, index
); /* Get guest pmap */
233 if (!gpmap
) { /* Caller guarantees that this will work */
234 panic("vmm_activate_gsa: Unexpected failure of vmm_get_adsp; act = %08X, idx = %d\n",
239 if (!hpmap
->pmapVmmExt
) { /* If there's no VMM extension for this host, create one */
240 hpmap
->pmapVmmExt
= vmm_build_shadow_hash(hpmap
); /* Build VMM extension plus shadow hash and attach */
241 if (hpmap
->pmapVmmExt
) { /* See if we succeeded */
242 hpmap
->pmapVmmExtPhys
= (addr64_t
)(vm_offset_t
)hpmap
->pmapVmmExt
^ hpmap
->pmapVmmExt
->vmxSalt
;
243 /* Get VMM extensions block physical address */
245 return KERN_RESOURCE_SHORTAGE
; /* Not enough mojo to go */
248 gpmap
->pmapVmmExt
= hpmap
->pmapVmmExt
; /* Copy VMM extension block virtual address into guest */
249 gpmap
->pmapVmmExtPhys
= hpmap
->pmapVmmExtPhys
; /* and its physical address, too */
250 gpmap
->pmapFlags
|= pmapVMgsaa
; /* Enable GSA for this guest */
251 CEntry
->vmmXAFlgs
|= vmmGSA
; /* Show GSA active here, too */
257 /*-----------------------------------------------------------------------
258 ** vmm_deactivate_gsa
260 ** Deactivate guest shadow assist
262 -----------------------------------------------------------------------*/
263 static void vmm_deactivate_gsa(
265 vmm_thread_index_t index
)
267 vmmCntrlEntry
*CEntry
= vmm_get_entry(act
, index
); /* Get context from index */
268 if (!CEntry
) { /* Caller guarantees that this will work */
269 panic("vmm_deactivate_gsa: Unexpected failure of vmm_get_entry; act = %08X, idx = %d\n",
274 pmap_t gpmap
= vmm_get_adsp(act
, index
); /* Get guest pmap */
275 if (!gpmap
) { /* Caller guarantees that this will work */
276 panic("vmm_deactivate_gsa: Unexpected failure of vmm_get_adsp; act = %08X, idx = %d\n",
281 gpmap
->pmapFlags
&= ~pmapVMgsaa
; /* Deactivate GSA for this guest */
282 CEntry
->vmmXAFlgs
&= ~vmmGSA
; /* Show GSA deactivated here, too */
286 /*-----------------------------------------------------------------------
289 ** Flush specified guest context, purging all guest mappings and clearing
292 -----------------------------------------------------------------------*/
293 static void vmm_flush_context(
295 vmm_thread_index_t index
)
297 vmmCntrlEntry
*CEntry
;
298 vmmCntrlTable
*CTable
;
299 vmm_state_page_t
*vks
;
300 vmm_version_t version
;
302 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
303 if (!CEntry
) { /* Caller guarantees that this will work */
304 panic("vmm_flush_context: Unexpected failure of vmm_get_entry; act = %08X, idx = %d\n",
309 if(CEntry
->vmmFacCtx
.FPUsave
) { /* Is there any floating point context? */
310 toss_live_fpu(&CEntry
->vmmFacCtx
); /* Get rid of any live context here */
311 save_release((savearea
*)CEntry
->vmmFacCtx
.FPUsave
); /* Release it */
314 if(CEntry
->vmmFacCtx
.VMXsave
) { /* Is there any vector context? */
315 toss_live_vec(&CEntry
->vmmFacCtx
); /* Get rid of any live context here */
316 save_release((savearea
*)CEntry
->vmmFacCtx
.VMXsave
); /* Release it */
319 vmm_unmap_all_pages(act
, index
); /* Blow away all mappings for this context */
321 CTable
= act
->machine
.vmmControl
; /* Get the control table address */
322 CTable
->vmmGFlags
= CTable
->vmmGFlags
& ~vmmLastAdSp
; /* Make sure we don't try to automap into this */
324 CEntry
->vmmFlags
&= vmmInUse
; /* Clear out all of the flags for this entry except in use */
325 CEntry
->vmmFacCtx
.FPUsave
= 0; /* Clear facility context control */
326 CEntry
->vmmFacCtx
.FPUlevel
= 0; /* Clear facility context control */
327 CEntry
->vmmFacCtx
.FPUcpu
= 0; /* Clear facility context control */
328 CEntry
->vmmFacCtx
.VMXsave
= 0; /* Clear facility context control */
329 CEntry
->vmmFacCtx
.VMXlevel
= 0; /* Clear facility context control */
330 CEntry
->vmmFacCtx
.VMXcpu
= 0; /* Clear facility context control */
332 vks
= CEntry
->vmmContextKern
; /* Get address of the context page */
333 version
= vks
->interface_version
; /* Save the version code */
334 bzero((char *)vks
, 4096); /* Clear all */
336 vks
->interface_version
= version
; /* Set our version code */
337 vks
->thread_index
= index
% vmmTInum
; /* Tell the user the index for this virtual machine */
339 return; /* Context is now flushed */
343 /*************************************************************************************
344 Virtual Machine Monitor Exported Functionality
346 The following routines are used to implement a quick-switch mechanism for
347 virtual machines that need to execute within their own processor envinroment
348 (including register and MMU state).
349 **************************************************************************************/
351 /*-----------------------------------------------------------------------
354 ** This function returns the current version of the virtual machine
355 ** interface. It is divided into two portions. The top 16 bits
356 ** represent the major version number, and the bottom 16 bits
357 ** represent the minor version number. Clients using the Vmm
358 ** functionality should make sure they are using a verison new
365 ** 32-bit number representing major/minor version of
367 -----------------------------------------------------------------------*/
369 int vmm_get_version(struct savearea
*save
)
371 save
->save_r3
= kVmmCurrentVersion
; /* Return the version */
376 /*-----------------------------------------------------------------------
379 ** This function returns a set of flags that represents the functionality
380 ** supported by the current verison of the Vmm interface. Clients should
381 ** use this to determine whether they can run on this system.
387 ** 32-bit number representing functionality supported by this
388 ** version of the Vmm module
389 -----------------------------------------------------------------------*/
391 int vmm_get_features(struct savearea
*save
)
393 save
->save_r3
= kVmmCurrentFeatures
; /* Return the features */
394 if(getPerProc()->pf
.Available
& pf64Bit
) {
395 save
->save_r3
&= ~kVmmFeature_LittleEndian
; /* No little endian here */
396 save
->save_r3
|= kVmmFeature_SixtyFourBit
; /* Set that we can do 64-bit */
402 /*-----------------------------------------------------------------------
405 ** This function returns the maximum addressable virtual address sported
408 ** Returns max address
409 -----------------------------------------------------------------------*/
411 addr64_t
vmm_max_addr(thread_t act
)
413 return vm_max_address
; /* Return the maximum address */
416 /*-----------------------------------------------------------------------
419 ** This function retrieves the eXtended Architecture flags for the specifed VM.
421 ** We need to return the result in the return code rather than in the return parameters
422 ** because we need an architecture independent format so the results are actually
423 ** usable by the host. For example, the return parameters for 64-bit are 8 bytes wide vs.
428 ** act - pointer to current thread activation structure
429 ** index - index returned by vmm_init_context
432 ** Return code is set to the XA flags. If the index is invalid or the
433 ** context has not been created, we return 0.
434 -----------------------------------------------------------------------*/
436 unsigned int vmm_get_XA(
438 vmm_thread_index_t index
)
440 vmmCntrlEntry
*CEntry
;
442 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
443 if (CEntry
== NULL
) return 0; /* Either this isn't a vmm or the index is bogus */
445 return CEntry
->vmmXAFlgs
; /* Return the flags */
448 /*-----------------------------------------------------------------------
451 ** This function initializes an emulation context. It allocates
452 ** a new pmap (address space) and fills in the initial processor
453 ** state within the specified structure. The structure, mapped
454 ** into the client's logical address space, must be page-aligned.
457 ** act - pointer to current thread activation
458 ** version - requested version of the Vmm interface (allowing
459 ** future versions of the interface to change, but still
460 ** support older clients)
461 ** vmm_user_state - pointer to a logical page within the
462 ** client's address space
465 ** kernel return code indicating success or failure
466 -----------------------------------------------------------------------*/
468 int vmm_init_context(struct savearea
*save
)
472 vmm_version_t version
;
473 vmm_state_page_t
* vmm_user_state
;
474 vmmCntrlTable
*CTable
;
476 vmm_state_page_t
* vks
;
483 vmm_user_state
= CAST_DOWN(vmm_state_page_t
*, save
->save_r4
); /* Get the user address of the comm area */
484 if ((unsigned int)vmm_user_state
& (PAGE_SIZE
- 1)) { /* Make sure the comm area is page aligned */
485 save
->save_r3
= KERN_FAILURE
; /* Return failure */
489 /* Make sure that the version requested is supported */
490 version
= save
->save_r3
; /* Pick up passed in version */
491 if (((version
>> 16) < kVmmMinMajorVersion
) || ((version
>> 16) > (kVmmCurrentVersion
>> 16))) {
492 save
->save_r3
= KERN_FAILURE
; /* Return failure */
496 if((version
& 0xFFFF) > kVmmCurMinorVersion
) { /* Check for valid minor */
497 save
->save_r3
= KERN_FAILURE
; /* Return failure */
501 act
= current_thread(); /* Pick up our activation */
503 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
505 task
= current_task(); /* Figure out who we are */
507 task_lock(task
); /* Lock our task */
509 fact
= (thread_t
)task
->threads
.next
; /* Get the first activation on task */
510 gact
= 0; /* Pretend we didn't find it yet */
512 for(i
= 0; i
< task
->thread_count
; i
++) { /* All of the activations */
513 if(fact
->machine
.vmmControl
) { /* Is this a virtual machine monitor? */
514 gact
= fact
; /* Yeah... */
515 break; /* Bail the loop... */
517 fact
= (thread_t
)fact
->task_threads
.next
; /* Go to the next one */
522 * We only allow one thread per task to be a virtual machine monitor right now. This solves
523 * a number of potential problems that I can't put my finger on right now.
525 * Utlimately, I think we want to move the controls and make all this task based instead of
526 * thread based. That would allow an emulator architecture to spawn a kernel thread for each
527 * VM (if they want) rather than hand dispatch contexts.
530 if(gact
&& (gact
!= act
)) { /* Check if another thread is a vmm or trying to be */
531 task_unlock(task
); /* Release task lock */
532 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
533 save
->save_r3
= KERN_FAILURE
; /* We must play alone... */
537 if(!gact
) act
->machine
.vmmControl
= (vmmCntrlTable
*)1; /* Temporarily mark that we are the vmm thread */
539 task_unlock(task
); /* Safe to release now (because we've marked ourselves) */
541 CTable
= act
->machine
.vmmControl
; /* Get the control table address */
542 if ((unsigned int)CTable
== 1) { /* If we are marked, try to allocate a new table, otherwise we have one */
543 if(!(CTable
= (vmmCntrlTable
*)kalloc(sizeof(vmmCntrlTable
)))) { /* Get a fresh emulation control table */
544 act
->machine
.vmmControl
= 0; /* Unmark us as vmm 'cause we failed */
545 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
546 save
->save_r3
= KERN_RESOURCE_SHORTAGE
; /* No storage... */
550 bzero((void *)CTable
, sizeof(vmmCntrlTable
)); /* Clean it up */
551 act
->machine
.vmmControl
= CTable
; /* Initialize the table anchor */
554 for(cvi
= 0; cvi
< kVmmMaxContexts
; cvi
++) { /* Search to find a free slot */
555 if(!(CTable
->vmmc
[cvi
].vmmFlags
& vmmInUse
)) break; /* Bail if we find an unused slot */
558 if(cvi
>= kVmmMaxContexts
) { /* Did we find one? */
559 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
560 save
->save_r3
= KERN_RESOURCE_SHORTAGE
; /* No empty slots... */
564 ret
= vm_map_wire( /* Wire the virtual machine monitor's context area */
566 (vm_offset_t
)vmm_user_state
,
567 (vm_offset_t
)vmm_user_state
+ PAGE_SIZE
,
568 VM_PROT_READ
| VM_PROT_WRITE
,
571 if (ret
!= KERN_SUCCESS
) /* The wire failed, return the code */
572 goto return_in_shame
;
574 /* Map the vmm state into the kernel's address space. */
575 conphys
= pmap_find_phys(act
->map
->pmap
, (addr64_t
)((uintptr_t)vmm_user_state
));
577 /* Find a virtual address to use. */
578 ret
= kmem_alloc_pageable(kernel_map
, &conkern
, PAGE_SIZE
);
579 if (ret
!= KERN_SUCCESS
) { /* Did we find an address? */
580 (void) vm_map_unwire(act
->map
, /* No, unwire the context area */
581 (vm_offset_t
)vmm_user_state
,
582 (vm_offset_t
)vmm_user_state
+ PAGE_SIZE
,
584 goto return_in_shame
;
587 /* Map it into the kernel's address space. */
589 pmap_enter(kernel_pmap
, conkern
, conphys
,
590 VM_PROT_READ
| VM_PROT_WRITE
,
591 VM_WIMG_USE_DEFAULT
, TRUE
);
593 /* Clear the vmm state structure. */
594 vks
= (vmm_state_page_t
*)conkern
;
595 bzero((char *)vks
, PAGE_SIZE
);
598 /* We're home free now. Simply fill in the necessary info and return. */
600 vks
->interface_version
= version
; /* Set our version code */
601 vks
->thread_index
= cvi
+ 1; /* Tell the user the index for this virtual machine */
603 CTable
->vmmc
[cvi
].vmmFlags
= vmmInUse
; /* Mark the slot in use and make sure the rest are clear */
604 CTable
->vmmc
[cvi
].vmmContextKern
= vks
; /* Remember the kernel address of comm area */
605 CTable
->vmmc
[cvi
].vmmContextPhys
= conphys
; /* Remember the state page physical addr */
606 CTable
->vmmc
[cvi
].vmmContextUser
= vmm_user_state
; /* Remember user address of comm area */
608 CTable
->vmmc
[cvi
].vmmFacCtx
.FPUsave
= 0; /* Clear facility context control */
609 CTable
->vmmc
[cvi
].vmmFacCtx
.FPUlevel
= 0; /* Clear facility context control */
610 CTable
->vmmc
[cvi
].vmmFacCtx
.FPUcpu
= 0; /* Clear facility context control */
611 CTable
->vmmc
[cvi
].vmmFacCtx
.VMXsave
= 0; /* Clear facility context control */
612 CTable
->vmmc
[cvi
].vmmFacCtx
.VMXlevel
= 0; /* Clear facility context control */
613 CTable
->vmmc
[cvi
].vmmFacCtx
.VMXcpu
= 0; /* Clear facility context control */
614 CTable
->vmmc
[cvi
].vmmFacCtx
.facAct
= act
; /* Point back to the activation */
616 hw_atomic_add((int *)&saveanchor
.savetarget
, 2); /* Account for the number of extra saveareas we think we might "need" */
618 pmap_t hpmap
= act
->map
->pmap
; /* Get host pmap */
619 pmap_t gpmap
= pmap_create(0); /* Make a fresh guest pmap */
620 if (gpmap
) { /* Did we succeed ? */
621 CTable
->vmmAdsp
[cvi
] = gpmap
; /* Remember guest pmap for new context */
622 if (lowGlo
.lgVMMforcedFeats
& vmmGSA
) { /* Forcing on guest shadow assist ? */
623 vmm_activate_gsa(act
, cvi
+1); /* Activate GSA */
626 ret
= KERN_RESOURCE_SHORTAGE
; /* We've failed to allocate a guest pmap */
627 goto return_in_shame
; /* Shame on us. */
630 if (!(hpmap
->pmapFlags
& pmapVMhost
)) { /* Do this stuff if this is our first time hosting */
631 hpmap
->pmapFlags
|= pmapVMhost
; /* We're now hosting */
634 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
635 save
->save_r3
= KERN_SUCCESS
; /* Hip, hip, horay... */
639 if(!gact
) kfree(CTable
, sizeof(vmmCntrlTable
)); /* Toss the table if we just allocated it */
640 act
->machine
.vmmControl
= 0; /* Unmark us as vmm 'cause we failed */
641 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
642 save
->save_r3
= ret
; /* Pass back return code... */
648 /*-----------------------------------------------------------------------
649 ** vmm_tear_down_context
651 ** This function uninitializes an emulation context. It deallocates
652 ** internal resources associated with the context block.
655 ** act - pointer to current thread activation structure
656 ** index - index returned by vmm_init_context
659 ** kernel return code indicating success or failure
662 ** This call will also trash the address space with the same ID. While this
663 ** is really not too cool, we have to do it because we need to make
664 ** sure that old VMM users (not that we really have any) who depend upon
665 ** the address space going away with the context still work the same.
666 -----------------------------------------------------------------------*/
668 kern_return_t
vmm_tear_down_context(
670 vmm_thread_index_t index
)
672 vmmCntrlEntry
*CEntry
;
673 vmmCntrlTable
*CTable
;
675 register savearea
*sv
;
677 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
678 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
680 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
682 hw_atomic_sub((int *)&saveanchor
.savetarget
, 2); /* We don't need these extra saveareas anymore */
684 if(CEntry
->vmmFacCtx
.FPUsave
) { /* Is there any floating point context? */
685 toss_live_fpu(&CEntry
->vmmFacCtx
); /* Get rid of any live context here */
686 save_release((savearea
*)CEntry
->vmmFacCtx
.FPUsave
); /* Release it */
689 if(CEntry
->vmmFacCtx
.VMXsave
) { /* Is there any vector context? */
690 toss_live_vec(&CEntry
->vmmFacCtx
); /* Get rid of any live context here */
691 save_release((savearea
*)CEntry
->vmmFacCtx
.VMXsave
); /* Release it */
694 CEntry
->vmmPmap
= 0; /* Remove this trace */
695 pmap_t gpmap
= act
->machine
.vmmControl
->vmmAdsp
[index
- 1];
696 /* Get context's guest pmap (if any) */
697 if (gpmap
) { /* Check if there is an address space assigned here */
698 if (gpmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist case specially */
699 hw_rem_all_gv(gpmap
); /* Remove all guest mappings from shadow hash table */
701 mapping_remove(gpmap
, 0xFFFFFFFFFFFFF000LL
);/* Remove final page explicitly because we might have mapped it */
702 pmap_remove(gpmap
, 0, 0xFFFFFFFFFFFFF000LL
);/* Remove all entries from this map */
704 pmap_destroy(gpmap
); /* Toss the pmap for this context */
705 act
->machine
.vmmControl
->vmmAdsp
[index
- 1] = NULL
; /* Clean it up */
708 (void) vm_map_unwire( /* Unwire the user comm page */
710 (vm_offset_t
)CEntry
->vmmContextUser
,
711 (vm_offset_t
)CEntry
->vmmContextUser
+ PAGE_SIZE
,
714 kmem_free(kernel_map
, (vm_offset_t
)CEntry
->vmmContextKern
, PAGE_SIZE
); /* Remove kernel's view of the comm page */
716 CTable
= act
->machine
.vmmControl
; /* Get the control table address */
717 CTable
->vmmGFlags
= CTable
->vmmGFlags
& ~vmmLastAdSp
; /* Make sure we don't try to automap into this */
719 CEntry
->vmmFlags
= 0; /* Clear out all of the flags for this entry including in use */
720 CEntry
->vmmContextKern
= 0; /* Clear the kernel address of comm area */
721 CEntry
->vmmContextUser
= 0; /* Clear the user address of comm area */
723 CEntry
->vmmFacCtx
.FPUsave
= 0; /* Clear facility context control */
724 CEntry
->vmmFacCtx
.FPUlevel
= 0; /* Clear facility context control */
725 CEntry
->vmmFacCtx
.FPUcpu
= 0; /* Clear facility context control */
726 CEntry
->vmmFacCtx
.VMXsave
= 0; /* Clear facility context control */
727 CEntry
->vmmFacCtx
.VMXlevel
= 0; /* Clear facility context control */
728 CEntry
->vmmFacCtx
.VMXcpu
= 0; /* Clear facility context control */
729 CEntry
->vmmFacCtx
.facAct
= 0; /* Clear facility context control */
731 for(cvi
= 0; cvi
< kVmmMaxContexts
; cvi
++) { /* Search to find a free slot */
732 if(CTable
->vmmc
[cvi
].vmmFlags
& vmmInUse
) { /* Return if there are still some in use */
733 ml_set_interrupts_enabled(FALSE
); /* No more interruptions */
734 return KERN_SUCCESS
; /* Leave... */
739 * When we have tossed the last context, toss any address spaces left over before releasing
740 * the VMM control block
743 for(cvi
= 1; cvi
<= kVmmMaxContexts
; cvi
++) { /* Look at all slots */
744 if(!act
->machine
.vmmControl
->vmmAdsp
[index
- 1]) continue; /* Nothing to remove here */
745 mapping_remove(act
->machine
.vmmControl
->vmmAdsp
[index
- 1], 0xFFFFFFFFFFFFF000LL
); /* Remove final page explicitly because we might have mapped it */
746 pmap_remove(act
->machine
.vmmControl
->vmmAdsp
[index
- 1], 0, 0xFFFFFFFFFFFFF000LL
); /* Remove all entries from this map */
747 pmap_destroy(act
->machine
.vmmControl
->vmmAdsp
[index
- 1]); /* Toss the pmap for this context */
748 act
->machine
.vmmControl
->vmmAdsp
[index
- 1] = 0; /* Clear just in case */
751 pmap_t pmap
= act
->map
->pmap
; /* Get our pmap */
752 if (pmap
->pmapVmmExt
) { /* Release any VMM pmap extension block and shadow hash table */
753 vmm_release_shadow_hash(pmap
->pmapVmmExt
); /* Release extension block and shadow hash table */
754 pmap
->pmapVmmExt
= 0; /* Forget extension block */
755 pmap
->pmapVmmExtPhys
= 0; /* Forget extension block's physical address, too */
757 pmap
->pmapFlags
&= ~pmapVMhost
; /* We're no longer hosting */
759 kfree(CTable
, sizeof(vmmCntrlTable
)); /* Toss the table because to tossed the last context */
760 act
->machine
.vmmControl
= 0; /* Unmark us as vmm */
762 ml_set_interrupts_enabled(FALSE
); /* No more interruptions */
768 /*-----------------------------------------------------------------------
771 ** This function activates the eXtended Architecture flags for the specifed VM.
773 ** We need to return the result in the return code rather than in the return parameters
774 ** because we need an architecture independent format so the results are actually
775 ** usable by the host. For example, the return parameters for 64-bit are 8 bytes wide vs.
778 ** Note that this function does a lot of the same stuff as vmm_tear_down_context
779 ** and vmm_init_context.
782 ** act - pointer to current thread activation structure
783 ** index - index returned by vmm_init_context
784 ** flags - the extended architecture flags
788 ** KERN_SUCCESS if vm is valid and initialized. KERN_FAILURE if not.
789 ** Also, the internal flags are set and, additionally, the VM is completely reset.
790 -----------------------------------------------------------------------*/
791 kern_return_t
vmm_activate_XA(
793 vmm_thread_index_t index
,
794 unsigned int xaflags
)
796 vmmCntrlEntry
*CEntry
;
797 kern_return_t result
= KERN_SUCCESS
; /* Assume success */
799 if ((xaflags
& ~kVmmSupportedSetXA
) || ((xaflags
& vmm64Bit
) && (!getPerProc()->pf
.Available
& pf64Bit
)))
800 return (KERN_FAILURE
); /* Unknown or unsupported feature requested */
802 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
803 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't a vmm or the index is bogus */
805 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
807 vmm_flush_context(act
, index
); /* Flush the context */
809 if (xaflags
& vmm64Bit
) { /* Activating 64-bit mode ? */
810 CEntry
->vmmXAFlgs
|= vmm64Bit
; /* Activate 64-bit mode */
813 if (xaflags
& vmmGSA
) { /* Activating guest shadow assist ? */
814 result
= vmm_activate_gsa(act
, index
); /* Activate guest shadow assist */
817 ml_set_interrupts_enabled(FALSE
); /* No more interruptions */
819 return result
; /* Return activate result */
822 /*-----------------------------------------------------------------------
825 -----------------------------------------------------------------------*/
826 kern_return_t
vmm_deactivate_XA(
828 vmm_thread_index_t index
,
829 unsigned int xaflags
)
831 vmmCntrlEntry
*CEntry
;
832 kern_return_t result
= KERN_SUCCESS
; /* Assume success */
834 if ((xaflags
& ~kVmmSupportedSetXA
) || ((xaflags
& vmm64Bit
) && (getPerProc()->pf
.Available
& pf64Bit
)))
835 return (KERN_FAILURE
); /* Unknown or unsupported feature requested */
837 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
838 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't a vmm or the index is bogus */
840 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
842 vmm_flush_context(act
, index
); /* Flush the context */
844 if (xaflags
& vmm64Bit
) { /* Deactivating 64-bit mode ? */
845 CEntry
->vmmXAFlgs
&= ~vmm64Bit
; /* Deactivate 64-bit mode */
848 if (xaflags
& vmmGSA
) { /* Deactivating guest shadow assist ? */
849 vmm_deactivate_gsa(act
, index
); /* Deactivate guest shadow assist */
852 ml_set_interrupts_enabled(FALSE
); /* No more interruptions */
854 return result
; /* Return deactivate result */
858 /*-----------------------------------------------------------------------
861 ** This function uninitializes all emulation contexts. If there are
862 ** any vmm contexts, it calls vmm_tear_down_context for each one.
864 ** Note: this can also be called from normal thread termination. Because of
865 ** that, we will context switch out of an alternate if we are currenty in it.
866 ** It will be terminated with no valid return code set because we don't expect
867 ** the activation to ever run again.
870 ** activation to tear down
873 ** All vmm contexts released and VMM shut down
874 -----------------------------------------------------------------------*/
875 void vmm_tear_down_all(thread_t act
) {
877 vmmCntrlTable
*CTable
;
883 if(act
->machine
.specFlags
& runningVM
) { /* Are we actually in a context right now? */
884 save
= find_user_regs(act
); /* Find the user state context */
885 if(!save
) { /* Did we find it? */
886 panic("vmm_tear_down_all: runningVM marked but no user state context\n");
890 save
->save_exception
= kVmmBogusContext
*4; /* Indicate that this context is bogus now */
891 s
= splhigh(); /* Make sure interrupts are off */
892 vmm_force_exit(act
, save
); /* Force and exit from VM state */
893 splx(s
); /* Restore interrupts */
896 if(CTable
= act
->machine
.vmmControl
) { /* Do we have a vmm control block? */
899 for(cvi
= 1; cvi
<= kVmmMaxContexts
; cvi
++) { /* Look at all slots */
900 if(CTable
->vmmc
[cvi
- 1].vmmFlags
& vmmInUse
) { /* Is this one in use */
901 ret
= vmm_tear_down_context(act
, cvi
); /* Take down the found context */
902 if(ret
!= KERN_SUCCESS
) { /* Did it go away? */
903 panic("vmm_tear_down_all: vmm_tear_down_context failed; ret=%08X, act = %08X, cvi = %d\n",
910 * Note that all address apces should be gone here.
912 if(act
->machine
.vmmControl
) { /* Did we find one? */
913 panic("vmm_tear_down_all: control table did not get deallocated\n"); /* Table did not go away */
920 /*-----------------------------------------------------------------------
923 ** This function maps a page from within the client's logical
924 ** address space into the alternate address space.
926 ** The page need not be locked or resident. If not resident, it will be faulted
927 ** in by this code, which may take some time. Also, if the page is not locked,
928 ** it, and this mapping may disappear at any time, even before it gets used. Note also
929 ** that reference and change information is NOT preserved when a page is unmapped, either
930 ** explicitly or implicitly (e.g., a pageout, being unmapped in the non-alternate address
931 ** space). This means that if RC is needed, the page MUST be wired.
933 ** Note that if there is already a mapping at the address, it is removed and all
934 ** information (including RC) is lost BEFORE an attempt is made to map it. Also,
935 ** if the map call fails, the old address is still unmapped..
938 ** act - pointer to current thread activation
939 ** index - index of address space to map into
940 ** va - virtual address within the client's address
942 ** ava - virtual address within the alternate address
944 ** prot - protection flags
946 ** Note that attempted mapping of areas in nested pmaps (shared libraries) or block mapped
947 ** areas are not allowed and will fail. Same with directly mapped I/O areas.
950 ** Interrupts disabled (from fast trap)
953 ** kernel return code indicating success or failure
954 ** if success, va resident and alternate mapping made
955 -----------------------------------------------------------------------*/
957 kern_return_t
vmm_map_page(
965 register mapping_t
*mp
;
967 addr64_t ova
, nextva
;
970 pmap
= vmm_get_adsp(act
, index
); /* Get the guest pmap for this address space */
971 if(!pmap
) return KERN_FAILURE
; /* Bogus address space, no VMs, or we can't make a pmap, failure... */
973 if(ava
> vm_max_address
) return kVmmInvalidAddress
; /* Does the machine support an address of this size? */
975 map
= current_thread()->map
; /* Get the host's map */
977 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Guest shadow assist active ? */
978 ret
= hw_res_map_gv(map
->pmap
, pmap
, cva
, ava
, getProtPPC(prot
));
979 /* Attempt to resume an existing gv->phys mapping */
980 if (mapRtOK
!= ret
) { /* Nothing to resume, construct a new mapping */
982 while (1) { /* Find host mapping or fail */
983 mp
= mapping_find(map
->pmap
, cva
, &nextva
, 0);
984 /* Attempt to find host mapping and pin it */
985 if (mp
) break; /* Got it */
987 ml_set_interrupts_enabled(TRUE
);
988 /* Open 'rupt window */
989 ret
= vm_fault(map
, /* Didn't find it, try to fault in host page read/write */
990 vm_map_trunc_page(cva
),
991 VM_PROT_READ
| VM_PROT_WRITE
,
992 FALSE
, /* change wiring */
996 ml_set_interrupts_enabled(FALSE
);
997 /* Close 'rupt window */
998 if (ret
!= KERN_SUCCESS
)
999 return KERN_FAILURE
; /* Fault failed, return failure */
1002 if (mpNormal
!= (mp
->mpFlags
& mpType
)) {
1003 /* Host mapping must be a vanilla page */
1004 mapping_drop_busy(mp
); /* Un-pin host mapping */
1005 return KERN_FAILURE
; /* Return failure */
1008 /* Partially construct gv->phys mapping */
1009 unsigned int pindex
;
1010 phys_entry_t
*physent
= mapping_phys_lookup(mp
->mpPAddr
, &pindex
);
1012 mapping_drop_busy(mp
);
1013 return KERN_FAILURE
;
1015 unsigned int pattr
= ((physent
->ppLink
& (ppI
| ppG
)) >> 60);
1016 unsigned int wimg
= 0x2;
1017 if (pattr
& mmFlgCInhib
) wimg
|= 0x4;
1018 if (pattr
& mmFlgGuarded
) wimg
|= 0x1;
1019 unsigned int mflags
= (pindex
<< 16) | mpGuest
;
1020 addr64_t gva
= ((ava
& ~mpHWFlags
) | (wimg
<< 3) | getProtPPC(prot
));
1022 hw_add_map_gv(map
->pmap
, pmap
, gva
, mflags
, mp
->mpPAddr
);
1023 /* Construct new guest->phys mapping */
1025 mapping_drop_busy(mp
); /* Un-pin host mapping */
1028 while(1) { /* Keep trying until we get it or until we fail */
1030 mp
= mapping_find(map
->pmap
, cva
, &nextva
, 0); /* Find the mapping for this address */
1032 if(mp
) break; /* We found it */
1034 ml_set_interrupts_enabled(TRUE
); /* Enable interruptions */
1035 ret
= vm_fault(map
, /* Didn't find it, try to fault it in read/write... */
1036 vm_map_trunc_page(cva
),
1037 VM_PROT_READ
| VM_PROT_WRITE
,
1038 FALSE
, /*change wiring */
1042 ml_set_interrupts_enabled(FALSE
); /* Disable interruptions */
1043 if (ret
!= KERN_SUCCESS
) return KERN_FAILURE
; /* There isn't a page there, return... */
1046 if((mp
->mpFlags
& mpType
) != mpNormal
) { /* If this is a block, a nest, or some other special thing, we can't map it */
1047 mapping_drop_busy(mp
); /* We have everything we need from the mapping */
1048 return KERN_FAILURE
; /* Leave in shame */
1051 while(1) { /* Keep trying the enter until it goes in */
1052 ova
= mapping_make(pmap
, ava
, mp
->mpPAddr
, 0, 1, prot
); /* Enter the mapping into the pmap */
1053 if(!ova
) break; /* If there were no collisions, we are done... */
1054 mapping_remove(pmap
, ova
); /* Remove the mapping that collided */
1057 mapping_drop_busy(mp
); /* We have everything we need from the mapping */
1060 if (!((getPerProc()->spcFlags
) & FamVMmode
)) {
1061 act
->machine
.vmmControl
->vmmLastMap
= ava
& 0xFFFFFFFFFFFFF000ULL
; /* Remember the last mapping we made */
1062 act
->machine
.vmmControl
->vmmGFlags
= (act
->machine
.vmmControl
->vmmGFlags
& ~vmmLastAdSp
) | index
; /* Remember last address space */
1065 return KERN_SUCCESS
;
1069 /*-----------------------------------------------------------------------
1072 ** This function maps a page from within the client's logical
1073 ** address space into the alternate address space of the
1074 ** Virtual Machine Monitor context and then directly starts executing.
1076 ** See description of vmm_map_page for details.
1079 ** Index is used for both the context and the address space ID.
1080 ** index[24:31] is the context id and index[16:23] is the address space.
1081 ** if the address space ID is 0, the context ID is used for it.
1084 ** Normal exit is to run the VM. Abnormal exit is triggered via a
1085 ** non-KERN_SUCCESS return from vmm_map_page or later during the
1086 ** attempt to transition into the VM.
1087 -----------------------------------------------------------------------*/
1089 vmm_return_code_t
vmm_map_execute(
1091 vmm_thread_index_t index
,
1097 vmmCntrlEntry
*CEntry
;
1099 vmm_thread_index_t cndx
;
1101 cndx
= index
& 0xFF; /* Clean it up */
1103 CEntry
= vmm_get_entry(act
, cndx
); /* Get and validate the index */
1104 if (CEntry
== NULL
) return kVmmBogusContext
; /* Return bogus context */
1106 if (((getPerProc()->spcFlags
) & FamVMmode
) && (CEntry
!= act
->machine
.vmmCEntry
))
1107 return kVmmBogusContext
; /* Yes, invalid index in Fam */
1109 adsp
= (index
>> 8) & 0xFF; /* Get any requested address space */
1110 if(!adsp
) adsp
= (index
& 0xFF); /* If 0, use context ID as address space ID */
1112 ret
= vmm_map_page(act
, adsp
, cva
, ava
, prot
); /* Go try to map the page on in */
1115 if(ret
== KERN_SUCCESS
) {
1116 act
->machine
.vmmControl
->vmmLastMap
= ava
& 0xFFFFFFFFFFFFF000ULL
; /* Remember the last mapping we made */
1117 act
->machine
.vmmControl
->vmmGFlags
= (act
->machine
.vmmControl
->vmmGFlags
& ~vmmLastAdSp
) | cndx
; /* Remember last address space */
1118 vmm_execute_vm(act
, cndx
); /* Return was ok, launch the VM */
1121 return ret
; /* We had trouble mapping in the page */
1125 /*-----------------------------------------------------------------------
1128 ** This function maps a list of pages into various address spaces
1131 ** act - pointer to current thread activation
1132 ** index - index of default address space (used if not specifed in list entry
1133 ** count - number of pages to release
1134 ** flavor - 0 if 32-bit version, 1 if 64-bit
1135 ** vmcpComm in the comm page contains up to kVmmMaxMapPages to map
1138 ** kernel return code indicating success or failure
1139 ** KERN_FAILURE is returned if kVmmMaxUnmapPages is exceeded
1140 ** or the vmm_map_page call fails.
1141 ** We return kVmmInvalidAddress if virtual address size is not supported
1142 -----------------------------------------------------------------------*/
1144 kern_return_t
vmm_map_list(
1146 vmm_adsp_id_t index
,
1148 unsigned int flavor
)
1150 vmmCntrlEntry
*CEntry
;
1160 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1161 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't a vmm or the index is bogus */
1163 if(cnt
> kVmmMaxMapPages
) return KERN_FAILURE
; /* They tried to map too many */
1164 if(!cnt
) return KERN_SUCCESS
; /* If they said none, we're done... */
1166 lst
= (vmmMList
*)&((vmm_comm_page_t
*)CEntry
->vmmContextKern
)->vmcpComm
[0]; /* Point to the first entry */
1167 lstx
= (vmmMList64
*)&((vmm_comm_page_t
*)CEntry
->vmmContextKern
)->vmcpComm
[0]; /* Point to the first entry */
1169 for(i
= 0; i
< cnt
; i
++) { /* Step and release all pages in list */
1170 if(flavor
) { /* Check if 32- or 64-bit addresses */
1171 cva
= lstx
[i
].vmlva
; /* Get the 64-bit actual address */
1172 ava
= lstx
[i
].vmlava
; /* Get the 64-bit guest address */
1175 cva
= lst
[i
].vmlva
; /* Get the 32-bit actual address */
1176 ava
= lst
[i
].vmlava
; /* Get the 32-bit guest address */
1179 prot
= ava
& vmmlProt
; /* Extract the protection bits */
1180 adsp
= (ava
& vmmlAdID
) >> 4; /* Extract an explicit address space request */
1181 if(!adsp
) adsp
= index
- 1; /* If no explicit, use supplied default */
1182 ava
= ava
&= 0xFFFFFFFFFFFFF000ULL
; /* Clean up the address */
1184 ret
= vmm_map_page(act
, index
, cva
, ava
, prot
); /* Go try to map the page on in */
1185 if(ret
!= KERN_SUCCESS
) return ret
; /* Bail if any error */
1188 return KERN_SUCCESS
; /* Return... */
1191 /*-----------------------------------------------------------------------
1192 ** vmm_get_page_mapping
1194 ** Given a context index and a guest virtual address, convert the address
1195 ** to its corresponding host virtual address.
1198 ** act - pointer to current thread activation
1199 ** index - context index
1200 ** gva - guest virtual address
1203 ** Host virtual address (page aligned) or -1 if not mapped or any failure
1206 ** If the host address space contains multiple virtual addresses mapping
1207 ** to the physical address corresponding to the specified guest virtual
1208 ** address (i.e., host virtual aliases), it is unpredictable which host
1209 ** virtual address (alias) will be returned. Moral of the story: No host
1211 -----------------------------------------------------------------------*/
1213 addr64_t
vmm_get_page_mapping(
1215 vmm_adsp_id_t index
,
1218 register mapping_t
*mp
;
1220 addr64_t nextva
, hva
;
1223 pmap
= vmm_get_adsp(act
, index
); /* Get and validate the index */
1224 if (!pmap
)return -1; /* No good, failure... */
1226 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Guest shadow assist (GSA) active ? */
1227 return (hw_gva_to_hva(pmap
, gva
)); /* Convert guest to host virtual address */
1229 mp
= mapping_find(pmap
, gva
, &nextva
, 0); /* Find guest mapping for this virtual address */
1231 if(!mp
) return -1; /* Not mapped, return -1 */
1233 pa
= mp
->mpPAddr
; /* Remember the physical page address */
1235 mapping_drop_busy(mp
); /* Go ahead and relase the mapping now */
1237 pmap
= current_thread()->map
->pmap
; /* Get the host pmap */
1238 hva
= mapping_p2v(pmap
, pa
); /* Now find the source virtual */
1240 if(hva
!= 0) return hva
; /* We found it... */
1242 panic("vmm_get_page_mapping: could not back-map guest va (%016llX)\n", gva
);
1243 /* We are bad wrong if we can't find it */
1245 return -1; /* Never executed, prevents compiler warning */
1249 /*-----------------------------------------------------------------------
1252 ** This function unmaps a page from the guest address space.
1255 ** act - pointer to current thread activation
1256 ** index - index of vmm state for this page
1257 ** va - virtual address within the vmm's address
1261 ** kernel return code indicating success or failure
1262 -----------------------------------------------------------------------*/
1264 kern_return_t
vmm_unmap_page(
1266 vmm_adsp_id_t index
,
1269 vmmCntrlEntry
*CEntry
;
1273 pmap
= vmm_get_adsp(act
, index
); /* Get and validate the index */
1274 if (!pmap
)return -1; /* No good, failure... */
1276 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1277 hw_susp_map_gv(act
->map
->pmap
, pmap
, va
); /* Suspend the mapping */
1278 return (KERN_SUCCESS
); /* Always returns success */
1280 nadd
= mapping_remove(pmap
, va
); /* Toss the mapping */
1282 return ((nadd
& 1) ? KERN_FAILURE
: KERN_SUCCESS
); /* Return... */
1286 /*-----------------------------------------------------------------------
1289 ** This function unmaps a list of pages from the alternate's logical
1293 ** act - pointer to current thread activation
1294 ** index - index of vmm state for this page
1295 ** count - number of pages to release
1296 ** flavor - 0 if 32-bit, 1 if 64-bit
1297 ** vmcpComm in the comm page contains up to kVmmMaxUnmapPages to unmap
1300 ** kernel return code indicating success or failure
1301 ** KERN_FAILURE is returned if kVmmMaxUnmapPages is exceeded
1302 -----------------------------------------------------------------------*/
1304 kern_return_t
vmm_unmap_list(
1306 vmm_adsp_id_t index
,
1308 unsigned int flavor
)
1310 vmmCntrlEntry
*CEntry
;
1312 kern_return_t kern_result
= KERN_SUCCESS
;
1313 unsigned int *pgaddr
, i
;
1320 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1321 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't a vmm or the index is bogus */
1323 if(cnt
> kVmmMaxUnmapPages
) return KERN_FAILURE
; /* They tried to unmap too many */
1324 if(!cnt
) return KERN_SUCCESS
; /* If they said none, we're done... */
1326 lst
= (vmmUMList
*)lstx
= (vmmUMList64
*) &((vmm_comm_page_t
*)CEntry
->vmmContextKern
)->vmcpComm
[0]; /* Point to the first entry */
1328 for(i
= 0; i
< cnt
; i
++) { /* Step and release all pages in list */
1329 if(flavor
) { /* Check if 32- or 64-bit addresses */
1330 gva
= lstx
[i
].vmlava
; /* Get the 64-bit guest address */
1333 gva
= lst
[i
].vmlava
; /* Get the 32-bit guest address */
1336 adsp
= (gva
& vmmlAdID
) >> 4; /* Extract an explicit address space request */
1337 if(!adsp
) adsp
= index
- 1; /* If no explicit, use supplied default */
1338 pmap
= act
->machine
.vmmControl
->vmmAdsp
[adsp
]; /* Get the pmap for this request */
1339 if(!pmap
) continue; /* Ain't nuthin' mapped here, no durn map... */
1341 gva
= gva
&= 0xFFFFFFFFFFFFF000ULL
; /* Clean up the address */
1342 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1343 hw_susp_map_gv(act
->map
->pmap
, pmap
, gva
);
1344 /* Suspend the mapping */
1346 (void)mapping_remove(pmap
, gva
); /* Toss the mapping */
1350 return KERN_SUCCESS
; /* Return... */
1353 /*-----------------------------------------------------------------------
1354 ** vmm_unmap_all_pages
1356 ** This function unmaps all pages from the alternates's logical
1360 ** act - pointer to current thread activation
1361 ** index - index of context state
1367 ** All pages are unmapped, but the address space (i.e., pmap) is still alive
1368 -----------------------------------------------------------------------*/
1370 void vmm_unmap_all_pages(
1372 vmm_adsp_id_t index
)
1374 vmmCntrlEntry
*CEntry
;
1377 pmap
= vmm_get_adsp(act
, index
); /* Convert index to entry */
1378 if (!pmap
) return; /* Either this isn't vmm thread or the index is bogus */
1380 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1381 hw_rem_all_gv(pmap
); /* Remove all guest's mappings from shadow hash table */
1384 * Note: the pmap code won't deal with the last page in the address space, so handle it explicitly
1386 mapping_remove(pmap
, 0xFFFFFFFFFFFFF000LL
); /* Remove final page explicitly because we might have mapped it */
1387 pmap_remove(pmap
, 0, 0xFFFFFFFFFFFFF000LL
); /* Remove all entries from this map */
1393 /*-----------------------------------------------------------------------
1394 ** vmm_get_page_dirty_flag
1396 ** This function returns the changed flag of the page
1397 ** and optionally clears clears the flag.
1400 ** act - pointer to current thread activation
1401 ** index - index of vmm state for this page
1402 ** va - virtual address within the vmm's address
1404 ** reset - Clears dirty if true, untouched if not
1408 ** clears the dirty bit in the pte if requested
1411 ** The RC bits are merged into the global physical entry
1412 -----------------------------------------------------------------------*/
1414 boolean_t
vmm_get_page_dirty_flag(
1416 vmm_adsp_id_t index
,
1420 vmmCntrlEntry
*CEntry
;
1421 register mapping_t
*mpv
, *mp
;
1425 pmap
= vmm_get_adsp(act
, index
); /* Convert index to entry */
1426 if (!pmap
) return 1; /* Either this isn't vmm thread or the index is bogus */
1428 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1429 RC
= hw_test_rc_gv(act
->map
->pmap
, pmap
, va
, reset
);/* Fetch the RC bits and clear if requested */
1431 RC
= hw_test_rc(pmap
, (addr64_t
)va
, reset
); /* Fetch the RC bits and clear if requested */
1434 switch (RC
& mapRetCode
) { /* Decode return code */
1436 case mapRtOK
: /* Changed */
1437 return ((RC
& (unsigned int)mpC
) == (unsigned int)mpC
); /* Return if dirty or not */
1440 case mapRtNotFnd
: /* Didn't find it */
1441 return 1; /* Return dirty */
1445 panic("vmm_get_page_dirty_flag: hw_test_rc failed - rc = %d, pmap = %08X, va = %016llX\n", RC
, pmap
, va
);
1449 return 1; /* Return the change bit */
1453 /*-----------------------------------------------------------------------
1456 ** This function sets the protection bits of a mapped page
1459 ** act - pointer to current thread activation
1460 ** index - index of vmm state for this page
1461 ** va - virtual address within the vmm's address
1463 ** prot - Protection flags
1467 ** Protection bits of the mapping are modifed
1469 -----------------------------------------------------------------------*/
1471 kern_return_t
vmm_protect_page(
1473 vmm_adsp_id_t index
,
1477 vmmCntrlEntry
*CEntry
;
1482 pmap
= vmm_get_adsp(act
, index
); /* Convert index to entry */
1483 if (!pmap
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1485 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1486 ret
= hw_protect_gv(pmap
, va
, prot
); /* Try to change protection, GSA varient */
1488 ret
= hw_protect(pmap
, va
, prot
, &nextva
); /* Try to change protection */
1491 switch (ret
) { /* Decode return code */
1493 case mapRtOK
: /* All ok... */
1494 break; /* Outta here */
1496 case mapRtNotFnd
: /* Didn't find it */
1497 return KERN_SUCCESS
; /* Ok, return... */
1501 panic("vmm_protect_page: hw_protect failed - rc = %d, pmap = %08X, va = %016llX\n", ret
, pmap
, (addr64_t
)va
);
1505 if (!((getPerProc()->spcFlags
) & FamVMmode
)) {
1506 act
->machine
.vmmControl
->vmmLastMap
= va
& 0xFFFFFFFFFFFFF000ULL
; /* Remember the last mapping we made */
1507 act
->machine
.vmmControl
->vmmGFlags
= (act
->machine
.vmmControl
->vmmGFlags
& ~vmmLastAdSp
) | index
; /* Remember last address space */
1510 return KERN_SUCCESS
; /* Return */
1514 /*-----------------------------------------------------------------------
1515 ** vmm_protect_execute
1517 ** This function sets the protection bits of a mapped page
1518 ** and then directly starts executing.
1520 ** See description of vmm_protect_page for details
1523 ** See vmm_protect_page and vmm_map_execute
1526 ** Normal exit is to run the VM. Abnormal exit is triggered via a
1527 ** non-KERN_SUCCESS return from vmm_map_page or later during the
1528 ** attempt to transition into the VM.
1529 -----------------------------------------------------------------------*/
1531 vmm_return_code_t
vmm_protect_execute(
1533 vmm_thread_index_t index
,
1538 vmmCntrlEntry
*CEntry
;
1540 vmm_thread_index_t cndx
;
1542 cndx
= index
& 0xFF; /* Clean it up */
1543 CEntry
= vmm_get_entry(act
, cndx
); /* Get and validate the index */
1544 if (CEntry
== NULL
) return kVmmBogusContext
; /* Return bogus context */
1546 adsp
= (index
>> 8) & 0xFF; /* Get any requested address space */
1547 if(!adsp
) adsp
= (index
& 0xFF); /* If 0, use context ID as address space ID */
1549 if (((getPerProc()->spcFlags
) & FamVMmode
) && (CEntry
!= act
->machine
.vmmCEntry
))
1550 return kVmmBogusContext
; /* Yes, invalid index in Fam */
1552 ret
= vmm_protect_page(act
, adsp
, va
, prot
); /* Go try to change access */
1554 if(ret
== KERN_SUCCESS
) {
1555 act
->machine
.vmmControl
->vmmLastMap
= va
& 0xFFFFFFFFFFFFF000ULL
; /* Remember the last mapping we made */
1556 act
->machine
.vmmControl
->vmmGFlags
= (act
->machine
.vmmControl
->vmmGFlags
& ~vmmLastAdSp
) | cndx
; /* Remember last address space */
1557 vmm_execute_vm(act
, cndx
); /* Return was ok, launch the VM */
1560 return ret
; /* We had trouble of some kind (shouldn't happen) */
1565 /*-----------------------------------------------------------------------
1566 ** vmm_get_float_state
1568 ** This function causes the current floating point state to
1569 ** be saved into the shared context area. It also clears the
1570 ** vmmFloatCngd changed flag.
1573 ** act - pointer to current thread activation structure
1574 ** index - index returned by vmm_init_context
1578 -----------------------------------------------------------------------*/
1580 kern_return_t
vmm_get_float_state(
1582 vmm_thread_index_t index
)
1584 vmmCntrlEntry
*CEntry
;
1585 vmmCntrlTable
*CTable
;
1587 register struct savearea_fpu
*sv
;
1589 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1590 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1592 act
->machine
.specFlags
&= ~floatCng
; /* Clear the special flag */
1593 CEntry
->vmmContextKern
->vmmStat
&= ~vmmFloatCngd
; /* Clear the change indication */
1595 fpu_save(&CEntry
->vmmFacCtx
); /* Save context if live */
1597 if(sv
= CEntry
->vmmFacCtx
.FPUsave
) { /* Is there context yet? */
1598 bcopy((char *)&sv
->save_fp0
, (char *)&(CEntry
->vmmContextKern
->vmm_proc_state
.ppcFPRs
), 32 * 8); /* 32 registers */
1599 return KERN_SUCCESS
;
1603 for(i
= 0; i
< 32; i
++) { /* Initialize floating points */
1604 CEntry
->vmmContextKern
->vmm_proc_state
.ppcFPRs
[i
].d
= FloatInit
; /* Initial value */
1607 return KERN_SUCCESS
;
1610 /*-----------------------------------------------------------------------
1611 ** vmm_get_vector_state
1613 ** This function causes the current vector state to
1614 ** be saved into the shared context area. It also clears the
1615 ** vmmVectorCngd changed flag.
1618 ** act - pointer to current thread activation structure
1619 ** index - index returned by vmm_init_context
1623 -----------------------------------------------------------------------*/
1625 kern_return_t
vmm_get_vector_state(
1627 vmm_thread_index_t index
)
1629 vmmCntrlEntry
*CEntry
;
1630 vmmCntrlTable
*CTable
;
1632 unsigned int vrvalidwrk
;
1633 register struct savearea_vec
*sv
;
1635 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1636 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1638 vec_save(&CEntry
->vmmFacCtx
); /* Save context if live */
1640 act
->machine
.specFlags
&= ~vectorCng
; /* Clear the special flag */
1641 CEntry
->vmmContextKern
->vmmStat
&= ~vmmVectCngd
; /* Clear the change indication */
1643 if(sv
= CEntry
->vmmFacCtx
.VMXsave
) { /* Is there context yet? */
1645 vrvalidwrk
= sv
->save_vrvalid
; /* Get the valid flags */
1647 for(i
= 0; i
< 32; i
++) { /* Copy the saved registers and invalidate the others */
1648 if(vrvalidwrk
& 0x80000000) { /* Do we have a valid value here? */
1649 for(j
= 0; j
< 4; j
++) { /* If so, copy it over */
1650 CEntry
->vmmContextKern
->vmm_proc_state
.ppcVRs
[i
].i
[j
] = ((unsigned int *)&(sv
->save_vr0
))[(i
* 4) + j
];
1654 for(j
= 0; j
< 4; j
++) { /* Otherwise set to empty value */
1655 CEntry
->vmmContextKern
->vmm_proc_state
.ppcVRs
[i
].i
[j
] = QNaNbarbarian
[j
];
1659 vrvalidwrk
= vrvalidwrk
<< 1; /* Shift over to the next */
1663 return KERN_SUCCESS
;
1666 for(i
= 0; i
< 32; i
++) { /* Initialize vector registers */
1667 for(j
=0; j
< 4; j
++) { /* Do words */
1668 CEntry
->vmmContextKern
->vmm_proc_state
.ppcVRs
[i
].i
[j
] = QNaNbarbarian
[j
]; /* Initial value */
1672 return KERN_SUCCESS
;
1675 /*-----------------------------------------------------------------------
1678 ** This function causes a timer (in AbsoluteTime) for a specific time
1679 ** to be set It also clears the vmmTimerPop flag if the timer is actually
1680 ** set, it is cleared otherwise.
1682 ** A timer is cleared by setting setting the time to 0. This will clear
1683 ** the vmmTimerPop bit. Simply setting the timer to earlier than the
1684 ** current time clears the internal timer request, but leaves the
1685 ** vmmTimerPop flag set.
1689 ** act - pointer to current thread activation structure
1690 ** index - index returned by vmm_init_context
1691 ** timerhi - high order word of AbsoluteTime to pop
1692 ** timerlo - low order word of AbsoluteTime to pop
1695 ** timer set, vmmTimerPop cleared
1696 -----------------------------------------------------------------------*/
1698 kern_return_t
vmm_set_timer(
1700 vmm_thread_index_t index
,
1701 unsigned int timerhi
,
1702 unsigned int timerlo
)
1704 vmmCntrlEntry
*CEntry
;
1706 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1707 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1709 CEntry
->vmmTimer
= ((uint64_t)timerhi
<< 32) | timerlo
;
1711 vmm_timer_pop(act
); /* Go adjust all of the timer stuff */
1712 return KERN_SUCCESS
; /* Leave now... */
1716 /*-----------------------------------------------------------------------
1719 ** This function causes the timer for a specified VM to be
1720 ** returned in return_params[0] and return_params[1].
1721 ** Note that this is kind of funky for 64-bit VMs because we
1722 ** split the timer into two parts so that we still set parms 0 and 1.
1723 ** Obviously, we don't need to do this because the parms are 8 bytes
1728 ** act - pointer to current thread activation structure
1729 ** index - index returned by vmm_init_context
1732 ** Timer value set in return_params[0] and return_params[1].
1733 ** Set to 0 if timer is not set.
1734 -----------------------------------------------------------------------*/
1736 kern_return_t
vmm_get_timer(
1738 vmm_thread_index_t index
)
1740 vmmCntrlEntry
*CEntry
;
1741 vmmCntrlTable
*CTable
;
1743 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1744 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1746 if(CEntry
->vmmXAFlgs
& vmm64Bit
) { /* A 64-bit virtual machine? */
1747 CEntry
->vmmContextKern
->vmmRet
.vmmrp64
.return_params
[0] = (uint32_t)(CEntry
->vmmTimer
>> 32); /* Return the last timer value */
1748 CEntry
->vmmContextKern
->vmmRet
.vmmrp64
.return_params
[1] = (uint32_t)CEntry
->vmmTimer
; /* Return the last timer value */
1751 CEntry
->vmmContextKern
->vmmRet
.vmmrp32
.return_params
[0] = (CEntry
->vmmTimer
>> 32); /* Return the last timer value */
1752 CEntry
->vmmContextKern
->vmmRet
.vmmrp32
.return_params
[1] = (uint32_t)CEntry
->vmmTimer
; /* Return the last timer value */
1754 return KERN_SUCCESS
;
1758 /*-----------------------------------------------------------------------
1761 ** This function causes all timers in the array of VMs to be updated.
1762 ** All appropriate flags are set or reset. If a VM is currently
1763 ** running and its timer expired, it is intercepted.
1765 ** The qactTimer value is set to the lowest unexpired timer. It is
1766 ** zeroed if all timers are expired or have been reset.
1769 ** act - pointer to current thread activation structure
1772 ** timers set, vmmTimerPop cleared or set
1773 -----------------------------------------------------------------------*/
1778 vmmCntrlEntry
*CEntry
;
1779 vmmCntrlTable
*CTable
;
1781 uint64_t now
, soonest
;
1784 if(!((unsigned int)act
->machine
.vmmControl
& 0xFFFFFFFE)) { /* Are there any virtual machines? */
1785 panic("vmm_timer_pop: No virtual machines defined; act = %08X\n", act
);
1788 soonest
= 0xFFFFFFFFFFFFFFFFULL
; /* Max time */
1790 clock_get_uptime(&now
); /* What time is it? */
1792 CTable
= act
->machine
.vmmControl
; /* Make this easier */
1793 any
= 0; /* Haven't found a running unexpired timer yet */
1795 for(cvi
= 0; cvi
< kVmmMaxContexts
; cvi
++) { /* Cycle through all and check time now */
1797 if(!(CTable
->vmmc
[cvi
].vmmFlags
& vmmInUse
)) continue; /* Do not check if the entry is empty */
1799 if(CTable
->vmmc
[cvi
].vmmTimer
== 0) { /* Is the timer reset? */
1800 CTable
->vmmc
[cvi
].vmmFlags
&= ~vmmTimerPop
; /* Clear timer popped */
1801 CTable
->vmmc
[cvi
].vmmContextKern
->vmmStat
&= ~vmmTimerPop
; /* Clear timer popped */
1802 continue; /* Check next */
1805 if (CTable
->vmmc
[cvi
].vmmTimer
<= now
) {
1806 CTable
->vmmc
[cvi
].vmmFlags
|= vmmTimerPop
; /* Set timer popped here */
1807 CTable
->vmmc
[cvi
].vmmContextKern
->vmmStat
|= vmmTimerPop
; /* Set timer popped here */
1808 if((unsigned int)&CTable
->vmmc
[cvi
] == (unsigned int)act
->machine
.vmmCEntry
) { /* Is this the running VM? */
1809 sv
= find_user_regs(act
); /* Get the user state registers */
1810 if(!sv
) { /* Did we find something? */
1811 panic("vmm_timer_pop: no user context; act = %08X\n", act
);
1813 sv
->save_exception
= kVmmReturnNull
*4; /* Indicate that this is a null exception */
1814 vmm_force_exit(act
, sv
); /* Intercept a running VM */
1816 continue; /* Check the rest */
1818 else { /* It hasn't popped yet */
1819 CTable
->vmmc
[cvi
].vmmFlags
&= ~vmmTimerPop
; /* Set timer not popped here */
1820 CTable
->vmmc
[cvi
].vmmContextKern
->vmmStat
&= ~vmmTimerPop
; /* Set timer not popped here */
1823 any
= 1; /* Show we found an active unexpired timer */
1825 if (CTable
->vmmc
[cvi
].vmmTimer
< soonest
)
1826 soonest
= CTable
->vmmc
[cvi
].vmmTimer
;
1830 if (act
->machine
.qactTimer
== 0 || soonest
<= act
->machine
.qactTimer
)
1831 act
->machine
.qactTimer
= soonest
; /* Set lowest timer */
1839 /*-----------------------------------------------------------------------
1842 ** This function prevents the specified VM(s) to from running.
1843 ** If any is currently executing, the execution is intercepted
1844 ** with a code of kVmmStopped. Note that execution of the VM is
1845 ** blocked until a vmmExecuteVM is called with the start flag set to 1.
1846 ** This provides the ability for a thread to stop execution of a VM and
1847 ** insure that it will not be run until the emulator has processed the
1848 ** "virtual" interruption.
1851 ** vmmask - 32 bit mask corresponding to the VMs to put in stop state
1852 ** NOTE: if this mask is all 0s, any executing VM is intercepted with
1853 * a kVmmStopped (but not marked stopped), otherwise this is a no-op. Also note that there
1854 ** note that there is a potential race here and the VM may not stop.
1857 ** kernel return code indicating success
1858 ** or if no VMs are enabled, an invalid syscall exception.
1859 -----------------------------------------------------------------------*/
1861 int vmm_stop_vm(struct savearea
*save
)
1865 vmmCntrlTable
*CTable
;
1869 unsigned int vmmask
;
1870 ReturnHandler
*stopapc
;
1872 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
1874 task
= current_task(); /* Figure out who we are */
1876 task_lock(task
); /* Lock our task */
1878 fact
= (thread_t
)task
->threads
.next
; /* Get the first activation on task */
1879 act
= 0; /* Pretend we didn't find it yet */
1881 for(i
= 0; i
< task
->thread_count
; i
++) { /* All of the activations */
1882 if(fact
->machine
.vmmControl
) { /* Is this a virtual machine monitor? */
1883 act
= fact
; /* Yeah... */
1884 break; /* Bail the loop... */
1886 fact
= (thread_t
)fact
->task_threads
.next
; /* Go to the next one */
1889 if(!((unsigned int)act
)) { /* See if we have VMMs yet */
1890 task_unlock(task
); /* No, unlock the task */
1891 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1892 return 0; /* Go generate a syscall exception */
1895 thread_reference(act
);
1897 task_unlock(task
); /* Safe to release now */
1899 thread_mtx_lock(act
);
1901 CTable
= act
->machine
.vmmControl
; /* Get the pointer to the table */
1903 if(!((unsigned int)CTable
& -2)) { /* Are there any all the way up yet? */
1904 thread_mtx_unlock(act
); /* Unlock the activation */
1905 thread_deallocate(act
);
1906 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1907 return 0; /* Go generate a syscall exception */
1910 if(!(vmmask
= save
->save_r3
)) { /* Get the stop mask and check if all zeros */
1911 thread_mtx_unlock(act
); /* Unlock the activation */
1912 thread_deallocate(act
);
1913 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1914 save
->save_r3
= KERN_SUCCESS
; /* Set success */
1915 return 1; /* Return... */
1918 for(cvi
= 0; cvi
< kVmmMaxContexts
; cvi
++) { /* Search slots */
1919 if((0x80000000 & vmmask
) && (CTable
->vmmc
[cvi
].vmmFlags
& vmmInUse
)) { /* See if we need to stop and if it is in use */
1920 hw_atomic_or(&CTable
->vmmc
[cvi
].vmmFlags
, vmmXStop
); /* Set this one to stop */
1922 vmmask
= vmmask
<< 1; /* Slide mask over */
1925 if(hw_compare_and_store(0, 1, &act
->machine
.emPendRupts
)) { /* See if there is already a stop pending and lock out others if not */
1926 thread_mtx_unlock(act
); /* Already one pending, unlock the activation */
1927 thread_deallocate(act
);
1928 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1929 save
->save_r3
= KERN_SUCCESS
; /* Say we did it... */
1930 return 1; /* Leave */
1933 if(!(stopapc
= (ReturnHandler
*)kalloc(sizeof(ReturnHandler
)))) { /* Get a return handler control block */
1934 act
->machine
.emPendRupts
= 0; /* No memory, say we have given up request */
1935 thread_mtx_unlock(act
); /* Unlock the activation */
1936 thread_deallocate(act
);
1937 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1938 save
->save_r3
= KERN_RESOURCE_SHORTAGE
; /* No storage... */
1939 return 1; /* Return... */
1942 ml_set_interrupts_enabled(FALSE
); /* Disable interruptions for now */
1944 stopapc
->handler
= vmm_interrupt
; /* Set interruption routine */
1946 stopapc
->next
= act
->handlers
; /* Put our interrupt at the start of the list */
1947 act
->handlers
= stopapc
; /* Point to us */
1949 act_set_apc(act
); /* Set an APC AST */
1950 ml_set_interrupts_enabled(TRUE
); /* Enable interruptions now */
1952 thread_mtx_unlock(act
); /* Unlock the activation */
1953 thread_deallocate(act
);
1955 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1956 save
->save_r3
= KERN_SUCCESS
; /* Hip, hip, horay... */
1960 /*-----------------------------------------------------------------------
1963 ** This function is executed asynchronously from an APC AST.
1964 ** It is to be used for anything that needs to interrupt a running VM.
1965 ** This include any kind of interruption generation (other than timer pop)
1966 ** or entering the stopped state.
1969 ** ReturnHandler *rh - the return handler control block as required by the APC.
1970 ** thread_t act - the activation
1973 ** Whatever needed to be done is done.
1974 -----------------------------------------------------------------------*/
1976 void vmm_interrupt(ReturnHandler
*rh
, thread_t act
) {
1978 vmmCntrlTable
*CTable
;
1984 kfree(rh
, sizeof(ReturnHandler
)); /* Release the return handler block */
1986 inter
= ml_set_interrupts_enabled(FALSE
); /* Disable interruptions for now */
1988 act
->machine
.emPendRupts
= 0; /* Say that there are no more interrupts pending */
1989 CTable
= act
->machine
.vmmControl
; /* Get the pointer to the table */
1991 if(!((unsigned int)CTable
& -2)) return; /* Leave if we aren't doing VMs any more... */
1993 if(act
->machine
.vmmCEntry
&& (act
->machine
.vmmCEntry
->vmmFlags
& vmmXStop
)) { /* Do we need to stop the running guy? */
1994 sv
= find_user_regs(act
); /* Get the user state registers */
1995 if(!sv
) { /* Did we find something? */
1996 panic("vmm_interrupt: no user context; act = %08X\n", act
);
1998 sv
->save_exception
= kVmmStopped
*4; /* Set a "stopped" exception */
1999 vmm_force_exit(act
, sv
); /* Intercept a running VM */
2001 ml_set_interrupts_enabled(inter
); /* Put interrupts back to what they were */