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