1 | /* $Id: MMAllPagePool.cpp 12989 2008-10-06 02:15:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MM - Memory Manager - Page Pool.
|
---|
4 | *
|
---|
5 | * @remarks This file is NOT built for the raw-mode context.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #define LOG_GROUP LOG_GROUP_MM_POOL
|
---|
28 | #include <VBox/mm.h>
|
---|
29 | #include <VBox/pgm.h>
|
---|
30 | #include <VBox/stam.h>
|
---|
31 | #include "MMInternal.h"
|
---|
32 | #include <VBox/vm.h>
|
---|
33 | #include <VBox/param.h>
|
---|
34 | #include <VBox/err.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 | #include <iprt/alloc.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #define USE_INLINE_ASM_BIT_OPS
|
---|
40 | #ifdef USE_INLINE_ASM_BIT_OPS
|
---|
41 | # include <iprt/asm.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Converts a pool address to a physical address.
|
---|
48 | * The specified allocation type must match with the address.
|
---|
49 | *
|
---|
50 | * @returns Physical address.
|
---|
51 | * @returns NIL_RTHCPHYS if not found or eType is not matching.
|
---|
52 | * @param pPool Pointer to the page pool.
|
---|
53 | * @param pv The address to convert.
|
---|
54 | * @thread The Emulation Thread.
|
---|
55 | */
|
---|
56 | VMMDECL(RTHCPHYS) mmPagePoolPtr2Phys(PMMPAGEPOOL pPool, void *pv)
|
---|
57 | {
|
---|
58 | #ifdef IN_RING3
|
---|
59 | VM_ASSERT_EMT(pPool->pVM);
|
---|
60 | #endif
|
---|
61 | /*
|
---|
62 | * Lookup the virtual address.
|
---|
63 | */
|
---|
64 | PMMPPLOOKUPHCPTR pLookup = (PMMPPLOOKUPHCPTR)RTAvlPVGetBestFit(&pPool->pLookupVirt, pv, false);
|
---|
65 | if (pLookup)
|
---|
66 | {
|
---|
67 | unsigned iPage = ((char *)pv - (char *)pLookup->pSubPool->pvPages) >> PAGE_SHIFT;
|
---|
68 | if (iPage < pLookup->pSubPool->cPages)
|
---|
69 | {
|
---|
70 | /*
|
---|
71 | * Convert the virtual address to a physical address.
|
---|
72 | */
|
---|
73 | STAM_COUNTER_INC(&pPool->cToPhysCalls);
|
---|
74 | AssertMsg( pLookup->pSubPool->paPhysPages[iPage].Phys
|
---|
75 | && !(pLookup->pSubPool->paPhysPages[iPage].Phys & PAGE_OFFSET_MASK),
|
---|
76 | ("Phys=%#x\n", pLookup->pSubPool->paPhysPages[iPage].Phys));
|
---|
77 | AssertMsg((uintptr_t)pLookup->pSubPool == pLookup->pSubPool->paPhysPages[iPage].uReserved,
|
---|
78 | ("pSubPool=%p uReserved=%p\n", pLookup->pSubPool, pLookup->pSubPool->paPhysPages[iPage].uReserved));
|
---|
79 | return pLookup->pSubPool->paPhysPages[iPage].Phys + ((uintptr_t)pv & PAGE_OFFSET_MASK);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return NIL_RTHCPHYS;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Converts a pool physical address to a linear address.
|
---|
87 | * The specified allocation type must match with the address.
|
---|
88 | *
|
---|
89 | * @returns Physical address.
|
---|
90 | * @returns NULL if not found or eType is not matching.
|
---|
91 | * @param pPool Pointer to the page pool.
|
---|
92 | * @param HCPhys The address to convert.
|
---|
93 | * @thread The Emulation Thread.
|
---|
94 | */
|
---|
95 | VMMDECL(void *) mmPagePoolPhys2Ptr(PMMPAGEPOOL pPool, RTHCPHYS HCPhys)
|
---|
96 | {
|
---|
97 | #if 0 /** @todo have to fix the debugger, but until then this is going on my nerves. */
|
---|
98 | #ifdef IN_RING3
|
---|
99 | VM_ASSERT_EMT(pPool->pVM);
|
---|
100 | #endif
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Lookup the virtual address.
|
---|
105 | */
|
---|
106 | PMMPPLOOKUPHCPHYS pLookup = (PMMPPLOOKUPHCPHYS)RTAvlHCPhysGet(&pPool->pLookupPhys, HCPhys & X86_PTE_PAE_PG_MASK);
|
---|
107 | if (pLookup)
|
---|
108 | {
|
---|
109 | STAM_COUNTER_INC(&pPool->cToVirtCalls);
|
---|
110 | PSUPPAGE pPhysPage = pLookup->pPhysPage;
|
---|
111 | PMMPAGESUBPOOL pSubPool = (PMMPAGESUBPOOL)pPhysPage->uReserved;
|
---|
112 | unsigned iPage = pPhysPage - pSubPool->paPhysPages;
|
---|
113 | return (char *)pSubPool->pvPages + (HCPhys & PAGE_OFFSET_MASK) + (iPage << PAGE_SHIFT);
|
---|
114 | }
|
---|
115 | return NULL;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Convert a page in the page pool to a HC physical address.
|
---|
121 | * This works for pages allocated by MMR3PageAlloc(), MMR3PageAllocPhys()
|
---|
122 | * and MMR3PageAllocLow().
|
---|
123 | *
|
---|
124 | * @returns Physical address for the specified page table.
|
---|
125 | * @param pVM VM handle.
|
---|
126 | * @param pvPage Page which physical address we query.
|
---|
127 | * @thread The Emulation Thread.
|
---|
128 | */
|
---|
129 | VMMDECL(RTHCPHYS) MMPage2Phys(PVM pVM, void *pvPage)
|
---|
130 | {
|
---|
131 | RTHCPHYS HCPhys = mmPagePoolPtr2Phys(pVM->mm.s.CTX_SUFF(pPagePool), pvPage);
|
---|
132 | if (HCPhys == NIL_RTHCPHYS)
|
---|
133 | {
|
---|
134 | HCPhys = mmPagePoolPtr2Phys(pVM->mm.s.CTX_SUFF(pPagePoolLow), pvPage);
|
---|
135 | if (HCPhys == NIL_RTHCPHYS)
|
---|
136 | {
|
---|
137 | STAM_COUNTER_INC(&pVM->mm.s.CTX_SUFF(pPagePool)->cErrors);
|
---|
138 | AssertMsgFailed(("Invalid pvPage=%p specified\n", pvPage));
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return HCPhys;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Convert physical address of a page to a HC virtual address.
|
---|
147 | * This works for pages allocated by MMR3PageAlloc(), MMR3PageAllocPhys()
|
---|
148 | * and MMR3PageAllocLow().
|
---|
149 | *
|
---|
150 | * @returns Pointer to the page at that physical address.
|
---|
151 | * @param pVM VM handle.
|
---|
152 | * @param HCPhysPage The physical address of a page.
|
---|
153 | * @thread The Emulation Thread.
|
---|
154 | */
|
---|
155 | VMMDECL(void *) MMPagePhys2Page(PVM pVM, RTHCPHYS HCPhysPage)
|
---|
156 | {
|
---|
157 | void *pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePool), HCPhysPage);
|
---|
158 | if (!pvPage)
|
---|
159 | {
|
---|
160 | pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePoolLow), HCPhysPage);
|
---|
161 | if (!pvPage)
|
---|
162 | {
|
---|
163 | STAM_COUNTER_INC(&pVM->mm.s.CTX_SUFF(pPagePool)->cErrors);
|
---|
164 | AssertMsg(pvPage, ("Invalid HCPhysPage=%VHp specified\n", HCPhysPage));
|
---|
165 | }
|
---|
166 | }
|
---|
167 | return pvPage;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Convert physical address of a page to a HC virtual address.
|
---|
173 | * This works for pages allocated by MMR3PageAlloc(), MMR3PageAllocPhys()
|
---|
174 | * and MMR3PageAllocLow().
|
---|
175 | *
|
---|
176 | * @returns VBox status code.
|
---|
177 | * @param pVM VM handle.
|
---|
178 | * @param HCPhysPage The physical address of a page.
|
---|
179 | * @param ppvPage Where to store the address corresponding to HCPhysPage.
|
---|
180 | * @thread The Emulation Thread.
|
---|
181 | */
|
---|
182 | VMMDECL(int) MMPagePhys2PageEx(PVM pVM, RTHCPHYS HCPhysPage, void **ppvPage)
|
---|
183 | {
|
---|
184 | void *pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePool), HCPhysPage);
|
---|
185 | if (!pvPage)
|
---|
186 | {
|
---|
187 | pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePoolLow), HCPhysPage);
|
---|
188 | if (!pvPage)
|
---|
189 | {
|
---|
190 | STAM_COUNTER_INC(&pVM->mm.s.CTX_SUFF(pPagePool)->cErrors);
|
---|
191 | AssertMsg(pvPage, ("Invalid HCPhysPage=%VHp specified\n", HCPhysPage));
|
---|
192 | return VERR_INVALID_POINTER;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | *ppvPage = pvPage;
|
---|
196 | return VINF_SUCCESS;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Try convert physical address of a page to a HC virtual address.
|
---|
202 | * This works for pages allocated by MMR3PageAlloc(), MMR3PageAllocPhys()
|
---|
203 | * and MMR3PageAllocLow().
|
---|
204 | *
|
---|
205 | * @returns VBox status code.
|
---|
206 | * @param pVM VM handle.
|
---|
207 | * @param HCPhysPage The physical address of a page.
|
---|
208 | * @param ppvPage Where to store the address corresponding to HCPhysPage.
|
---|
209 | * @thread The Emulation Thread.
|
---|
210 | */
|
---|
211 | VMMDECL(int) MMPagePhys2PageTry(PVM pVM, RTHCPHYS HCPhysPage, void **ppvPage)
|
---|
212 | {
|
---|
213 | void *pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePool), HCPhysPage);
|
---|
214 | if (!pvPage)
|
---|
215 | {
|
---|
216 | pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePoolLow), HCPhysPage);
|
---|
217 | if (!pvPage)
|
---|
218 | return VERR_INVALID_POINTER;
|
---|
219 | }
|
---|
220 | *ppvPage = pvPage;
|
---|
221 | return VINF_SUCCESS;
|
---|
222 | }
|
---|
223 |
|
---|