1 | /* $Id: MMAllPagePool.cpp 14157 2008-11-13 02:50:46Z 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 | #ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Converts a pool physical address to a linear address.
|
---|
49 | * The specified allocation type must match with the address.
|
---|
50 | *
|
---|
51 | * @returns Physical address.
|
---|
52 | * @returns NULL if not found or eType is not matching.
|
---|
53 | * @param pPool Pointer to the page pool.
|
---|
54 | * @param HCPhys The address to convert.
|
---|
55 | * @thread The Emulation Thread.
|
---|
56 | */
|
---|
57 | void *mmPagePoolPhys2Ptr(PMMPAGEPOOL pPool, RTHCPHYS HCPhys)
|
---|
58 | {
|
---|
59 | #if 0 /** @todo have to fix the debugger, but until then this is going on my nerves. */
|
---|
60 | #ifdef IN_RING3
|
---|
61 | VM_ASSERT_EMT(pPool->pVM);
|
---|
62 | #endif
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Lookup the virtual address.
|
---|
67 | */
|
---|
68 | PMMPPLOOKUPHCPHYS pLookup = (PMMPPLOOKUPHCPHYS)RTAvlHCPhysGet(&pPool->pLookupPhys, HCPhys & X86_PTE_PAE_PG_MASK);
|
---|
69 | if (pLookup)
|
---|
70 | {
|
---|
71 | STAM_COUNTER_INC(&pPool->cToVirtCalls);
|
---|
72 | PSUPPAGE pPhysPage = pLookup->pPhysPage;
|
---|
73 | PMMPAGESUBPOOL pSubPool = (PMMPAGESUBPOOL)pPhysPage->uReserved;
|
---|
74 | unsigned iPage = pPhysPage - pSubPool->paPhysPages;
|
---|
75 | return (char *)pSubPool->pvPages + (HCPhys & PAGE_OFFSET_MASK) + (iPage << PAGE_SHIFT);
|
---|
76 | }
|
---|
77 | return NULL;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Convert physical address of a page to a HC virtual address.
|
---|
83 | * This works for pages allocated by MMR3PageAlloc(), MMR3PageAllocPhys()
|
---|
84 | * and MMR3PageAllocLow().
|
---|
85 | *
|
---|
86 | * @returns VBox status code.
|
---|
87 | * @param pVM VM handle.
|
---|
88 | * @param HCPhysPage The physical address of a page.
|
---|
89 | * @param ppvPage Where to store the address corresponding to HCPhysPage.
|
---|
90 | * @thread The Emulation Thread.
|
---|
91 | */
|
---|
92 | VMMDECL(int) MMPagePhys2PageEx(PVM pVM, RTHCPHYS HCPhysPage, void **ppvPage)
|
---|
93 | {
|
---|
94 | void *pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePool), HCPhysPage);
|
---|
95 | if (!pvPage)
|
---|
96 | {
|
---|
97 | pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePoolLow), HCPhysPage);
|
---|
98 | if (!pvPage)
|
---|
99 | {
|
---|
100 | STAM_COUNTER_INC(&pVM->mm.s.CTX_SUFF(pPagePool)->cErrors);
|
---|
101 | AssertMsg(pvPage, ("Invalid HCPhysPage=%RHp specified\n", HCPhysPage));
|
---|
102 | return VERR_INVALID_POINTER;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | *ppvPage = pvPage;
|
---|
106 | return VINF_SUCCESS;
|
---|
107 | }
|
---|
108 |
|
---|
109 | #endif /* !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
|
---|
110 | #ifdef IN_RING3
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Convert physical address of a page to a HC virtual address.
|
---|
114 | * This works for pages allocated by MMR3PageAlloc(), MMR3PageAllocPhys()
|
---|
115 | * and MMR3PageAllocLow().
|
---|
116 | *
|
---|
117 | * @returns Pointer to the page at that physical address.
|
---|
118 | * @param pVM VM handle.
|
---|
119 | * @param HCPhysPage The physical address of a page.
|
---|
120 | * @thread The Emulation Thread.
|
---|
121 | */
|
---|
122 | VMMDECL(void *) MMPagePhys2Page(PVM pVM, RTHCPHYS HCPhysPage)
|
---|
123 | {
|
---|
124 | void *pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePool), HCPhysPage);
|
---|
125 | if (!pvPage)
|
---|
126 | {
|
---|
127 | pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePoolLow), HCPhysPage);
|
---|
128 | if (!pvPage)
|
---|
129 | {
|
---|
130 | STAM_COUNTER_INC(&pVM->mm.s.CTX_SUFF(pPagePool)->cErrors);
|
---|
131 | AssertMsg(pvPage, ("Invalid HCPhysPage=%RHp specified\n", HCPhysPage));
|
---|
132 | }
|
---|
133 | }
|
---|
134 | return pvPage;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Try convert physical address of a page to a HC virtual address.
|
---|
140 | * This works for pages allocated by MMR3PageAlloc(), MMR3PageAllocPhys()
|
---|
141 | * and MMR3PageAllocLow().
|
---|
142 | *
|
---|
143 | * @returns VBox status code.
|
---|
144 | * @param pVM VM handle.
|
---|
145 | * @param HCPhysPage The physical address of a page.
|
---|
146 | * @param ppvPage Where to store the address corresponding to HCPhysPage.
|
---|
147 | * @thread The Emulation Thread.
|
---|
148 | */
|
---|
149 | VMMDECL(int) MMPagePhys2PageTry(PVM pVM, RTHCPHYS HCPhysPage, void **ppvPage)
|
---|
150 | {
|
---|
151 | void *pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePool), HCPhysPage);
|
---|
152 | if (!pvPage)
|
---|
153 | {
|
---|
154 | pvPage = mmPagePoolPhys2Ptr(pVM->mm.s.CTX_SUFF(pPagePoolLow), HCPhysPage);
|
---|
155 | if (!pvPage)
|
---|
156 | return VERR_INVALID_POINTER;
|
---|
157 | }
|
---|
158 | *ppvPage = pvPage;
|
---|
159 | return VINF_SUCCESS;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Converts a pool address to a physical address.
|
---|
165 | * The specified allocation type must match with the address.
|
---|
166 | *
|
---|
167 | * @returns Physical address.
|
---|
168 | * @returns NIL_RTHCPHYS if not found or eType is not matching.
|
---|
169 | * @param pPool Pointer to the page pool.
|
---|
170 | * @param pv The address to convert.
|
---|
171 | * @thread The Emulation Thread.
|
---|
172 | */
|
---|
173 | RTHCPHYS mmPagePoolPtr2Phys(PMMPAGEPOOL pPool, void *pv)
|
---|
174 | {
|
---|
175 | #ifdef IN_RING3
|
---|
176 | VM_ASSERT_EMT(pPool->pVM);
|
---|
177 | #endif
|
---|
178 | /*
|
---|
179 | * Lookup the virtual address.
|
---|
180 | */
|
---|
181 | PMMPPLOOKUPHCPTR pLookup = (PMMPPLOOKUPHCPTR)RTAvlPVGetBestFit(&pPool->pLookupVirt, pv, false);
|
---|
182 | if (pLookup)
|
---|
183 | {
|
---|
184 | unsigned iPage = ((char *)pv - (char *)pLookup->pSubPool->pvPages) >> PAGE_SHIFT;
|
---|
185 | if (iPage < pLookup->pSubPool->cPages)
|
---|
186 | {
|
---|
187 | /*
|
---|
188 | * Convert the virtual address to a physical address.
|
---|
189 | */
|
---|
190 | STAM_COUNTER_INC(&pPool->cToPhysCalls);
|
---|
191 | AssertMsg( pLookup->pSubPool->paPhysPages[iPage].Phys
|
---|
192 | && !(pLookup->pSubPool->paPhysPages[iPage].Phys & PAGE_OFFSET_MASK),
|
---|
193 | ("Phys=%#x\n", pLookup->pSubPool->paPhysPages[iPage].Phys));
|
---|
194 | AssertMsg((uintptr_t)pLookup->pSubPool == pLookup->pSubPool->paPhysPages[iPage].uReserved,
|
---|
195 | ("pSubPool=%p uReserved=%p\n", pLookup->pSubPool, pLookup->pSubPool->paPhysPages[iPage].uReserved));
|
---|
196 | return pLookup->pSubPool->paPhysPages[iPage].Phys + ((uintptr_t)pv & PAGE_OFFSET_MASK);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | return NIL_RTHCPHYS;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Convert a page in the page pool to a HC physical address.
|
---|
205 | * This works for pages allocated by MMR3PageAlloc(), MMR3PageAllocPhys()
|
---|
206 | * and MMR3PageAllocLow().
|
---|
207 | *
|
---|
208 | * @returns Physical address for the specified page table.
|
---|
209 | * @param pVM VM handle.
|
---|
210 | * @param pvPage Page which physical address we query.
|
---|
211 | * @thread The Emulation Thread.
|
---|
212 | */
|
---|
213 | VMMDECL(RTHCPHYS) MMPage2Phys(PVM pVM, void *pvPage)
|
---|
214 | {
|
---|
215 | RTHCPHYS HCPhys = mmPagePoolPtr2Phys(pVM->mm.s.CTX_SUFF(pPagePool), pvPage);
|
---|
216 | if (HCPhys == NIL_RTHCPHYS)
|
---|
217 | {
|
---|
218 | HCPhys = mmPagePoolPtr2Phys(pVM->mm.s.CTX_SUFF(pPagePoolLow), pvPage);
|
---|
219 | if (HCPhys == NIL_RTHCPHYS)
|
---|
220 | {
|
---|
221 | STAM_COUNTER_INC(&pVM->mm.s.CTX_SUFF(pPagePool)->cErrors);
|
---|
222 | AssertMsgFailed(("Invalid pvPage=%p specified\n", pvPage));
|
---|
223 | }
|
---|
224 | }
|
---|
225 | return HCPhys;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | #endif /* IN_RING3 */
|
---|
230 |
|
---|