2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /*-----------------------------------------------------------------------
31 ** C routines that we are adding to the MacOS X kernel.
33 -----------------------------------------------------------------------*/
35 #include <mach/mach_types.h>
36 #include <mach/kern_return.h>
37 #include <mach/host_info.h>
38 #include <kern/kern_types.h>
39 #include <kern/kalloc.h>
40 #include <kern/host.h>
41 #include <kern/task.h>
42 #include <kern/thread.h>
43 #include <ppc/exception.h>
44 #include <ppc/mappings.h>
45 #include <ppc/thread.h>
46 #include <ppc/savearea.h>
47 #include <ppc/misc_protos.h>
48 #include <ppc/fpu_protos.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_fault.h>
52 #include <ppc/vmachmon.h>
53 #include <ppc/lowglobals.h>
55 extern double FloatInit
;
56 extern unsigned long QNaNbarbarian
[4];
58 /*************************************************************************************
59 Virtual Machine Monitor Internal Routines
60 **************************************************************************************/
62 /*-----------------------------------------------------------------------
65 ** This function verifies and return a vmm context entry index
68 ** act - pointer to current thread activation
69 ** index - index into vmm control table (this is a "one based" value)
72 ** address of a vmmCntrlEntry or 0 if not found
73 -----------------------------------------------------------------------*/
75 static vmmCntrlEntry
*vmm_get_entry(
77 vmm_thread_index_t index
)
79 vmmCntrlTable
*CTable
;
80 vmmCntrlEntry
*CEntry
;
82 index
= index
& vmmTInum
; /* Clean up the index */
84 if (act
->machine
.vmmControl
== 0) return NULL
; /* No control table means no vmm */
85 if ((index
- 1) >= kVmmMaxContexts
) return NULL
; /* Index not in range */
87 CTable
= act
->machine
.vmmControl
; /* Make the address a bit more convienient */
88 CEntry
= &CTable
->vmmc
[index
- 1]; /* Point to the entry */
90 if (!(CEntry
->vmmFlags
& vmmInUse
)) return NULL
; /* See if the slot is actually in use */
95 /*-----------------------------------------------------------------------
98 ** This function verifies and returns the pmap for an address space.
99 ** If there is none and the request is valid, a pmap will be created.
102 ** act - pointer to current thread activation
103 ** index - index into vmm control table (this is a "one based" value)
106 ** address of a pmap or 0 if not found or could no be created
107 ** Note that if there is no pmap for the address space it will be created.
108 -----------------------------------------------------------------------*/
110 static pmap_t
vmm_get_adsp(thread_t act
, vmm_thread_index_t index
)
114 if (act
->machine
.vmmControl
== 0) return NULL
; /* No control table means no vmm */
115 if ((index
- 1) >= kVmmMaxContexts
) return NULL
; /* Index not in range */
117 pmap
= act
->machine
.vmmControl
->vmmAdsp
[index
- 1]; /* Get the pmap */
118 return (pmap
); /* and return it. */
121 /*-----------------------------------------------------------------------
122 ** vmm_build_shadow_hash
124 ** Allocate and initialize a shadow hash table.
126 ** This function assumes that PAGE_SIZE is 4k-bytes.
128 -----------------------------------------------------------------------*/
129 static pmap_vmm_ext
*vmm_build_shadow_hash(pmap_t pmap
)
131 pmap_vmm_ext
*ext
; /* VMM pmap extension we're building */
132 ppnum_t extPP
; /* VMM pmap extension physical page number */
133 kern_return_t ret
; /* Return code from various calls */
134 uint32_t pages
= GV_HPAGES
; /* Number of pages in the hash table */
135 vm_offset_t free
= VMX_HPIDX_OFFSET
; /* Offset into extension page of free area (128-byte aligned) */
136 uint32_t freeSize
= PAGE_SIZE
- free
; /* Number of free bytes in the extension page */
139 if ((pages
* sizeof(addr64_t
)) + (pages
* sizeof(vm_offset_t
)) > freeSize
) {
140 panic("vmm_build_shadow_hash: too little pmap_vmm_ext free space\n");
143 ret
= kmem_alloc_wired(kernel_map
, (vm_offset_t
*)&ext
, PAGE_SIZE
);
144 /* Allocate a page-sized extension block */
145 if (ret
!= KERN_SUCCESS
) return (NULL
); /* Return NULL for failed allocate */
146 bzero((char *)ext
, PAGE_SIZE
); /* Zero the entire extension block page */
148 extPP
= pmap_find_phys(kernel_pmap
, (vm_offset_t
)ext
);
149 /* Get extension block's physical page number */
150 if (!extPP
) { /* This should not fail, but then again... */
151 panic("vmm_build_shadow_hash: could not translate pmap_vmm_ext vaddr %p\n", ext
);
154 ext
->vmxSalt
= (addr64_t
)(vm_offset_t
)ext
^ ptoa_64(extPP
);
155 /* Set effective<->physical conversion salt */
156 ext
->vmxHostPmapPhys
= (addr64_t
)(vm_offset_t
)pmap
^ pmap
->pmapvr
;
157 /* Set host pmap's physical address */
158 ext
->vmxHostPmap
= pmap
; /* Set host pmap's effective address */
159 ext
->vmxHashPgIdx
= (addr64_t
*)((vm_offset_t
)ext
+ VMX_HPIDX_OFFSET
);
160 /* Allocate physical index */
161 ext
->vmxHashPgList
= (vm_offset_t
*)((vm_offset_t
)ext
+ VMX_HPLIST_OFFSET
);
162 /* Allocate page list */
163 ext
->vmxActiveBitmap
= (vm_offset_t
*)((vm_offset_t
)ext
+ VMX_ACTMAP_OFFSET
);
164 /* Allocate active mapping bitmap */
166 /* The hash table is typically larger than a single page, but we don't require it to be in a
167 contiguous virtual or physical chunk. So, we allocate it page by page, noting the effective and
168 physical address of each page in vmxHashPgList and vmxHashPgIdx, respectively. */
169 for (idx
= 0; idx
< pages
; idx
++) {
172 ret
= kmem_alloc_wired(kernel_map
, &ext
->vmxHashPgList
[idx
], PAGE_SIZE
);
173 /* Allocate a hash-table page */
174 if (ret
!= KERN_SUCCESS
) goto fail
; /* Allocation failed, exit through cleanup */
175 bzero((char *)ext
->vmxHashPgList
[idx
], PAGE_SIZE
); /* Zero the page */
176 ext
->vmxHashPgIdx
[idx
] = ptoa_64(pmap_find_phys(kernel_pmap
, (addr64_t
)ext
->vmxHashPgList
[idx
]));
177 /* Put page's physical address into index */
178 if (!ext
->vmxHashPgIdx
[idx
]) { /* Hash-table page's LRA failed */
179 panic("vmm_build_shadow_hash: could not translate hash-table vaddr %08X\n", ext
->vmxHashPgList
[idx
]);
181 map
= (mapping_t
*)ext
->vmxHashPgList
[idx
];
182 for (mapIdx
= 0; mapIdx
< GV_SLTS_PPG
; mapIdx
++) { /* Iterate over mappings in this page */
183 map
->mpFlags
= (mpGuest
| mpgFree
); /* Mark guest type and free */
184 map
= (mapping_t
*)((char *)map
+ GV_SLOT_SZ
); /* Next slot-sized mapping */
188 return (ext
); /* Return newly-minted VMM pmap extension */
191 for (idx
= 0; idx
< pages
; idx
++) { /* De-allocate any pages we managed to allocate */
192 if (ext
->vmxHashPgList
[idx
]) {
193 kmem_free(kernel_map
, ext
->vmxHashPgList
[idx
], PAGE_SIZE
);
196 kmem_free(kernel_map
, (vm_offset_t
)ext
, PAGE_SIZE
); /* Release the VMM pmap extension page */
197 return (NULL
); /* Return NULL for failure */
201 /*-----------------------------------------------------------------------
202 ** vmm_release_shadow_hash
204 ** Release shadow hash table and VMM extension block
206 -----------------------------------------------------------------------*/
207 static void vmm_release_shadow_hash(pmap_vmm_ext
*ext
)
211 for (idx
= 0; idx
< GV_HPAGES
; idx
++) { /* Release the hash table page by page */
212 kmem_free(kernel_map
, ext
->vmxHashPgList
[idx
], PAGE_SIZE
);
215 kmem_free(kernel_map
, (vm_offset_t
)ext
, PAGE_SIZE
); /* Release the VMM pmap extension page */
218 /*-----------------------------------------------------------------------
221 ** Activate guest shadow assist
223 -----------------------------------------------------------------------*/
224 static kern_return_t
vmm_activate_gsa(
226 vmm_thread_index_t index
)
228 vmmCntrlTable
*CTable
= act
->machine
.vmmControl
; /* Get VMM control table */
229 vmmCntrlEntry
*CEntry
;
232 if (!CTable
) { /* Caller guarantees that this will work */
233 panic("vmm_activate_gsa: VMM control table not present; act = %p, idx = %lu\n",
237 CEntry
= vmm_get_entry(act
, index
); /* Get context from index */
238 if (!CEntry
) { /* Caller guarantees that this will work */
239 panic("vmm_activate_gsa: Unexpected failure of vmm_get_entry; act = %p, idx = %lu\n",
244 hpmap
= act
->map
->pmap
; /* Get host pmap */
245 gpmap
= vmm_get_adsp(act
, index
); /* Get guest pmap */
246 if (!gpmap
) { /* Caller guarantees that this will work */
247 panic("vmm_activate_gsa: Unexpected failure of vmm_get_adsp; act = %p, idx = %lu\n",
252 if (!hpmap
->pmapVmmExt
) { /* If there's no VMM extension for this host, create one */
253 hpmap
->pmapVmmExt
= vmm_build_shadow_hash(hpmap
); /* Build VMM extension plus shadow hash and attach */
254 if (hpmap
->pmapVmmExt
) { /* See if we succeeded */
255 hpmap
->pmapVmmExtPhys
= (addr64_t
)(vm_offset_t
)hpmap
->pmapVmmExt
^ hpmap
->pmapVmmExt
->vmxSalt
;
256 /* Get VMM extensions block physical address */
258 return KERN_RESOURCE_SHORTAGE
; /* Not enough mojo to go */
261 gpmap
->pmapVmmExt
= hpmap
->pmapVmmExt
; /* Copy VMM extension block virtual address into guest */
262 gpmap
->pmapVmmExtPhys
= hpmap
->pmapVmmExtPhys
; /* and its physical address, too */
263 gpmap
->pmapFlags
|= pmapVMgsaa
; /* Enable GSA for this guest */
264 CEntry
->vmmXAFlgs
|= vmmGSA
; /* Show GSA active here, too */
270 /*-----------------------------------------------------------------------
271 ** vmm_deactivate_gsa
273 ** Deactivate guest shadow assist
275 -----------------------------------------------------------------------*/
279 vmm_thread_index_t index
)
281 vmmCntrlEntry
*CEntry
= vmm_get_entry(act
, index
); /* Get context from index */
283 if (!CEntry
) { /* Caller guarantees that this will work */
284 panic("vmm_deactivate_gsa: Unexpected failure of vmm_get_entry; act = %p, idx = %lu\n",
288 gpmap
= vmm_get_adsp(act
, index
); /* Get guest pmap */
289 if (!gpmap
) { /* Caller guarantees that this will work */
290 panic("vmm_deactivate_gsa: Unexpected failure of vmm_get_adsp; act = %p, idx = %lu\n",
294 gpmap
->pmapFlags
&= ~pmapVMgsaa
; /* Deactivate GSA for this guest */
295 CEntry
->vmmXAFlgs
&= ~vmmGSA
; /* Show GSA deactivated here, too */
299 /*-----------------------------------------------------------------------
302 ** Flush specified guest context, purging all guest mappings and clearing
305 -----------------------------------------------------------------------*/
306 static void vmm_flush_context(
308 vmm_thread_index_t index
)
310 vmmCntrlEntry
*CEntry
;
311 vmmCntrlTable
*CTable
;
312 vmm_state_page_t
*vks
;
313 vmm_version_t version
;
315 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
316 if (!CEntry
) { /* Caller guarantees that this will work */
317 panic("vmm_flush_context: Unexpected failure of vmm_get_entry; act = %p, idx = %lu\n",
322 if(CEntry
->vmmFacCtx
.FPUsave
) { /* Is there any floating point context? */
323 toss_live_fpu(&CEntry
->vmmFacCtx
); /* Get rid of any live context here */
324 save_release((struct savearea
*)CEntry
->vmmFacCtx
.FPUsave
); /* Release it */
327 if(CEntry
->vmmFacCtx
.VMXsave
) { /* Is there any vector context? */
328 toss_live_vec(&CEntry
->vmmFacCtx
); /* Get rid of any live context here */
329 save_release((struct savearea
*)CEntry
->vmmFacCtx
.VMXsave
); /* Release it */
332 vmm_unmap_all_pages(act
, index
); /* Blow away all mappings for this context */
334 CTable
= act
->machine
.vmmControl
; /* Get the control table address */
335 CTable
->vmmGFlags
= CTable
->vmmGFlags
& ~vmmLastAdSp
; /* Make sure we don't try to automap into this */
337 CEntry
->vmmFlags
&= vmmInUse
; /* Clear out all of the flags for this entry except in use */
338 CEntry
->vmmFacCtx
.FPUsave
= NULL
; /* Clear facility context control */
339 CEntry
->vmmFacCtx
.FPUlevel
= NULL
; /* Clear facility context control */
340 CEntry
->vmmFacCtx
.FPUcpu
= 0; /* Clear facility context control */
341 CEntry
->vmmFacCtx
.VMXsave
= NULL
; /* Clear facility context control */
342 CEntry
->vmmFacCtx
.VMXlevel
= NULL
; /* Clear facility context control */
343 CEntry
->vmmFacCtx
.VMXcpu
= 0; /* Clear facility context control */
345 vks
= CEntry
->vmmContextKern
; /* Get address of the context page */
346 version
= vks
->interface_version
; /* Save the version code */
347 bzero((char *)vks
, 4096); /* Clear all */
349 vks
->interface_version
= version
; /* Set our version code */
350 vks
->thread_index
= index
% vmmTInum
; /* Tell the user the index for this virtual machine */
352 /* Context is now flushed */
356 /*************************************************************************************
357 Virtual Machine Monitor Exported Functionality
359 The following routines are used to implement a quick-switch mechanism for
360 virtual machines that need to execute within their own processor envinroment
361 (including register and MMU state).
362 **************************************************************************************/
364 /*-----------------------------------------------------------------------
367 ** This function returns the current version of the virtual machine
368 ** interface. It is divided into two portions. The top 16 bits
369 ** represent the major version number, and the bottom 16 bits
370 ** represent the minor version number. Clients using the Vmm
371 ** functionality should make sure they are using a verison new
378 ** 32-bit number representing major/minor version of
380 -----------------------------------------------------------------------*/
382 int vmm_get_version(struct savearea
*save
)
384 save
->save_r3
= kVmmCurrentVersion
; /* Return the version */
389 /*-----------------------------------------------------------------------
392 ** This function returns a set of flags that represents the functionality
393 ** supported by the current verison of the Vmm interface. Clients should
394 ** use this to determine whether they can run on this system.
400 ** 32-bit number representing functionality supported by this
401 ** version of the Vmm module
402 -----------------------------------------------------------------------*/
404 int vmm_get_features(struct savearea
*save
)
406 save
->save_r3
= kVmmCurrentFeatures
; /* Return the features */
407 if(getPerProc()->pf
.Available
& pf64Bit
) {
408 save
->save_r3
&= ~kVmmFeature_LittleEndian
; /* No little endian here */
409 save
->save_r3
|= kVmmFeature_SixtyFourBit
; /* Set that we can do 64-bit */
415 /*-----------------------------------------------------------------------
418 ** This function returns the maximum addressable virtual address sported
421 ** Returns max address
422 -----------------------------------------------------------------------*/
425 vmm_max_addr(__unused thread_t act
)
427 return vm_max_address
; /* Return the maximum address */
430 /*-----------------------------------------------------------------------
433 ** This function retrieves the eXtended Architecture flags for the specifed VM.
435 ** We need to return the result in the return code rather than in the return parameters
436 ** because we need an architecture independent format so the results are actually
437 ** usable by the host. For example, the return parameters for 64-bit are 8 bytes wide vs.
442 ** act - pointer to current thread activation structure
443 ** index - index returned by vmm_init_context
446 ** Return code is set to the XA flags. If the index is invalid or the
447 ** context has not been created, we return 0.
448 -----------------------------------------------------------------------*/
450 unsigned int vmm_get_XA(
452 vmm_thread_index_t index
)
454 vmmCntrlEntry
*CEntry
;
456 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
457 if (CEntry
== NULL
) return 0; /* Either this isn't a vmm or the index is bogus */
459 return CEntry
->vmmXAFlgs
; /* Return the flags */
462 /*-----------------------------------------------------------------------
465 ** This function initializes an emulation context. It allocates
466 ** a new pmap (address space) and fills in the initial processor
467 ** state within the specified structure. The structure, mapped
468 ** into the client's logical address space, must be page-aligned.
471 ** act - pointer to current thread activation
472 ** version - requested version of the Vmm interface (allowing
473 ** future versions of the interface to change, but still
474 ** support older clients)
475 ** vmm_user_state - pointer to a logical page within the
476 ** client's address space
479 ** kernel return code indicating success or failure
480 -----------------------------------------------------------------------*/
482 int vmm_init_context(struct savearea
*save
)
486 vmm_version_t version
;
487 vmm_state_page_t
* vmm_user_state
;
488 vmmCntrlTable
*CTable
;
490 vmm_state_page_t
* vks
;
499 vmm_user_state
= CAST_DOWN(vmm_state_page_t
*, save
->save_r4
); /* Get the user address of the comm area */
500 if ((unsigned int)vmm_user_state
& (PAGE_SIZE
- 1)) { /* Make sure the comm area is page aligned */
501 save
->save_r3
= KERN_FAILURE
; /* Return failure */
505 /* Make sure that the version requested is supported */
506 version
= save
->save_r3
; /* Pick up passed in version */
507 if (((version
>> 16) < kVmmMinMajorVersion
) || ((version
>> 16) > (kVmmCurrentVersion
>> 16))) {
508 save
->save_r3
= KERN_FAILURE
; /* Return failure */
512 if((version
& 0xFFFF) > kVmmCurMinorVersion
) { /* Check for valid minor */
513 save
->save_r3
= KERN_FAILURE
; /* Return failure */
517 act
= current_thread(); /* Pick up our activation */
519 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
521 task
= current_task(); /* Figure out who we are */
523 task_lock(task
); /* Lock our task */
525 fact
= (thread_t
)task
->threads
.next
; /* Get the first activation on task */
526 gact
= NULL
; /* Pretend we didn't find it yet */
528 for(i
= 0; i
< task
->thread_count
; i
++) { /* All of the activations */
529 if(fact
->machine
.vmmControl
) { /* Is this a virtual machine monitor? */
530 gact
= fact
; /* Yeah... */
531 break; /* Bail the loop... */
533 fact
= (thread_t
)fact
->task_threads
.next
; /* Go to the next one */
538 * We only allow one thread per task to be a virtual machine monitor right now. This solves
539 * a number of potential problems that I can't put my finger on right now.
541 * Utlimately, I think we want to move the controls and make all this task based instead of
542 * thread based. That would allow an emulator architecture to spawn a kernel thread for each
543 * VM (if they want) rather than hand dispatch contexts.
546 if(gact
&& (gact
!= act
)) { /* Check if another thread is a vmm or trying to be */
547 task_unlock(task
); /* Release task lock */
548 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
549 save
->save_r3
= KERN_FAILURE
; /* We must play alone... */
553 if(!gact
) act
->machine
.vmmControl
= (vmmCntrlTable
*)1; /* Temporarily mark that we are the vmm thread */
555 task_unlock(task
); /* Safe to release now (because we've marked ourselves) */
557 CTable
= act
->machine
.vmmControl
; /* Get the control table address */
558 if ((unsigned int)CTable
== 1) { /* If we are marked, try to allocate a new table, otherwise we have one */
559 if(!(CTable
= (vmmCntrlTable
*)kalloc(sizeof(vmmCntrlTable
)))) { /* Get a fresh emulation control table */
560 act
->machine
.vmmControl
= NULL
; /* Unmark us as vmm 'cause we failed */
561 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
562 save
->save_r3
= KERN_RESOURCE_SHORTAGE
; /* No storage... */
566 bzero((void *)CTable
, sizeof(vmmCntrlTable
)); /* Clean it up */
567 act
->machine
.vmmControl
= CTable
; /* Initialize the table anchor */
570 for(cvi
= 0; cvi
< kVmmMaxContexts
; cvi
++) { /* Search to find a free slot */
571 if(!(CTable
->vmmc
[cvi
].vmmFlags
& vmmInUse
)) break; /* Bail if we find an unused slot */
574 if(cvi
>= kVmmMaxContexts
) { /* Did we find one? */
575 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
576 save
->save_r3
= KERN_RESOURCE_SHORTAGE
; /* No empty slots... */
580 ret
= vm_map_wire( /* Wire the virtual machine monitor's context area */
582 (vm_offset_t
)vmm_user_state
,
583 (vm_offset_t
)vmm_user_state
+ PAGE_SIZE
,
584 VM_PROT_READ
| VM_PROT_WRITE
,
587 if (ret
!= KERN_SUCCESS
) /* The wire failed, return the code */
588 goto return_in_shame
;
590 /* Map the vmm state into the kernel's address space. */
591 conphys
= pmap_find_phys(act
->map
->pmap
, (addr64_t
)((uintptr_t)vmm_user_state
));
593 /* Find a virtual address to use. */
594 ret
= kmem_alloc_pageable(kernel_map
, &conkern
, PAGE_SIZE
);
595 if (ret
!= KERN_SUCCESS
) { /* Did we find an address? */
596 (void) vm_map_unwire(act
->map
, /* No, unwire the context area */
597 (vm_offset_t
)vmm_user_state
,
598 (vm_offset_t
)vmm_user_state
+ PAGE_SIZE
,
600 goto return_in_shame
;
603 /* Map it into the kernel's address space. */
605 pmap_enter(kernel_pmap
, conkern
, conphys
,
606 VM_PROT_READ
| VM_PROT_WRITE
,
607 VM_WIMG_USE_DEFAULT
, TRUE
);
609 /* Clear the vmm state structure. */
610 vks
= (vmm_state_page_t
*)conkern
;
611 bzero((char *)vks
, PAGE_SIZE
);
614 /* We're home free now. Simply fill in the necessary info and return. */
616 vks
->interface_version
= version
; /* Set our version code */
617 vks
->thread_index
= cvi
+ 1; /* Tell the user the index for this virtual machine */
619 CTable
->vmmc
[cvi
].vmmFlags
= vmmInUse
; /* Mark the slot in use and make sure the rest are clear */
620 CTable
->vmmc
[cvi
].vmmContextKern
= vks
; /* Remember the kernel address of comm area */
621 CTable
->vmmc
[cvi
].vmmContextPhys
= conphys
; /* Remember the state page physical addr */
622 CTable
->vmmc
[cvi
].vmmContextUser
= vmm_user_state
; /* Remember user address of comm area */
624 CTable
->vmmc
[cvi
].vmmFacCtx
.FPUsave
= NULL
; /* Clear facility context control */
625 CTable
->vmmc
[cvi
].vmmFacCtx
.FPUlevel
= NULL
; /* Clear facility context control */
626 CTable
->vmmc
[cvi
].vmmFacCtx
.FPUcpu
= 0; /* Clear facility context control */
627 CTable
->vmmc
[cvi
].vmmFacCtx
.VMXsave
= NULL
; /* Clear facility context control */
628 CTable
->vmmc
[cvi
].vmmFacCtx
.VMXlevel
= NULL
; /* Clear facility context control */
629 CTable
->vmmc
[cvi
].vmmFacCtx
.VMXcpu
= 0; /* Clear facility context control */
630 CTable
->vmmc
[cvi
].vmmFacCtx
.facAct
= act
; /* Point back to the activation */
632 (void)hw_atomic_add(&saveanchor
.savetarget
, 2); /* Account for the number of extra saveareas we think we might "need" */
634 hpmap
= act
->map
->pmap
; /* Get host pmap */
635 gpmap
= pmap_create(0, FALSE
); /* Make a fresh guest pmap */
636 if (gpmap
) { /* Did we succeed ? */
637 CTable
->vmmAdsp
[cvi
] = gpmap
; /* Remember guest pmap for new context */
638 if (lowGlo
.lgVMMforcedFeats
& vmmGSA
) { /* Forcing on guest shadow assist ? */
639 vmm_activate_gsa(act
, cvi
+1); /* Activate GSA */
642 ret
= KERN_RESOURCE_SHORTAGE
; /* We've failed to allocate a guest pmap */
643 goto return_in_shame
; /* Shame on us. */
646 if (!(hpmap
->pmapFlags
& pmapVMhost
)) { /* Do this stuff if this is our first time hosting */
647 hpmap
->pmapFlags
|= pmapVMhost
; /* We're now hosting */
650 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
651 save
->save_r3
= KERN_SUCCESS
; /* Hip, hip, horay... */
655 if(!gact
) kfree(CTable
, sizeof(vmmCntrlTable
)); /* Toss the table if we just allocated it */
656 act
->machine
.vmmControl
= NULL
; /* Unmark us as vmm 'cause we failed */
657 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
658 save
->save_r3
= ret
; /* Pass back return code... */
664 /*-----------------------------------------------------------------------
665 ** vmm_tear_down_context
667 ** This function uninitializes an emulation context. It deallocates
668 ** internal resources associated with the context block.
671 ** act - pointer to current thread activation structure
672 ** index - index returned by vmm_init_context
675 ** kernel return code indicating success or failure
678 ** This call will also trash the address space with the same ID. While this
679 ** is really not too cool, we have to do it because we need to make
680 ** sure that old VMM users (not that we really have any) who depend upon
681 ** the address space going away with the context still work the same.
682 -----------------------------------------------------------------------*/
684 kern_return_t
vmm_tear_down_context(
686 vmm_thread_index_t index
)
688 vmmCntrlEntry
*CEntry
;
689 vmmCntrlTable
*CTable
;
694 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
695 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
697 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
699 (void)hw_atomic_sub(&saveanchor
.savetarget
, 2); /* We don't need these extra saveareas anymore */
701 if(CEntry
->vmmFacCtx
.FPUsave
) { /* Is there any floating point context? */
702 toss_live_fpu(&CEntry
->vmmFacCtx
); /* Get rid of any live context here */
703 save_release((struct savearea
*)CEntry
->vmmFacCtx
.FPUsave
); /* Release it */
706 if(CEntry
->vmmFacCtx
.VMXsave
) { /* Is there any vector context? */
707 toss_live_vec(&CEntry
->vmmFacCtx
); /* Get rid of any live context here */
708 save_release((struct savearea
*)CEntry
->vmmFacCtx
.VMXsave
); /* Release it */
711 CEntry
->vmmPmap
= NULL
; /* Remove this trace */
712 gpmap
= act
->machine
.vmmControl
->vmmAdsp
[index
- 1];
713 /* Get context's guest pmap (if any) */
714 if (gpmap
) { /* Check if there is an address space assigned here */
715 if (gpmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist case specially */
716 hw_rem_all_gv(gpmap
); /* Remove all guest mappings from shadow hash table */
718 mapping_remove(gpmap
, 0xFFFFFFFFFFFFF000LL
);/* Remove final page explicitly because we might have mapped it */
719 pmap_remove(gpmap
, 0, 0xFFFFFFFFFFFFF000LL
);/* Remove all entries from this map */
721 pmap_destroy(gpmap
); /* Toss the pmap for this context */
722 act
->machine
.vmmControl
->vmmAdsp
[index
- 1] = NULL
; /* Clean it up */
725 (void) vm_map_unwire( /* Unwire the user comm page */
727 (vm_offset_t
)CEntry
->vmmContextUser
,
728 (vm_offset_t
)CEntry
->vmmContextUser
+ PAGE_SIZE
,
731 kmem_free(kernel_map
, (vm_offset_t
)CEntry
->vmmContextKern
, PAGE_SIZE
); /* Remove kernel's view of the comm page */
733 CTable
= act
->machine
.vmmControl
; /* Get the control table address */
734 CTable
->vmmGFlags
= CTable
->vmmGFlags
& ~vmmLastAdSp
; /* Make sure we don't try to automap into this */
736 CEntry
->vmmFlags
= 0; /* Clear out all of the flags for this entry including in use */
737 CEntry
->vmmContextKern
= NULL
; /* Clear the kernel address of comm area */
738 CEntry
->vmmContextUser
= NULL
; /* Clear the user address of comm area */
740 CEntry
->vmmFacCtx
.FPUsave
= NULL
; /* Clear facility context control */
741 CEntry
->vmmFacCtx
.FPUlevel
= NULL
; /* Clear facility context control */
742 CEntry
->vmmFacCtx
.FPUcpu
= 0; /* Clear facility context control */
743 CEntry
->vmmFacCtx
.VMXsave
= NULL
; /* Clear facility context control */
744 CEntry
->vmmFacCtx
.VMXlevel
= NULL
; /* Clear facility context control */
745 CEntry
->vmmFacCtx
.VMXcpu
= 0; /* Clear facility context control */
746 CEntry
->vmmFacCtx
.facAct
= NULL
; /* Clear facility context control */
748 for(cvi
= 0; cvi
< kVmmMaxContexts
; cvi
++) { /* Search to find a free slot */
749 if(CTable
->vmmc
[cvi
].vmmFlags
& vmmInUse
) { /* Return if there are still some in use */
750 ml_set_interrupts_enabled(FALSE
); /* No more interruptions */
751 return KERN_SUCCESS
; /* Leave... */
756 * When we have tossed the last context, toss any address spaces left over before releasing
757 * the VMM control block
760 for(cvi
= 1; cvi
<= kVmmMaxContexts
; cvi
++) { /* Look at all slots */
761 if(!act
->machine
.vmmControl
->vmmAdsp
[index
- 1]) continue; /* Nothing to remove here */
762 mapping_remove(act
->machine
.vmmControl
->vmmAdsp
[index
- 1], 0xFFFFFFFFFFFFF000LL
); /* Remove final page explicitly because we might have mapped it */
763 pmap_remove(act
->machine
.vmmControl
->vmmAdsp
[index
- 1], 0, 0xFFFFFFFFFFFFF000LL
); /* Remove all entries from this map */
764 pmap_destroy(act
->machine
.vmmControl
->vmmAdsp
[index
- 1]); /* Toss the pmap for this context */
765 act
->machine
.vmmControl
->vmmAdsp
[index
- 1] = NULL
; /* Clear just in case */
768 pmap
= act
->map
->pmap
; /* Get our pmap */
769 if (pmap
->pmapVmmExt
) { /* Release any VMM pmap extension block and shadow hash table */
770 vmm_release_shadow_hash(pmap
->pmapVmmExt
); /* Release extension block and shadow hash table */
771 pmap
->pmapVmmExt
= NULL
; /* Forget extension block */
772 pmap
->pmapVmmExtPhys
= 0; /* Forget extension block's physical address, too */
774 pmap
->pmapFlags
&= ~pmapVMhost
; /* We're no longer hosting */
776 kfree(CTable
, sizeof(vmmCntrlTable
)); /* Toss the table because to tossed the last context */
777 act
->machine
.vmmControl
= NULL
; /* Unmark us as vmm */
779 ml_set_interrupts_enabled(FALSE
); /* No more interruptions */
785 /*-----------------------------------------------------------------------
788 ** This function activates the eXtended Architecture flags for the specifed VM.
790 ** We need to return the result in the return code rather than in the return parameters
791 ** because we need an architecture independent format so the results are actually
792 ** usable by the host. For example, the return parameters for 64-bit are 8 bytes wide vs.
795 ** Note that this function does a lot of the same stuff as vmm_tear_down_context
796 ** and vmm_init_context.
799 ** act - pointer to current thread activation structure
800 ** index - index returned by vmm_init_context
801 ** flags - the extended architecture flags
805 ** KERN_SUCCESS if vm is valid and initialized. KERN_FAILURE if not.
806 ** Also, the internal flags are set and, additionally, the VM is completely reset.
807 -----------------------------------------------------------------------*/
808 kern_return_t
vmm_activate_XA(
810 vmm_thread_index_t index
,
811 unsigned int xaflags
)
813 vmmCntrlEntry
*CEntry
;
814 kern_return_t result
= KERN_SUCCESS
; /* Assume success */
816 if ((xaflags
& ~kVmmSupportedSetXA
) || ((xaflags
& vmm64Bit
) && (!getPerProc()->pf
.Available
& pf64Bit
)))
817 return (KERN_FAILURE
); /* Unknown or unsupported feature requested */
819 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
820 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't a vmm or the index is bogus */
822 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
824 vmm_flush_context(act
, index
); /* Flush the context */
826 if (xaflags
& vmm64Bit
) { /* Activating 64-bit mode ? */
827 CEntry
->vmmXAFlgs
|= vmm64Bit
; /* Activate 64-bit mode */
830 if (xaflags
& vmmGSA
) { /* Activating guest shadow assist ? */
831 result
= vmm_activate_gsa(act
, index
); /* Activate guest shadow assist */
834 ml_set_interrupts_enabled(FALSE
); /* No more interruptions */
836 return result
; /* Return activate result */
839 /*-----------------------------------------------------------------------
842 -----------------------------------------------------------------------*/
843 kern_return_t
vmm_deactivate_XA(
845 vmm_thread_index_t index
,
846 unsigned int xaflags
)
848 vmmCntrlEntry
*CEntry
;
849 kern_return_t result
= KERN_SUCCESS
; /* Assume success */
851 if ((xaflags
& ~kVmmSupportedSetXA
) || ((xaflags
& vmm64Bit
) && (getPerProc()->pf
.Available
& pf64Bit
)))
852 return (KERN_FAILURE
); /* Unknown or unsupported feature requested */
854 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
855 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't a vmm or the index is bogus */
857 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
859 vmm_flush_context(act
, index
); /* Flush the context */
861 if (xaflags
& vmm64Bit
) { /* Deactivating 64-bit mode ? */
862 CEntry
->vmmXAFlgs
&= ~vmm64Bit
; /* Deactivate 64-bit mode */
865 if (xaflags
& vmmGSA
) { /* Deactivating guest shadow assist ? */
866 vmm_deactivate_gsa(act
, index
); /* Deactivate guest shadow assist */
869 ml_set_interrupts_enabled(FALSE
); /* No more interruptions */
871 return result
; /* Return deactivate result */
875 /*-----------------------------------------------------------------------
878 ** This function uninitializes all emulation contexts. If there are
879 ** any vmm contexts, it calls vmm_tear_down_context for each one.
881 ** Note: this can also be called from normal thread termination. Because of
882 ** that, we will context switch out of an alternate if we are currenty in it.
883 ** It will be terminated with no valid return code set because we don't expect
884 ** the activation to ever run again.
887 ** activation to tear down
890 ** All vmm contexts released and VMM shut down
891 -----------------------------------------------------------------------*/
892 void vmm_tear_down_all(thread_t act
) {
894 vmmCntrlTable
*CTable
;
897 struct savearea
*save
;
900 if(act
->machine
.specFlags
& runningVM
) { /* Are we actually in a context right now? */
901 save
= find_user_regs(act
); /* Find the user state context */
902 if(!save
) { /* Did we find it? */
903 panic("vmm_tear_down_all: runningVM marked but no user state context\n");
907 save
->save_exception
= kVmmBogusContext
*4; /* Indicate that this context is bogus now */
908 s
= splhigh(); /* Make sure interrupts are off */
909 vmm_force_exit(act
, save
); /* Force and exit from VM state */
910 splx(s
); /* Restore interrupts */
913 if(act
->machine
.vmmControl
) { /* Do we have a vmm control block? */
914 CTable
= act
->machine
.vmmControl
;
915 for(cvi
= 1; cvi
<= kVmmMaxContexts
; cvi
++) { /* Look at all slots */
916 if(CTable
->vmmc
[cvi
- 1].vmmFlags
& vmmInUse
) { /* Is this one in use */
917 ret
= vmm_tear_down_context(act
, cvi
); /* Take down the found context */
918 if(ret
!= KERN_SUCCESS
) { /* Did it go away? */
919 panic("vmm_tear_down_all: vmm_tear_down_context failed; ret=%08X, act = %p, cvi = %d\n",
926 * Note that all address apces should be gone here.
928 if(act
->machine
.vmmControl
) { /* Did we find one? */
929 panic("vmm_tear_down_all: control table did not get deallocated\n"); /* Table did not go away */
934 /*-----------------------------------------------------------------------
937 ** This function maps a page from within the client's logical
938 ** address space into the alternate address space.
940 ** The page need not be locked or resident. If not resident, it will be faulted
941 ** in by this code, which may take some time. Also, if the page is not locked,
942 ** it, and this mapping may disappear at any time, even before it gets used. Note also
943 ** that reference and change information is NOT preserved when a page is unmapped, either
944 ** explicitly or implicitly (e.g., a pageout, being unmapped in the non-alternate address
945 ** space). This means that if RC is needed, the page MUST be wired.
947 ** Note that if there is already a mapping at the address, it is removed and all
948 ** information (including RC) is lost BEFORE an attempt is made to map it. Also,
949 ** if the map call fails, the old address is still unmapped..
952 ** act - pointer to current thread activation
953 ** index - index of address space to map into
954 ** va - virtual address within the client's address
956 ** ava - virtual address within the alternate address
958 ** prot - protection flags
960 ** Note that attempted mapping of areas in nested pmaps (shared libraries) or block mapped
961 ** areas are not allowed and will fail. Same with directly mapped I/O areas.
964 ** Interrupts disabled (from fast trap)
967 ** kernel return code indicating success or failure
968 ** if success, va resident and alternate mapping made
969 -----------------------------------------------------------------------*/
971 kern_return_t
vmm_map_page(
979 register mapping_t
*mp
;
981 addr64_t ova
, nextva
;
984 pmap
= vmm_get_adsp(act
, index
); /* Get the guest pmap for this address space */
985 if(!pmap
) return KERN_FAILURE
; /* Bogus address space, no VMs, or we can't make a pmap, failure... */
987 if(ava
> vm_max_address
) return kVmmInvalidAddress
; /* Does the machine support an address of this size? */
989 map
= current_thread()->map
; /* Get the host's map */
991 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Guest shadow assist active ? */
992 ret
= hw_res_map_gv(map
->pmap
, pmap
, cva
, ava
, getProtPPC(prot
, TRUE
));
993 /* Attempt to resume an existing gv->phys mapping */
994 if (mapRtOK
!= ret
) { /* Nothing to resume, construct a new mapping */
996 phys_entry_t
*physent
;
1002 while (1) { /* Find host mapping or fail */
1003 mp
= mapping_find(map
->pmap
, cva
, &nextva
, 0);
1004 /* Attempt to find host mapping and pin it */
1005 if (mp
) break; /* Got it */
1007 ml_set_interrupts_enabled(TRUE
);
1008 /* Open 'rupt window */
1009 ret
= vm_fault(map
, /* Didn't find it, try to fault in host page read/write */
1010 vm_map_trunc_page(cva
),
1011 VM_PROT_READ
| VM_PROT_WRITE
,
1012 FALSE
, /* change wiring */
1016 ml_set_interrupts_enabled(FALSE
);
1017 /* Close 'rupt window */
1018 if (ret
!= KERN_SUCCESS
)
1019 return KERN_FAILURE
; /* Fault failed, return failure */
1022 if (mpNormal
!= (mp
->mpFlags
& mpType
)) {
1023 /* Host mapping must be a vanilla page */
1024 mapping_drop_busy(mp
); /* Un-pin host mapping */
1025 return KERN_FAILURE
; /* Return failure */
1028 /* Partially construct gv->phys mapping */
1029 physent
= mapping_phys_lookup(mp
->mpPAddr
, &pindex
);
1031 mapping_drop_busy(mp
);
1032 return KERN_FAILURE
;
1034 pattr
= ((physent
->ppLink
& (ppI
| ppG
)) >> 60);
1036 if (pattr
& mmFlgCInhib
) wimg
|= 0x4;
1037 if (pattr
& mmFlgGuarded
) wimg
|= 0x1;
1038 mflags
= (pindex
<< 16) | mpGuest
;
1039 gva
= ((ava
& ~mpHWFlags
) | (wimg
<< 3) | getProtPPC(prot
, TRUE
));
1041 hw_add_map_gv(map
->pmap
, pmap
, gva
, mflags
, mp
->mpPAddr
);
1042 /* Construct new guest->phys mapping */
1044 mapping_drop_busy(mp
); /* Un-pin host mapping */
1047 while(1) { /* Keep trying until we get it or until we fail */
1049 mp
= mapping_find(map
->pmap
, cva
, &nextva
, 0); /* Find the mapping for this address */
1051 if(mp
) break; /* We found it */
1053 ml_set_interrupts_enabled(TRUE
); /* Enable interruptions */
1054 ret
= vm_fault(map
, /* Didn't find it, try to fault it in read/write... */
1055 vm_map_trunc_page(cva
),
1056 VM_PROT_READ
| VM_PROT_WRITE
,
1057 FALSE
, /*change wiring */
1061 ml_set_interrupts_enabled(FALSE
); /* Disable interruptions */
1062 if (ret
!= KERN_SUCCESS
) return KERN_FAILURE
; /* There isn't a page there, return... */
1065 if((mp
->mpFlags
& mpType
) != mpNormal
) { /* If this is a block, a nest, or some other special thing, we can't map it */
1066 mapping_drop_busy(mp
); /* We have everything we need from the mapping */
1067 return KERN_FAILURE
; /* Leave in shame */
1070 while(1) { /* Keep trying the enter until it goes in */
1071 ova
= mapping_make(pmap
, ava
, mp
->mpPAddr
, 0, 1, prot
); /* Enter the mapping into the pmap */
1072 if(!ova
) break; /* If there were no collisions, we are done... */
1073 mapping_remove(pmap
, ova
); /* Remove the mapping that collided */
1076 mapping_drop_busy(mp
); /* We have everything we need from the mapping */
1079 if (!((getPerProc()->spcFlags
) & FamVMmode
)) {
1080 act
->machine
.vmmControl
->vmmLastMap
= ava
& 0xFFFFFFFFFFFFF000ULL
; /* Remember the last mapping we made */
1081 act
->machine
.vmmControl
->vmmGFlags
= (act
->machine
.vmmControl
->vmmGFlags
& ~vmmLastAdSp
) | index
; /* Remember last address space */
1084 return KERN_SUCCESS
;
1088 /*-----------------------------------------------------------------------
1091 ** This function maps a page from within the client's logical
1092 ** address space into the alternate address space of the
1093 ** Virtual Machine Monitor context and then directly starts executing.
1095 ** See description of vmm_map_page for details.
1098 ** Index is used for both the context and the address space ID.
1099 ** index[24:31] is the context id and index[16:23] is the address space.
1100 ** if the address space ID is 0, the context ID is used for it.
1103 ** Normal exit is to run the VM. Abnormal exit is triggered via a
1104 ** non-KERN_SUCCESS return from vmm_map_page or later during the
1105 ** attempt to transition into the VM.
1106 -----------------------------------------------------------------------*/
1108 vmm_return_code_t
vmm_map_execute(
1110 vmm_thread_index_t index
,
1116 vmmCntrlEntry
*CEntry
;
1118 vmm_thread_index_t cndx
;
1120 cndx
= index
& 0xFF; /* Clean it up */
1122 CEntry
= vmm_get_entry(act
, cndx
); /* Get and validate the index */
1123 if (CEntry
== NULL
) return kVmmBogusContext
; /* Return bogus context */
1125 if (((getPerProc()->spcFlags
) & FamVMmode
) && (CEntry
!= act
->machine
.vmmCEntry
))
1126 return kVmmBogusContext
; /* Yes, invalid index in Fam */
1128 adsp
= (index
>> 8) & 0xFF; /* Get any requested address space */
1129 if(!adsp
) adsp
= (index
& 0xFF); /* If 0, use context ID as address space ID */
1131 ret
= vmm_map_page(act
, adsp
, cva
, ava
, prot
); /* Go try to map the page on in */
1134 if(ret
== KERN_SUCCESS
) {
1135 act
->machine
.vmmControl
->vmmLastMap
= ava
& 0xFFFFFFFFFFFFF000ULL
; /* Remember the last mapping we made */
1136 act
->machine
.vmmControl
->vmmGFlags
= (act
->machine
.vmmControl
->vmmGFlags
& ~vmmLastAdSp
) | cndx
; /* Remember last address space */
1137 vmm_execute_vm(act
, cndx
); /* Return was ok, launch the VM */
1140 return ret
; /* We had trouble mapping in the page */
1144 /*-----------------------------------------------------------------------
1147 ** This function maps a list of pages into various address spaces
1150 ** act - pointer to current thread activation
1151 ** index - index of default address space (used if not specifed in list entry
1152 ** count - number of pages to release
1153 ** flavor - 0 if 32-bit version, 1 if 64-bit
1154 ** vmcpComm in the comm page contains up to kVmmMaxMapPages to map
1157 ** kernel return code indicating success or failure
1158 ** KERN_FAILURE is returned if kVmmMaxUnmapPages is exceeded
1159 ** or the vmm_map_page call fails.
1160 ** We return kVmmInvalidAddress if virtual address size is not supported
1161 -----------------------------------------------------------------------*/
1163 kern_return_t
vmm_map_list(
1165 vmm_adsp_id_t index
,
1167 unsigned int flavor
)
1169 vmmCntrlEntry
*CEntry
;
1179 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1180 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't a vmm or the index is bogus */
1182 if(cnt
> kVmmMaxMapPages
) return KERN_FAILURE
; /* They tried to map too many */
1183 if(!cnt
) return KERN_SUCCESS
; /* If they said none, we're done... */
1185 lst
= (vmmMList
*)&((vmm_comm_page_t
*)CEntry
->vmmContextKern
)->vmcpComm
[0]; /* Point to the first entry */
1186 lstx
= (vmmMList64
*)&((vmm_comm_page_t
*)CEntry
->vmmContextKern
)->vmcpComm
[0]; /* Point to the first entry */
1188 for(i
= 0; i
< cnt
; i
++) { /* Step and release all pages in list */
1189 if(flavor
) { /* Check if 32- or 64-bit addresses */
1190 cva
= lstx
[i
].vmlva
; /* Get the 64-bit actual address */
1191 ava
= lstx
[i
].vmlava
; /* Get the 64-bit guest address */
1194 cva
= lst
[i
].vmlva
; /* Get the 32-bit actual address */
1195 ava
= lst
[i
].vmlava
; /* Get the 32-bit guest address */
1198 prot
= ava
& vmmlProt
; /* Extract the protection bits */
1199 adsp
= (ava
& vmmlAdID
) >> 4; /* Extract an explicit address space request */
1200 if(!adsp
) /* If no explicit, use supplied default */
1202 ava
&= 0xFFFFFFFFFFFFF000ULL
; /* Clean up the address */
1204 ret
= vmm_map_page(act
, index
, cva
, ava
, prot
); /* Go try to map the page on in */
1205 if(ret
!= KERN_SUCCESS
) /* Bail if any error */
1209 return KERN_SUCCESS
;
1212 /*-----------------------------------------------------------------------
1213 ** vmm_get_page_mapping
1215 ** Given a context index and a guest virtual address, convert the address
1216 ** to its corresponding host virtual address.
1219 ** act - pointer to current thread activation
1220 ** index - context index
1221 ** gva - guest virtual address
1224 ** Host virtual address (page aligned) or -1 if not mapped or any failure
1227 ** If the host address space contains multiple virtual addresses mapping
1228 ** to the physical address corresponding to the specified guest virtual
1229 ** address (i.e., host virtual aliases), it is unpredictable which host
1230 ** virtual address (alias) will be returned. Moral of the story: No host
1232 -----------------------------------------------------------------------*/
1234 addr64_t
vmm_get_page_mapping(
1236 vmm_adsp_id_t index
,
1239 register mapping_t
*mp
;
1241 addr64_t nextva
, hva
;
1244 pmap
= vmm_get_adsp(act
, index
); /* Get and validate the index */
1245 if (!pmap
)return -1; /* No good, failure... */
1247 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Guest shadow assist (GSA) active ? */
1248 return (hw_gva_to_hva(pmap
, gva
)); /* Convert guest to host virtual address */
1250 mp
= mapping_find(pmap
, gva
, &nextva
, 0); /* Find guest mapping for this virtual address */
1252 if(!mp
) return -1; /* Not mapped, return -1 */
1254 pa
= mp
->mpPAddr
; /* Remember the physical page address */
1256 mapping_drop_busy(mp
); /* Go ahead and relase the mapping now */
1258 pmap
= current_thread()->map
->pmap
; /* Get the host pmap */
1259 hva
= mapping_p2v(pmap
, pa
); /* Now find the source virtual */
1261 if(hva
!= 0) return hva
; /* We found it... */
1263 panic("vmm_get_page_mapping: could not back-map guest va (%016llX)\n", gva
);
1264 /* We are bad wrong if we can't find it */
1266 return -1; /* Never executed, prevents compiler warning */
1270 /*-----------------------------------------------------------------------
1273 ** This function unmaps a page from the guest address space.
1276 ** act - pointer to current thread activation
1277 ** index - index of vmm state for this page
1278 ** va - virtual address within the vmm's address
1282 ** kernel return code indicating success or failure
1283 -----------------------------------------------------------------------*/
1285 kern_return_t
vmm_unmap_page(
1287 vmm_adsp_id_t index
,
1293 pmap
= vmm_get_adsp(act
, index
); /* Get and validate the index */
1294 if (!pmap
)return -1; /* No good, failure... */
1296 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1297 hw_susp_map_gv(act
->map
->pmap
, pmap
, va
); /* Suspend the mapping */
1298 return (KERN_SUCCESS
); /* Always returns success */
1300 nadd
= mapping_remove(pmap
, va
); /* Toss the mapping */
1302 return ((nadd
& 1) ? KERN_FAILURE
: KERN_SUCCESS
); /* Return... */
1306 /*-----------------------------------------------------------------------
1309 ** This function unmaps a list of pages from the alternate's logical
1313 ** act - pointer to current thread activation
1314 ** index - index of vmm state for this page
1315 ** count - number of pages to release
1316 ** flavor - 0 if 32-bit, 1 if 64-bit
1317 ** vmcpComm in the comm page contains up to kVmmMaxUnmapPages to unmap
1320 ** kernel return code indicating success or failure
1321 ** KERN_FAILURE is returned if kVmmMaxUnmapPages is exceeded
1322 -----------------------------------------------------------------------*/
1324 kern_return_t
vmm_unmap_list(
1326 vmm_adsp_id_t index
,
1328 unsigned int flavor
)
1330 vmmCntrlEntry
*CEntry
;
1331 kern_return_t kern_result
= KERN_SUCCESS
;
1339 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1340 if (CEntry
== NULL
) { /* Either this isn't a vmm or the index is bogus */
1341 kern_result
= KERN_FAILURE
;
1345 if(cnt
> kVmmMaxUnmapPages
) { /* They tried to unmap too many */
1346 kern_result
= KERN_FAILURE
;
1349 if(!cnt
) { /* If they said none, we're done... */
1350 kern_result
= KERN_SUCCESS
;
1354 lstx
= (vmmUMList64
*) &((vmm_comm_page_t
*)CEntry
->vmmContextKern
)->vmcpComm
[0]; /* Point to the first entry */
1355 lst
= (vmmUMList
*)lstx
;
1357 for(i
= 0; i
< cnt
; i
++) { /* Step and release all pages in list */
1358 if(flavor
) { /* Check if 32- or 64-bit addresses */
1359 gva
= lstx
[i
].vmlava
; /* Get the 64-bit guest address */
1362 gva
= lst
[i
].vmlava
; /* Get the 32-bit guest address */
1365 adsp
= (gva
& vmmlAdID
) >> 4; /* Extract an explicit address space request */
1366 if(!adsp
) /* If no explicit, use supplied default */
1368 pmap
= act
->machine
.vmmControl
->vmmAdsp
[adsp
]; /* Get the pmap for this request */
1370 continue; /* Ain't nuthin' mapped here, no durn map... */
1372 gva
&= 0xFFFFFFFFFFFFF000ULL
; /* Clean up the address */
1373 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1374 hw_susp_map_gv(act
->map
->pmap
, pmap
, gva
);
1375 /* Suspend the mapping */
1377 (void)mapping_remove(pmap
, gva
); /* Toss the mapping */
1385 /*-----------------------------------------------------------------------
1386 ** vmm_unmap_all_pages
1388 ** This function unmaps all pages from the alternates's logical
1392 ** act - pointer to current thread activation
1393 ** index - index of context state
1399 ** All pages are unmapped, but the address space (i.e., pmap) is still alive
1400 -----------------------------------------------------------------------*/
1402 void vmm_unmap_all_pages(
1404 vmm_adsp_id_t index
)
1408 pmap
= vmm_get_adsp(act
, index
); /* Convert index to entry */
1409 if (!pmap
) return; /* Either this isn't vmm thread or the index is bogus */
1411 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1412 hw_rem_all_gv(pmap
); /* Remove all guest's mappings from shadow hash table */
1415 * Note: the pmap code won't deal with the last page in the address space, so handle it explicitly
1417 mapping_remove(pmap
, 0xFFFFFFFFFFFFF000LL
); /* Remove final page explicitly because we might have mapped it */
1418 pmap_remove(pmap
, 0, 0xFFFFFFFFFFFFF000LL
); /* Remove all entries from this map */
1423 /*-----------------------------------------------------------------------
1424 ** vmm_get_page_dirty_flag
1426 ** This function returns the changed flag of the page
1427 ** and optionally clears clears the flag.
1430 ** act - pointer to current thread activation
1431 ** index - index of vmm state for this page
1432 ** va - virtual address within the vmm's address
1434 ** reset - Clears dirty if true, untouched if not
1438 ** clears the dirty bit in the pte if requested
1441 ** The RC bits are merged into the global physical entry
1442 -----------------------------------------------------------------------*/
1444 boolean_t
vmm_get_page_dirty_flag(
1446 vmm_adsp_id_t index
,
1453 pmap
= vmm_get_adsp(act
, index
); /* Convert index to entry */
1454 if (!pmap
) return 1; /* Either this isn't vmm thread or the index is bogus */
1456 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1457 RC
= hw_test_rc_gv(act
->map
->pmap
, pmap
, va
, reset
);/* Fetch the RC bits and clear if requested */
1459 RC
= hw_test_rc(pmap
, (addr64_t
)va
, reset
); /* Fetch the RC bits and clear if requested */
1462 switch (RC
& mapRetCode
) { /* Decode return code */
1464 case mapRtOK
: /* Changed */
1465 return ((RC
& (unsigned int)mpC
) == (unsigned int)mpC
); /* Return if dirty or not */
1468 case mapRtNotFnd
: /* Didn't find it */
1469 return 1; /* Return dirty */
1473 panic("vmm_get_page_dirty_flag: hw_test_rc failed - rc = %d, pmap = %p, va = %016llX\n", RC
, pmap
, va
);
1477 return 1; /* Return the change bit */
1481 /*-----------------------------------------------------------------------
1484 ** This function sets the protection bits of a mapped page
1487 ** act - pointer to current thread activation
1488 ** index - index of vmm state for this page
1489 ** va - virtual address within the vmm's address
1491 ** prot - Protection flags
1495 ** Protection bits of the mapping are modifed
1497 -----------------------------------------------------------------------*/
1499 kern_return_t
vmm_protect_page(
1501 vmm_adsp_id_t index
,
1509 pmap
= vmm_get_adsp(act
, index
); /* Convert index to entry */
1510 if (!pmap
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1512 if (pmap
->pmapFlags
& pmapVMgsaa
) { /* Handle guest shadow assist specially */
1513 ret
= hw_protect_gv(pmap
, va
, prot
); /* Try to change protection, GSA varient */
1515 ret
= hw_protect(pmap
, va
, prot
, &nextva
); /* Try to change protection */
1518 switch (ret
) { /* Decode return code */
1520 case mapRtOK
: /* All ok... */
1521 break; /* Outta here */
1523 case mapRtNotFnd
: /* Didn't find it */
1524 return KERN_SUCCESS
; /* Ok, return... */
1528 panic("vmm_protect_page: hw_protect failed - rc = %d, pmap = %p, va = %016llX\n", ret
, pmap
, (addr64_t
)va
);
1532 if (!((getPerProc()->spcFlags
) & FamVMmode
)) {
1533 act
->machine
.vmmControl
->vmmLastMap
= va
& 0xFFFFFFFFFFFFF000ULL
; /* Remember the last mapping we made */
1534 act
->machine
.vmmControl
->vmmGFlags
= (act
->machine
.vmmControl
->vmmGFlags
& ~vmmLastAdSp
) | index
; /* Remember last address space */
1537 return KERN_SUCCESS
; /* Return */
1541 /*-----------------------------------------------------------------------
1542 ** vmm_protect_execute
1544 ** This function sets the protection bits of a mapped page
1545 ** and then directly starts executing.
1547 ** See description of vmm_protect_page for details
1550 ** See vmm_protect_page and vmm_map_execute
1553 ** Normal exit is to run the VM. Abnormal exit is triggered via a
1554 ** non-KERN_SUCCESS return from vmm_map_page or later during the
1555 ** attempt to transition into the VM.
1556 -----------------------------------------------------------------------*/
1558 vmm_return_code_t
vmm_protect_execute(
1560 vmm_thread_index_t index
,
1565 vmmCntrlEntry
*CEntry
;
1567 vmm_thread_index_t cndx
;
1569 cndx
= index
& 0xFF; /* Clean it up */
1570 CEntry
= vmm_get_entry(act
, cndx
); /* Get and validate the index */
1571 if (CEntry
== NULL
) return kVmmBogusContext
; /* Return bogus context */
1573 adsp
= (index
>> 8) & 0xFF; /* Get any requested address space */
1574 if(!adsp
) adsp
= (index
& 0xFF); /* If 0, use context ID as address space ID */
1576 if (((getPerProc()->spcFlags
) & FamVMmode
) && (CEntry
!= act
->machine
.vmmCEntry
))
1577 return kVmmBogusContext
; /* Yes, invalid index in Fam */
1579 ret
= vmm_protect_page(act
, adsp
, va
, prot
); /* Go try to change access */
1581 if(ret
== KERN_SUCCESS
) {
1582 act
->machine
.vmmControl
->vmmLastMap
= va
& 0xFFFFFFFFFFFFF000ULL
; /* Remember the last mapping we made */
1583 act
->machine
.vmmControl
->vmmGFlags
= (act
->machine
.vmmControl
->vmmGFlags
& ~vmmLastAdSp
) | cndx
; /* Remember last address space */
1584 vmm_execute_vm(act
, cndx
); /* Return was ok, launch the VM */
1587 return ret
; /* We had trouble of some kind (shouldn't happen) */
1592 /*-----------------------------------------------------------------------
1593 ** vmm_get_float_state
1595 ** This function causes the current floating point state to
1596 ** be saved into the shared context area. It also clears the
1597 ** vmmFloatCngd changed flag.
1600 ** act - pointer to current thread activation structure
1601 ** index - index returned by vmm_init_context
1605 -----------------------------------------------------------------------*/
1607 kern_return_t
vmm_get_float_state(
1609 vmm_thread_index_t index
)
1611 vmmCntrlEntry
*CEntry
;
1613 register struct savearea_fpu
*sv
;
1615 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1616 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1618 act
->machine
.specFlags
&= ~floatCng
; /* Clear the special flag */
1619 CEntry
->vmmContextKern
->vmmStat
&= ~vmmFloatCngd
; /* Clear the change indication */
1621 fpu_save(&CEntry
->vmmFacCtx
); /* Save context if live */
1623 if(CEntry
->vmmFacCtx
.FPUsave
) { /* Is there context yet? */
1624 sv
= CEntry
->vmmFacCtx
.FPUsave
;
1625 bcopy((char *)&sv
->save_fp0
, (char *)&(CEntry
->vmmContextKern
->vmm_proc_state
.ppcFPRs
), 32 * 8); /* 32 registers */
1626 return KERN_SUCCESS
;
1630 for(i
= 0; i
< 32; i
++) { /* Initialize floating points */
1631 CEntry
->vmmContextKern
->vmm_proc_state
.ppcFPRs
[i
].d
= FloatInit
; /* Initial value */
1634 return KERN_SUCCESS
;
1637 /*-----------------------------------------------------------------------
1638 ** vmm_get_vector_state
1640 ** This function causes the current vector state to
1641 ** be saved into the shared context area. It also clears the
1642 ** vmmVectorCngd changed flag.
1645 ** act - pointer to current thread activation structure
1646 ** index - index returned by vmm_init_context
1650 -----------------------------------------------------------------------*/
1652 kern_return_t
vmm_get_vector_state(
1654 vmm_thread_index_t index
)
1656 vmmCntrlEntry
*CEntry
;
1658 unsigned int vrvalidwrk
;
1659 register struct savearea_vec
*sv
;
1661 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1662 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1664 vec_save(&CEntry
->vmmFacCtx
); /* Save context if live */
1666 act
->machine
.specFlags
&= ~vectorCng
; /* Clear the special flag */
1667 CEntry
->vmmContextKern
->vmmStat
&= ~vmmVectCngd
; /* Clear the change indication */
1669 if(CEntry
->vmmFacCtx
.VMXsave
) { /* Is there context yet? */
1670 sv
= CEntry
->vmmFacCtx
.VMXsave
;
1671 vrvalidwrk
= sv
->save_vrvalid
; /* Get the valid flags */
1673 for(i
= 0; i
< 32; i
++) { /* Copy the saved registers and invalidate the others */
1674 if(vrvalidwrk
& 0x80000000) { /* Do we have a valid value here? */
1675 for(j
= 0; j
< 4; j
++) { /* If so, copy it over */
1676 CEntry
->vmmContextKern
->vmm_proc_state
.ppcVRs
[i
].i
[j
] = ((unsigned int *)&(sv
->save_vr0
))[(i
* 4) + j
];
1680 for(j
= 0; j
< 4; j
++) { /* Otherwise set to empty value */
1681 CEntry
->vmmContextKern
->vmm_proc_state
.ppcVRs
[i
].i
[j
] = QNaNbarbarian
[j
];
1685 vrvalidwrk
= vrvalidwrk
<< 1; /* Shift over to the next */
1689 return KERN_SUCCESS
;
1692 for(i
= 0; i
< 32; i
++) { /* Initialize vector registers */
1693 for(j
=0; j
< 4; j
++) { /* Do words */
1694 CEntry
->vmmContextKern
->vmm_proc_state
.ppcVRs
[i
].i
[j
] = QNaNbarbarian
[j
]; /* Initial value */
1698 return KERN_SUCCESS
;
1701 /*-----------------------------------------------------------------------
1704 ** This function causes a timer (in AbsoluteTime) for a specific time
1705 ** to be set It also clears the vmmTimerPop flag if the timer is actually
1706 ** set, it is cleared otherwise.
1708 ** A timer is cleared by setting setting the time to 0. This will clear
1709 ** the vmmTimerPop bit. Simply setting the timer to earlier than the
1710 ** current time clears the internal timer request, but leaves the
1711 ** vmmTimerPop flag set.
1715 ** act - pointer to current thread activation structure
1716 ** index - index returned by vmm_init_context
1717 ** timerhi - high order word of AbsoluteTime to pop
1718 ** timerlo - low order word of AbsoluteTime to pop
1721 ** timer set, vmmTimerPop cleared
1722 -----------------------------------------------------------------------*/
1724 kern_return_t
vmm_set_timer(
1726 vmm_thread_index_t index
,
1727 unsigned int timerhi
,
1728 unsigned int timerlo
)
1730 vmmCntrlEntry
*CEntry
;
1732 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1733 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1735 CEntry
->vmmTimer
= ((uint64_t)timerhi
<< 32) | timerlo
;
1737 vmm_timer_pop(act
); /* Go adjust all of the timer stuff */
1738 return KERN_SUCCESS
; /* Leave now... */
1742 /*-----------------------------------------------------------------------
1745 ** This function causes the timer for a specified VM to be
1746 ** returned in return_params[0] and return_params[1].
1747 ** Note that this is kind of funky for 64-bit VMs because we
1748 ** split the timer into two parts so that we still set parms 0 and 1.
1749 ** Obviously, we don't need to do this because the parms are 8 bytes
1754 ** act - pointer to current thread activation structure
1755 ** index - index returned by vmm_init_context
1758 ** Timer value set in return_params[0] and return_params[1].
1759 ** Set to 0 if timer is not set.
1760 -----------------------------------------------------------------------*/
1762 kern_return_t
vmm_get_timer(
1764 vmm_thread_index_t index
)
1766 vmmCntrlEntry
*CEntry
;
1768 CEntry
= vmm_get_entry(act
, index
); /* Convert index to entry */
1769 if (CEntry
== NULL
) return KERN_FAILURE
; /* Either this isn't vmm thread or the index is bogus */
1771 if(CEntry
->vmmXAFlgs
& vmm64Bit
) { /* A 64-bit virtual machine? */
1772 CEntry
->vmmContextKern
->vmmRet
.vmmrp64
.return_params
[0] = (uint32_t)(CEntry
->vmmTimer
>> 32); /* Return the last timer value */
1773 CEntry
->vmmContextKern
->vmmRet
.vmmrp64
.return_params
[1] = (uint32_t)CEntry
->vmmTimer
; /* Return the last timer value */
1776 CEntry
->vmmContextKern
->vmmRet
.vmmrp32
.return_params
[0] = (CEntry
->vmmTimer
>> 32); /* Return the last timer value */
1777 CEntry
->vmmContextKern
->vmmRet
.vmmrp32
.return_params
[1] = (uint32_t)CEntry
->vmmTimer
; /* Return the last timer value */
1779 return KERN_SUCCESS
;
1783 /*-----------------------------------------------------------------------
1786 ** This function causes all timers in the array of VMs to be updated.
1787 ** All appropriate flags are set or reset. If a VM is currently
1788 ** running and its timer expired, it is intercepted.
1790 ** The qactTimer value is set to the lowest unexpired timer. It is
1791 ** zeroed if all timers are expired or have been reset.
1794 ** act - pointer to current thread activation structure
1797 ** timers set, vmmTimerPop cleared or set
1798 -----------------------------------------------------------------------*/
1803 vmmCntrlTable
*CTable
;
1805 uint64_t now
, soonest
;
1806 struct savearea
*sv
;
1808 if(!((unsigned int)act
->machine
.vmmControl
& 0xFFFFFFFE)) { /* Are there any virtual machines? */
1809 panic("vmm_timer_pop: No virtual machines defined; act = %p\n", act
);
1812 soonest
= 0xFFFFFFFFFFFFFFFFULL
; /* Max time */
1814 clock_get_uptime(&now
); /* What time is it? */
1816 CTable
= act
->machine
.vmmControl
; /* Make this easier */
1817 any
= 0; /* Haven't found a running unexpired timer yet */
1819 for(cvi
= 0; cvi
< kVmmMaxContexts
; cvi
++) { /* Cycle through all and check time now */
1821 if(!(CTable
->vmmc
[cvi
].vmmFlags
& vmmInUse
)) continue; /* Do not check if the entry is empty */
1823 if(CTable
->vmmc
[cvi
].vmmTimer
== 0) { /* Is the timer reset? */
1824 CTable
->vmmc
[cvi
].vmmFlags
&= ~vmmTimerPop
; /* Clear timer popped */
1825 CTable
->vmmc
[cvi
].vmmContextKern
->vmmStat
&= ~vmmTimerPop
; /* Clear timer popped */
1826 continue; /* Check next */
1829 if (CTable
->vmmc
[cvi
].vmmTimer
<= now
) {
1830 CTable
->vmmc
[cvi
].vmmFlags
|= vmmTimerPop
; /* Set timer popped here */
1831 CTable
->vmmc
[cvi
].vmmContextKern
->vmmStat
|= vmmTimerPop
; /* Set timer popped here */
1832 if((unsigned int)&CTable
->vmmc
[cvi
] == (unsigned int)act
->machine
.vmmCEntry
) { /* Is this the running VM? */
1833 sv
= find_user_regs(act
); /* Get the user state registers */
1834 if(!sv
) { /* Did we find something? */
1835 panic("vmm_timer_pop: no user context; act = %p\n", act
);
1837 sv
->save_exception
= kVmmReturnNull
*4; /* Indicate that this is a null exception */
1838 vmm_force_exit(act
, sv
); /* Intercept a running VM */
1840 continue; /* Check the rest */
1842 else { /* It hasn't popped yet */
1843 CTable
->vmmc
[cvi
].vmmFlags
&= ~vmmTimerPop
; /* Set timer not popped here */
1844 CTable
->vmmc
[cvi
].vmmContextKern
->vmmStat
&= ~vmmTimerPop
; /* Set timer not popped here */
1847 any
= 1; /* Show we found an active unexpired timer */
1849 if (CTable
->vmmc
[cvi
].vmmTimer
< soonest
)
1850 soonest
= CTable
->vmmc
[cvi
].vmmTimer
;
1854 if (act
->machine
.qactTimer
== 0 || soonest
<= act
->machine
.qactTimer
)
1855 act
->machine
.qactTimer
= soonest
; /* Set lowest timer */
1861 /*-----------------------------------------------------------------------
1864 ** This function prevents the specified VM(s) to from running.
1865 ** If any is currently executing, the execution is intercepted
1866 ** with a code of kVmmStopped. Note that execution of the VM is
1867 ** blocked until a vmmExecuteVM is called with the start flag set to 1.
1868 ** This provides the ability for a thread to stop execution of a VM and
1869 ** insure that it will not be run until the emulator has processed the
1870 ** "virtual" interruption.
1873 ** vmmask - 32 bit mask corresponding to the VMs to put in stop state
1874 ** NOTE: if this mask is all 0s, any executing VM is intercepted with
1875 * a kVmmStopped (but not marked stopped), otherwise this is a no-op. Also note that there
1876 ** note that there is a potential race here and the VM may not stop.
1879 ** kernel return code indicating success
1880 ** or if no VMs are enabled, an invalid syscall exception.
1881 -----------------------------------------------------------------------*/
1883 int vmm_stop_vm(struct savearea
*save
)
1887 vmmCntrlTable
*CTable
;
1891 unsigned int vmmask
;
1892 ReturnHandler
*stopapc
;
1894 ml_set_interrupts_enabled(TRUE
); /* This can take a bit of time so pass interruptions */
1896 task
= current_task(); /* Figure out who we are */
1898 task_lock(task
); /* Lock our task */
1900 fact
= (thread_t
)task
->threads
.next
; /* Get the first activation on task */
1901 act
= NULL
; /* Pretend we didn't find it yet */
1903 for(i
= 0; i
< task
->thread_count
; i
++) { /* All of the activations */
1904 if(fact
->machine
.vmmControl
) { /* Is this a virtual machine monitor? */
1905 act
= fact
; /* Yeah... */
1906 break; /* Bail the loop... */
1908 fact
= (thread_t
)fact
->task_threads
.next
; /* Go to the next one */
1911 if(!((unsigned int)act
)) { /* See if we have VMMs yet */
1912 task_unlock(task
); /* No, unlock the task */
1913 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1914 return 0; /* Go generate a syscall exception */
1917 thread_reference(act
);
1919 task_unlock(task
); /* Safe to release now */
1921 thread_mtx_lock(act
);
1923 CTable
= act
->machine
.vmmControl
; /* Get the pointer to the table */
1925 if(!((unsigned int)CTable
& -2)) { /* Are there any all the way up yet? */
1926 thread_mtx_unlock(act
); /* Unlock the activation */
1927 thread_deallocate(act
);
1928 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1929 return 0; /* Go generate a syscall exception */
1932 if(!(vmmask
= save
->save_r3
)) { /* Get the stop mask and check if all zeros */
1933 thread_mtx_unlock(act
); /* Unlock the activation */
1934 thread_deallocate(act
);
1935 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1936 save
->save_r3
= KERN_SUCCESS
; /* Set success */
1937 return 1; /* Return... */
1940 for(cvi
= 0; cvi
< kVmmMaxContexts
; cvi
++) { /* Search slots */
1941 if((0x80000000 & vmmask
) && (CTable
->vmmc
[cvi
].vmmFlags
& vmmInUse
)) { /* See if we need to stop and if it is in use */
1942 (void)hw_atomic_or(&CTable
->vmmc
[cvi
].vmmFlags
, vmmXStop
); /* Set this one to stop */
1944 vmmask
= vmmask
<< 1; /* Slide mask over */
1947 if(hw_compare_and_store(0, 1, &act
->machine
.emPendRupts
)) { /* See if there is already a stop pending and lock out others if not */
1948 thread_mtx_unlock(act
); /* Already one pending, unlock the activation */
1949 thread_deallocate(act
);
1950 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1951 save
->save_r3
= KERN_SUCCESS
; /* Say we did it... */
1952 return 1; /* Leave */
1955 if(!(stopapc
= (ReturnHandler
*)kalloc(sizeof(ReturnHandler
)))) { /* Get a return handler control block */
1956 act
->machine
.emPendRupts
= 0; /* No memory, say we have given up request */
1957 thread_mtx_unlock(act
); /* Unlock the activation */
1958 thread_deallocate(act
);
1959 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1960 save
->save_r3
= KERN_RESOURCE_SHORTAGE
; /* No storage... */
1961 return 1; /* Return... */
1964 ml_set_interrupts_enabled(FALSE
); /* Disable interruptions for now */
1966 stopapc
->handler
= vmm_interrupt
; /* Set interruption routine */
1968 stopapc
->next
= act
->handlers
; /* Put our interrupt at the start of the list */
1969 act
->handlers
= stopapc
; /* Point to us */
1971 act_set_apc(act
); /* Set an APC AST */
1972 ml_set_interrupts_enabled(TRUE
); /* Enable interruptions now */
1974 thread_mtx_unlock(act
); /* Unlock the activation */
1975 thread_deallocate(act
);
1977 ml_set_interrupts_enabled(FALSE
); /* Set back interruptions */
1978 save
->save_r3
= KERN_SUCCESS
; /* Hip, hip, horay... */
1982 /*-----------------------------------------------------------------------
1985 ** This function is executed asynchronously from an APC AST.
1986 ** It is to be used for anything that needs to interrupt a running VM.
1987 ** This include any kind of interruption generation (other than timer pop)
1988 ** or entering the stopped state.
1991 ** ReturnHandler *rh - the return handler control block as required by the APC.
1992 ** thread_t act - the activation
1995 ** Whatever needed to be done is done.
1996 -----------------------------------------------------------------------*/
1998 void vmm_interrupt(ReturnHandler
*rh
, thread_t act
) {
2000 vmmCntrlTable
*CTable
;
2001 struct savearea
*sv
;
2006 kfree(rh
, sizeof(ReturnHandler
)); /* Release the return handler block */
2008 inter
= ml_set_interrupts_enabled(FALSE
); /* Disable interruptions for now */
2010 act
->machine
.emPendRupts
= 0; /* Say that there are no more interrupts pending */
2011 CTable
= act
->machine
.vmmControl
; /* Get the pointer to the table */
2013 if(!((unsigned int)CTable
& -2)) return; /* Leave if we aren't doing VMs any more... */
2015 if(act
->machine
.vmmCEntry
&& (act
->machine
.vmmCEntry
->vmmFlags
& vmmXStop
)) { /* Do we need to stop the running guy? */
2016 sv
= find_user_regs(act
); /* Get the user state registers */
2017 if(!sv
) { /* Did we find something? */
2018 panic("vmm_interrupt: no user context; act = %p\n", act
);
2020 sv
->save_exception
= kVmmStopped
*4; /* Set a "stopped" exception */
2021 vmm_force_exit(act
, sv
); /* Intercept a running VM */
2023 ml_set_interrupts_enabled(inter
); /* Put interrupts back to what they were */