1 | /* $Id: MM.cpp 17547 2009-03-09 02:48:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MM - Memory Manager.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /** @page pg_mm MM - The Memory Manager
|
---|
24 | *
|
---|
25 | * The memory manager is in charge of the following memory:
|
---|
26 | * - Hypervisor Memory Area (HMA) - Address space management.
|
---|
27 | * - Hypervisor Heap - A memory heap that lives in all contexts.
|
---|
28 | * - Tagged ring-3 heap.
|
---|
29 | * - Page pools - Primarily used by PGM for shadow page tables.
|
---|
30 | * - Locked process memory - Guest RAM and other. (reduce/obsolete this)
|
---|
31 | * - Physical guest memory (RAM & ROM) - Moving to PGM. (obsolete this)
|
---|
32 | *
|
---|
33 | * The global memory manager (GMM) is the global counter part / partner of MM.
|
---|
34 | * MM will provide therefore ring-3 callable interfaces for some of the GMM APIs
|
---|
35 | * related to resource tracking (PGM is the user).
|
---|
36 | *
|
---|
37 | * @see grp_mm
|
---|
38 | *
|
---|
39 | *
|
---|
40 | * @section sec_mm_hma Hypervisor Memory Area
|
---|
41 | *
|
---|
42 | * The HMA is used when executing in raw-mode. We borrow, with the help of
|
---|
43 | * PGMMap, some unused space (one or more page directory entries to be precise)
|
---|
44 | * in the guest's virtual memory context. PGM will monitor the guest's virtual
|
---|
45 | * address space for changes and relocate the HMA when required.
|
---|
46 | *
|
---|
47 | * To give some idea what's in the HMA, study the 'info hma' output:
|
---|
48 | * @verbatim
|
---|
49 | VBoxDbg> info hma
|
---|
50 | Hypervisor Memory Area (HMA) Layout: Base 00000000a0000000, 0x00800000 bytes
|
---|
51 | 00000000a05cc000-00000000a05cd000 DYNAMIC fence
|
---|
52 | 00000000a05c4000-00000000a05cc000 DYNAMIC Dynamic mapping
|
---|
53 | 00000000a05c3000-00000000a05c4000 DYNAMIC fence
|
---|
54 | 00000000a05b8000-00000000a05c3000 DYNAMIC Paging
|
---|
55 | 00000000a05b6000-00000000a05b8000 MMIO2 0000000000000000 PCNetShMem
|
---|
56 | 00000000a0536000-00000000a05b6000 MMIO2 0000000000000000 VGA VRam
|
---|
57 | 00000000a0523000-00000000a0536000 00002aaab3d0c000 LOCKED autofree alloc once (PDM_DEVICE)
|
---|
58 | 00000000a0522000-00000000a0523000 DYNAMIC fence
|
---|
59 | 00000000a051e000-00000000a0522000 00002aaab36f5000 LOCKED autofree VBoxDD2GC.gc
|
---|
60 | 00000000a051d000-00000000a051e000 DYNAMIC fence
|
---|
61 | 00000000a04eb000-00000000a051d000 00002aaab36c3000 LOCKED autofree VBoxDDGC.gc
|
---|
62 | 00000000a04ea000-00000000a04eb000 DYNAMIC fence
|
---|
63 | 00000000a04e9000-00000000a04ea000 00002aaab36c2000 LOCKED autofree ram range (High ROM Region)
|
---|
64 | 00000000a04e8000-00000000a04e9000 DYNAMIC fence
|
---|
65 | 00000000a040e000-00000000a04e8000 00002aaab2e6d000 LOCKED autofree VMMGC.gc
|
---|
66 | 00000000a0208000-00000000a040e000 00002aaab2c67000 LOCKED autofree alloc once (PATM)
|
---|
67 | 00000000a01f7000-00000000a0208000 00002aaaab92d000 LOCKED autofree alloc once (SELM)
|
---|
68 | 00000000a01e7000-00000000a01f7000 00002aaaab5e8000 LOCKED autofree alloc once (SELM)
|
---|
69 | 00000000a01e6000-00000000a01e7000 DYNAMIC fence
|
---|
70 | 00000000a01e5000-00000000a01e6000 00002aaaab5e7000 HCPHYS 00000000c363c000 Core Code
|
---|
71 | 00000000a01e4000-00000000a01e5000 DYNAMIC fence
|
---|
72 | 00000000a01e3000-00000000a01e4000 00002aaaaab26000 HCPHYS 00000000619cf000 GIP
|
---|
73 | 00000000a01a2000-00000000a01e3000 00002aaaabf32000 LOCKED autofree alloc once (PGM_PHYS)
|
---|
74 | 00000000a016b000-00000000a01a2000 00002aaab233f000 LOCKED autofree alloc once (PGM_POOL)
|
---|
75 | 00000000a016a000-00000000a016b000 DYNAMIC fence
|
---|
76 | 00000000a0165000-00000000a016a000 DYNAMIC CR3 mapping
|
---|
77 | 00000000a0164000-00000000a0165000 DYNAMIC fence
|
---|
78 | 00000000a0024000-00000000a0164000 00002aaab215f000 LOCKED autofree Heap
|
---|
79 | 00000000a0023000-00000000a0024000 DYNAMIC fence
|
---|
80 | 00000000a0001000-00000000a0023000 00002aaab1d24000 LOCKED pages VM
|
---|
81 | 00000000a0000000-00000000a0001000 DYNAMIC fence
|
---|
82 | @endverbatim
|
---|
83 | *
|
---|
84 | *
|
---|
85 | * @section sec_mm_hyperheap Hypervisor Heap
|
---|
86 | *
|
---|
87 | * The heap is accessible from ring-3, ring-0 and the raw-mode context. That
|
---|
88 | * said, it's not necessarily mapped into ring-0 on if that's possible since we
|
---|
89 | * don't wish to waste kernel address space without a good reason.
|
---|
90 | *
|
---|
91 | * Allocations within the heap are always in the same relative position in all
|
---|
92 | * contexts, so, it's possible to use offset based linking. In fact, the heap is
|
---|
93 | * internally using offset based linked lists tracking heap blocks. We use
|
---|
94 | * offset linked AVL trees and lists in a lot of places where share structures
|
---|
95 | * between RC, R3 and R0, so this is a strict requirement of the heap. However
|
---|
96 | * this means that we cannot easily extend the heap since the extension won't
|
---|
97 | * necessarily be in the continuation of the current heap memory in all (or any)
|
---|
98 | * context.
|
---|
99 | *
|
---|
100 | * All allocations are tagged. Per tag allocation statistics will be maintaing
|
---|
101 | * and exposed thru STAM when VBOX_WITH_STATISTICS is defined.
|
---|
102 | *
|
---|
103 | *
|
---|
104 | * @section sec_mm_r3heap Tagged Ring-3 Heap
|
---|
105 | *
|
---|
106 | * The ring-3 heap is a wrapper around the RTMem API adding allocation
|
---|
107 | * statistics and automatic cleanup on VM destruction.
|
---|
108 | *
|
---|
109 | * Per tag allocation statistics will be maintaing and exposed thru STAM when
|
---|
110 | * VBOX_WITH_STATISTICS is defined.
|
---|
111 | *
|
---|
112 | *
|
---|
113 | * @section sec_mm_page Page Pool
|
---|
114 | *
|
---|
115 | * The MM manages a page pool from which other components can allocate locked,
|
---|
116 | * page aligned and page sized memory objects. The pool provides facilities to
|
---|
117 | * convert back and forth between (host) physical and virtual addresses (within
|
---|
118 | * the pool of course). Several specialized interfaces are provided for the most
|
---|
119 | * common alloctions and convertions to save the caller from bothersome casting
|
---|
120 | * and extra parameter passing.
|
---|
121 | *
|
---|
122 | *
|
---|
123 | * @section sec_mm_locked Locked Process Memory
|
---|
124 | *
|
---|
125 | * MM manages the locked process memory. This is used for a bunch of things
|
---|
126 | * (count the LOCKED entries in the'info hma' output found in @ref sec_mm_hma),
|
---|
127 | * but the main consumer of memory is currently for guest RAM. There is an
|
---|
128 | * ongoing rewrite that will move all the guest RAM allocation to PGM and
|
---|
129 | * GMM.
|
---|
130 | *
|
---|
131 | * The locking of memory is something doing in cooperation with the VirtualBox
|
---|
132 | * support driver, SUPDrv (aka. VBoxDrv), thru the support library API,
|
---|
133 | * SUPR3 (aka. SUPLib).
|
---|
134 | *
|
---|
135 | *
|
---|
136 | * @section sec_mm_phys Physical Guest Memory
|
---|
137 | *
|
---|
138 | * MM is currently managing the physical memory for the guest. It relies heavily
|
---|
139 | * on PGM for this. There is an ongoing rewrite that will move this to PGM. (The
|
---|
140 | * rewrite is driven by the need for more flexible guest ram allocation, but
|
---|
141 | * also motivated by the fact that MMPhys is just adding stupid bureaucracy and
|
---|
142 | * that MMR3PhysReserve is a totally weird artifact that must go away.)
|
---|
143 | *
|
---|
144 | */
|
---|
145 |
|
---|
146 |
|
---|
147 | /*******************************************************************************
|
---|
148 | * Header Files *
|
---|
149 | *******************************************************************************/
|
---|
150 | #define LOG_GROUP LOG_GROUP_MM
|
---|
151 | #include <VBox/mm.h>
|
---|
152 | #include <VBox/pgm.h>
|
---|
153 | #include <VBox/cfgm.h>
|
---|
154 | #include <VBox/ssm.h>
|
---|
155 | #include <VBox/gmm.h>
|
---|
156 | #include "MMInternal.h"
|
---|
157 | #include <VBox/vm.h>
|
---|
158 | #include <VBox/uvm.h>
|
---|
159 | #include <VBox/err.h>
|
---|
160 | #include <VBox/param.h>
|
---|
161 |
|
---|
162 | #include <VBox/log.h>
|
---|
163 | #include <iprt/alloc.h>
|
---|
164 | #include <iprt/assert.h>
|
---|
165 | #include <iprt/string.h>
|
---|
166 |
|
---|
167 |
|
---|
168 | /*******************************************************************************
|
---|
169 | * Defined Constants And Macros *
|
---|
170 | *******************************************************************************/
|
---|
171 | /** The current saved state versino of MM. */
|
---|
172 | #define MM_SAVED_STATE_VERSION 2
|
---|
173 |
|
---|
174 |
|
---|
175 | /*******************************************************************************
|
---|
176 | * Internal Functions *
|
---|
177 | *******************************************************************************/
|
---|
178 | static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM);
|
---|
179 | static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
|
---|
180 |
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Initializes the MM members of the UVM.
|
---|
186 | *
|
---|
187 | * This is currently only the ring-3 heap.
|
---|
188 | *
|
---|
189 | * @returns VBox status code.
|
---|
190 | * @param pUVM Pointer to the user mode VM structure.
|
---|
191 | */
|
---|
192 | VMMR3DECL(int) MMR3InitUVM(PUVM pUVM)
|
---|
193 | {
|
---|
194 | /*
|
---|
195 | * Assert sizes and order.
|
---|
196 | */
|
---|
197 | AssertCompile(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
|
---|
198 | AssertRelease(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
|
---|
199 | Assert(!pUVM->mm.s.pHeap);
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * Init the heap.
|
---|
203 | */
|
---|
204 | return mmR3HeapCreateU(pUVM, &pUVM->mm.s.pHeap);
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Initializes the MM.
|
---|
210 | *
|
---|
211 | * MM is managing the virtual address space (among other things) and
|
---|
212 | * setup the hypvervisor memory area mapping in the VM structure and
|
---|
213 | * the hypvervisor alloc-only-heap. Assuming the current init order
|
---|
214 | * and components the hypvervisor memory area looks like this:
|
---|
215 | * -# VM Structure.
|
---|
216 | * -# Hypervisor alloc only heap (also call Hypervisor memory region).
|
---|
217 | * -# Core code.
|
---|
218 | *
|
---|
219 | * MM determins the virtual address of the hypvervisor memory area by
|
---|
220 | * checking for location at previous run. If that property isn't available
|
---|
221 | * it will choose a default starting location, currently 0xa0000000.
|
---|
222 | *
|
---|
223 | * @returns VBox status code.
|
---|
224 | * @param pVM The VM to operate on.
|
---|
225 | */
|
---|
226 | VMMR3DECL(int) MMR3Init(PVM pVM)
|
---|
227 | {
|
---|
228 | LogFlow(("MMR3Init\n"));
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Assert alignment, sizes and order.
|
---|
232 | */
|
---|
233 | AssertRelease(!(RT_OFFSETOF(VM, mm.s) & 31));
|
---|
234 | AssertRelease(sizeof(pVM->mm.s) <= sizeof(pVM->mm.padding));
|
---|
235 | AssertMsg(pVM->mm.s.offVM == 0, ("Already initialized!\n"));
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Init the structure.
|
---|
239 | */
|
---|
240 | pVM->mm.s.offVM = RT_OFFSETOF(VM, mm);
|
---|
241 | pVM->mm.s.offLookupHyper = NIL_OFFSET;
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Init the page pool.
|
---|
245 | */
|
---|
246 | int rc = mmR3PagePoolInit(pVM);
|
---|
247 | if (RT_SUCCESS(rc))
|
---|
248 | {
|
---|
249 | /*
|
---|
250 | * Init the hypervisor related stuff.
|
---|
251 | */
|
---|
252 | rc = mmR3HyperInit(pVM);
|
---|
253 | if (RT_SUCCESS(rc))
|
---|
254 | {
|
---|
255 | /*
|
---|
256 | * Register the saved state data unit.
|
---|
257 | */
|
---|
258 | rc = SSMR3RegisterInternal(pVM, "mm", 1, MM_SAVED_STATE_VERSION, sizeof(uint32_t) * 2,
|
---|
259 | NULL, mmR3Save, NULL,
|
---|
260 | NULL, mmR3Load, NULL);
|
---|
261 | if (RT_SUCCESS(rc))
|
---|
262 | return rc;
|
---|
263 |
|
---|
264 | /* .... failure .... */
|
---|
265 | }
|
---|
266 | }
|
---|
267 | MMR3Term(pVM);
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Initializes the MM parts which depends on PGM being initialized.
|
---|
274 | *
|
---|
275 | * @returns VBox status code.
|
---|
276 | * @param pVM The VM to operate on.
|
---|
277 | * @remark No cleanup necessary since MMR3Term() will be called on failure.
|
---|
278 | */
|
---|
279 | VMMR3DECL(int) MMR3InitPaging(PVM pVM)
|
---|
280 | {
|
---|
281 | LogFlow(("MMR3InitPaging:\n"));
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * Query the CFGM values.
|
---|
285 | */
|
---|
286 | int rc;
|
---|
287 | PCFGMNODE pMMCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
|
---|
288 | if (!pMMCfg)
|
---|
289 | {
|
---|
290 | rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "MM", &pMMCfg);
|
---|
291 | AssertRCReturn(rc, rc);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /** @cfgm{RamPreAlloc, boolean, false}
|
---|
295 | * Indicates whether the base RAM should all be allocated before starting
|
---|
296 | * the VM (default), or if it should be allocated when first written to.
|
---|
297 | */
|
---|
298 | bool fPreAlloc;
|
---|
299 | rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "RamPreAlloc", &fPreAlloc);
|
---|
300 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
301 | fPreAlloc = false;
|
---|
302 | else
|
---|
303 | AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamPreAlloc\", rc=%Rrc.\n", rc), rc);
|
---|
304 |
|
---|
305 | /** @cfgm{RamSize, uint64_t, 0, 16TB, 0}
|
---|
306 | * Specifies the size of the base RAM that is to be set up during
|
---|
307 | * VM initialization.
|
---|
308 | */
|
---|
309 | uint64_t cbRam;
|
---|
310 | rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "RamSize", &cbRam);
|
---|
311 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
312 | cbRam = 0;
|
---|
313 | else
|
---|
314 | AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamSize\", rc=%Rrc.\n", rc), rc);
|
---|
315 | AssertLogRelMsg(!(cbRam & ~X86_PTE_PAE_PG_MASK), ("%RGp X86_PTE_PAE_PG_MASK=%RX64\n", cbRam, X86_PTE_PAE_PG_MASK));
|
---|
316 | AssertLogRelMsgReturn(cbRam <= GMM_GCPHYS_LAST, ("cbRam=%RGp GMM_GCPHYS_LAST=%RX64\n", cbRam, GMM_GCPHYS_LAST), VERR_OUT_OF_RANGE);
|
---|
317 | cbRam &= X86_PTE_PAE_PG_MASK;
|
---|
318 | pVM->mm.s.cbRamBase = cbRam;
|
---|
319 |
|
---|
320 | /** @cfgm{RamHoleSize, uint32_t, 0, 4032MB, 512MB}
|
---|
321 | * Specifies the size of the memory hole. The memory hole is used
|
---|
322 | * to avoid mapping RAM to the range normally used for PCI memory regions.
|
---|
323 | * Must be aligned on a 4MB boundrary. */
|
---|
324 | uint32_t cbRamHole;
|
---|
325 | rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "RamHoleSize", &cbRamHole, MM_RAM_HOLE_SIZE_DEFAULT);
|
---|
326 | AssertLogRelMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamHoleSize\", rc=%Rrc.\n", rc), rc);
|
---|
327 | AssertLogRelMsgReturn(cbRamHole <= 4032U * _1M,
|
---|
328 | ("Configuration error: \"RamHoleSize\"=%#RX32 is too large.\n", cbRamHole), VERR_OUT_OF_RANGE);
|
---|
329 | AssertLogRelMsgReturn(cbRamHole > 16 * _1M,
|
---|
330 | ("Configuration error: \"RamHoleSize\"=%#RX32 is too large.\n", cbRamHole), VERR_OUT_OF_RANGE);
|
---|
331 | AssertLogRelMsgReturn(!(cbRamHole & (_4M - 1)),
|
---|
332 | ("Configuration error: \"RamHoleSize\"=%#RX32 is misaligned.\n", cbRamHole), VERR_OUT_OF_RANGE);
|
---|
333 | uint64_t const offRamHole = _4G - cbRamHole;
|
---|
334 | if (cbRam < offRamHole)
|
---|
335 | Log(("MM: %RU64 bytes of RAM%s\n", cbRam, fPreAlloc ? " (PreAlloc)" : ""));
|
---|
336 | else
|
---|
337 | Log(("MM: %RU64 bytes of RAM%s with a hole at %RU64 up to 4GB.\n", cbRam, fPreAlloc ? " (PreAlloc)" : "", offRamHole));
|
---|
338 |
|
---|
339 | /** @cfgm{MM/Policy, string, no overcommitment}
|
---|
340 | * Specifies the policy to use when reserving memory for this VM. The recognized
|
---|
341 | * value is 'no overcommitment' (default). See GMMPOLICY.
|
---|
342 | */
|
---|
343 | GMMOCPOLICY enmOcPolicy;
|
---|
344 | char sz[64];
|
---|
345 | rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Policy", sz, sizeof(sz));
|
---|
346 | if (RT_SUCCESS(rc))
|
---|
347 | {
|
---|
348 | if ( !RTStrICmp(sz, "no_oc")
|
---|
349 | || !RTStrICmp(sz, "no overcommitment"))
|
---|
350 | enmOcPolicy = GMMOCPOLICY_NO_OC;
|
---|
351 | else
|
---|
352 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Policy\" value \"%s\"", sz);
|
---|
353 | }
|
---|
354 | else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
355 | enmOcPolicy = GMMOCPOLICY_NO_OC;
|
---|
356 | else
|
---|
357 | AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Policy\", rc=%Rrc.\n", rc), rc);
|
---|
358 |
|
---|
359 | /** @cfgm{MM/Priority, string, normal}
|
---|
360 | * Specifies the memory priority of this VM. The priority comes into play when the
|
---|
361 | * system is overcommitted and the VMs needs to be milked for memory. The recognized
|
---|
362 | * values are 'low', 'normal' (default) and 'high'. See GMMPRIORITY.
|
---|
363 | */
|
---|
364 | GMMPRIORITY enmPriority;
|
---|
365 | rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Priority", sz, sizeof(sz));
|
---|
366 | if (RT_SUCCESS(rc))
|
---|
367 | {
|
---|
368 | if (!RTStrICmp(sz, "low"))
|
---|
369 | enmPriority = GMMPRIORITY_LOW;
|
---|
370 | else if (!RTStrICmp(sz, "normal"))
|
---|
371 | enmPriority = GMMPRIORITY_NORMAL;
|
---|
372 | else if (!RTStrICmp(sz, "high"))
|
---|
373 | enmPriority = GMMPRIORITY_HIGH;
|
---|
374 | else
|
---|
375 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Priority\" value \"%s\"", sz);
|
---|
376 | }
|
---|
377 | else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
378 | enmPriority = GMMPRIORITY_NORMAL;
|
---|
379 | else
|
---|
380 | AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Priority\", rc=%Rrc.\n", rc), rc);
|
---|
381 |
|
---|
382 | /*
|
---|
383 | * Make the initial memory reservation with GMM.
|
---|
384 | */
|
---|
385 | uint64_t cBasePages = (cbRam >> PAGE_SHIFT) + pVM->mm.s.cBasePages;
|
---|
386 | rc = GMMR3InitialReservation(pVM,
|
---|
387 | RT_MAX(cBasePages + pVM->mm.s.cHandyPages, 1),
|
---|
388 | RT_MAX(pVM->mm.s.cShadowPages, 1),
|
---|
389 | RT_MAX(pVM->mm.s.cFixedPages, 1),
|
---|
390 | enmOcPolicy,
|
---|
391 | enmPriority);
|
---|
392 | if (RT_FAILURE(rc))
|
---|
393 | {
|
---|
394 | if (rc == VERR_GMM_MEMORY_RESERVATION_DECLINED)
|
---|
395 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
396 | N_("Insufficient free memory to start the VM (cbRam=%#RX64 enmOcPolicy=%d enmPriority=%d)"),
|
---|
397 | cbRam, enmOcPolicy, enmPriority);
|
---|
398 | return VMSetError(pVM, rc, RT_SRC_POS, "GMMR3InitialReservation(,%#RX64,0,0,%d,%d)",
|
---|
399 | cbRam >> PAGE_SHIFT, enmOcPolicy, enmPriority);
|
---|
400 | }
|
---|
401 |
|
---|
402 | /*
|
---|
403 | * If RamSize is 0 we're done now.
|
---|
404 | */
|
---|
405 | if (cbRam < PAGE_SIZE)
|
---|
406 | {
|
---|
407 | Log(("MM: No RAM configured\n"));
|
---|
408 | return VINF_SUCCESS;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /*
|
---|
412 | * Setup the base ram (PGM).
|
---|
413 | */
|
---|
414 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
415 | if (cbRam > offRamHole)
|
---|
416 | {
|
---|
417 | rc = PGMR3PhysRegisterRam(pVM, 0, offRamHole, "Base RAM");
|
---|
418 | if (RT_SUCCESS(rc))
|
---|
419 | rc = PGMR3PhysRegisterRam(pVM, _4G, cbRam - offRamHole, "Above 4GB Base RAM");
|
---|
420 | }
|
---|
421 | else
|
---|
422 | rc = PGMR3PhysRegisterRam(pVM, 0, RT_MIN(cbRam, offRamHole), "Base RAM");
|
---|
423 | if ( RT_SUCCESS(rc)
|
---|
424 | && fPreAlloc)
|
---|
425 | {
|
---|
426 | /** @todo RamPreAlloc should be handled at the very end of the VM creation. (lazy bird) */
|
---|
427 | return VM_SET_ERROR(pVM, VERR_NOT_IMPLEMENTED, "TODO: RamPreAlloc");
|
---|
428 | }
|
---|
429 | #else
|
---|
430 | rc = PGMR3PhysRegisterRam(pVM, 0, cbRam, "Base RAM");
|
---|
431 | if (RT_SUCCESS(rc))
|
---|
432 | {
|
---|
433 | /*
|
---|
434 | * Allocate the first chunk, as we'll map ROM ranges there.
|
---|
435 | * If requested, allocated the rest too.
|
---|
436 | */
|
---|
437 | RTGCPHYS GCPhys = (RTGCPHYS)0;
|
---|
438 | rc = PGM3PhysGrowRange(pVM, &GCPhys);
|
---|
439 | if (RT_SUCCESS(rc) && fPreAlloc)
|
---|
440 | for (GCPhys = PGM_DYNAMIC_CHUNK_SIZE;
|
---|
441 | GCPhys < cbRam && RT_SUCCESS(rc);
|
---|
442 | GCPhys += PGM_DYNAMIC_CHUNK_SIZE)
|
---|
443 | rc = PGM3PhysGrowRange(pVM, &GCPhys);
|
---|
444 | }
|
---|
445 | #endif
|
---|
446 |
|
---|
447 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
448 | /*
|
---|
449 | * Enabled mmR3UpdateReservation here since we don't want the
|
---|
450 | * PGMR3PhysRegisterRam calls above mess things up.
|
---|
451 | */
|
---|
452 | pVM->mm.s.fDoneMMR3InitPaging = true;
|
---|
453 | AssertMsg(pVM->mm.s.cBasePages == cBasePages || RT_FAILURE(rc), ("%RX64 != %RX64\n", pVM->mm.s.cBasePages, cBasePages));
|
---|
454 | #endif
|
---|
455 |
|
---|
456 | LogFlow(("MMR3InitPaging: returns %Rrc\n", rc));
|
---|
457 | return rc;
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Terminates the MM.
|
---|
463 | *
|
---|
464 | * Termination means cleaning up and freeing all resources,
|
---|
465 | * the VM it self is at this point powered off or suspended.
|
---|
466 | *
|
---|
467 | * @returns VBox status code.
|
---|
468 | * @param pVM The VM to operate on.
|
---|
469 | */
|
---|
470 | VMMR3DECL(int) MMR3Term(PVM pVM)
|
---|
471 | {
|
---|
472 | /*
|
---|
473 | * Destroy the page pool. (first as it used the hyper heap)
|
---|
474 | */
|
---|
475 | mmR3PagePoolTerm(pVM);
|
---|
476 |
|
---|
477 | /*
|
---|
478 | * Release locked memory.
|
---|
479 | * (Associated record are released by the heap.)
|
---|
480 | */
|
---|
481 | PMMLOCKEDMEM pLockedMem = pVM->mm.s.pLockedMem;
|
---|
482 | while (pLockedMem)
|
---|
483 | {
|
---|
484 | int rc = SUPPageUnlock(pLockedMem->pv);
|
---|
485 | AssertMsgRC(rc, ("SUPPageUnlock(%p) -> rc=%d\n", pLockedMem->pv, rc));
|
---|
486 | switch (pLockedMem->eType)
|
---|
487 | {
|
---|
488 | case MM_LOCKED_TYPE_HYPER:
|
---|
489 | rc = SUPPageFree(pLockedMem->pv, pLockedMem->cb >> PAGE_SHIFT);
|
---|
490 | AssertMsgRC(rc, ("SUPPageFree(%p) -> rc=%d\n", pLockedMem->pv, rc));
|
---|
491 | break;
|
---|
492 | case MM_LOCKED_TYPE_HYPER_NOFREE:
|
---|
493 | case MM_LOCKED_TYPE_HYPER_PAGES:
|
---|
494 | case MM_LOCKED_TYPE_PHYS:
|
---|
495 | /* nothing to do. */
|
---|
496 | break;
|
---|
497 | }
|
---|
498 | /* next */
|
---|
499 | pLockedMem = pLockedMem->pNext;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /*
|
---|
503 | * Zero stuff to detect after termination use of the MM interface
|
---|
504 | */
|
---|
505 | pVM->mm.s.offLookupHyper = NIL_OFFSET;
|
---|
506 | pVM->mm.s.pLockedMem = NULL;
|
---|
507 | pVM->mm.s.pHyperHeapR3 = NULL; /* freed above. */
|
---|
508 | pVM->mm.s.pHyperHeapR0 = NIL_RTR0PTR; /* freed above. */
|
---|
509 | pVM->mm.s.pHyperHeapRC = NIL_RTRCPTR; /* freed above. */
|
---|
510 | pVM->mm.s.offVM = 0; /* init assertion on this */
|
---|
511 |
|
---|
512 | return VINF_SUCCESS;
|
---|
513 | }
|
---|
514 |
|
---|
515 |
|
---|
516 | /**
|
---|
517 | * Terminates the UVM part of MM.
|
---|
518 | *
|
---|
519 | * Termination means cleaning up and freeing all resources,
|
---|
520 | * the VM it self is at this point powered off or suspended.
|
---|
521 | *
|
---|
522 | * @returns VBox status code.
|
---|
523 | * @param pUVM Pointer to the user mode VM structure.
|
---|
524 | */
|
---|
525 | VMMR3DECL(void) MMR3TermUVM(PUVM pUVM)
|
---|
526 | {
|
---|
527 | /*
|
---|
528 | * Destroy the heap.
|
---|
529 | */
|
---|
530 | mmR3HeapDestroy(pUVM->mm.s.pHeap);
|
---|
531 | pUVM->mm.s.pHeap = NULL;
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * Reset notification.
|
---|
537 | *
|
---|
538 | * MM will reload shadow ROMs into RAM at this point and make
|
---|
539 | * the ROM writable.
|
---|
540 | *
|
---|
541 | * @param pVM The VM handle.
|
---|
542 | */
|
---|
543 | VMMR3DECL(void) MMR3Reset(PVM pVM)
|
---|
544 | {
|
---|
545 | #ifndef VBOX_WITH_NEW_PHYS_CODE
|
---|
546 | mmR3PhysRomReset(pVM);
|
---|
547 | #endif
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | /**
|
---|
552 | * Execute state save operation.
|
---|
553 | *
|
---|
554 | * @returns VBox status code.
|
---|
555 | * @param pVM VM Handle.
|
---|
556 | * @param pSSM SSM operation handle.
|
---|
557 | */
|
---|
558 | static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
559 | {
|
---|
560 | LogFlow(("mmR3Save:\n"));
|
---|
561 |
|
---|
562 | /* (PGM saves the physical memory.) */
|
---|
563 | SSMR3PutU64(pSSM, pVM->mm.s.cBasePages);
|
---|
564 | return SSMR3PutU64(pSSM, pVM->mm.s.cbRamBase);
|
---|
565 | }
|
---|
566 |
|
---|
567 |
|
---|
568 | /**
|
---|
569 | * Execute state load operation.
|
---|
570 | *
|
---|
571 | * @returns VBox status code.
|
---|
572 | * @param pVM VM Handle.
|
---|
573 | * @param pSSM SSM operation handle.
|
---|
574 | * @param u32Version Data layout version.
|
---|
575 | */
|
---|
576 | static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
577 | {
|
---|
578 | LogFlow(("mmR3Load:\n"));
|
---|
579 |
|
---|
580 | /*
|
---|
581 | * Validate version.
|
---|
582 | */
|
---|
583 | if ( SSM_VERSION_MAJOR_CHANGED(u32Version, MM_SAVED_STATE_VERSION)
|
---|
584 | || !u32Version)
|
---|
585 | {
|
---|
586 | AssertMsgFailed(("mmR3Load: Invalid version u32Version=%d!\n", u32Version));
|
---|
587 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
588 | }
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * Check the cBasePages and cbRamBase values.
|
---|
592 | */
|
---|
593 | int rc;
|
---|
594 | RTUINT cb1;
|
---|
595 |
|
---|
596 | /* cBasePages */
|
---|
597 | uint64_t cPages;
|
---|
598 | if (u32Version != 1)
|
---|
599 | rc = SSMR3GetU64(pSSM, &cPages);
|
---|
600 | else
|
---|
601 | {
|
---|
602 | rc = SSMR3GetUInt(pSSM, &cb1);
|
---|
603 | cPages = cb1 >> PAGE_SHIFT;
|
---|
604 | }
|
---|
605 | if (RT_FAILURE(rc))
|
---|
606 | return rc;
|
---|
607 | if (cPages != pVM->mm.s.cBasePages)
|
---|
608 | {
|
---|
609 | LogRel(("mmR3Load: Memory configuration has changed. cPages=%#RX64 saved=%#RX64\n", pVM->mm.s.cBasePages, cPages));
|
---|
610 | return VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH;
|
---|
611 | }
|
---|
612 |
|
---|
613 | /* cbRamBase */
|
---|
614 | uint64_t cb;
|
---|
615 | if (u32Version != 1)
|
---|
616 | rc = SSMR3GetU64(pSSM, &cb);
|
---|
617 | else
|
---|
618 | {
|
---|
619 | rc = SSMR3GetUInt(pSSM, &cb1);
|
---|
620 | cb = cb1;
|
---|
621 | }
|
---|
622 | if (RT_FAILURE(rc))
|
---|
623 | return rc;
|
---|
624 | if (cb != pVM->mm.s.cbRamBase)
|
---|
625 | {
|
---|
626 | LogRel(("mmR3Load: Memory configuration has changed. cbRamBase=%#RX64 save=%#RX64\n", pVM->mm.s.cbRamBase, cb));
|
---|
627 | return VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH;
|
---|
628 | }
|
---|
629 |
|
---|
630 | /* (PGM restores the physical memory.) */
|
---|
631 | return rc;
|
---|
632 | }
|
---|
633 |
|
---|
634 |
|
---|
635 | /**
|
---|
636 | * Updates GMM with memory reservation changes.
|
---|
637 | *
|
---|
638 | * Called when MM::cbRamRegistered, MM::cShadowPages or MM::cFixedPages changes.
|
---|
639 | *
|
---|
640 | * @returns VBox status code - see GMMR0UpdateReservation.
|
---|
641 | * @param pVM The shared VM structure.
|
---|
642 | */
|
---|
643 | int mmR3UpdateReservation(PVM pVM)
|
---|
644 | {
|
---|
645 | VM_ASSERT_EMT(pVM);
|
---|
646 | if (pVM->mm.s.fDoneMMR3InitPaging)
|
---|
647 | return GMMR3UpdateReservation(pVM,
|
---|
648 | RT_MAX(pVM->mm.s.cBasePages + pVM->mm.s.cHandyPages, 1),
|
---|
649 | RT_MAX(pVM->mm.s.cShadowPages, 1),
|
---|
650 | RT_MAX(pVM->mm.s.cFixedPages, 1));
|
---|
651 | return VINF_SUCCESS;
|
---|
652 | }
|
---|
653 |
|
---|
654 |
|
---|
655 | /**
|
---|
656 | * Interface for PGM to increase the reservation of RAM and ROM pages.
|
---|
657 | *
|
---|
658 | * This can be called before MMR3InitPaging.
|
---|
659 | *
|
---|
660 | * @returns VBox status code. Will set VM error on failure.
|
---|
661 | * @param pVM The shared VM structure.
|
---|
662 | * @param cAddBasePages The number of pages to add.
|
---|
663 | */
|
---|
664 | VMMR3DECL(int) MMR3IncreaseBaseReservation(PVM pVM, uint64_t cAddBasePages)
|
---|
665 | {
|
---|
666 | uint64_t cOld = pVM->mm.s.cBasePages;
|
---|
667 | pVM->mm.s.cBasePages += cAddBasePages;
|
---|
668 | LogFlow(("MMR3IncreaseBaseReservation: +%RU64 (%RU64 -> %RU64\n", cAddBasePages, cOld, pVM->mm.s.cBasePages));
|
---|
669 | int rc = mmR3UpdateReservation(pVM);
|
---|
670 | if (RT_FAILURE(rc))
|
---|
671 | {
|
---|
672 | VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 -> %#RX64 + %#RX32)"),
|
---|
673 | cOld, pVM->mm.s.cBasePages, pVM->mm.s.cHandyPages);
|
---|
674 | pVM->mm.s.cBasePages = cOld;
|
---|
675 | }
|
---|
676 | return rc;
|
---|
677 | }
|
---|
678 |
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Interface for PGM to make reservations for handy pages in addition to the
|
---|
682 | * base memory.
|
---|
683 | *
|
---|
684 | * This can be called before MMR3InitPaging.
|
---|
685 | *
|
---|
686 | * @returns VBox status code. Will set VM error on failure.
|
---|
687 | * @param pVM The shared VM structure.
|
---|
688 | * @param cHandyPages The number of handy pages.
|
---|
689 | */
|
---|
690 | VMMR3DECL(int) MMR3ReserveHandyPages(PVM pVM, uint32_t cHandyPages)
|
---|
691 | {
|
---|
692 | AssertReturn(!pVM->mm.s.cHandyPages, VERR_WRONG_ORDER);
|
---|
693 |
|
---|
694 | pVM->mm.s.cHandyPages = cHandyPages;
|
---|
695 | LogFlow(("MMR3ReserveHandyPages: %RU32 (base %RU64)\n", pVM->mm.s.cHandyPages, pVM->mm.s.cBasePages));
|
---|
696 | int rc = mmR3UpdateReservation(pVM);
|
---|
697 | if (RT_FAILURE(rc))
|
---|
698 | {
|
---|
699 | VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 + %#RX32)"),
|
---|
700 | pVM->mm.s.cBasePages, pVM->mm.s.cHandyPages);
|
---|
701 | pVM->mm.s.cHandyPages = 0;
|
---|
702 | }
|
---|
703 | return rc;
|
---|
704 | }
|
---|
705 |
|
---|
706 |
|
---|
707 | /**
|
---|
708 | * Interface for PGM to adjust the reservation of fixed pages.
|
---|
709 | *
|
---|
710 | * This can be called before MMR3InitPaging.
|
---|
711 | *
|
---|
712 | * @returns VBox status code. Will set VM error on failure.
|
---|
713 | * @param pVM The shared VM structure.
|
---|
714 | * @param cDeltaFixedPages The number of pages to add (positive) or subtract (negative).
|
---|
715 | * @param pszDesc Some description associated with the reservation.
|
---|
716 | */
|
---|
717 | VMMR3DECL(int) MMR3AdjustFixedReservation(PVM pVM, int32_t cDeltaFixedPages, const char *pszDesc)
|
---|
718 | {
|
---|
719 | const uint32_t cOld = pVM->mm.s.cFixedPages;
|
---|
720 | pVM->mm.s.cFixedPages += cDeltaFixedPages;
|
---|
721 | LogFlow(("MMR3AdjustFixedReservation: %d (%u -> %u)\n", cDeltaFixedPages, cOld, pVM->mm.s.cFixedPages));
|
---|
722 | int rc = mmR3UpdateReservation(pVM);
|
---|
723 | if (RT_FAILURE(rc))
|
---|
724 | {
|
---|
725 | VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory (%#x -> %#x; %s)"),
|
---|
726 | cOld, pVM->mm.s.cFixedPages, pszDesc);
|
---|
727 | pVM->mm.s.cFixedPages = cOld;
|
---|
728 | }
|
---|
729 | return rc;
|
---|
730 | }
|
---|
731 |
|
---|
732 |
|
---|
733 | /**
|
---|
734 | * Interface for PGM to update the reservation of shadow pages.
|
---|
735 | *
|
---|
736 | * This can be called before MMR3InitPaging.
|
---|
737 | *
|
---|
738 | * @returns VBox status code. Will set VM error on failure.
|
---|
739 | * @param pVM The shared VM structure.
|
---|
740 | * @param cShadowPages The new page count.
|
---|
741 | */
|
---|
742 | VMMR3DECL(int) MMR3UpdateShadowReservation(PVM pVM, uint32_t cShadowPages)
|
---|
743 | {
|
---|
744 | const uint32_t cOld = pVM->mm.s.cShadowPages;
|
---|
745 | pVM->mm.s.cShadowPages = cShadowPages;
|
---|
746 | LogFlow(("MMR3UpdateShadowReservation: %u -> %u\n", cOld, pVM->mm.s.cShadowPages));
|
---|
747 | int rc = mmR3UpdateReservation(pVM);
|
---|
748 | if (RT_FAILURE(rc))
|
---|
749 | {
|
---|
750 | VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory for shadow page tables (%#x -> %#x)"), cOld, pVM->mm.s.cShadowPages);
|
---|
751 | pVM->mm.s.cShadowPages = cOld;
|
---|
752 | }
|
---|
753 | return rc;
|
---|
754 | }
|
---|
755 |
|
---|
756 |
|
---|
757 | /**
|
---|
758 | * Locks physical memory which backs a virtual memory range (HC) adding
|
---|
759 | * the required records to the pLockedMem list.
|
---|
760 | *
|
---|
761 | * @returns VBox status code.
|
---|
762 | * @param pVM The VM handle.
|
---|
763 | * @param pv Pointer to memory range which shall be locked down.
|
---|
764 | * This pointer is page aligned.
|
---|
765 | * @param cb Size of memory range (in bytes). This size is page aligned.
|
---|
766 | * @param eType Memory type.
|
---|
767 | * @param ppLockedMem Where to store the pointer to the created locked memory record.
|
---|
768 | * This is optional, pass NULL if not used.
|
---|
769 | * @param fSilentFailure Don't raise an error when unsuccessful. Upper layer with deal with it.
|
---|
770 | */
|
---|
771 | int mmR3LockMem(PVM pVM, void *pv, size_t cb, MMLOCKEDTYPE eType, PMMLOCKEDMEM *ppLockedMem, bool fSilentFailure)
|
---|
772 | {
|
---|
773 | Assert(RT_ALIGN_P(pv, PAGE_SIZE) == pv);
|
---|
774 | Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
|
---|
775 |
|
---|
776 | if (ppLockedMem)
|
---|
777 | *ppLockedMem = NULL;
|
---|
778 |
|
---|
779 | /*
|
---|
780 | * Allocate locked mem structure.
|
---|
781 | */
|
---|
782 | unsigned cPages = (unsigned)(cb >> PAGE_SHIFT);
|
---|
783 | AssertReturn(cPages == (cb >> PAGE_SHIFT), VERR_OUT_OF_RANGE);
|
---|
784 | PMMLOCKEDMEM pLockedMem = (PMMLOCKEDMEM)MMR3HeapAlloc(pVM, MM_TAG_MM, RT_OFFSETOF(MMLOCKEDMEM, aPhysPages[cPages]));
|
---|
785 | if (!pLockedMem)
|
---|
786 | return VERR_NO_MEMORY;
|
---|
787 | pLockedMem->pv = pv;
|
---|
788 | pLockedMem->cb = cb;
|
---|
789 | pLockedMem->eType = eType;
|
---|
790 | memset(&pLockedMem->u, 0, sizeof(pLockedMem->u));
|
---|
791 |
|
---|
792 | /*
|
---|
793 | * Lock the memory.
|
---|
794 | */
|
---|
795 | int rc = SUPPageLock(pv, cPages, &pLockedMem->aPhysPages[0]);
|
---|
796 | if (RT_SUCCESS(rc))
|
---|
797 | {
|
---|
798 | /*
|
---|
799 | * Setup the reserved field.
|
---|
800 | */
|
---|
801 | PSUPPAGE pPhysPage = &pLockedMem->aPhysPages[0];
|
---|
802 | for (unsigned c = cPages; c > 0; c--, pPhysPage++)
|
---|
803 | pPhysPage->uReserved = (RTHCUINTPTR)pLockedMem;
|
---|
804 |
|
---|
805 | /*
|
---|
806 | * Insert into the list.
|
---|
807 | *
|
---|
808 | * ASSUME no protected needed here as only one thread in the system can possibly
|
---|
809 | * be doing this. No other threads will walk this list either we assume.
|
---|
810 | */
|
---|
811 | pLockedMem->pNext = pVM->mm.s.pLockedMem;
|
---|
812 | pVM->mm.s.pLockedMem = pLockedMem;
|
---|
813 | /* Set return value. */
|
---|
814 | if (ppLockedMem)
|
---|
815 | *ppLockedMem = pLockedMem;
|
---|
816 | }
|
---|
817 | else
|
---|
818 | {
|
---|
819 | AssertMsgFailed(("SUPPageLock failed with rc=%d\n", rc));
|
---|
820 | MMR3HeapFree(pLockedMem);
|
---|
821 | if (!fSilentFailure)
|
---|
822 | rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to lock %d bytes of host memory (out of memory)"), cb);
|
---|
823 | }
|
---|
824 |
|
---|
825 | return rc;
|
---|
826 | }
|
---|
827 |
|
---|
828 |
|
---|
829 | /**
|
---|
830 | * Maps a part of or an entire locked memory region into the guest context.
|
---|
831 | *
|
---|
832 | * @returns VBox status.
|
---|
833 | * God knows what happens if we fail...
|
---|
834 | * @param pVM VM handle.
|
---|
835 | * @param pLockedMem Locked memory structure.
|
---|
836 | * @param Addr GC Address where to start the mapping.
|
---|
837 | * @param iPage Page number in the locked memory region.
|
---|
838 | * @param cPages Number of pages to map.
|
---|
839 | * @param fFlags See the fFlags argument of PGR3Map().
|
---|
840 | */
|
---|
841 | int mmR3MapLocked(PVM pVM, PMMLOCKEDMEM pLockedMem, RTGCPTR Addr, unsigned iPage, size_t cPages, unsigned fFlags)
|
---|
842 | {
|
---|
843 | /*
|
---|
844 | * Adjust ~0 argument
|
---|
845 | */
|
---|
846 | if (cPages == ~(size_t)0)
|
---|
847 | cPages = (pLockedMem->cb >> PAGE_SHIFT) - iPage;
|
---|
848 | Assert(cPages != ~0U);
|
---|
849 | /* no incorrect arguments are accepted */
|
---|
850 | Assert(RT_ALIGN_GCPT(Addr, PAGE_SIZE, RTGCPTR) == Addr);
|
---|
851 | AssertMsg(iPage < (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad iPage(=%d)\n", iPage));
|
---|
852 | AssertMsg(iPage + cPages <= (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad cPages(=%d)\n", cPages));
|
---|
853 |
|
---|
854 | /*
|
---|
855 | * Map the pages.
|
---|
856 | */
|
---|
857 | PSUPPAGE pPhysPage = &pLockedMem->aPhysPages[iPage];
|
---|
858 | while (cPages)
|
---|
859 | {
|
---|
860 | RTHCPHYS HCPhys = pPhysPage->Phys;
|
---|
861 | int rc = PGMMap(pVM, Addr, HCPhys, PAGE_SIZE, fFlags);
|
---|
862 | if (RT_FAILURE(rc))
|
---|
863 | {
|
---|
864 | /** @todo how the hell can we do a proper bailout here. */
|
---|
865 | return rc;
|
---|
866 | }
|
---|
867 |
|
---|
868 | /* next */
|
---|
869 | cPages--;
|
---|
870 | iPage++;
|
---|
871 | pPhysPage++;
|
---|
872 | Addr += PAGE_SIZE;
|
---|
873 | }
|
---|
874 |
|
---|
875 | return VINF_SUCCESS;
|
---|
876 | }
|
---|
877 |
|
---|
878 |
|
---|
879 | /**
|
---|
880 | * Convert HC Physical address to HC Virtual address.
|
---|
881 | *
|
---|
882 | * @returns VBox status.
|
---|
883 | * @param pVM VM handle.
|
---|
884 | * @param HCPhys The host context virtual address.
|
---|
885 | * @param ppv Where to store the resulting address.
|
---|
886 | * @thread The Emulation Thread.
|
---|
887 | *
|
---|
888 | * @remarks Avoid whenever possible.
|
---|
889 | * Intended for the debugger facility only.
|
---|
890 | * @todo Rename to indicate the special usage.
|
---|
891 | */
|
---|
892 | VMMR3DECL(int) MMR3HCPhys2HCVirt(PVM pVM, RTHCPHYS HCPhys, void **ppv)
|
---|
893 | {
|
---|
894 | /*
|
---|
895 | * Try page tables.
|
---|
896 | */
|
---|
897 | int rc = MMPagePhys2PageTry(pVM, HCPhys, ppv);
|
---|
898 | if (RT_SUCCESS(rc))
|
---|
899 | return rc;
|
---|
900 |
|
---|
901 | /*
|
---|
902 | * Iterate the locked memory - very slow.
|
---|
903 | */
|
---|
904 | uint32_t off = HCPhys & PAGE_OFFSET_MASK;
|
---|
905 | HCPhys &= X86_PTE_PAE_PG_MASK;
|
---|
906 | for (PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem; pCur; pCur = pCur->pNext)
|
---|
907 | {
|
---|
908 | size_t iPage = pCur->cb >> PAGE_SHIFT;
|
---|
909 | while (iPage-- > 0)
|
---|
910 | if ((pCur->aPhysPages[iPage].Phys & X86_PTE_PAE_PG_MASK) == HCPhys)
|
---|
911 | {
|
---|
912 | *ppv = (char *)pCur->pv + (iPage << PAGE_SHIFT) + off;
|
---|
913 | return VINF_SUCCESS;
|
---|
914 | }
|
---|
915 | }
|
---|
916 | /* give up */
|
---|
917 | return VERR_INVALID_POINTER;
|
---|
918 | }
|
---|
919 |
|
---|
920 |
|
---|
921 | /**
|
---|
922 | * Read memory from GC virtual address using the current guest CR3.
|
---|
923 | *
|
---|
924 | * @returns VBox status.
|
---|
925 | * @param pVM VM handle.
|
---|
926 | * @param pvDst Destination address (HC of course).
|
---|
927 | * @param GCPtr GC virtual address.
|
---|
928 | * @param cb Number of bytes to read.
|
---|
929 | *
|
---|
930 | * @remarks Intended for the debugger facility only.
|
---|
931 | * @todo Move to DBGF, it's only selecting which functions to use!
|
---|
932 | */
|
---|
933 | VMMR3DECL(int) MMR3ReadGCVirt(PVM pVM, void *pvDst, RTGCPTR GCPtr, size_t cb)
|
---|
934 | {
|
---|
935 | if (GCPtr - pVM->mm.s.pvHyperAreaGC < pVM->mm.s.cbHyperArea)
|
---|
936 | return MMR3HyperReadGCVirt(pVM, pvDst, GCPtr, cb);
|
---|
937 | return PGMPhysSimpleReadGCPtr(pVM, pvDst, GCPtr, cb);
|
---|
938 | }
|
---|
939 |
|
---|
940 |
|
---|
941 | /**
|
---|
942 | * Write to memory at GC virtual address translated using the current guest CR3.
|
---|
943 | *
|
---|
944 | * @returns VBox status.
|
---|
945 | * @param pVM VM handle.
|
---|
946 | * @param GCPtrDst GC virtual address.
|
---|
947 | * @param pvSrc The source address (HC of course).
|
---|
948 | * @param cb Number of bytes to read.
|
---|
949 | *
|
---|
950 | * @remarks Intended for the debugger facility only.
|
---|
951 | * @todo Move to DBGF, it's only selecting which functions to use!
|
---|
952 | */
|
---|
953 | VMMR3DECL(int) MMR3WriteGCVirt(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
954 | {
|
---|
955 | if (GCPtrDst - pVM->mm.s.pvHyperAreaGC < pVM->mm.s.cbHyperArea)
|
---|
956 | return VERR_ACCESS_DENIED;
|
---|
957 | return PGMPhysSimpleWriteGCPtr(pVM, GCPtrDst, pvSrc, cb);
|
---|
958 | }
|
---|
959 |
|
---|
960 |
|
---|
961 | /**
|
---|
962 | * Get the size of the base RAM.
|
---|
963 | * This usually means the size of the first contigous block of physical memory.
|
---|
964 | *
|
---|
965 | * @returns The guest base RAM size.
|
---|
966 | * @param pVM The VM handle.
|
---|
967 | * @thread Any.
|
---|
968 | *
|
---|
969 | * @deprecated
|
---|
970 | */
|
---|
971 | VMMR3DECL(uint64_t) MMR3PhysGetRamSize(PVM pVM)
|
---|
972 | {
|
---|
973 | return pVM->mm.s.cbRamBase;
|
---|
974 | }
|
---|
975 |
|
---|