1 | /* $Id: GMMR0.cpp 20900 2009-06-24 18:05:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * GMM - Global Memory Manager.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 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_gmm GMM - The Global Memory Manager
|
---|
24 | *
|
---|
25 | * As the name indicates, this component is responsible for global memory
|
---|
26 | * management. Currently only guest RAM is allocated from the GMM, but this
|
---|
27 | * may change to include shadow page tables and other bits later.
|
---|
28 | *
|
---|
29 | * Guest RAM is managed as individual pages, but allocated from the host OS
|
---|
30 | * in chunks for reasons of portability / efficiency. To minimize the memory
|
---|
31 | * footprint all tracking structure must be as small as possible without
|
---|
32 | * unnecessary performance penalties.
|
---|
33 | *
|
---|
34 | * The allocation chunks has fixed sized, the size defined at compile time
|
---|
35 | * by the #GMM_CHUNK_SIZE \#define.
|
---|
36 | *
|
---|
37 | * Each chunk is given an unquie ID. Each page also has a unique ID. The
|
---|
38 | * relation ship between the two IDs is:
|
---|
39 | * @code
|
---|
40 | * GMM_CHUNK_SHIFT = log2(GMM_CHUNK_SIZE / PAGE_SIZE);
|
---|
41 | * idPage = (idChunk << GMM_CHUNK_SHIFT) | iPage;
|
---|
42 | * @endcode
|
---|
43 | * Where iPage is the index of the page within the chunk. This ID scheme
|
---|
44 | * permits for efficient chunk and page lookup, but it relies on the chunk size
|
---|
45 | * to be set at compile time. The chunks are organized in an AVL tree with their
|
---|
46 | * IDs being the keys.
|
---|
47 | *
|
---|
48 | * The physical address of each page in an allocation chunk is maintained by
|
---|
49 | * the #RTR0MEMOBJ and obtained using #RTR0MemObjGetPagePhysAddr. There is no
|
---|
50 | * need to duplicate this information (it'll cost 8-bytes per page if we did).
|
---|
51 | *
|
---|
52 | * So what do we need to track per page? Most importantly we need to know
|
---|
53 | * which state the page is in:
|
---|
54 | * - Private - Allocated for (eventually) backing one particular VM page.
|
---|
55 | * - Shared - Readonly page that is used by one or more VMs and treated
|
---|
56 | * as COW by PGM.
|
---|
57 | * - Free - Not used by anyone.
|
---|
58 | *
|
---|
59 | * For the page replacement operations (sharing, defragmenting and freeing)
|
---|
60 | * to be somewhat efficient, private pages needs to be associated with a
|
---|
61 | * particular page in a particular VM.
|
---|
62 | *
|
---|
63 | * Tracking the usage of shared pages is impractical and expensive, so we'll
|
---|
64 | * settle for a reference counting system instead.
|
---|
65 | *
|
---|
66 | * Free pages will be chained on LIFOs
|
---|
67 | *
|
---|
68 | * On 64-bit systems we will use a 64-bit bitfield per page, while on 32-bit
|
---|
69 | * systems a 32-bit bitfield will have to suffice because of address space
|
---|
70 | * limitations. The #GMMPAGE structure shows the details.
|
---|
71 | *
|
---|
72 | *
|
---|
73 | * @section sec_gmm_alloc_strat Page Allocation Strategy
|
---|
74 | *
|
---|
75 | * The strategy for allocating pages has to take fragmentation and shared
|
---|
76 | * pages into account, or we may end up with with 2000 chunks with only
|
---|
77 | * a few pages in each. Shared pages cannot easily be reallocated because
|
---|
78 | * of the inaccurate usage accounting (see above). Private pages can be
|
---|
79 | * reallocated by a defragmentation thread in the same manner that sharing
|
---|
80 | * is done.
|
---|
81 | *
|
---|
82 | * The first approach is to manage the free pages in two sets depending on
|
---|
83 | * whether they are mainly for the allocation of shared or private pages.
|
---|
84 | * In the initial implementation there will be almost no possibility for
|
---|
85 | * mixing shared and private pages in the same chunk (only if we're really
|
---|
86 | * stressed on memory), but when we implement forking of VMs and have to
|
---|
87 | * deal with lots of COW pages it'll start getting kind of interesting.
|
---|
88 | *
|
---|
89 | * The sets are lists of chunks with approximately the same number of
|
---|
90 | * free pages. Say the chunk size is 1MB, meaning 256 pages, and a set
|
---|
91 | * consists of 16 lists. So, the first list will contain the chunks with
|
---|
92 | * 1-7 free pages, the second covers 8-15, and so on. The chunks will be
|
---|
93 | * moved between the lists as pages are freed up or allocated.
|
---|
94 | *
|
---|
95 | *
|
---|
96 | * @section sec_gmm_costs Costs
|
---|
97 | *
|
---|
98 | * The per page cost in kernel space is 32-bit plus whatever RTR0MEMOBJ
|
---|
99 | * entails. In addition there is the chunk cost of approximately
|
---|
100 | * (sizeof(RT0MEMOBJ) + sizof(CHUNK)) / 2^CHUNK_SHIFT bytes per page.
|
---|
101 | *
|
---|
102 | * On Windows the per page #RTR0MEMOBJ cost is 32-bit on 32-bit windows
|
---|
103 | * and 64-bit on 64-bit windows (a PFN_NUMBER in the MDL). So, 64-bit per page.
|
---|
104 | * The cost on Linux is identical, but here it's because of sizeof(struct page *).
|
---|
105 | *
|
---|
106 | *
|
---|
107 | * @section sec_gmm_legacy Legacy Mode for Non-Tier-1 Platforms
|
---|
108 | *
|
---|
109 | * In legacy mode the page source is locked user pages and not
|
---|
110 | * #RTR0MemObjAllocPhysNC, this means that a page can only be allocated
|
---|
111 | * by the VM that locked it. We will make no attempt at implementing
|
---|
112 | * page sharing on these systems, just do enough to make it all work.
|
---|
113 | *
|
---|
114 | *
|
---|
115 | * @subsection sub_gmm_locking Serializing
|
---|
116 | *
|
---|
117 | * One simple fast mutex will be employed in the initial implementation, not
|
---|
118 | * two as metioned in @ref subsec_pgmPhys_Serializing.
|
---|
119 | *
|
---|
120 | * @see @ref subsec_pgmPhys_Serializing
|
---|
121 | *
|
---|
122 | *
|
---|
123 | * @section sec_gmm_overcommit Memory Over-Commitment Management
|
---|
124 | *
|
---|
125 | * The GVM will have to do the system wide memory over-commitment
|
---|
126 | * management. My current ideas are:
|
---|
127 | * - Per VM oc policy that indicates how much to initially commit
|
---|
128 | * to it and what to do in a out-of-memory situation.
|
---|
129 | * - Prevent overtaxing the host.
|
---|
130 | *
|
---|
131 | * There are some challenges here, the main ones are configurability and
|
---|
132 | * security. Should we for instance permit anyone to request 100% memory
|
---|
133 | * commitment? Who should be allowed to do runtime adjustments of the
|
---|
134 | * config. And how to prevent these settings from being lost when the last
|
---|
135 | * VM process exits? The solution is probably to have an optional root
|
---|
136 | * daemon the will keep VMMR0.r0 in memory and enable the security measures.
|
---|
137 | *
|
---|
138 | *
|
---|
139 | *
|
---|
140 | * @section sec_gmm_numa NUMA
|
---|
141 | *
|
---|
142 | * NUMA considerations will be designed and implemented a bit later.
|
---|
143 | *
|
---|
144 | * The preliminary guesses is that we will have to try allocate memory as
|
---|
145 | * close as possible to the CPUs the VM is executed on (EMT and additional CPU
|
---|
146 | * threads). Which means it's mostly about allocation and sharing policies.
|
---|
147 | * Both the scheduler and allocator interface will to supply some NUMA info
|
---|
148 | * and we'll need to have a way to calc access costs.
|
---|
149 | *
|
---|
150 | */
|
---|
151 |
|
---|
152 |
|
---|
153 | /*******************************************************************************
|
---|
154 | * Header Files *
|
---|
155 | *******************************************************************************/
|
---|
156 | #define LOG_GROUP LOG_GROUP_GMM
|
---|
157 | #include <VBox/gmm.h>
|
---|
158 | #include "GMMR0Internal.h"
|
---|
159 | #include <VBox/gvm.h>
|
---|
160 | #include <VBox/log.h>
|
---|
161 | #include <VBox/param.h>
|
---|
162 | #include <VBox/err.h>
|
---|
163 | #include <iprt/avl.h>
|
---|
164 | #include <iprt/mem.h>
|
---|
165 | #include <iprt/memobj.h>
|
---|
166 | #include <iprt/semaphore.h>
|
---|
167 | #include <iprt/string.h>
|
---|
168 |
|
---|
169 |
|
---|
170 | /*******************************************************************************
|
---|
171 | * Structures and Typedefs *
|
---|
172 | *******************************************************************************/
|
---|
173 | /** Pointer to set of free chunks. */
|
---|
174 | typedef struct GMMCHUNKFREESET *PGMMCHUNKFREESET;
|
---|
175 |
|
---|
176 | /** Pointer to a GMM allocation chunk. */
|
---|
177 | typedef struct GMMCHUNK *PGMMCHUNK;
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * The per-page tracking structure employed by the GMM.
|
---|
181 | *
|
---|
182 | * On 32-bit hosts we'll some trickery is necessary to compress all
|
---|
183 | * the information into 32-bits. When the fSharedFree member is set,
|
---|
184 | * the 30th bit decides whether it's a free page or not.
|
---|
185 | *
|
---|
186 | * Because of the different layout on 32-bit and 64-bit hosts, macros
|
---|
187 | * are used to get and set some of the data.
|
---|
188 | */
|
---|
189 | typedef union GMMPAGE
|
---|
190 | {
|
---|
191 | #if HC_ARCH_BITS == 64
|
---|
192 | /** Unsigned integer view. */
|
---|
193 | uint64_t u;
|
---|
194 |
|
---|
195 | /** The common view. */
|
---|
196 | struct GMMPAGECOMMON
|
---|
197 | {
|
---|
198 | uint32_t uStuff1 : 32;
|
---|
199 | uint32_t uStuff2 : 30;
|
---|
200 | /** The page state. */
|
---|
201 | uint32_t u2State : 2;
|
---|
202 | } Common;
|
---|
203 |
|
---|
204 | /** The view of a private page. */
|
---|
205 | struct GMMPAGEPRIVATE
|
---|
206 | {
|
---|
207 | /** The guest page frame number. (Max addressable: 2 ^ 44 - 16) */
|
---|
208 | uint32_t pfn;
|
---|
209 | /** The GVM handle. (64K VMs) */
|
---|
210 | uint32_t hGVM : 16;
|
---|
211 | /** Reserved. */
|
---|
212 | uint32_t u16Reserved : 14;
|
---|
213 | /** The page state. */
|
---|
214 | uint32_t u2State : 2;
|
---|
215 | } Private;
|
---|
216 |
|
---|
217 | /** The view of a shared page. */
|
---|
218 | struct GMMPAGESHARED
|
---|
219 | {
|
---|
220 | /** The reference count. */
|
---|
221 | uint32_t cRefs;
|
---|
222 | /** Reserved. Checksum or something? Two hGVMs for forking? */
|
---|
223 | uint32_t u30Reserved : 30;
|
---|
224 | /** The page state. */
|
---|
225 | uint32_t u2State : 2;
|
---|
226 | } Shared;
|
---|
227 |
|
---|
228 | /** The view of a free page. */
|
---|
229 | struct GMMPAGEFREE
|
---|
230 | {
|
---|
231 | /** The index of the next page in the free list. UINT16_MAX is NIL. */
|
---|
232 | uint16_t iNext;
|
---|
233 | /** Reserved. Checksum or something? */
|
---|
234 | uint16_t u16Reserved0;
|
---|
235 | /** Reserved. Checksum or something? */
|
---|
236 | uint32_t u30Reserved1 : 30;
|
---|
237 | /** The page state. */
|
---|
238 | uint32_t u2State : 2;
|
---|
239 | } Free;
|
---|
240 |
|
---|
241 | #else /* 32-bit */
|
---|
242 | /** Unsigned integer view. */
|
---|
243 | uint32_t u;
|
---|
244 |
|
---|
245 | /** The common view. */
|
---|
246 | struct GMMPAGECOMMON
|
---|
247 | {
|
---|
248 | uint32_t uStuff : 30;
|
---|
249 | /** The page state. */
|
---|
250 | uint32_t u2State : 2;
|
---|
251 | } Common;
|
---|
252 |
|
---|
253 | /** The view of a private page. */
|
---|
254 | struct GMMPAGEPRIVATE
|
---|
255 | {
|
---|
256 | /** The guest page frame number. (Max addressable: 2 ^ 36) */
|
---|
257 | uint32_t pfn : 24;
|
---|
258 | /** The GVM handle. (127 VMs) */
|
---|
259 | uint32_t hGVM : 7;
|
---|
260 | /** The top page state bit, MBZ. */
|
---|
261 | uint32_t fZero : 1;
|
---|
262 | } Private;
|
---|
263 |
|
---|
264 | /** The view of a shared page. */
|
---|
265 | struct GMMPAGESHARED
|
---|
266 | {
|
---|
267 | /** The reference count. */
|
---|
268 | uint32_t cRefs : 30;
|
---|
269 | /** The page state. */
|
---|
270 | uint32_t u2State : 2;
|
---|
271 | } Shared;
|
---|
272 |
|
---|
273 | /** The view of a free page. */
|
---|
274 | struct GMMPAGEFREE
|
---|
275 | {
|
---|
276 | /** The index of the next page in the free list. UINT16_MAX is NIL. */
|
---|
277 | uint32_t iNext : 16;
|
---|
278 | /** Reserved. Checksum or something? */
|
---|
279 | uint32_t u14Reserved : 14;
|
---|
280 | /** The page state. */
|
---|
281 | uint32_t u2State : 2;
|
---|
282 | } Free;
|
---|
283 | #endif
|
---|
284 | } GMMPAGE;
|
---|
285 | AssertCompileSize(GMMPAGE, sizeof(RTHCUINTPTR));
|
---|
286 | /** Pointer to a GMMPAGE. */
|
---|
287 | typedef GMMPAGE *PGMMPAGE;
|
---|
288 |
|
---|
289 |
|
---|
290 | /** @name The Page States.
|
---|
291 | * @{ */
|
---|
292 | /** A private page. */
|
---|
293 | #define GMM_PAGE_STATE_PRIVATE 0
|
---|
294 | /** A private page - alternative value used on the 32-bit implemenation.
|
---|
295 | * This will never be used on 64-bit hosts. */
|
---|
296 | #define GMM_PAGE_STATE_PRIVATE_32 1
|
---|
297 | /** A shared page. */
|
---|
298 | #define GMM_PAGE_STATE_SHARED 2
|
---|
299 | /** A free page. */
|
---|
300 | #define GMM_PAGE_STATE_FREE 3
|
---|
301 | /** @} */
|
---|
302 |
|
---|
303 |
|
---|
304 | /** @def GMM_PAGE_IS_PRIVATE
|
---|
305 | *
|
---|
306 | * @returns true if private, false if not.
|
---|
307 | * @param pPage The GMM page.
|
---|
308 | */
|
---|
309 | #if HC_ARCH_BITS == 64
|
---|
310 | # define GMM_PAGE_IS_PRIVATE(pPage) ( (pPage)->Common.u2State == GMM_PAGE_STATE_PRIVATE )
|
---|
311 | #else
|
---|
312 | # define GMM_PAGE_IS_PRIVATE(pPage) ( (pPage)->Private.fZero == 0 )
|
---|
313 | #endif
|
---|
314 |
|
---|
315 | /** @def GMM_PAGE_IS_SHARED
|
---|
316 | *
|
---|
317 | * @returns true if shared, false if not.
|
---|
318 | * @param pPage The GMM page.
|
---|
319 | */
|
---|
320 | #define GMM_PAGE_IS_SHARED(pPage) ( (pPage)->Common.u2State == GMM_PAGE_STATE_SHARED )
|
---|
321 |
|
---|
322 | /** @def GMM_PAGE_IS_FREE
|
---|
323 | *
|
---|
324 | * @returns true if free, false if not.
|
---|
325 | * @param pPage The GMM page.
|
---|
326 | */
|
---|
327 | #define GMM_PAGE_IS_FREE(pPage) ( (pPage)->Common.u2State == GMM_PAGE_STATE_FREE )
|
---|
328 |
|
---|
329 | /** @def GMM_PAGE_PFN_LAST
|
---|
330 | * The last valid guest pfn range.
|
---|
331 | * @remark Some of the values outside the range has special meaning,
|
---|
332 | * see GMM_PAGE_PFN_UNSHAREABLE.
|
---|
333 | */
|
---|
334 | #if HC_ARCH_BITS == 64
|
---|
335 | # define GMM_PAGE_PFN_LAST UINT32_C(0xfffffff0)
|
---|
336 | #else
|
---|
337 | # define GMM_PAGE_PFN_LAST UINT32_C(0x00fffff0)
|
---|
338 | #endif
|
---|
339 | AssertCompile(GMM_PAGE_PFN_LAST == (GMM_GCPHYS_LAST >> PAGE_SHIFT));
|
---|
340 |
|
---|
341 | /** @def GMM_PAGE_PFN_UNSHAREABLE
|
---|
342 | * Indicates that this page isn't used for normal guest memory and thus isn't shareable.
|
---|
343 | */
|
---|
344 | #if HC_ARCH_BITS == 64
|
---|
345 | # define GMM_PAGE_PFN_UNSHAREABLE UINT32_C(0xfffffff1)
|
---|
346 | #else
|
---|
347 | # define GMM_PAGE_PFN_UNSHAREABLE UINT32_C(0x00fffff1)
|
---|
348 | #endif
|
---|
349 | AssertCompile(GMM_PAGE_PFN_UNSHAREABLE == (GMM_GCPHYS_UNSHAREABLE >> PAGE_SHIFT));
|
---|
350 |
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * A GMM allocation chunk ring-3 mapping record.
|
---|
354 | *
|
---|
355 | * This should really be associated with a session and not a VM, but
|
---|
356 | * it's simpler to associated with a VM and cleanup with the VM object
|
---|
357 | * is destroyed.
|
---|
358 | */
|
---|
359 | typedef struct GMMCHUNKMAP
|
---|
360 | {
|
---|
361 | /** The mapping object. */
|
---|
362 | RTR0MEMOBJ MapObj;
|
---|
363 | /** The VM owning the mapping. */
|
---|
364 | PGVM pGVM;
|
---|
365 | } GMMCHUNKMAP;
|
---|
366 | /** Pointer to a GMM allocation chunk mapping. */
|
---|
367 | typedef struct GMMCHUNKMAP *PGMMCHUNKMAP;
|
---|
368 |
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * A GMM allocation chunk.
|
---|
372 | */
|
---|
373 | typedef struct GMMCHUNK
|
---|
374 | {
|
---|
375 | /** The AVL node core.
|
---|
376 | * The Key is the chunk ID. */
|
---|
377 | AVLU32NODECORE Core;
|
---|
378 | /** The memory object.
|
---|
379 | * Either from RTR0MemObjAllocPhysNC or RTR0MemObjLockUser depending on
|
---|
380 | * what the host can dish up with. */
|
---|
381 | RTR0MEMOBJ MemObj;
|
---|
382 | /** Pointer to the next chunk in the free list. */
|
---|
383 | PGMMCHUNK pFreeNext;
|
---|
384 | /** Pointer to the previous chunk in the free list. */
|
---|
385 | PGMMCHUNK pFreePrev;
|
---|
386 | /** Pointer to the free set this chunk belongs to. NULL for
|
---|
387 | * chunks with no free pages. */
|
---|
388 | PGMMCHUNKFREESET pSet;
|
---|
389 | /** Pointer to an array of mappings. */
|
---|
390 | PGMMCHUNKMAP paMappings;
|
---|
391 | /** The number of mappings. */
|
---|
392 | uint16_t cMappings;
|
---|
393 | /** The head of the list of free pages. UINT16_MAX is the NIL value. */
|
---|
394 | uint16_t iFreeHead;
|
---|
395 | /** The number of free pages. */
|
---|
396 | uint16_t cFree;
|
---|
397 | /** The GVM handle of the VM that first allocated pages from this chunk, this
|
---|
398 | * is used as a preference when there are several chunks to choose from.
|
---|
399 | * When in bound memory mode this isn't a preference any longer. */
|
---|
400 | uint16_t hGVM;
|
---|
401 | /** The number of private pages. */
|
---|
402 | uint16_t cPrivate;
|
---|
403 | /** The number of shared pages. */
|
---|
404 | uint16_t cShared;
|
---|
405 | #if HC_ARCH_BITS == 64
|
---|
406 | /** Reserved for later. */
|
---|
407 | uint16_t au16Reserved[2];
|
---|
408 | #endif
|
---|
409 | /** The pages. */
|
---|
410 | GMMPAGE aPages[GMM_CHUNK_SIZE >> PAGE_SHIFT];
|
---|
411 | } GMMCHUNK;
|
---|
412 |
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * An allocation chunk TLB entry.
|
---|
416 | */
|
---|
417 | typedef struct GMMCHUNKTLBE
|
---|
418 | {
|
---|
419 | /** The chunk id. */
|
---|
420 | uint32_t idChunk;
|
---|
421 | /** Pointer to the chunk. */
|
---|
422 | PGMMCHUNK pChunk;
|
---|
423 | } GMMCHUNKTLBE;
|
---|
424 | /** Pointer to an allocation chunk TLB entry. */
|
---|
425 | typedef GMMCHUNKTLBE *PGMMCHUNKTLBE;
|
---|
426 |
|
---|
427 |
|
---|
428 | /** The number of entries tin the allocation chunk TLB. */
|
---|
429 | #define GMM_CHUNKTLB_ENTRIES 32
|
---|
430 | /** Gets the TLB entry index for the given Chunk ID. */
|
---|
431 | #define GMM_CHUNKTLB_IDX(idChunk) ( (idChunk) & (GMM_CHUNKTLB_ENTRIES - 1) )
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * An allocation chunk TLB.
|
---|
435 | */
|
---|
436 | typedef struct GMMCHUNKTLB
|
---|
437 | {
|
---|
438 | /** The TLB entries. */
|
---|
439 | GMMCHUNKTLBE aEntries[GMM_CHUNKTLB_ENTRIES];
|
---|
440 | } GMMCHUNKTLB;
|
---|
441 | /** Pointer to an allocation chunk TLB. */
|
---|
442 | typedef GMMCHUNKTLB *PGMMCHUNKTLB;
|
---|
443 |
|
---|
444 |
|
---|
445 | /** The GMMCHUNK::cFree shift count. */
|
---|
446 | #define GMM_CHUNK_FREE_SET_SHIFT 4
|
---|
447 | /** The GMMCHUNK::cFree mask for use when considering relinking a chunk. */
|
---|
448 | #define GMM_CHUNK_FREE_SET_MASK 15
|
---|
449 | /** The number of lists in set. */
|
---|
450 | #define GMM_CHUNK_FREE_SET_LISTS (GMM_CHUNK_NUM_PAGES >> GMM_CHUNK_FREE_SET_SHIFT)
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * A set of free chunks.
|
---|
454 | */
|
---|
455 | typedef struct GMMCHUNKFREESET
|
---|
456 | {
|
---|
457 | /** The number of free pages in the set. */
|
---|
458 | uint64_t cFreePages;
|
---|
459 | /** Chunks ordered by increasing number of free pages. */
|
---|
460 | PGMMCHUNK apLists[GMM_CHUNK_FREE_SET_LISTS];
|
---|
461 | } GMMCHUNKFREESET;
|
---|
462 |
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * The GMM instance data.
|
---|
466 | */
|
---|
467 | typedef struct GMM
|
---|
468 | {
|
---|
469 | /** Magic / eye catcher. GMM_MAGIC */
|
---|
470 | uint32_t u32Magic;
|
---|
471 | /** The fast mutex protecting the GMM.
|
---|
472 | * More fine grained locking can be implemented later if necessary. */
|
---|
473 | RTSEMFASTMUTEX Mtx;
|
---|
474 | /** The chunk tree. */
|
---|
475 | PAVLU32NODECORE pChunks;
|
---|
476 | /** The chunk TLB. */
|
---|
477 | GMMCHUNKTLB ChunkTLB;
|
---|
478 | /** The private free set. */
|
---|
479 | GMMCHUNKFREESET Private;
|
---|
480 | /** The shared free set. */
|
---|
481 | GMMCHUNKFREESET Shared;
|
---|
482 |
|
---|
483 | /** The maximum number of pages we're allowed to allocate.
|
---|
484 | * @gcfgm 64-bit GMM/MaxPages Direct.
|
---|
485 | * @gcfgm 32-bit GMM/PctPages Relative to the number of host pages. */
|
---|
486 | uint64_t cMaxPages;
|
---|
487 | /** The number of pages that has been reserved.
|
---|
488 | * The deal is that cReservedPages - cOverCommittedPages <= cMaxPages. */
|
---|
489 | uint64_t cReservedPages;
|
---|
490 | /** The number of pages that we have over-committed in reservations. */
|
---|
491 | uint64_t cOverCommittedPages;
|
---|
492 | /** The number of actually allocated (committed if you like) pages. */
|
---|
493 | uint64_t cAllocatedPages;
|
---|
494 | /** The number of pages that are shared. A subset of cAllocatedPages. */
|
---|
495 | uint64_t cSharedPages;
|
---|
496 | /** The number of pages that are shared that has been left behind by
|
---|
497 | * VMs not doing proper cleanups. */
|
---|
498 | uint64_t cLeftBehindSharedPages;
|
---|
499 | /** The number of allocation chunks.
|
---|
500 | * (The number of pages we've allocated from the host can be derived from this.) */
|
---|
501 | uint32_t cChunks;
|
---|
502 | /** The number of current ballooned pages. */
|
---|
503 | uint64_t cBalloonedPages;
|
---|
504 |
|
---|
505 | /** The legacy allocation mode indicator.
|
---|
506 | * This is determined at initialization time. */
|
---|
507 | bool fLegacyAllocationMode;
|
---|
508 | /** The bound memory mode indicator.
|
---|
509 | * When set, the memory will be bound to a specific VM and never
|
---|
510 | * shared. This is always set if fLegacyAllocationMode is set.
|
---|
511 | * (Also determined at initialization time.) */
|
---|
512 | bool fBoundMemoryMode;
|
---|
513 | /** The number of registered VMs. */
|
---|
514 | uint16_t cRegisteredVMs;
|
---|
515 |
|
---|
516 | /** The previous allocated Chunk ID.
|
---|
517 | * Used as a hint to avoid scanning the whole bitmap. */
|
---|
518 | uint32_t idChunkPrev;
|
---|
519 | /** Chunk ID allocation bitmap.
|
---|
520 | * Bits of allocated IDs are set, free ones are clear.
|
---|
521 | * The NIL id (0) is marked allocated. */
|
---|
522 | uint32_t bmChunkId[(GMM_CHUNKID_LAST + 1 + 31) / 32];
|
---|
523 | } GMM;
|
---|
524 | /** Pointer to the GMM instance. */
|
---|
525 | typedef GMM *PGMM;
|
---|
526 |
|
---|
527 | /** The value of GMM::u32Magic (Katsuhiro Otomo). */
|
---|
528 | #define GMM_MAGIC 0x19540414
|
---|
529 |
|
---|
530 |
|
---|
531 | /*******************************************************************************
|
---|
532 | * Global Variables *
|
---|
533 | *******************************************************************************/
|
---|
534 | /** Pointer to the GMM instance data. */
|
---|
535 | static PGMM g_pGMM = NULL;
|
---|
536 |
|
---|
537 | /** Macro for obtaining and validating the g_pGMM pointer.
|
---|
538 | * On failure it will return from the invoking function with the specified return value.
|
---|
539 | *
|
---|
540 | * @param pGMM The name of the pGMM variable.
|
---|
541 | * @param rc The return value on failure. Use VERR_INTERNAL_ERROR for
|
---|
542 | * VBox status codes.
|
---|
543 | */
|
---|
544 | #define GMM_GET_VALID_INSTANCE(pGMM, rc) \
|
---|
545 | do { \
|
---|
546 | (pGMM) = g_pGMM; \
|
---|
547 | AssertPtrReturn((pGMM), (rc)); \
|
---|
548 | AssertMsgReturn((pGMM)->u32Magic == GMM_MAGIC, ("%p - %#x\n", (pGMM), (pGMM)->u32Magic), (rc)); \
|
---|
549 | } while (0)
|
---|
550 |
|
---|
551 | /** Macro for obtaining and validating the g_pGMM pointer, void function variant.
|
---|
552 | * On failure it will return from the invoking function.
|
---|
553 | *
|
---|
554 | * @param pGMM The name of the pGMM variable.
|
---|
555 | */
|
---|
556 | #define GMM_GET_VALID_INSTANCE_VOID(pGMM) \
|
---|
557 | do { \
|
---|
558 | (pGMM) = g_pGMM; \
|
---|
559 | AssertPtrReturnVoid((pGMM)); \
|
---|
560 | AssertMsgReturnVoid((pGMM)->u32Magic == GMM_MAGIC, ("%p - %#x\n", (pGMM), (pGMM)->u32Magic)); \
|
---|
561 | } while (0)
|
---|
562 |
|
---|
563 |
|
---|
564 | /** @def GMM_CHECK_SANITY_UPON_ENTERING
|
---|
565 | * Checks the sanity of the GMM instance data before making changes.
|
---|
566 | *
|
---|
567 | * This is macro is a stub by default and must be enabled manually in the code.
|
---|
568 | *
|
---|
569 | * @returns true if sane, false if not.
|
---|
570 | * @param pGMM The name of the pGMM variable.
|
---|
571 | */
|
---|
572 | #if defined(VBOX_STRICT) && 0
|
---|
573 | # define GMM_CHECK_SANITY_UPON_ENTERING(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
|
---|
574 | #else
|
---|
575 | # define GMM_CHECK_SANITY_UPON_ENTERING(pGMM) (true)
|
---|
576 | #endif
|
---|
577 |
|
---|
578 | /** @def GMM_CHECK_SANITY_UPON_LEAVING
|
---|
579 | * Checks the sanity of the GMM instance data after making changes.
|
---|
580 | *
|
---|
581 | * This is macro is a stub by default and must be enabled manually in the code.
|
---|
582 | *
|
---|
583 | * @returns true if sane, false if not.
|
---|
584 | * @param pGMM The name of the pGMM variable.
|
---|
585 | */
|
---|
586 | #if defined(VBOX_STRICT) && 0
|
---|
587 | # define GMM_CHECK_SANITY_UPON_LEAVING(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
|
---|
588 | #else
|
---|
589 | # define GMM_CHECK_SANITY_UPON_LEAVING(pGMM) (true)
|
---|
590 | #endif
|
---|
591 |
|
---|
592 | /** @def GMM_CHECK_SANITY_IN_LOOPS
|
---|
593 | * Checks the sanity of the GMM instance in the allocation loops.
|
---|
594 | *
|
---|
595 | * This is macro is a stub by default and must be enabled manually in the code.
|
---|
596 | *
|
---|
597 | * @returns true if sane, false if not.
|
---|
598 | * @param pGMM The name of the pGMM variable.
|
---|
599 | */
|
---|
600 | #if defined(VBOX_STRICT) && 0
|
---|
601 | # define GMM_CHECK_SANITY_IN_LOOPS(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
|
---|
602 | #else
|
---|
603 | # define GMM_CHECK_SANITY_IN_LOOPS(pGMM) (true)
|
---|
604 | #endif
|
---|
605 |
|
---|
606 |
|
---|
607 | /*******************************************************************************
|
---|
608 | * Internal Functions *
|
---|
609 | *******************************************************************************/
|
---|
610 | static DECLCALLBACK(int) gmmR0TermDestroyChunk(PAVLU32NODECORE pNode, void *pvGMM);
|
---|
611 | static DECLCALLBACK(int) gmmR0CleanupVMScanChunk(PAVLU32NODECORE pNode, void *pvGMM);
|
---|
612 | /*static*/ DECLCALLBACK(int) gmmR0CleanupVMDestroyChunk(PAVLU32NODECORE pNode, void *pvGVM);
|
---|
613 | DECLINLINE(void) gmmR0LinkChunk(PGMMCHUNK pChunk, PGMMCHUNKFREESET pSet);
|
---|
614 | DECLINLINE(void) gmmR0UnlinkChunk(PGMMCHUNK pChunk);
|
---|
615 | static uint32_t gmmR0SanityCheck(PGMM pGMM, const char *pszFunction, unsigned uLineNo);
|
---|
616 | static void gmmR0FreeChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk);
|
---|
617 | static void gmmR0FreeSharedPage(PGMM pGMM, uint32_t idPage, PGMMPAGE pPage);
|
---|
618 | static int gmmR0UnmapChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk);
|
---|
619 |
|
---|
620 |
|
---|
621 |
|
---|
622 | /**
|
---|
623 | * Initializes the GMM component.
|
---|
624 | *
|
---|
625 | * This is called when the VMMR0.r0 module is loaded and protected by the
|
---|
626 | * loader semaphore.
|
---|
627 | *
|
---|
628 | * @returns VBox status code.
|
---|
629 | */
|
---|
630 | GMMR0DECL(int) GMMR0Init(void)
|
---|
631 | {
|
---|
632 | LogFlow(("GMMInit:\n"));
|
---|
633 |
|
---|
634 | /*
|
---|
635 | * Allocate the instance data and the lock(s).
|
---|
636 | */
|
---|
637 | PGMM pGMM = (PGMM)RTMemAllocZ(sizeof(*pGMM));
|
---|
638 | if (!pGMM)
|
---|
639 | return VERR_NO_MEMORY;
|
---|
640 | pGMM->u32Magic = GMM_MAGIC;
|
---|
641 | for (unsigned i = 0; i < RT_ELEMENTS(pGMM->ChunkTLB.aEntries); i++)
|
---|
642 | pGMM->ChunkTLB.aEntries[i].idChunk = NIL_GMM_CHUNKID;
|
---|
643 | ASMBitSet(&pGMM->bmChunkId[0], NIL_GMM_CHUNKID);
|
---|
644 |
|
---|
645 | int rc = RTSemFastMutexCreate(&pGMM->Mtx);
|
---|
646 | if (RT_SUCCESS(rc))
|
---|
647 | {
|
---|
648 | /*
|
---|
649 | * Check and see if RTR0MemObjAllocPhysNC works.
|
---|
650 | */
|
---|
651 | #if 0 /* later, see #3170. */
|
---|
652 | RTR0MEMOBJ MemObj;
|
---|
653 | rc = RTR0MemObjAllocPhysNC(&MemObj, _64K, NIL_RTHCPHYS);
|
---|
654 | if (RT_SUCCESS(rc))
|
---|
655 | {
|
---|
656 | rc = RTR0MemObjFree(MemObj, true);
|
---|
657 | AssertRC(rc);
|
---|
658 | }
|
---|
659 | else if (rc == VERR_NOT_SUPPORTED)
|
---|
660 | pGMM->fLegacyAllocationMode = pGMM->fBoundMemoryMode = true;
|
---|
661 | else
|
---|
662 | SUPR0Printf("GMMR0Init: RTR0MemObjAllocPhysNC(,64K,Any) -> %d!\n", rc);
|
---|
663 | #else
|
---|
664 | # ifdef RT_OS_WINDOWS
|
---|
665 | pGMM->fLegacyAllocationMode = false;
|
---|
666 | # else
|
---|
667 | pGMM->fLegacyAllocationMode = true;
|
---|
668 | # endif
|
---|
669 | pGMM->fBoundMemoryMode = true;
|
---|
670 | #endif
|
---|
671 |
|
---|
672 | /*
|
---|
673 | * Query system page count and guess a reasonable cMaxPages value.
|
---|
674 | */
|
---|
675 | pGMM->cMaxPages = UINT32_MAX; /** @todo IPRT function for query ram size and such. */
|
---|
676 |
|
---|
677 | g_pGMM = pGMM;
|
---|
678 | LogFlow(("GMMInit: pGMM=%p fLegacyAllocationMode=%RTbool fBoundMemoryMode=%RTbool\n", pGMM, pGMM->fLegacyAllocationMode, pGMM->fBoundMemoryMode));
|
---|
679 | return VINF_SUCCESS;
|
---|
680 | }
|
---|
681 |
|
---|
682 | RTMemFree(pGMM);
|
---|
683 | SUPR0Printf("GMMR0Init: failed! rc=%d\n", rc);
|
---|
684 | return rc;
|
---|
685 | }
|
---|
686 |
|
---|
687 |
|
---|
688 | /**
|
---|
689 | * Terminates the GMM component.
|
---|
690 | */
|
---|
691 | GMMR0DECL(void) GMMR0Term(void)
|
---|
692 | {
|
---|
693 | LogFlow(("GMMTerm:\n"));
|
---|
694 |
|
---|
695 | /*
|
---|
696 | * Take care / be paranoid...
|
---|
697 | */
|
---|
698 | PGMM pGMM = g_pGMM;
|
---|
699 | if (!VALID_PTR(pGMM))
|
---|
700 | return;
|
---|
701 | if (pGMM->u32Magic != GMM_MAGIC)
|
---|
702 | {
|
---|
703 | SUPR0Printf("GMMR0Term: u32Magic=%#x\n", pGMM->u32Magic);
|
---|
704 | return;
|
---|
705 | }
|
---|
706 |
|
---|
707 | /*
|
---|
708 | * Undo what init did and free all the resources we've acquired.
|
---|
709 | */
|
---|
710 | /* Destroy the fundamentals. */
|
---|
711 | g_pGMM = NULL;
|
---|
712 | pGMM->u32Magic++;
|
---|
713 | RTSemFastMutexDestroy(pGMM->Mtx);
|
---|
714 | pGMM->Mtx = NIL_RTSEMFASTMUTEX;
|
---|
715 |
|
---|
716 | /* free any chunks still hanging around. */
|
---|
717 | RTAvlU32Destroy(&pGMM->pChunks, gmmR0TermDestroyChunk, pGMM);
|
---|
718 |
|
---|
719 | /* finally the instance data itself. */
|
---|
720 | RTMemFree(pGMM);
|
---|
721 | LogFlow(("GMMTerm: done\n"));
|
---|
722 | }
|
---|
723 |
|
---|
724 |
|
---|
725 | /**
|
---|
726 | * RTAvlU32Destroy callback.
|
---|
727 | *
|
---|
728 | * @returns 0
|
---|
729 | * @param pNode The node to destroy.
|
---|
730 | * @param pvGMM The GMM handle.
|
---|
731 | */
|
---|
732 | static DECLCALLBACK(int) gmmR0TermDestroyChunk(PAVLU32NODECORE pNode, void *pvGMM)
|
---|
733 | {
|
---|
734 | PGMMCHUNK pChunk = (PGMMCHUNK)pNode;
|
---|
735 |
|
---|
736 | if (pChunk->cFree != (GMM_CHUNK_SIZE >> PAGE_SHIFT))
|
---|
737 | SUPR0Printf("GMMR0Term: %p/%#x: cFree=%d cPrivate=%d cShared=%d cMappings=%d\n", pChunk,
|
---|
738 | pChunk->Core.Key, pChunk->cFree, pChunk->cPrivate, pChunk->cShared, pChunk->cMappings);
|
---|
739 |
|
---|
740 | int rc = RTR0MemObjFree(pChunk->MemObj, true /* fFreeMappings */);
|
---|
741 | if (RT_FAILURE(rc))
|
---|
742 | {
|
---|
743 | SUPR0Printf("GMMR0Term: %p/%#x: RTRMemObjFree(%p,true) -> %d (cMappings=%d)\n", pChunk,
|
---|
744 | pChunk->Core.Key, pChunk->MemObj, rc, pChunk->cMappings);
|
---|
745 | AssertRC(rc);
|
---|
746 | }
|
---|
747 | pChunk->MemObj = NIL_RTR0MEMOBJ;
|
---|
748 |
|
---|
749 | RTMemFree(pChunk->paMappings);
|
---|
750 | pChunk->paMappings = NULL;
|
---|
751 |
|
---|
752 | RTMemFree(pChunk);
|
---|
753 | NOREF(pvGMM);
|
---|
754 | return 0;
|
---|
755 | }
|
---|
756 |
|
---|
757 |
|
---|
758 | /**
|
---|
759 | * Initializes the per-VM data for the GMM.
|
---|
760 | *
|
---|
761 | * This is called from within the GVMM lock (from GVMMR0CreateVM)
|
---|
762 | * and should only initialize the data members so GMMR0CleanupVM
|
---|
763 | * can deal with them. We reserve no memory or anything here,
|
---|
764 | * that's done later in GMMR0InitVM.
|
---|
765 | *
|
---|
766 | * @param pGVM Pointer to the Global VM structure.
|
---|
767 | */
|
---|
768 | GMMR0DECL(void) GMMR0InitPerVMData(PGVM pGVM)
|
---|
769 | {
|
---|
770 | AssertCompile(RT_SIZEOFMEMB(GVM,gmm.s) <= RT_SIZEOFMEMB(GVM,gmm.padding));
|
---|
771 |
|
---|
772 | pGVM->gmm.s.enmPolicy = GMMOCPOLICY_INVALID;
|
---|
773 | pGVM->gmm.s.enmPriority = GMMPRIORITY_INVALID;
|
---|
774 | pGVM->gmm.s.fMayAllocate = false;
|
---|
775 | }
|
---|
776 |
|
---|
777 |
|
---|
778 | /**
|
---|
779 | * Cleans up when a VM is terminating.
|
---|
780 | *
|
---|
781 | * @param pGVM Pointer to the Global VM structure.
|
---|
782 | */
|
---|
783 | GMMR0DECL(void) GMMR0CleanupVM(PGVM pGVM)
|
---|
784 | {
|
---|
785 | LogFlow(("GMMR0CleanupVM: pGVM=%p:{.pVM=%p, .hSelf=%#x}\n", pGVM, pGVM->pVM, pGVM->hSelf));
|
---|
786 |
|
---|
787 | PGMM pGMM;
|
---|
788 | GMM_GET_VALID_INSTANCE_VOID(pGMM);
|
---|
789 |
|
---|
790 | int rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
791 | AssertRC(rc);
|
---|
792 | GMM_CHECK_SANITY_UPON_ENTERING(pGMM);
|
---|
793 |
|
---|
794 | /*
|
---|
795 | * The policy is 'INVALID' until the initial reservation
|
---|
796 | * request has been serviced.
|
---|
797 | */
|
---|
798 | if ( pGVM->gmm.s.enmPolicy > GMMOCPOLICY_INVALID
|
---|
799 | && pGVM->gmm.s.enmPolicy < GMMOCPOLICY_END)
|
---|
800 | {
|
---|
801 | /*
|
---|
802 | * If it's the last VM around, we can skip walking all the chunk looking
|
---|
803 | * for the pages owned by this VM and instead flush the whole shebang.
|
---|
804 | *
|
---|
805 | * This takes care of the eventuality that a VM has left shared page
|
---|
806 | * references behind (shouldn't happen of course, but you never know).
|
---|
807 | */
|
---|
808 | Assert(pGMM->cRegisteredVMs);
|
---|
809 | pGMM->cRegisteredVMs--;
|
---|
810 | #if 0 /* disabled so it won't hide bugs. */
|
---|
811 | if (!pGMM->cRegisteredVMs)
|
---|
812 | {
|
---|
813 | RTAvlU32Destroy(&pGMM->pChunks, gmmR0CleanupVMDestroyChunk, pGMM);
|
---|
814 |
|
---|
815 | for (unsigned i = 0; i < RT_ELEMENTS(pGMM->ChunkTLB.aEntries); i++)
|
---|
816 | {
|
---|
817 | pGMM->ChunkTLB.aEntries[i].idChunk = NIL_GMM_CHUNKID;
|
---|
818 | pGMM->ChunkTLB.aEntries[i].pChunk = NULL;
|
---|
819 | }
|
---|
820 |
|
---|
821 | memset(&pGMM->Private, 0, sizeof(pGMM->Private));
|
---|
822 | memset(&pGMM->Shared, 0, sizeof(pGMM->Shared));
|
---|
823 |
|
---|
824 | memset(&pGMM->bmChunkId[0], 0, sizeof(pGMM->bmChunkId));
|
---|
825 | ASMBitSet(&pGMM->bmChunkId[0], NIL_GMM_CHUNKID);
|
---|
826 |
|
---|
827 | pGMM->cReservedPages = 0;
|
---|
828 | pGMM->cOverCommittedPages = 0;
|
---|
829 | pGMM->cAllocatedPages = 0;
|
---|
830 | pGMM->cSharedPages = 0;
|
---|
831 | pGMM->cLeftBehindSharedPages = 0;
|
---|
832 | pGMM->cChunks = 0;
|
---|
833 | pGMM->cBalloonedPages = 0;
|
---|
834 | }
|
---|
835 | else
|
---|
836 | #endif
|
---|
837 | {
|
---|
838 | /*
|
---|
839 | * Walk the entire pool looking for pages that belongs to this VM
|
---|
840 | * and left over mappings. (This'll only catch private pages, shared
|
---|
841 | * pages will be 'left behind'.)
|
---|
842 | */
|
---|
843 | uint64_t cPrivatePages = pGVM->gmm.s.cPrivatePages; /* save */
|
---|
844 | RTAvlU32DoWithAll(&pGMM->pChunks, true /* fFromLeft */, gmmR0CleanupVMScanChunk, pGVM);
|
---|
845 | if (pGVM->gmm.s.cPrivatePages)
|
---|
846 | SUPR0Printf("GMMR0CleanupVM: hGVM=%#x has %#x private pages that cannot be found!\n", pGVM->hSelf, pGVM->gmm.s.cPrivatePages);
|
---|
847 | pGMM->cAllocatedPages -= cPrivatePages;
|
---|
848 |
|
---|
849 | /* free empty chunks. */
|
---|
850 | if (cPrivatePages)
|
---|
851 | {
|
---|
852 | PGMMCHUNK pCur = pGMM->Private.apLists[RT_ELEMENTS(pGMM->Private.apLists) - 1];
|
---|
853 | while (pCur)
|
---|
854 | {
|
---|
855 | PGMMCHUNK pNext = pCur->pFreeNext;
|
---|
856 | if ( pCur->cFree == GMM_CHUNK_NUM_PAGES
|
---|
857 | && ( !pGMM->fBoundMemoryMode
|
---|
858 | || pCur->hGVM == pGVM->hSelf))
|
---|
859 | gmmR0FreeChunk(pGMM, pGVM, pCur);
|
---|
860 | pCur = pNext;
|
---|
861 | }
|
---|
862 | }
|
---|
863 |
|
---|
864 | /* account for shared pages that weren't freed. */
|
---|
865 | if (pGVM->gmm.s.cSharedPages)
|
---|
866 | {
|
---|
867 | Assert(pGMM->cSharedPages >= pGVM->gmm.s.cSharedPages);
|
---|
868 | SUPR0Printf("GMMR0CleanupVM: hGVM=%#x left %#x shared pages behind!\n", pGVM->hSelf, pGVM->gmm.s.cSharedPages);
|
---|
869 | pGMM->cLeftBehindSharedPages += pGVM->gmm.s.cSharedPages;
|
---|
870 | }
|
---|
871 |
|
---|
872 | /*
|
---|
873 | * Update the over-commitment management statistics.
|
---|
874 | */
|
---|
875 | pGMM->cReservedPages -= pGVM->gmm.s.Reserved.cBasePages
|
---|
876 | + pGVM->gmm.s.Reserved.cFixedPages
|
---|
877 | + pGVM->gmm.s.Reserved.cShadowPages;
|
---|
878 | switch (pGVM->gmm.s.enmPolicy)
|
---|
879 | {
|
---|
880 | case GMMOCPOLICY_NO_OC:
|
---|
881 | break;
|
---|
882 | default:
|
---|
883 | /** @todo Update GMM->cOverCommittedPages */
|
---|
884 | break;
|
---|
885 | }
|
---|
886 | }
|
---|
887 | }
|
---|
888 |
|
---|
889 | /* zap the GVM data. */
|
---|
890 | pGVM->gmm.s.enmPolicy = GMMOCPOLICY_INVALID;
|
---|
891 | pGVM->gmm.s.enmPriority = GMMPRIORITY_INVALID;
|
---|
892 | pGVM->gmm.s.fMayAllocate = false;
|
---|
893 |
|
---|
894 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
895 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
896 |
|
---|
897 | LogFlow(("GMMR0CleanupVM: returns\n"));
|
---|
898 | }
|
---|
899 |
|
---|
900 |
|
---|
901 | /**
|
---|
902 | * RTAvlU32DoWithAll callback.
|
---|
903 | *
|
---|
904 | * @returns 0
|
---|
905 | * @param pNode The node to search.
|
---|
906 | * @param pvGVM Pointer to the shared VM structure.
|
---|
907 | */
|
---|
908 | static DECLCALLBACK(int) gmmR0CleanupVMScanChunk(PAVLU32NODECORE pNode, void *pvGVM)
|
---|
909 | {
|
---|
910 | PGMMCHUNK pChunk = (PGMMCHUNK)pNode;
|
---|
911 | PGVM pGVM = (PGVM)pvGVM;
|
---|
912 |
|
---|
913 | /*
|
---|
914 | * Look for pages belonging to the VM.
|
---|
915 | * (Perform some internal checks while we're scanning.)
|
---|
916 | */
|
---|
917 | #ifndef VBOX_STRICT
|
---|
918 | if (pChunk->cFree != (GMM_CHUNK_SIZE >> PAGE_SHIFT))
|
---|
919 | #endif
|
---|
920 | {
|
---|
921 | unsigned cPrivate = 0;
|
---|
922 | unsigned cShared = 0;
|
---|
923 | unsigned cFree = 0;
|
---|
924 |
|
---|
925 | gmmR0UnlinkChunk(pChunk); /* avoiding cFreePages updates. */
|
---|
926 |
|
---|
927 | uint16_t hGVM = pGVM->hSelf;
|
---|
928 | unsigned iPage = (GMM_CHUNK_SIZE >> PAGE_SHIFT);
|
---|
929 | while (iPage-- > 0)
|
---|
930 | if (GMM_PAGE_IS_PRIVATE(&pChunk->aPages[iPage]))
|
---|
931 | {
|
---|
932 | if (pChunk->aPages[iPage].Private.hGVM == hGVM)
|
---|
933 | {
|
---|
934 | /*
|
---|
935 | * Free the page.
|
---|
936 | *
|
---|
937 | * The reason for not using gmmR0FreePrivatePage here is that we
|
---|
938 | * must *not* cause the chunk to be freed from under us - we're in
|
---|
939 | * an AVL tree walk here.
|
---|
940 | */
|
---|
941 | pChunk->aPages[iPage].u = 0;
|
---|
942 | pChunk->aPages[iPage].Free.iNext = pChunk->iFreeHead;
|
---|
943 | pChunk->aPages[iPage].Free.u2State = GMM_PAGE_STATE_FREE;
|
---|
944 | pChunk->iFreeHead = iPage;
|
---|
945 | pChunk->cPrivate--;
|
---|
946 | pChunk->cFree++;
|
---|
947 | pGVM->gmm.s.cPrivatePages--;
|
---|
948 | cFree++;
|
---|
949 | }
|
---|
950 | else
|
---|
951 | cPrivate++;
|
---|
952 | }
|
---|
953 | else if (GMM_PAGE_IS_FREE(&pChunk->aPages[iPage]))
|
---|
954 | cFree++;
|
---|
955 | else
|
---|
956 | cShared++;
|
---|
957 |
|
---|
958 | gmmR0LinkChunk(pChunk, pChunk->cShared ? &g_pGMM->Shared : &g_pGMM->Private);
|
---|
959 |
|
---|
960 | /*
|
---|
961 | * Did it add up?
|
---|
962 | */
|
---|
963 | if (RT_UNLIKELY( pChunk->cFree != cFree
|
---|
964 | || pChunk->cPrivate != cPrivate
|
---|
965 | || pChunk->cShared != cShared))
|
---|
966 | {
|
---|
967 | SUPR0Printf("gmmR0CleanupVMScanChunk: Chunk %p/%#x has bogus stats - free=%d/%d private=%d/%d shared=%d/%d\n",
|
---|
968 | pChunk->cFree, cFree, pChunk->cPrivate, cPrivate, pChunk->cShared, cShared);
|
---|
969 | pChunk->cFree = cFree;
|
---|
970 | pChunk->cPrivate = cPrivate;
|
---|
971 | pChunk->cShared = cShared;
|
---|
972 | }
|
---|
973 | }
|
---|
974 |
|
---|
975 | /*
|
---|
976 | * Look for the mapping belonging to the terminating VM.
|
---|
977 | */
|
---|
978 | for (unsigned i = 0; i < pChunk->cMappings; i++)
|
---|
979 | if (pChunk->paMappings[i].pGVM == pGVM)
|
---|
980 | {
|
---|
981 | RTR0MEMOBJ MemObj = pChunk->paMappings[i].MapObj;
|
---|
982 |
|
---|
983 | pChunk->cMappings--;
|
---|
984 | if (i < pChunk->cMappings)
|
---|
985 | pChunk->paMappings[i] = pChunk->paMappings[pChunk->cMappings];
|
---|
986 | pChunk->paMappings[pChunk->cMappings].pGVM = NULL;
|
---|
987 | pChunk->paMappings[pChunk->cMappings].MapObj = NIL_RTR0MEMOBJ;
|
---|
988 |
|
---|
989 | int rc = RTR0MemObjFree(MemObj, false /* fFreeMappings (NA) */);
|
---|
990 | if (RT_FAILURE(rc))
|
---|
991 | {
|
---|
992 | SUPR0Printf("gmmR0CleanupVMScanChunk: %p/%#x: mapping #%x: RTRMemObjFree(%p,false) -> %d \n",
|
---|
993 | pChunk, pChunk->Core.Key, i, MemObj, rc);
|
---|
994 | AssertRC(rc);
|
---|
995 | }
|
---|
996 | break;
|
---|
997 | }
|
---|
998 |
|
---|
999 | /*
|
---|
1000 | * If not in bound memory mode, we should reset the hGVM field
|
---|
1001 | * if it has our handle in it.
|
---|
1002 | */
|
---|
1003 | if (pChunk->hGVM == pGVM->hSelf)
|
---|
1004 | {
|
---|
1005 | if (!g_pGMM->fBoundMemoryMode)
|
---|
1006 | pChunk->hGVM = NIL_GVM_HANDLE;
|
---|
1007 | else if (pChunk->cFree != GMM_CHUNK_NUM_PAGES)
|
---|
1008 | {
|
---|
1009 | SUPR0Printf("gmmR0CleanupVMScanChunk: %p/%#x: cFree=%#x - it should be 0 in bound mode!\n",
|
---|
1010 | pChunk, pChunk->Core.Key, pChunk->cFree);
|
---|
1011 | AssertMsgFailed(("%p/%#x: cFree=%#x - it should be 0 in bound mode!\n", pChunk, pChunk->Core.Key, pChunk->cFree));
|
---|
1012 |
|
---|
1013 | gmmR0UnlinkChunk(pChunk);
|
---|
1014 | pChunk->cFree = GMM_CHUNK_NUM_PAGES;
|
---|
1015 | gmmR0LinkChunk(pChunk, pChunk->cShared ? &g_pGMM->Shared : &g_pGMM->Private);
|
---|
1016 | }
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | return 0;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 |
|
---|
1023 | /**
|
---|
1024 | * RTAvlU32Destroy callback for GMMR0CleanupVM.
|
---|
1025 | *
|
---|
1026 | * @returns 0
|
---|
1027 | * @param pNode The node (allocation chunk) to destroy.
|
---|
1028 | * @param pvGVM Pointer to the shared VM structure.
|
---|
1029 | */
|
---|
1030 | /*static*/ DECLCALLBACK(int) gmmR0CleanupVMDestroyChunk(PAVLU32NODECORE pNode, void *pvGVM)
|
---|
1031 | {
|
---|
1032 | PGMMCHUNK pChunk = (PGMMCHUNK)pNode;
|
---|
1033 | PGVM pGVM = (PGVM)pvGVM;
|
---|
1034 |
|
---|
1035 | for (unsigned i = 0; i < pChunk->cMappings; i++)
|
---|
1036 | {
|
---|
1037 | if (pChunk->paMappings[i].pGVM != pGVM)
|
---|
1038 | SUPR0Printf("gmmR0CleanupVMDestroyChunk: %p/%#x: mapping #%x: pGVM=%p exepcted %p\n", pChunk,
|
---|
1039 | pChunk->Core.Key, i, pChunk->paMappings[i].pGVM, pGVM);
|
---|
1040 | int rc = RTR0MemObjFree(pChunk->paMappings[i].MapObj, false /* fFreeMappings (NA) */);
|
---|
1041 | if (RT_FAILURE(rc))
|
---|
1042 | {
|
---|
1043 | SUPR0Printf("gmmR0CleanupVMDestroyChunk: %p/%#x: mapping #%x: RTRMemObjFree(%p,false) -> %d \n", pChunk,
|
---|
1044 | pChunk->Core.Key, i, pChunk->paMappings[i].MapObj, rc);
|
---|
1045 | AssertRC(rc);
|
---|
1046 | }
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | int rc = RTR0MemObjFree(pChunk->MemObj, true /* fFreeMappings */);
|
---|
1050 | if (RT_FAILURE(rc))
|
---|
1051 | {
|
---|
1052 | SUPR0Printf("gmmR0CleanupVMDestroyChunk: %p/%#x: RTRMemObjFree(%p,true) -> %d (cMappings=%d)\n", pChunk,
|
---|
1053 | pChunk->Core.Key, pChunk->MemObj, rc, pChunk->cMappings);
|
---|
1054 | AssertRC(rc);
|
---|
1055 | }
|
---|
1056 | pChunk->MemObj = NIL_RTR0MEMOBJ;
|
---|
1057 |
|
---|
1058 | RTMemFree(pChunk->paMappings);
|
---|
1059 | pChunk->paMappings = NULL;
|
---|
1060 |
|
---|
1061 | RTMemFree(pChunk);
|
---|
1062 | return 0;
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 |
|
---|
1066 | /**
|
---|
1067 | * The initial resource reservations.
|
---|
1068 | *
|
---|
1069 | * This will make memory reservations according to policy and priority. If there isn't
|
---|
1070 | * sufficient resources available to sustain the VM this function will fail and all
|
---|
1071 | * future allocations requests will fail as well.
|
---|
1072 | *
|
---|
1073 | * These are just the initial reservations made very very early during the VM creation
|
---|
1074 | * process and will be adjusted later in the GMMR0UpdateReservation call after the
|
---|
1075 | * ring-3 init has completed.
|
---|
1076 | *
|
---|
1077 | * @returns VBox status code.
|
---|
1078 | * @retval VERR_GMM_MEMORY_RESERVATION_DECLINED
|
---|
1079 | * @retval VERR_GMM_
|
---|
1080 | *
|
---|
1081 | * @param pVM Pointer to the shared VM structure.
|
---|
1082 | * @param idCpu VCPU id
|
---|
1083 | * @param cBasePages The number of pages that may be allocated for the base RAM and ROMs.
|
---|
1084 | * This does not include MMIO2 and similar.
|
---|
1085 | * @param cShadowPages The number of pages that may be allocated for shadow pageing structures.
|
---|
1086 | * @param cFixedPages The number of pages that may be allocated for fixed objects like the
|
---|
1087 | * hyper heap, MMIO2 and similar.
|
---|
1088 | * @param enmPolicy The OC policy to use on this VM.
|
---|
1089 | * @param enmPriority The priority in an out-of-memory situation.
|
---|
1090 | *
|
---|
1091 | * @thread The creator thread / EMT.
|
---|
1092 | */
|
---|
1093 | GMMR0DECL(int) GMMR0InitialReservation(PVM pVM, VMCPUID idCpu, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages,
|
---|
1094 | GMMOCPOLICY enmPolicy, GMMPRIORITY enmPriority)
|
---|
1095 | {
|
---|
1096 | LogFlow(("GMMR0InitialReservation: pVM=%p cBasePages=%#llx cShadowPages=%#x cFixedPages=%#x enmPolicy=%d enmPriority=%d\n",
|
---|
1097 | pVM, cBasePages, cShadowPages, cFixedPages, enmPolicy, enmPriority));
|
---|
1098 |
|
---|
1099 | /*
|
---|
1100 | * Validate, get basics and take the semaphore.
|
---|
1101 | */
|
---|
1102 | PGMM pGMM;
|
---|
1103 | GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
|
---|
1104 | PGVM pGVM;
|
---|
1105 | int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
|
---|
1106 | if (RT_FAILURE(rc))
|
---|
1107 | return rc;
|
---|
1108 |
|
---|
1109 | AssertReturn(cBasePages, VERR_INVALID_PARAMETER);
|
---|
1110 | AssertReturn(cShadowPages, VERR_INVALID_PARAMETER);
|
---|
1111 | AssertReturn(cFixedPages, VERR_INVALID_PARAMETER);
|
---|
1112 | AssertReturn(enmPolicy > GMMOCPOLICY_INVALID && enmPolicy < GMMOCPOLICY_END, VERR_INVALID_PARAMETER);
|
---|
1113 | AssertReturn(enmPriority > GMMPRIORITY_INVALID && enmPriority < GMMPRIORITY_END, VERR_INVALID_PARAMETER);
|
---|
1114 |
|
---|
1115 | rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
1116 | AssertRC(rc);
|
---|
1117 | if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
1118 | {
|
---|
1119 | if ( !pGVM->gmm.s.Reserved.cBasePages
|
---|
1120 | && !pGVM->gmm.s.Reserved.cFixedPages
|
---|
1121 | && !pGVM->gmm.s.Reserved.cShadowPages)
|
---|
1122 | {
|
---|
1123 | /*
|
---|
1124 | * Check if we can accomodate this.
|
---|
1125 | */
|
---|
1126 | /* ... later ... */
|
---|
1127 | if (RT_SUCCESS(rc))
|
---|
1128 | {
|
---|
1129 | /*
|
---|
1130 | * Update the records.
|
---|
1131 | */
|
---|
1132 | pGVM->gmm.s.Reserved.cBasePages = cBasePages;
|
---|
1133 | pGVM->gmm.s.Reserved.cFixedPages = cFixedPages;
|
---|
1134 | pGVM->gmm.s.Reserved.cShadowPages = cShadowPages;
|
---|
1135 | pGVM->gmm.s.enmPolicy = enmPolicy;
|
---|
1136 | pGVM->gmm.s.enmPriority = enmPriority;
|
---|
1137 | pGVM->gmm.s.fMayAllocate = true;
|
---|
1138 |
|
---|
1139 | pGMM->cReservedPages += cBasePages + cFixedPages + cShadowPages;
|
---|
1140 | pGMM->cRegisteredVMs++;
|
---|
1141 | }
|
---|
1142 | }
|
---|
1143 | else
|
---|
1144 | rc = VERR_WRONG_ORDER;
|
---|
1145 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
1146 | }
|
---|
1147 | else
|
---|
1148 | rc = VERR_INTERNAL_ERROR_5;
|
---|
1149 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
1150 | LogFlow(("GMMR0InitialReservation: returns %Rrc\n", rc));
|
---|
1151 | return rc;
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 |
|
---|
1155 | /**
|
---|
1156 | * VMMR0 request wrapper for GMMR0InitialReservation.
|
---|
1157 | *
|
---|
1158 | * @returns see GMMR0InitialReservation.
|
---|
1159 | * @param pVM Pointer to the shared VM structure.
|
---|
1160 | * @param idCpu VCPU id
|
---|
1161 | * @param pReq The request packet.
|
---|
1162 | */
|
---|
1163 | GMMR0DECL(int) GMMR0InitialReservationReq(PVM pVM, VMCPUID idCpu, PGMMINITIALRESERVATIONREQ pReq)
|
---|
1164 | {
|
---|
1165 | /*
|
---|
1166 | * Validate input and pass it on.
|
---|
1167 | */
|
---|
1168 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
1169 | AssertPtrReturn(pReq, VERR_INVALID_POINTER);
|
---|
1170 | AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
|
---|
1171 |
|
---|
1172 | return GMMR0InitialReservation(pVM, idCpu, pReq->cBasePages, pReq->cShadowPages, pReq->cFixedPages, pReq->enmPolicy, pReq->enmPriority);
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 |
|
---|
1176 | /**
|
---|
1177 | * This updates the memory reservation with the additional MMIO2 and ROM pages.
|
---|
1178 | *
|
---|
1179 | * @returns VBox status code.
|
---|
1180 | * @retval VERR_GMM_MEMORY_RESERVATION_DECLINED
|
---|
1181 | *
|
---|
1182 | * @param pVM Pointer to the shared VM structure.
|
---|
1183 | * @param idCpu VCPU id
|
---|
1184 | * @param cBasePages The number of pages that may be allocated for the base RAM and ROMs.
|
---|
1185 | * This does not include MMIO2 and similar.
|
---|
1186 | * @param cShadowPages The number of pages that may be allocated for shadow pageing structures.
|
---|
1187 | * @param cFixedPages The number of pages that may be allocated for fixed objects like the
|
---|
1188 | * hyper heap, MMIO2 and similar.
|
---|
1189 | *
|
---|
1190 | * @thread EMT.
|
---|
1191 | */
|
---|
1192 | GMMR0DECL(int) GMMR0UpdateReservation(PVM pVM, VMCPUID idCpu, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages)
|
---|
1193 | {
|
---|
1194 | LogFlow(("GMMR0UpdateReservation: pVM=%p cBasePages=%#llx cShadowPages=%#x cFixedPages=%#x\n",
|
---|
1195 | pVM, cBasePages, cShadowPages, cFixedPages));
|
---|
1196 |
|
---|
1197 | /*
|
---|
1198 | * Validate, get basics and take the semaphore.
|
---|
1199 | */
|
---|
1200 | PGMM pGMM;
|
---|
1201 | GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
|
---|
1202 | PGVM pGVM;
|
---|
1203 | int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
|
---|
1204 | if (RT_FAILURE(rc))
|
---|
1205 | return rc;
|
---|
1206 |
|
---|
1207 | AssertReturn(cBasePages, VERR_INVALID_PARAMETER);
|
---|
1208 | AssertReturn(cShadowPages, VERR_INVALID_PARAMETER);
|
---|
1209 | AssertReturn(cFixedPages, VERR_INVALID_PARAMETER);
|
---|
1210 |
|
---|
1211 | rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
1212 | AssertRC(rc);
|
---|
1213 | if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
1214 | {
|
---|
1215 | if ( pGVM->gmm.s.Reserved.cBasePages
|
---|
1216 | && pGVM->gmm.s.Reserved.cFixedPages
|
---|
1217 | && pGVM->gmm.s.Reserved.cShadowPages)
|
---|
1218 | {
|
---|
1219 | /*
|
---|
1220 | * Check if we can accomodate this.
|
---|
1221 | */
|
---|
1222 | /* ... later ... */
|
---|
1223 | if (RT_SUCCESS(rc))
|
---|
1224 | {
|
---|
1225 | /*
|
---|
1226 | * Update the records.
|
---|
1227 | */
|
---|
1228 | pGMM->cReservedPages -= pGVM->gmm.s.Reserved.cBasePages
|
---|
1229 | + pGVM->gmm.s.Reserved.cFixedPages
|
---|
1230 | + pGVM->gmm.s.Reserved.cShadowPages;
|
---|
1231 | pGMM->cReservedPages += cBasePages + cFixedPages + cShadowPages;
|
---|
1232 |
|
---|
1233 | pGVM->gmm.s.Reserved.cBasePages = cBasePages;
|
---|
1234 | pGVM->gmm.s.Reserved.cFixedPages = cFixedPages;
|
---|
1235 | pGVM->gmm.s.Reserved.cShadowPages = cShadowPages;
|
---|
1236 | }
|
---|
1237 | }
|
---|
1238 | else
|
---|
1239 | rc = VERR_WRONG_ORDER;
|
---|
1240 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
1241 | }
|
---|
1242 | else
|
---|
1243 | rc = VERR_INTERNAL_ERROR_5;
|
---|
1244 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
1245 | LogFlow(("GMMR0UpdateReservation: returns %Rrc\n", rc));
|
---|
1246 | return rc;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 |
|
---|
1250 | /**
|
---|
1251 | * VMMR0 request wrapper for GMMR0UpdateReservation.
|
---|
1252 | *
|
---|
1253 | * @returns see GMMR0UpdateReservation.
|
---|
1254 | * @param pVM Pointer to the shared VM structure.
|
---|
1255 | * @param idCpu VCPU id
|
---|
1256 | * @param pReq The request packet.
|
---|
1257 | */
|
---|
1258 | GMMR0DECL(int) GMMR0UpdateReservationReq(PVM pVM, VMCPUID idCpu, PGMMUPDATERESERVATIONREQ pReq)
|
---|
1259 | {
|
---|
1260 | /*
|
---|
1261 | * Validate input and pass it on.
|
---|
1262 | */
|
---|
1263 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
1264 | AssertPtrReturn(pReq, VERR_INVALID_POINTER);
|
---|
1265 | AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
|
---|
1266 |
|
---|
1267 | return GMMR0UpdateReservation(pVM, idCpu, pReq->cBasePages, pReq->cShadowPages, pReq->cFixedPages);
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 |
|
---|
1271 | /**
|
---|
1272 | * Performs sanity checks on a free set.
|
---|
1273 | *
|
---|
1274 | * @returns Error count.
|
---|
1275 | *
|
---|
1276 | * @param pGMM Pointer to the GMM instance.
|
---|
1277 | * @param pSet Pointer to the set.
|
---|
1278 | * @param pszSetName The set name.
|
---|
1279 | * @param pszFunction The function from which it was called.
|
---|
1280 | * @param uLine The line number.
|
---|
1281 | */
|
---|
1282 | static uint32_t gmmR0SanityCheckSet(PGMM pGMM, PGMMCHUNKFREESET pSet, const char *pszSetName,
|
---|
1283 | const char *pszFunction, unsigned uLineNo)
|
---|
1284 | {
|
---|
1285 | uint32_t cErrors = 0;
|
---|
1286 |
|
---|
1287 | /*
|
---|
1288 | * Count the free pages in all the chunks and match it against pSet->cFreePages.
|
---|
1289 | */
|
---|
1290 | uint32_t cPages = 0;
|
---|
1291 | for (unsigned i = 0; i < RT_ELEMENTS(pSet->apLists); i++)
|
---|
1292 | {
|
---|
1293 | for (PGMMCHUNK pCur = pSet->apLists[i]; pCur; pCur = pCur->pFreeNext)
|
---|
1294 | {
|
---|
1295 | /** @todo check that the chunk is hash into the right set. */
|
---|
1296 | cPages += pCur->cFree;
|
---|
1297 | }
|
---|
1298 | }
|
---|
1299 | if (RT_UNLIKELY(cPages != pSet->cFreePages))
|
---|
1300 | {
|
---|
1301 | SUPR0Printf("GMM insanity: found %#x pages in the %s set, expected %#x. (%s, line %u)\n",
|
---|
1302 | cPages, pszSetName, pSet->cFreePages, pszFunction, uLineNo);
|
---|
1303 | cErrors++;
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | return cErrors;
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 |
|
---|
1310 | /**
|
---|
1311 | * Performs some sanity checks on the GMM while owning lock.
|
---|
1312 | *
|
---|
1313 | * @returns Error count.
|
---|
1314 | *
|
---|
1315 | * @param pGMM Pointer to the GMM instance.
|
---|
1316 | * @param pszFunction The function from which it is called.
|
---|
1317 | * @param uLineNo The line number.
|
---|
1318 | */
|
---|
1319 | static uint32_t gmmR0SanityCheck(PGMM pGMM, const char *pszFunction, unsigned uLineNo)
|
---|
1320 | {
|
---|
1321 | uint32_t cErrors = 0;
|
---|
1322 |
|
---|
1323 | cErrors += gmmR0SanityCheckSet(pGMM, &pGMM->Private, "private", pszFunction, uLineNo);
|
---|
1324 | cErrors += gmmR0SanityCheckSet(pGMM, &pGMM->Shared, "shared", pszFunction, uLineNo);
|
---|
1325 | /** @todo add more sanity checks. */
|
---|
1326 |
|
---|
1327 | return cErrors;
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 |
|
---|
1331 | /**
|
---|
1332 | * Looks up a chunk in the tree and fill in the TLB entry for it.
|
---|
1333 | *
|
---|
1334 | * This is not expected to fail and will bitch if it does.
|
---|
1335 | *
|
---|
1336 | * @returns Pointer to the allocation chunk, NULL if not found.
|
---|
1337 | * @param pGMM Pointer to the GMM instance.
|
---|
1338 | * @param idChunk The ID of the chunk to find.
|
---|
1339 | * @param pTlbe Pointer to the TLB entry.
|
---|
1340 | */
|
---|
1341 | static PGMMCHUNK gmmR0GetChunkSlow(PGMM pGMM, uint32_t idChunk, PGMMCHUNKTLBE pTlbe)
|
---|
1342 | {
|
---|
1343 | PGMMCHUNK pChunk = (PGMMCHUNK)RTAvlU32Get(&pGMM->pChunks, idChunk);
|
---|
1344 | AssertMsgReturn(pChunk, ("Chunk %#x not found!\n", idChunk), NULL);
|
---|
1345 | pTlbe->idChunk = idChunk;
|
---|
1346 | pTlbe->pChunk = pChunk;
|
---|
1347 | return pChunk;
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 |
|
---|
1351 | /**
|
---|
1352 | * Finds a allocation chunk.
|
---|
1353 | *
|
---|
1354 | * This is not expected to fail and will bitch if it does.
|
---|
1355 | *
|
---|
1356 | * @returns Pointer to the allocation chunk, NULL if not found.
|
---|
1357 | * @param pGMM Pointer to the GMM instance.
|
---|
1358 | * @param idChunk The ID of the chunk to find.
|
---|
1359 | */
|
---|
1360 | DECLINLINE(PGMMCHUNK) gmmR0GetChunk(PGMM pGMM, uint32_t idChunk)
|
---|
1361 | {
|
---|
1362 | /*
|
---|
1363 | * Do a TLB lookup, branch if not in the TLB.
|
---|
1364 | */
|
---|
1365 | PGMMCHUNKTLBE pTlbe = &pGMM->ChunkTLB.aEntries[GMM_CHUNKTLB_IDX(idChunk)];
|
---|
1366 | if ( pTlbe->idChunk != idChunk
|
---|
1367 | || !pTlbe->pChunk)
|
---|
1368 | return gmmR0GetChunkSlow(pGMM, idChunk, pTlbe);
|
---|
1369 | return pTlbe->pChunk;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 |
|
---|
1373 | /**
|
---|
1374 | * Finds a page.
|
---|
1375 | *
|
---|
1376 | * This is not expected to fail and will bitch if it does.
|
---|
1377 | *
|
---|
1378 | * @returns Pointer to the page, NULL if not found.
|
---|
1379 | * @param pGMM Pointer to the GMM instance.
|
---|
1380 | * @param idPage The ID of the page to find.
|
---|
1381 | */
|
---|
1382 | DECLINLINE(PGMMPAGE) gmmR0GetPage(PGMM pGMM, uint32_t idPage)
|
---|
1383 | {
|
---|
1384 | PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
|
---|
1385 | if (RT_LIKELY(pChunk))
|
---|
1386 | return &pChunk->aPages[idPage & GMM_PAGEID_IDX_MASK];
|
---|
1387 | return NULL;
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 |
|
---|
1391 | /**
|
---|
1392 | * Unlinks the chunk from the free list it's currently on (if any).
|
---|
1393 | *
|
---|
1394 | * @param pChunk The allocation chunk.
|
---|
1395 | */
|
---|
1396 | DECLINLINE(void) gmmR0UnlinkChunk(PGMMCHUNK pChunk)
|
---|
1397 | {
|
---|
1398 | PGMMCHUNKFREESET pSet = pChunk->pSet;
|
---|
1399 | if (RT_LIKELY(pSet))
|
---|
1400 | {
|
---|
1401 | pSet->cFreePages -= pChunk->cFree;
|
---|
1402 |
|
---|
1403 | PGMMCHUNK pPrev = pChunk->pFreePrev;
|
---|
1404 | PGMMCHUNK pNext = pChunk->pFreeNext;
|
---|
1405 | if (pPrev)
|
---|
1406 | pPrev->pFreeNext = pNext;
|
---|
1407 | else
|
---|
1408 | pSet->apLists[(pChunk->cFree - 1) >> GMM_CHUNK_FREE_SET_SHIFT] = pNext;
|
---|
1409 | if (pNext)
|
---|
1410 | pNext->pFreePrev = pPrev;
|
---|
1411 |
|
---|
1412 | pChunk->pSet = NULL;
|
---|
1413 | pChunk->pFreeNext = NULL;
|
---|
1414 | pChunk->pFreePrev = NULL;
|
---|
1415 | }
|
---|
1416 | else
|
---|
1417 | {
|
---|
1418 | Assert(!pChunk->pFreeNext);
|
---|
1419 | Assert(!pChunk->pFreePrev);
|
---|
1420 | Assert(!pChunk->cFree);
|
---|
1421 | }
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 |
|
---|
1425 | /**
|
---|
1426 | * Links the chunk onto the appropriate free list in the specified free set.
|
---|
1427 | *
|
---|
1428 | * If no free entries, it's not linked into any list.
|
---|
1429 | *
|
---|
1430 | * @param pChunk The allocation chunk.
|
---|
1431 | * @param pSet The free set.
|
---|
1432 | */
|
---|
1433 | DECLINLINE(void) gmmR0LinkChunk(PGMMCHUNK pChunk, PGMMCHUNKFREESET pSet)
|
---|
1434 | {
|
---|
1435 | Assert(!pChunk->pSet);
|
---|
1436 | Assert(!pChunk->pFreeNext);
|
---|
1437 | Assert(!pChunk->pFreePrev);
|
---|
1438 |
|
---|
1439 | if (pChunk->cFree > 0)
|
---|
1440 | {
|
---|
1441 | pChunk->pSet = pSet;
|
---|
1442 | pChunk->pFreePrev = NULL;
|
---|
1443 | unsigned iList = (pChunk->cFree - 1) >> GMM_CHUNK_FREE_SET_SHIFT;
|
---|
1444 | pChunk->pFreeNext = pSet->apLists[iList];
|
---|
1445 | if (pChunk->pFreeNext)
|
---|
1446 | pChunk->pFreeNext->pFreePrev = pChunk;
|
---|
1447 | pSet->apLists[iList] = pChunk;
|
---|
1448 |
|
---|
1449 | pSet->cFreePages += pChunk->cFree;
|
---|
1450 | }
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 |
|
---|
1454 | /**
|
---|
1455 | * Frees a Chunk ID.
|
---|
1456 | *
|
---|
1457 | * @param pGMM Pointer to the GMM instance.
|
---|
1458 | * @param idChunk The Chunk ID to free.
|
---|
1459 | */
|
---|
1460 | static void gmmR0FreeChunkId(PGMM pGMM, uint32_t idChunk)
|
---|
1461 | {
|
---|
1462 | AssertReturnVoid(idChunk != NIL_GMM_CHUNKID);
|
---|
1463 | AssertMsg(ASMBitTest(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk));
|
---|
1464 | ASMAtomicBitClear(&pGMM->bmChunkId[0], idChunk);
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 |
|
---|
1468 | /**
|
---|
1469 | * Allocates a new Chunk ID.
|
---|
1470 | *
|
---|
1471 | * @returns The Chunk ID.
|
---|
1472 | * @param pGMM Pointer to the GMM instance.
|
---|
1473 | */
|
---|
1474 | static uint32_t gmmR0AllocateChunkId(PGMM pGMM)
|
---|
1475 | {
|
---|
1476 | AssertCompile(!((GMM_CHUNKID_LAST + 1) & 31)); /* must be a multiple of 32 */
|
---|
1477 | AssertCompile(NIL_GMM_CHUNKID == 0);
|
---|
1478 |
|
---|
1479 | /*
|
---|
1480 | * Try the next sequential one.
|
---|
1481 | */
|
---|
1482 | int32_t idChunk = ++pGMM->idChunkPrev;
|
---|
1483 | #if 0 /* test the fallback first */
|
---|
1484 | if ( idChunk <= GMM_CHUNKID_LAST
|
---|
1485 | && idChunk > NIL_GMM_CHUNKID
|
---|
1486 | && !ASMAtomicBitTestAndSet(&pVMM->bmChunkId[0], idChunk))
|
---|
1487 | return idChunk;
|
---|
1488 | #endif
|
---|
1489 |
|
---|
1490 | /*
|
---|
1491 | * Scan sequentially from the last one.
|
---|
1492 | */
|
---|
1493 | if ( (uint32_t)idChunk < GMM_CHUNKID_LAST
|
---|
1494 | && idChunk > NIL_GMM_CHUNKID)
|
---|
1495 | {
|
---|
1496 | idChunk = ASMBitNextClear(&pGMM->bmChunkId[0], GMM_CHUNKID_LAST + 1, idChunk);
|
---|
1497 | if (idChunk > NIL_GMM_CHUNKID)
|
---|
1498 | {
|
---|
1499 | AssertMsgReturn(!ASMAtomicBitTestAndSet(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk), NIL_GMM_CHUNKID);
|
---|
1500 | return pGMM->idChunkPrev = idChunk;
|
---|
1501 | }
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 | /*
|
---|
1505 | * Ok, scan from the start.
|
---|
1506 | * We're not racing anyone, so there is no need to expect failures or have restart loops.
|
---|
1507 | */
|
---|
1508 | idChunk = ASMBitFirstClear(&pGMM->bmChunkId[0], GMM_CHUNKID_LAST + 1);
|
---|
1509 | AssertMsgReturn(idChunk > NIL_GMM_CHUNKID, ("%#x\n", idChunk), NIL_GVM_HANDLE);
|
---|
1510 | AssertMsgReturn(!ASMAtomicBitTestAndSet(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk), NIL_GMM_CHUNKID);
|
---|
1511 |
|
---|
1512 | return pGMM->idChunkPrev = idChunk;
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 |
|
---|
1516 | /**
|
---|
1517 | * Registers a new chunk of memory.
|
---|
1518 | *
|
---|
1519 | * This is called by both gmmR0AllocateOneChunk and GMMR0SeedChunk. Will take
|
---|
1520 | * the mutex, the caller must not own it.
|
---|
1521 | *
|
---|
1522 | * @returns VBox status code.
|
---|
1523 | * @param pGMM Pointer to the GMM instance.
|
---|
1524 | * @param pSet Pointer to the set.
|
---|
1525 | * @param MemObj The memory object for the chunk.
|
---|
1526 | * @param hGVM The affinity of the chunk. NIL_GVM_HANDLE for no
|
---|
1527 | * affinity.
|
---|
1528 | */
|
---|
1529 | static int gmmR0RegisterChunk(PGMM pGMM, PGMMCHUNKFREESET pSet, RTR0MEMOBJ MemObj, uint16_t hGVM)
|
---|
1530 | {
|
---|
1531 | Assert(hGVM != NIL_GVM_HANDLE || pGMM->fBoundMemoryMode);
|
---|
1532 |
|
---|
1533 | int rc;
|
---|
1534 | PGMMCHUNK pChunk = (PGMMCHUNK)RTMemAllocZ(sizeof(*pChunk));
|
---|
1535 | if (pChunk)
|
---|
1536 | {
|
---|
1537 | /*
|
---|
1538 | * Initialize it.
|
---|
1539 | */
|
---|
1540 | pChunk->MemObj = MemObj;
|
---|
1541 | pChunk->cFree = GMM_CHUNK_NUM_PAGES;
|
---|
1542 | pChunk->hGVM = hGVM;
|
---|
1543 | pChunk->iFreeHead = 0;
|
---|
1544 | for (unsigned iPage = 0; iPage < RT_ELEMENTS(pChunk->aPages) - 1; iPage++)
|
---|
1545 | {
|
---|
1546 | pChunk->aPages[iPage].Free.u2State = GMM_PAGE_STATE_FREE;
|
---|
1547 | pChunk->aPages[iPage].Free.iNext = iPage + 1;
|
---|
1548 | }
|
---|
1549 | pChunk->aPages[RT_ELEMENTS(pChunk->aPages) - 1].Free.u2State = GMM_PAGE_STATE_FREE;
|
---|
1550 | pChunk->aPages[RT_ELEMENTS(pChunk->aPages) - 1].Free.iNext = UINT16_MAX;
|
---|
1551 |
|
---|
1552 | /*
|
---|
1553 | * Allocate a Chunk ID and insert it into the tree.
|
---|
1554 | * This has to be done behind the mutex of course.
|
---|
1555 | */
|
---|
1556 | rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
1557 | if (RT_SUCCESS(rc))
|
---|
1558 | {
|
---|
1559 | if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
1560 | {
|
---|
1561 | pChunk->Core.Key = gmmR0AllocateChunkId(pGMM);
|
---|
1562 | if ( pChunk->Core.Key != NIL_GMM_CHUNKID
|
---|
1563 | && pChunk->Core.Key <= GMM_CHUNKID_LAST
|
---|
1564 | && RTAvlU32Insert(&pGMM->pChunks, &pChunk->Core))
|
---|
1565 | {
|
---|
1566 | pGMM->cChunks++;
|
---|
1567 | gmmR0LinkChunk(pChunk, pSet);
|
---|
1568 | LogFlow(("gmmR0RegisterChunk: pChunk=%p id=%#x cChunks=%d\n", pChunk, pChunk->Core.Key, pGMM->cChunks));
|
---|
1569 |
|
---|
1570 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
1571 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
1572 | return VINF_SUCCESS;
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 | /* bail out */
|
---|
1576 | rc = VERR_INTERNAL_ERROR;
|
---|
1577 | }
|
---|
1578 | else
|
---|
1579 | rc = VERR_INTERNAL_ERROR_5;
|
---|
1580 |
|
---|
1581 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
1582 | }
|
---|
1583 | RTMemFree(pChunk);
|
---|
1584 | }
|
---|
1585 | else
|
---|
1586 | rc = VERR_NO_MEMORY;
|
---|
1587 | return rc;
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 |
|
---|
1591 | /**
|
---|
1592 | * Allocate one new chunk and add it to the specified free set.
|
---|
1593 | *
|
---|
1594 | * @returns VBox status code.
|
---|
1595 | * @param pGMM Pointer to the GMM instance.
|
---|
1596 | * @param pSet Pointer to the set.
|
---|
1597 | * @param hGVM The affinity of the new chunk.
|
---|
1598 | *
|
---|
1599 | * @remarks Called without owning the mutex.
|
---|
1600 | */
|
---|
1601 | static int gmmR0AllocateOneChunk(PGMM pGMM, PGMMCHUNKFREESET pSet, uint16_t hGVM)
|
---|
1602 | {
|
---|
1603 | /*
|
---|
1604 | * Allocate the memory.
|
---|
1605 | */
|
---|
1606 | RTR0MEMOBJ MemObj;
|
---|
1607 | int rc = RTR0MemObjAllocPhysNC(&MemObj, GMM_CHUNK_SIZE, NIL_RTHCPHYS);
|
---|
1608 | if (RT_SUCCESS(rc))
|
---|
1609 | {
|
---|
1610 | rc = gmmR0RegisterChunk(pGMM, pSet, MemObj, hGVM);
|
---|
1611 | if (RT_FAILURE(rc))
|
---|
1612 | RTR0MemObjFree(MemObj, false /* fFreeMappings */);
|
---|
1613 | }
|
---|
1614 | /** @todo Check that RTR0MemObjAllocPhysNC always returns VERR_NO_MEMORY on
|
---|
1615 | * allocation failure. */
|
---|
1616 | return rc;
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 |
|
---|
1620 | /**
|
---|
1621 | * Attempts to allocate more pages until the requested amount is met.
|
---|
1622 | *
|
---|
1623 | * @returns VBox status code.
|
---|
1624 | * @param pGMM Pointer to the GMM instance data.
|
---|
1625 | * @param pGVM The calling VM.
|
---|
1626 | * @param pSet Pointer to the free set to grow.
|
---|
1627 | * @param cPages The number of pages needed.
|
---|
1628 | *
|
---|
1629 | * @remarks Called owning the mutex, but will leave it temporarily while
|
---|
1630 | * allocating the memory!
|
---|
1631 | */
|
---|
1632 | static int gmmR0AllocateMoreChunks(PGMM pGMM, PGVM pGVM, PGMMCHUNKFREESET pSet, uint32_t cPages)
|
---|
1633 | {
|
---|
1634 | Assert(!pGMM->fLegacyAllocationMode);
|
---|
1635 |
|
---|
1636 | if (!GMM_CHECK_SANITY_IN_LOOPS(pGMM))
|
---|
1637 | return VERR_INTERNAL_ERROR_4;
|
---|
1638 |
|
---|
1639 | if (!pGMM->fBoundMemoryMode)
|
---|
1640 | {
|
---|
1641 | /*
|
---|
1642 | * Try steal free chunks from the other set first. (Only take 100% free chunks.)
|
---|
1643 | */
|
---|
1644 | PGMMCHUNKFREESET pOtherSet = pSet == &pGMM->Private ? &pGMM->Shared : &pGMM->Private;
|
---|
1645 | while ( pSet->cFreePages < cPages
|
---|
1646 | && pOtherSet->cFreePages >= GMM_CHUNK_NUM_PAGES)
|
---|
1647 | {
|
---|
1648 | PGMMCHUNK pChunk = pOtherSet->apLists[GMM_CHUNK_FREE_SET_LISTS - 1];
|
---|
1649 | while (pChunk && pChunk->cFree != GMM_CHUNK_NUM_PAGES)
|
---|
1650 | pChunk = pChunk->pFreeNext;
|
---|
1651 | if (!pChunk)
|
---|
1652 | break;
|
---|
1653 |
|
---|
1654 | gmmR0UnlinkChunk(pChunk);
|
---|
1655 | gmmR0LinkChunk(pChunk, pSet);
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | /*
|
---|
1659 | * If we need still more pages, allocate new chunks.
|
---|
1660 | * Note! We will leave the mutex while doing the allocation,
|
---|
1661 | * gmmR0AllocateOneChunk will re-take it temporarily while registering the chunk.
|
---|
1662 | */
|
---|
1663 | while (pSet->cFreePages < cPages)
|
---|
1664 | {
|
---|
1665 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
1666 | int rc = gmmR0AllocateOneChunk(pGMM, pSet, NIL_GVM_HANDLE);
|
---|
1667 | int rc2 = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
1668 | AssertRCReturn(rc2, rc2);
|
---|
1669 | if (RT_FAILURE(rc))
|
---|
1670 | return rc;
|
---|
1671 | if (!GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
1672 | return VERR_INTERNAL_ERROR_5;
|
---|
1673 | }
|
---|
1674 | }
|
---|
1675 | else
|
---|
1676 | {
|
---|
1677 | /*
|
---|
1678 | * The memory is bound to the VM allocating it, so we have to count
|
---|
1679 | * the free pages carefully as well as making sure we brand them with
|
---|
1680 | * our VM handle.
|
---|
1681 | *
|
---|
1682 | * Note! We will leave the mutex while doing the allocation,
|
---|
1683 | * gmmR0AllocateOneChunk will re-take it temporarily while registering the chunk.
|
---|
1684 | */
|
---|
1685 | uint16_t const hGVM = pGVM->hSelf;
|
---|
1686 | for (;;)
|
---|
1687 | {
|
---|
1688 | /* Count and see if we've reached the goal. */
|
---|
1689 | uint32_t cPagesFound = 0;
|
---|
1690 | for (unsigned i = 0; i < RT_ELEMENTS(pSet->apLists); i++)
|
---|
1691 | for (PGMMCHUNK pCur = pSet->apLists[i]; pCur; pCur = pCur->pFreeNext)
|
---|
1692 | if (pCur->hGVM == hGVM)
|
---|
1693 | {
|
---|
1694 | cPagesFound += pCur->cFree;
|
---|
1695 | if (cPagesFound >= cPages)
|
---|
1696 | break;
|
---|
1697 | }
|
---|
1698 | if (cPagesFound >= cPages)
|
---|
1699 | break;
|
---|
1700 |
|
---|
1701 | /* Allocate more. */
|
---|
1702 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
1703 | int rc = gmmR0AllocateOneChunk(pGMM, pSet, hGVM);
|
---|
1704 | int rc2 = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
1705 | AssertRCReturn(rc2, rc2);
|
---|
1706 | if (RT_FAILURE(rc))
|
---|
1707 | return rc;
|
---|
1708 | if (!GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
1709 | return VERR_INTERNAL_ERROR_5;
|
---|
1710 | }
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | return VINF_SUCCESS;
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 |
|
---|
1717 | /**
|
---|
1718 | * Allocates one private page.
|
---|
1719 | *
|
---|
1720 | * Worker for gmmR0AllocatePages.
|
---|
1721 | *
|
---|
1722 | * @param pGMM Pointer to the GMM instance data.
|
---|
1723 | * @param hGVM The GVM handle of the VM requesting memory.
|
---|
1724 | * @param pChunk The chunk to allocate it from.
|
---|
1725 | * @param pPageDesc The page descriptor.
|
---|
1726 | */
|
---|
1727 | static void gmmR0AllocatePage(PGMM pGMM, uint32_t hGVM, PGMMCHUNK pChunk, PGMMPAGEDESC pPageDesc)
|
---|
1728 | {
|
---|
1729 | /* update the chunk stats. */
|
---|
1730 | if (pChunk->hGVM == NIL_GVM_HANDLE)
|
---|
1731 | pChunk->hGVM = hGVM;
|
---|
1732 | Assert(pChunk->cFree);
|
---|
1733 | pChunk->cFree--;
|
---|
1734 | pChunk->cPrivate++;
|
---|
1735 |
|
---|
1736 | /* unlink the first free page. */
|
---|
1737 | const uint32_t iPage = pChunk->iFreeHead;
|
---|
1738 | AssertReleaseMsg(iPage < RT_ELEMENTS(pChunk->aPages), ("%d\n", iPage));
|
---|
1739 | PGMMPAGE pPage = &pChunk->aPages[iPage];
|
---|
1740 | Assert(GMM_PAGE_IS_FREE(pPage));
|
---|
1741 | pChunk->iFreeHead = pPage->Free.iNext;
|
---|
1742 | Log3(("A pPage=%p iPage=%#x/%#x u2State=%d iFreeHead=%#x iNext=%#x\n",
|
---|
1743 | pPage, iPage, (pChunk->Core.Key << GMM_CHUNKID_SHIFT) | iPage,
|
---|
1744 | pPage->Common.u2State, pChunk->iFreeHead, pPage->Free.iNext));
|
---|
1745 |
|
---|
1746 | /* make the page private. */
|
---|
1747 | pPage->u = 0;
|
---|
1748 | AssertCompile(GMM_PAGE_STATE_PRIVATE == 0);
|
---|
1749 | pPage->Private.hGVM = hGVM;
|
---|
1750 | AssertCompile(NIL_RTHCPHYS >= GMM_GCPHYS_LAST);
|
---|
1751 | AssertCompile(GMM_GCPHYS_UNSHAREABLE >= GMM_GCPHYS_LAST);
|
---|
1752 | if (pPageDesc->HCPhysGCPhys <= GMM_GCPHYS_LAST)
|
---|
1753 | pPage->Private.pfn = pPageDesc->HCPhysGCPhys >> PAGE_SHIFT;
|
---|
1754 | else
|
---|
1755 | pPage->Private.pfn = GMM_PAGE_PFN_UNSHAREABLE; /* unshareable / unassigned - same thing. */
|
---|
1756 |
|
---|
1757 | /* update the page descriptor. */
|
---|
1758 | pPageDesc->HCPhysGCPhys = RTR0MemObjGetPagePhysAddr(pChunk->MemObj, iPage);
|
---|
1759 | Assert(pPageDesc->HCPhysGCPhys != NIL_RTHCPHYS);
|
---|
1760 | pPageDesc->idPage = (pChunk->Core.Key << GMM_CHUNKID_SHIFT) | iPage;
|
---|
1761 | pPageDesc->idSharedPage = NIL_GMM_PAGEID;
|
---|
1762 | }
|
---|
1763 |
|
---|
1764 |
|
---|
1765 | /**
|
---|
1766 | * Common worker for GMMR0AllocateHandyPages and GMMR0AllocatePages.
|
---|
1767 | *
|
---|
1768 | * @returns VBox status code:
|
---|
1769 | * @retval VINF_SUCCESS on success.
|
---|
1770 | * @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk or
|
---|
1771 | * gmmR0AllocateMoreChunks is necessary.
|
---|
1772 | * @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
|
---|
1773 | * @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
|
---|
1774 | * that is we're trying to allocate more than we've reserved.
|
---|
1775 | *
|
---|
1776 | * @param pGMM Pointer to the GMM instance data.
|
---|
1777 | * @param pGVM Pointer to the shared VM structure.
|
---|
1778 | * @param cPages The number of pages to allocate.
|
---|
1779 | * @param paPages Pointer to the page descriptors.
|
---|
1780 | * See GMMPAGEDESC for details on what is expected on input.
|
---|
1781 | * @param enmAccount The account to charge.
|
---|
1782 | */
|
---|
1783 | static int gmmR0AllocatePages(PGMM pGMM, PGVM pGVM, uint32_t cPages, PGMMPAGEDESC paPages, GMMACCOUNT enmAccount)
|
---|
1784 | {
|
---|
1785 | /*
|
---|
1786 | * Check allocation limits.
|
---|
1787 | */
|
---|
1788 | if (RT_UNLIKELY(pGMM->cAllocatedPages + cPages > pGMM->cMaxPages))
|
---|
1789 | return VERR_GMM_HIT_GLOBAL_LIMIT;
|
---|
1790 |
|
---|
1791 | switch (enmAccount)
|
---|
1792 | {
|
---|
1793 | case GMMACCOUNT_BASE:
|
---|
1794 | if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cBasePages + cPages > pGVM->gmm.s.Reserved.cBasePages))
|
---|
1795 | {
|
---|
1796 | Log(("gmmR0AllocatePages: Reserved=%#llx Allocated+Requested=%#llx+%#x!\n",
|
---|
1797 | pGVM->gmm.s.Reserved.cBasePages, pGVM->gmm.s.Allocated.cBasePages, cPages));
|
---|
1798 | return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
|
---|
1799 | }
|
---|
1800 | break;
|
---|
1801 | case GMMACCOUNT_SHADOW:
|
---|
1802 | if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cShadowPages + cPages > pGVM->gmm.s.Reserved.cShadowPages))
|
---|
1803 | {
|
---|
1804 | Log(("gmmR0AllocatePages: Reserved=%#llx Allocated+Requested=%#llx+%#x!\n",
|
---|
1805 | pGVM->gmm.s.Reserved.cShadowPages, pGVM->gmm.s.Allocated.cShadowPages, cPages));
|
---|
1806 | return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
|
---|
1807 | }
|
---|
1808 | break;
|
---|
1809 | case GMMACCOUNT_FIXED:
|
---|
1810 | if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cFixedPages + cPages > pGVM->gmm.s.Reserved.cFixedPages))
|
---|
1811 | {
|
---|
1812 | Log(("gmmR0AllocatePages: Reserved=%#llx Allocated+Requested=%#llx+%#x!\n",
|
---|
1813 | pGVM->gmm.s.Reserved.cFixedPages, pGVM->gmm.s.Allocated.cFixedPages, cPages));
|
---|
1814 | return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
|
---|
1815 | }
|
---|
1816 | break;
|
---|
1817 | default:
|
---|
1818 | AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_INTERNAL_ERROR);
|
---|
1819 | }
|
---|
1820 |
|
---|
1821 | /*
|
---|
1822 | * Check if we need to allocate more memory or not. In bound memory mode this
|
---|
1823 | * is a bit extra work but it's easier to do it upfront than bailing out later.
|
---|
1824 | */
|
---|
1825 | PGMMCHUNKFREESET pSet = &pGMM->Private;
|
---|
1826 | if (pSet->cFreePages < cPages)
|
---|
1827 | return VERR_GMM_SEED_ME;
|
---|
1828 | if (pGMM->fBoundMemoryMode)
|
---|
1829 | {
|
---|
1830 | uint16_t hGVM = pGVM->hSelf;
|
---|
1831 | uint32_t cPagesFound = 0;
|
---|
1832 | for (unsigned i = 0; i < RT_ELEMENTS(pSet->apLists); i++)
|
---|
1833 | for (PGMMCHUNK pCur = pSet->apLists[i]; pCur; pCur = pCur->pFreeNext)
|
---|
1834 | if (pCur->hGVM == hGVM)
|
---|
1835 | {
|
---|
1836 | cPagesFound += pCur->cFree;
|
---|
1837 | if (cPagesFound >= cPages)
|
---|
1838 | break;
|
---|
1839 | }
|
---|
1840 | if (cPagesFound < cPages)
|
---|
1841 | return VERR_GMM_SEED_ME;
|
---|
1842 | }
|
---|
1843 |
|
---|
1844 | /*
|
---|
1845 | * Pick the pages.
|
---|
1846 | * Try make some effort keeping VMs sharing private chunks.
|
---|
1847 | */
|
---|
1848 | uint16_t hGVM = pGVM->hSelf;
|
---|
1849 | uint32_t iPage = 0;
|
---|
1850 |
|
---|
1851 | /* first round, pick from chunks with an affinity to the VM. */
|
---|
1852 | for (unsigned i = 0; i < RT_ELEMENTS(pSet->apLists) && iPage < cPages; i++)
|
---|
1853 | {
|
---|
1854 | PGMMCHUNK pCurFree = NULL;
|
---|
1855 | PGMMCHUNK pCur = pSet->apLists[i];
|
---|
1856 | while (pCur && iPage < cPages)
|
---|
1857 | {
|
---|
1858 | PGMMCHUNK pNext = pCur->pFreeNext;
|
---|
1859 |
|
---|
1860 | if ( pCur->hGVM == hGVM
|
---|
1861 | && pCur->cFree < GMM_CHUNK_NUM_PAGES)
|
---|
1862 | {
|
---|
1863 | gmmR0UnlinkChunk(pCur);
|
---|
1864 | for (; pCur->cFree && iPage < cPages; iPage++)
|
---|
1865 | gmmR0AllocatePage(pGMM, hGVM, pCur, &paPages[iPage]);
|
---|
1866 | gmmR0LinkChunk(pCur, pSet);
|
---|
1867 | }
|
---|
1868 |
|
---|
1869 | pCur = pNext;
|
---|
1870 | }
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | if (iPage < cPages)
|
---|
1874 | {
|
---|
1875 | /* second round, pick pages from the 100% empty chunks we just skipped above. */
|
---|
1876 | PGMMCHUNK pCurFree = NULL;
|
---|
1877 | PGMMCHUNK pCur = pSet->apLists[RT_ELEMENTS(pSet->apLists) - 1];
|
---|
1878 | while (pCur && iPage < cPages)
|
---|
1879 | {
|
---|
1880 | PGMMCHUNK pNext = pCur->pFreeNext;
|
---|
1881 |
|
---|
1882 | if ( pCur->cFree == GMM_CHUNK_NUM_PAGES
|
---|
1883 | && ( pCur->hGVM == hGVM
|
---|
1884 | || !pGMM->fBoundMemoryMode))
|
---|
1885 | {
|
---|
1886 | gmmR0UnlinkChunk(pCur);
|
---|
1887 | for (; pCur->cFree && iPage < cPages; iPage++)
|
---|
1888 | gmmR0AllocatePage(pGMM, hGVM, pCur, &paPages[iPage]);
|
---|
1889 | gmmR0LinkChunk(pCur, pSet);
|
---|
1890 | }
|
---|
1891 |
|
---|
1892 | pCur = pNext;
|
---|
1893 | }
|
---|
1894 | }
|
---|
1895 |
|
---|
1896 | if ( iPage < cPages
|
---|
1897 | && !pGMM->fBoundMemoryMode)
|
---|
1898 | {
|
---|
1899 | /* third round, disregard affinity. */
|
---|
1900 | unsigned i = RT_ELEMENTS(pSet->apLists);
|
---|
1901 | while (i-- > 0 && iPage < cPages)
|
---|
1902 | {
|
---|
1903 | PGMMCHUNK pCurFree = NULL;
|
---|
1904 | PGMMCHUNK pCur = pSet->apLists[i];
|
---|
1905 | while (pCur && iPage < cPages)
|
---|
1906 | {
|
---|
1907 | PGMMCHUNK pNext = pCur->pFreeNext;
|
---|
1908 |
|
---|
1909 | if ( pCur->cFree > GMM_CHUNK_NUM_PAGES / 2
|
---|
1910 | && cPages >= GMM_CHUNK_NUM_PAGES / 2)
|
---|
1911 | pCur->hGVM = hGVM; /* change chunk affinity */
|
---|
1912 |
|
---|
1913 | gmmR0UnlinkChunk(pCur);
|
---|
1914 | for (; pCur->cFree && iPage < cPages; iPage++)
|
---|
1915 | gmmR0AllocatePage(pGMM, hGVM, pCur, &paPages[iPage]);
|
---|
1916 | gmmR0LinkChunk(pCur, pSet);
|
---|
1917 |
|
---|
1918 | pCur = pNext;
|
---|
1919 | }
|
---|
1920 | }
|
---|
1921 | }
|
---|
1922 |
|
---|
1923 | /*
|
---|
1924 | * Update the account.
|
---|
1925 | */
|
---|
1926 | switch (enmAccount)
|
---|
1927 | {
|
---|
1928 | case GMMACCOUNT_BASE: pGVM->gmm.s.Allocated.cBasePages += iPage; break;
|
---|
1929 | case GMMACCOUNT_SHADOW: pGVM->gmm.s.Allocated.cShadowPages += iPage; break;
|
---|
1930 | case GMMACCOUNT_FIXED: pGVM->gmm.s.Allocated.cFixedPages += iPage; break;
|
---|
1931 | default:
|
---|
1932 | AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_INTERNAL_ERROR);
|
---|
1933 | }
|
---|
1934 | pGVM->gmm.s.cPrivatePages += iPage;
|
---|
1935 | pGMM->cAllocatedPages += iPage;
|
---|
1936 |
|
---|
1937 | AssertMsgReturn(iPage == cPages, ("%u != %u\n", iPage, cPages), VERR_INTERNAL_ERROR);
|
---|
1938 |
|
---|
1939 | /*
|
---|
1940 | * Check if we've reached some threshold and should kick one or two VMs and tell
|
---|
1941 | * them to inflate their balloons a bit more... later.
|
---|
1942 | */
|
---|
1943 |
|
---|
1944 | return VINF_SUCCESS;
|
---|
1945 | }
|
---|
1946 |
|
---|
1947 |
|
---|
1948 | /**
|
---|
1949 | * Updates the previous allocations and allocates more pages.
|
---|
1950 | *
|
---|
1951 | * The handy pages are always taken from the 'base' memory account.
|
---|
1952 | * The allocated pages are not cleared and will contains random garbage.
|
---|
1953 | *
|
---|
1954 | * @returns VBox status code:
|
---|
1955 | * @retval VINF_SUCCESS on success.
|
---|
1956 | * @retval VERR_NOT_OWNER if the caller is not an EMT.
|
---|
1957 | * @retval VERR_GMM_PAGE_NOT_FOUND if one of the pages to update wasn't found.
|
---|
1958 | * @retval VERR_GMM_PAGE_NOT_PRIVATE if one of the pages to update wasn't a
|
---|
1959 | * private page.
|
---|
1960 | * @retval VERR_GMM_PAGE_NOT_SHARED if one of the pages to update wasn't a
|
---|
1961 | * shared page.
|
---|
1962 | * @retval VERR_GMM_NOT_PAGE_OWNER if one of the pages to be updated wasn't
|
---|
1963 | * owned by the VM.
|
---|
1964 | * @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk is necessary.
|
---|
1965 | * @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
|
---|
1966 | * @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
|
---|
1967 | * that is we're trying to allocate more than we've reserved.
|
---|
1968 | *
|
---|
1969 | * @param pVM Pointer to the shared VM structure.
|
---|
1970 | * @param idCpu VCPU id
|
---|
1971 | * @param cPagesToUpdate The number of pages to update (starting from the head).
|
---|
1972 | * @param cPagesToAlloc The number of pages to allocate (starting from the head).
|
---|
1973 | * @param paPages The array of page descriptors.
|
---|
1974 | * See GMMPAGEDESC for details on what is expected on input.
|
---|
1975 | * @thread EMT.
|
---|
1976 | */
|
---|
1977 | GMMR0DECL(int) GMMR0AllocateHandyPages(PVM pVM, VMCPUID idCpu, uint32_t cPagesToUpdate, uint32_t cPagesToAlloc, PGMMPAGEDESC paPages)
|
---|
1978 | {
|
---|
1979 | LogFlow(("GMMR0AllocateHandyPages: pVM=%p cPagesToUpdate=%#x cPagesToAlloc=%#x paPages=%p\n",
|
---|
1980 | pVM, cPagesToUpdate, cPagesToAlloc, paPages));
|
---|
1981 |
|
---|
1982 | /*
|
---|
1983 | * Validate, get basics and take the semaphore.
|
---|
1984 | * (This is a relatively busy path, so make predictions where possible.)
|
---|
1985 | */
|
---|
1986 | PGMM pGMM;
|
---|
1987 | GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
|
---|
1988 | PGVM pGVM;
|
---|
1989 | int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
|
---|
1990 | if (RT_FAILURE(rc))
|
---|
1991 | return rc;
|
---|
1992 |
|
---|
1993 | AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
|
---|
1994 | AssertMsgReturn( (cPagesToUpdate && cPagesToUpdate < 1024)
|
---|
1995 | || (cPagesToAlloc && cPagesToAlloc < 1024),
|
---|
1996 | ("cPagesToUpdate=%#x cPagesToAlloc=%#x\n", cPagesToUpdate, cPagesToAlloc),
|
---|
1997 | VERR_INVALID_PARAMETER);
|
---|
1998 |
|
---|
1999 | unsigned iPage = 0;
|
---|
2000 | for (; iPage < cPagesToUpdate; iPage++)
|
---|
2001 | {
|
---|
2002 | AssertMsgReturn( ( paPages[iPage].HCPhysGCPhys <= GMM_GCPHYS_LAST
|
---|
2003 | && !(paPages[iPage].HCPhysGCPhys & PAGE_OFFSET_MASK))
|
---|
2004 | || paPages[iPage].HCPhysGCPhys == NIL_RTHCPHYS
|
---|
2005 | || paPages[iPage].HCPhysGCPhys == GMM_GCPHYS_UNSHAREABLE,
|
---|
2006 | ("#%#x: %RHp\n", iPage, paPages[iPage].HCPhysGCPhys),
|
---|
2007 | VERR_INVALID_PARAMETER);
|
---|
2008 | AssertMsgReturn( paPages[iPage].idPage <= GMM_PAGEID_LAST
|
---|
2009 | /*|| paPages[iPage].idPage == NIL_GMM_PAGEID*/,
|
---|
2010 | ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
|
---|
2011 | AssertMsgReturn( paPages[iPage].idPage <= GMM_PAGEID_LAST
|
---|
2012 | /*|| paPages[iPage].idSharedPage == NIL_GMM_PAGEID*/,
|
---|
2013 | ("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
|
---|
2014 | }
|
---|
2015 |
|
---|
2016 | for (; iPage < cPagesToAlloc; iPage++)
|
---|
2017 | {
|
---|
2018 | AssertMsgReturn(paPages[iPage].HCPhysGCPhys == NIL_RTHCPHYS, ("#%#x: %RHp\n", iPage, paPages[iPage].HCPhysGCPhys), VERR_INVALID_PARAMETER);
|
---|
2019 | AssertMsgReturn(paPages[iPage].idPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
|
---|
2020 | AssertMsgReturn(paPages[iPage].idSharedPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
|
---|
2021 | }
|
---|
2022 |
|
---|
2023 | rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
2024 | AssertRC(rc);
|
---|
2025 | if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
2026 | {
|
---|
2027 |
|
---|
2028 | /* No allocations before the initial reservation has been made! */
|
---|
2029 | if (RT_LIKELY( pGVM->gmm.s.Reserved.cBasePages
|
---|
2030 | && pGVM->gmm.s.Reserved.cFixedPages
|
---|
2031 | && pGVM->gmm.s.Reserved.cShadowPages))
|
---|
2032 | {
|
---|
2033 | /*
|
---|
2034 | * Perform the updates.
|
---|
2035 | * Stop on the first error.
|
---|
2036 | */
|
---|
2037 | for (iPage = 0; iPage < cPagesToUpdate; iPage++)
|
---|
2038 | {
|
---|
2039 | if (paPages[iPage].idPage != NIL_GMM_PAGEID)
|
---|
2040 | {
|
---|
2041 | PGMMPAGE pPage = gmmR0GetPage(pGMM, paPages[iPage].idPage);
|
---|
2042 | if (RT_LIKELY(pPage))
|
---|
2043 | {
|
---|
2044 | if (RT_LIKELY(GMM_PAGE_IS_PRIVATE(pPage)))
|
---|
2045 | {
|
---|
2046 | if (RT_LIKELY(pPage->Private.hGVM == pGVM->hSelf))
|
---|
2047 | {
|
---|
2048 | AssertCompile(NIL_RTHCPHYS > GMM_GCPHYS_LAST && GMM_GCPHYS_UNSHAREABLE > GMM_GCPHYS_LAST);
|
---|
2049 | if (RT_LIKELY(paPages[iPage].HCPhysGCPhys <= GMM_GCPHYS_LAST))
|
---|
2050 | pPage->Private.pfn = paPages[iPage].HCPhysGCPhys >> PAGE_SHIFT;
|
---|
2051 | else if (paPages[iPage].HCPhysGCPhys == GMM_GCPHYS_UNSHAREABLE)
|
---|
2052 | pPage->Private.pfn = GMM_PAGE_PFN_UNSHAREABLE;
|
---|
2053 | /* else: NIL_RTHCPHYS nothing */
|
---|
2054 |
|
---|
2055 | paPages[iPage].idPage = NIL_GMM_PAGEID;
|
---|
2056 | paPages[iPage].HCPhysGCPhys = NIL_RTHCPHYS;
|
---|
2057 | }
|
---|
2058 | else
|
---|
2059 | {
|
---|
2060 | Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not owner! hGVM=%#x hSelf=%#x\n",
|
---|
2061 | iPage, paPages[iPage].idPage, pPage->Private.hGVM, pGVM->hSelf));
|
---|
2062 | rc = VERR_GMM_NOT_PAGE_OWNER;
|
---|
2063 | break;
|
---|
2064 | }
|
---|
2065 | }
|
---|
2066 | else
|
---|
2067 | {
|
---|
2068 | Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not private! %.*Rhxs\n", iPage, paPages[iPage].idPage, sizeof(*pPage), pPage));
|
---|
2069 | rc = VERR_GMM_PAGE_NOT_PRIVATE;
|
---|
2070 | break;
|
---|
2071 | }
|
---|
2072 | }
|
---|
2073 | else
|
---|
2074 | {
|
---|
2075 | Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not found! (private)\n", iPage, paPages[iPage].idPage));
|
---|
2076 | rc = VERR_GMM_PAGE_NOT_FOUND;
|
---|
2077 | break;
|
---|
2078 | }
|
---|
2079 | }
|
---|
2080 |
|
---|
2081 | if (paPages[iPage].idSharedPage != NIL_GMM_PAGEID)
|
---|
2082 | {
|
---|
2083 | PGMMPAGE pPage = gmmR0GetPage(pGMM, paPages[iPage].idSharedPage);
|
---|
2084 | if (RT_LIKELY(pPage))
|
---|
2085 | {
|
---|
2086 | if (RT_LIKELY(GMM_PAGE_IS_SHARED(pPage)))
|
---|
2087 | {
|
---|
2088 | AssertCompile(NIL_RTHCPHYS > GMM_GCPHYS_LAST && GMM_GCPHYS_UNSHAREABLE > GMM_GCPHYS_LAST);
|
---|
2089 | Assert(pPage->Shared.cRefs);
|
---|
2090 | Assert(pGVM->gmm.s.cSharedPages);
|
---|
2091 | Assert(pGVM->gmm.s.Allocated.cBasePages);
|
---|
2092 |
|
---|
2093 | pGVM->gmm.s.cSharedPages--;
|
---|
2094 | pGVM->gmm.s.Allocated.cBasePages--;
|
---|
2095 | if (!--pPage->Shared.cRefs)
|
---|
2096 | gmmR0FreeSharedPage(pGMM, paPages[iPage].idSharedPage, pPage);
|
---|
2097 |
|
---|
2098 | paPages[iPage].idSharedPage = NIL_GMM_PAGEID;
|
---|
2099 | }
|
---|
2100 | else
|
---|
2101 | {
|
---|
2102 | Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not shared!\n", iPage, paPages[iPage].idSharedPage));
|
---|
2103 | rc = VERR_GMM_PAGE_NOT_SHARED;
|
---|
2104 | break;
|
---|
2105 | }
|
---|
2106 | }
|
---|
2107 | else
|
---|
2108 | {
|
---|
2109 | Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not found! (shared)\n", iPage, paPages[iPage].idSharedPage));
|
---|
2110 | rc = VERR_GMM_PAGE_NOT_FOUND;
|
---|
2111 | break;
|
---|
2112 | }
|
---|
2113 | }
|
---|
2114 | }
|
---|
2115 |
|
---|
2116 | /*
|
---|
2117 | * Join paths with GMMR0AllocatePages for the allocation.
|
---|
2118 | * Note! gmmR0AllocateMoreChunks may leave the protection of the mutex!
|
---|
2119 | */
|
---|
2120 | while (RT_SUCCESS(rc))
|
---|
2121 | {
|
---|
2122 | rc = gmmR0AllocatePages(pGMM, pGVM, cPagesToAlloc, paPages, GMMACCOUNT_BASE);
|
---|
2123 | if ( rc != VERR_GMM_SEED_ME
|
---|
2124 | || pGMM->fLegacyAllocationMode)
|
---|
2125 | break;
|
---|
2126 | rc = gmmR0AllocateMoreChunks(pGMM, pGVM, &pGMM->Private, cPagesToAlloc);
|
---|
2127 | }
|
---|
2128 | }
|
---|
2129 | else
|
---|
2130 | rc = VERR_WRONG_ORDER;
|
---|
2131 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
2132 | }
|
---|
2133 | else
|
---|
2134 | rc = VERR_INTERNAL_ERROR_5;
|
---|
2135 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
2136 | LogFlow(("GMMR0AllocateHandyPages: returns %Rrc\n", rc));
|
---|
2137 | return rc;
|
---|
2138 | }
|
---|
2139 |
|
---|
2140 |
|
---|
2141 | /**
|
---|
2142 | * Allocate one or more pages.
|
---|
2143 | *
|
---|
2144 | * This is typically used for ROMs and MMIO2 (VRAM) during VM creation.
|
---|
2145 | * The allocated pages are not cleared and will contains random garbage.
|
---|
2146 | *
|
---|
2147 | * @returns VBox status code:
|
---|
2148 | * @retval VINF_SUCCESS on success.
|
---|
2149 | * @retval VERR_NOT_OWNER if the caller is not an EMT.
|
---|
2150 | * @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk is necessary.
|
---|
2151 | * @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
|
---|
2152 | * @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
|
---|
2153 | * that is we're trying to allocate more than we've reserved.
|
---|
2154 | *
|
---|
2155 | * @param pVM Pointer to the shared VM structure.
|
---|
2156 | * @param idCpu VCPU id
|
---|
2157 | * @param cPages The number of pages to allocate.
|
---|
2158 | * @param paPages Pointer to the page descriptors.
|
---|
2159 | * See GMMPAGEDESC for details on what is expected on input.
|
---|
2160 | * @param enmAccount The account to charge.
|
---|
2161 | *
|
---|
2162 | * @thread EMT.
|
---|
2163 | */
|
---|
2164 | GMMR0DECL(int) GMMR0AllocatePages(PVM pVM, VMCPUID idCpu, uint32_t cPages, PGMMPAGEDESC paPages, GMMACCOUNT enmAccount)
|
---|
2165 | {
|
---|
2166 | LogFlow(("GMMR0AllocatePages: pVM=%p cPages=%#x paPages=%p enmAccount=%d\n", pVM, cPages, paPages, enmAccount));
|
---|
2167 |
|
---|
2168 | /*
|
---|
2169 | * Validate, get basics and take the semaphore.
|
---|
2170 | */
|
---|
2171 | PGMM pGMM;
|
---|
2172 | GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
|
---|
2173 | PGVM pGVM;
|
---|
2174 | int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
|
---|
2175 | if (RT_FAILURE(rc))
|
---|
2176 | return rc;
|
---|
2177 |
|
---|
2178 | AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
|
---|
2179 | AssertMsgReturn(enmAccount > GMMACCOUNT_INVALID && enmAccount < GMMACCOUNT_END, ("%d\n", enmAccount), VERR_INVALID_PARAMETER);
|
---|
2180 | AssertMsgReturn(cPages > 0 && cPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cPages), VERR_INVALID_PARAMETER);
|
---|
2181 |
|
---|
2182 | for (unsigned iPage = 0; iPage < cPages; iPage++)
|
---|
2183 | {
|
---|
2184 | AssertMsgReturn( paPages[iPage].HCPhysGCPhys == NIL_RTHCPHYS
|
---|
2185 | || paPages[iPage].HCPhysGCPhys == GMM_GCPHYS_UNSHAREABLE
|
---|
2186 | || ( enmAccount == GMMACCOUNT_BASE
|
---|
2187 | && paPages[iPage].HCPhysGCPhys <= GMM_GCPHYS_LAST
|
---|
2188 | && !(paPages[iPage].HCPhysGCPhys & PAGE_OFFSET_MASK)),
|
---|
2189 | ("#%#x: %RHp enmAccount=%d\n", iPage, paPages[iPage].HCPhysGCPhys, enmAccount),
|
---|
2190 | VERR_INVALID_PARAMETER);
|
---|
2191 | AssertMsgReturn(paPages[iPage].idPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
|
---|
2192 | AssertMsgReturn(paPages[iPage].idSharedPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
|
---|
2193 | }
|
---|
2194 |
|
---|
2195 | rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
2196 | AssertRC(rc);
|
---|
2197 | if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
2198 | {
|
---|
2199 |
|
---|
2200 | /* No allocations before the initial reservation has been made! */
|
---|
2201 | if (RT_LIKELY( pGVM->gmm.s.Reserved.cBasePages
|
---|
2202 | && pGVM->gmm.s.Reserved.cFixedPages
|
---|
2203 | && pGVM->gmm.s.Reserved.cShadowPages))
|
---|
2204 | {
|
---|
2205 | /*
|
---|
2206 | * gmmR0AllocatePages seed loop.
|
---|
2207 | * Note! gmmR0AllocateMoreChunks may leave the protection of the mutex!
|
---|
2208 | */
|
---|
2209 | while (RT_SUCCESS(rc))
|
---|
2210 | {
|
---|
2211 | rc = gmmR0AllocatePages(pGMM, pGVM, cPages, paPages, enmAccount);
|
---|
2212 | if ( rc != VERR_GMM_SEED_ME
|
---|
2213 | || pGMM->fLegacyAllocationMode)
|
---|
2214 | break;
|
---|
2215 | rc = gmmR0AllocateMoreChunks(pGMM, pGVM, &pGMM->Private, cPages);
|
---|
2216 | }
|
---|
2217 | }
|
---|
2218 | else
|
---|
2219 | rc = VERR_WRONG_ORDER;
|
---|
2220 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
2221 | }
|
---|
2222 | else
|
---|
2223 | rc = VERR_INTERNAL_ERROR_5;
|
---|
2224 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
2225 | LogFlow(("GMMR0AllocatePages: returns %Rrc\n", rc));
|
---|
2226 | return rc;
|
---|
2227 | }
|
---|
2228 |
|
---|
2229 |
|
---|
2230 | /**
|
---|
2231 | * VMMR0 request wrapper for GMMR0AllocatePages.
|
---|
2232 | *
|
---|
2233 | * @returns see GMMR0AllocatePages.
|
---|
2234 | * @param pVM Pointer to the shared VM structure.
|
---|
2235 | * @param idCpu VCPU id
|
---|
2236 | * @param pReq The request packet.
|
---|
2237 | */
|
---|
2238 | GMMR0DECL(int) GMMR0AllocatePagesReq(PVM pVM, VMCPUID idCpu, PGMMALLOCATEPAGESREQ pReq)
|
---|
2239 | {
|
---|
2240 | /*
|
---|
2241 | * Validate input and pass it on.
|
---|
2242 | */
|
---|
2243 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
2244 | AssertPtrReturn(pReq, VERR_INVALID_POINTER);
|
---|
2245 | AssertMsgReturn(pReq->Hdr.cbReq >= RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[0]),
|
---|
2246 | ("%#x < %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[0])),
|
---|
2247 | VERR_INVALID_PARAMETER);
|
---|
2248 | AssertMsgReturn(pReq->Hdr.cbReq == RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[pReq->cPages]),
|
---|
2249 | ("%#x != %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[pReq->cPages])),
|
---|
2250 | VERR_INVALID_PARAMETER);
|
---|
2251 |
|
---|
2252 | return GMMR0AllocatePages(pVM, idCpu, pReq->cPages, &pReq->aPages[0], pReq->enmAccount);
|
---|
2253 | }
|
---|
2254 |
|
---|
2255 |
|
---|
2256 | /**
|
---|
2257 | * Frees a chunk, giving it back to the host OS.
|
---|
2258 | *
|
---|
2259 | * @param pGMM Pointer to the GMM instance.
|
---|
2260 | * @param pGVM This is set when called from GMMR0CleanupVM so we can
|
---|
2261 | * unmap and free the chunk in one go.
|
---|
2262 | * @param pChunk The chunk to free.
|
---|
2263 | */
|
---|
2264 | static void gmmR0FreeChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk)
|
---|
2265 | {
|
---|
2266 | Assert(pChunk->Core.Key != NIL_GMM_CHUNKID);
|
---|
2267 |
|
---|
2268 | /*
|
---|
2269 | * Cleanup hack! Unmap the chunk from the callers address space.
|
---|
2270 | */
|
---|
2271 | if ( pChunk->cMappings
|
---|
2272 | && pGVM)
|
---|
2273 | gmmR0UnmapChunk(pGMM, pGVM, pChunk);
|
---|
2274 |
|
---|
2275 | /*
|
---|
2276 | * If there are current mappings of the chunk, then request the
|
---|
2277 | * VMs to unmap them. Reposition the chunk in the free list so
|
---|
2278 | * it won't be a likely candidate for allocations.
|
---|
2279 | */
|
---|
2280 | if (pChunk->cMappings)
|
---|
2281 | {
|
---|
2282 | /** @todo R0 -> VM request */
|
---|
2283 | }
|
---|
2284 | else
|
---|
2285 | {
|
---|
2286 | /*
|
---|
2287 | * Try free the memory object.
|
---|
2288 | */
|
---|
2289 | int rc = RTR0MemObjFree(pChunk->MemObj, false /* fFreeMappings */);
|
---|
2290 | if (RT_SUCCESS(rc))
|
---|
2291 | {
|
---|
2292 | pChunk->MemObj = NIL_RTR0MEMOBJ;
|
---|
2293 |
|
---|
2294 | /*
|
---|
2295 | * Unlink it from everywhere.
|
---|
2296 | */
|
---|
2297 | gmmR0UnlinkChunk(pChunk);
|
---|
2298 |
|
---|
2299 | PAVLU32NODECORE pCore = RTAvlU32Remove(&pGMM->pChunks, pChunk->Core.Key);
|
---|
2300 | Assert(pCore == &pChunk->Core); NOREF(pCore);
|
---|
2301 |
|
---|
2302 | PGMMCHUNKTLBE pTlbe = &pGMM->ChunkTLB.aEntries[GMM_CHUNKTLB_IDX(pChunk->Core.Key)];
|
---|
2303 | if (pTlbe->pChunk == pChunk)
|
---|
2304 | {
|
---|
2305 | pTlbe->idChunk = NIL_GMM_CHUNKID;
|
---|
2306 | pTlbe->pChunk = NULL;
|
---|
2307 | }
|
---|
2308 |
|
---|
2309 | Assert(pGMM->cChunks > 0);
|
---|
2310 | pGMM->cChunks--;
|
---|
2311 |
|
---|
2312 | /*
|
---|
2313 | * Free the Chunk ID and struct.
|
---|
2314 | */
|
---|
2315 | gmmR0FreeChunkId(pGMM, pChunk->Core.Key);
|
---|
2316 | pChunk->Core.Key = NIL_GMM_CHUNKID;
|
---|
2317 |
|
---|
2318 | RTMemFree(pChunk->paMappings);
|
---|
2319 | pChunk->paMappings = NULL;
|
---|
2320 |
|
---|
2321 | RTMemFree(pChunk);
|
---|
2322 | }
|
---|
2323 | else
|
---|
2324 | AssertRC(rc);
|
---|
2325 | }
|
---|
2326 | }
|
---|
2327 |
|
---|
2328 |
|
---|
2329 | /**
|
---|
2330 | * Free page worker.
|
---|
2331 | *
|
---|
2332 | * The caller does all the statistic decrementing, we do all the incrementing.
|
---|
2333 | *
|
---|
2334 | * @param pGMM Pointer to the GMM instance data.
|
---|
2335 | * @param pChunk Pointer to the chunk this page belongs to.
|
---|
2336 | * @param idPage The Page ID.
|
---|
2337 | * @param pPage Pointer to the page.
|
---|
2338 | */
|
---|
2339 | static void gmmR0FreePageWorker(PGMM pGMM, PGMMCHUNK pChunk, uint32_t idPage, PGMMPAGE pPage)
|
---|
2340 | {
|
---|
2341 | Log3(("F pPage=%p iPage=%#x/%#x u2State=%d iFreeHead=%#x\n",
|
---|
2342 | pPage, pPage - &pChunk->aPages[0], idPage, pPage->Common.u2State, pChunk->iFreeHead)); NOREF(idPage);
|
---|
2343 |
|
---|
2344 | /*
|
---|
2345 | * Put the page on the free list.
|
---|
2346 | */
|
---|
2347 | pPage->u = 0;
|
---|
2348 | pPage->Free.u2State = GMM_PAGE_STATE_FREE;
|
---|
2349 | Assert(pChunk->iFreeHead < RT_ELEMENTS(pChunk->aPages) || pChunk->iFreeHead == UINT16_MAX);
|
---|
2350 | pPage->Free.iNext = pChunk->iFreeHead;
|
---|
2351 | pChunk->iFreeHead = pPage - &pChunk->aPages[0];
|
---|
2352 |
|
---|
2353 | /*
|
---|
2354 | * Update statistics (the cShared/cPrivate stats are up to date already),
|
---|
2355 | * and relink the chunk if necessary.
|
---|
2356 | */
|
---|
2357 | if ((pChunk->cFree & GMM_CHUNK_FREE_SET_MASK) == 0)
|
---|
2358 | {
|
---|
2359 | gmmR0UnlinkChunk(pChunk);
|
---|
2360 | pChunk->cFree++;
|
---|
2361 | gmmR0LinkChunk(pChunk, pChunk->cShared ? &pGMM->Shared : &pGMM->Private);
|
---|
2362 | }
|
---|
2363 | else
|
---|
2364 | {
|
---|
2365 | pChunk->cFree++;
|
---|
2366 | pChunk->pSet->cFreePages++;
|
---|
2367 |
|
---|
2368 | /*
|
---|
2369 | * If the chunk becomes empty, consider giving memory back to the host OS.
|
---|
2370 | *
|
---|
2371 | * The current strategy is to try give it back if there are other chunks
|
---|
2372 | * in this free list, meaning if there are at least 240 free pages in this
|
---|
2373 | * category. Note that since there are probably mappings of the chunk,
|
---|
2374 | * it won't be freed up instantly, which probably screws up this logic
|
---|
2375 | * a bit...
|
---|
2376 | */
|
---|
2377 | if (RT_UNLIKELY( pChunk->cFree == GMM_CHUNK_NUM_PAGES
|
---|
2378 | && pChunk->pFreeNext
|
---|
2379 | && pChunk->pFreePrev
|
---|
2380 | && !pGMM->fLegacyAllocationMode))
|
---|
2381 | gmmR0FreeChunk(pGMM, NULL, pChunk);
|
---|
2382 | }
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 |
|
---|
2386 | /**
|
---|
2387 | * Frees a shared page, the page is known to exist and be valid and such.
|
---|
2388 | *
|
---|
2389 | * @param pGMM Pointer to the GMM instance.
|
---|
2390 | * @param idPage The Page ID
|
---|
2391 | * @param pPage The page structure.
|
---|
2392 | */
|
---|
2393 | DECLINLINE(void) gmmR0FreeSharedPage(PGMM pGMM, uint32_t idPage, PGMMPAGE pPage)
|
---|
2394 | {
|
---|
2395 | PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
|
---|
2396 | Assert(pChunk);
|
---|
2397 | Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
|
---|
2398 | Assert(pChunk->cShared > 0);
|
---|
2399 | Assert(pGMM->cSharedPages > 0);
|
---|
2400 | Assert(pGMM->cAllocatedPages > 0);
|
---|
2401 | Assert(!pPage->Shared.cRefs);
|
---|
2402 |
|
---|
2403 | pChunk->cShared--;
|
---|
2404 | pGMM->cAllocatedPages--;
|
---|
2405 | pGMM->cSharedPages--;
|
---|
2406 | gmmR0FreePageWorker(pGMM, pChunk, idPage, pPage);
|
---|
2407 | }
|
---|
2408 |
|
---|
2409 |
|
---|
2410 | /**
|
---|
2411 | * Frees a private page, the page is known to exist and be valid and such.
|
---|
2412 | *
|
---|
2413 | * @param pGMM Pointer to the GMM instance.
|
---|
2414 | * @param idPage The Page ID
|
---|
2415 | * @param pPage The page structure.
|
---|
2416 | */
|
---|
2417 | DECLINLINE(void) gmmR0FreePrivatePage(PGMM pGMM, uint32_t idPage, PGMMPAGE pPage)
|
---|
2418 | {
|
---|
2419 | PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
|
---|
2420 | Assert(pChunk);
|
---|
2421 | Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
|
---|
2422 | Assert(pChunk->cPrivate > 0);
|
---|
2423 | Assert(pGMM->cAllocatedPages > 0);
|
---|
2424 |
|
---|
2425 | pChunk->cPrivate--;
|
---|
2426 | pGMM->cAllocatedPages--;
|
---|
2427 | gmmR0FreePageWorker(pGMM, pChunk, idPage, pPage);
|
---|
2428 | }
|
---|
2429 |
|
---|
2430 |
|
---|
2431 | /**
|
---|
2432 | * Common worker for GMMR0FreePages and GMMR0BalloonedPages.
|
---|
2433 | *
|
---|
2434 | * @returns VBox status code:
|
---|
2435 | * @retval xxx
|
---|
2436 | *
|
---|
2437 | * @param pGMM Pointer to the GMM instance data.
|
---|
2438 | * @param pGVM Pointer to the shared VM structure.
|
---|
2439 | * @param cPages The number of pages to free.
|
---|
2440 | * @param paPages Pointer to the page descriptors.
|
---|
2441 | * @param enmAccount The account this relates to.
|
---|
2442 | */
|
---|
2443 | static int gmmR0FreePages(PGMM pGMM, PGVM pGVM, uint32_t cPages, PGMMFREEPAGEDESC paPages, GMMACCOUNT enmAccount)
|
---|
2444 | {
|
---|
2445 | /*
|
---|
2446 | * Check that the request isn't impossible wrt to the account status.
|
---|
2447 | */
|
---|
2448 | switch (enmAccount)
|
---|
2449 | {
|
---|
2450 | case GMMACCOUNT_BASE:
|
---|
2451 | if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cBasePages < cPages))
|
---|
2452 | {
|
---|
2453 | Log(("gmmR0FreePages: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Allocated.cBasePages, cPages));
|
---|
2454 | return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
|
---|
2455 | }
|
---|
2456 | break;
|
---|
2457 | case GMMACCOUNT_SHADOW:
|
---|
2458 | if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cShadowPages < cPages))
|
---|
2459 | {
|
---|
2460 | Log(("gmmR0FreePages: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Allocated.cShadowPages, cPages));
|
---|
2461 | return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
|
---|
2462 | }
|
---|
2463 | break;
|
---|
2464 | case GMMACCOUNT_FIXED:
|
---|
2465 | if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cFixedPages < cPages))
|
---|
2466 | {
|
---|
2467 | Log(("gmmR0FreePages: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Allocated.cFixedPages, cPages));
|
---|
2468 | return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
|
---|
2469 | }
|
---|
2470 | break;
|
---|
2471 | default:
|
---|
2472 | AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_INTERNAL_ERROR);
|
---|
2473 | }
|
---|
2474 |
|
---|
2475 | /*
|
---|
2476 | * Walk the descriptors and free the pages.
|
---|
2477 | *
|
---|
2478 | * Statistics (except the account) are being updated as we go along,
|
---|
2479 | * unlike the alloc code. Also, stop on the first error.
|
---|
2480 | */
|
---|
2481 | int rc = VINF_SUCCESS;
|
---|
2482 | uint32_t iPage;
|
---|
2483 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
2484 | {
|
---|
2485 | uint32_t idPage = paPages[iPage].idPage;
|
---|
2486 | PGMMPAGE pPage = gmmR0GetPage(pGMM, idPage);
|
---|
2487 | if (RT_LIKELY(pPage))
|
---|
2488 | {
|
---|
2489 | if (RT_LIKELY(GMM_PAGE_IS_PRIVATE(pPage)))
|
---|
2490 | {
|
---|
2491 | if (RT_LIKELY(pPage->Private.hGVM == pGVM->hSelf))
|
---|
2492 | {
|
---|
2493 | Assert(pGVM->gmm.s.cPrivatePages);
|
---|
2494 | pGVM->gmm.s.cPrivatePages--;
|
---|
2495 | gmmR0FreePrivatePage(pGMM, idPage, pPage);
|
---|
2496 | }
|
---|
2497 | else
|
---|
2498 | {
|
---|
2499 | Log(("gmmR0AllocatePages: #%#x/%#x: not owner! hGVM=%#x hSelf=%#x\n", iPage, idPage,
|
---|
2500 | pPage->Private.hGVM, pGVM->hSelf));
|
---|
2501 | rc = VERR_GMM_NOT_PAGE_OWNER;
|
---|
2502 | break;
|
---|
2503 | }
|
---|
2504 | }
|
---|
2505 | else if (RT_LIKELY(GMM_PAGE_IS_SHARED(pPage)))
|
---|
2506 | {
|
---|
2507 | Assert(pGVM->gmm.s.cSharedPages);
|
---|
2508 | pGVM->gmm.s.cSharedPages--;
|
---|
2509 | Assert(pPage->Shared.cRefs);
|
---|
2510 | if (!--pPage->Shared.cRefs)
|
---|
2511 | gmmR0FreeSharedPage(pGMM, idPage, pPage);
|
---|
2512 | }
|
---|
2513 | else
|
---|
2514 | {
|
---|
2515 | Log(("gmmR0AllocatePages: #%#x/%#x: already free!\n", iPage, idPage));
|
---|
2516 | rc = VERR_GMM_PAGE_ALREADY_FREE;
|
---|
2517 | break;
|
---|
2518 | }
|
---|
2519 | }
|
---|
2520 | else
|
---|
2521 | {
|
---|
2522 | Log(("gmmR0AllocatePages: #%#x/%#x: not found!\n", iPage, idPage));
|
---|
2523 | rc = VERR_GMM_PAGE_NOT_FOUND;
|
---|
2524 | break;
|
---|
2525 | }
|
---|
2526 | paPages[iPage].idPage = NIL_GMM_PAGEID;
|
---|
2527 | }
|
---|
2528 |
|
---|
2529 | /*
|
---|
2530 | * Update the account.
|
---|
2531 | */
|
---|
2532 | switch (enmAccount)
|
---|
2533 | {
|
---|
2534 | case GMMACCOUNT_BASE: pGVM->gmm.s.Allocated.cBasePages -= iPage; break;
|
---|
2535 | case GMMACCOUNT_SHADOW: pGVM->gmm.s.Allocated.cShadowPages -= iPage; break;
|
---|
2536 | case GMMACCOUNT_FIXED: pGVM->gmm.s.Allocated.cFixedPages -= iPage; break;
|
---|
2537 | default:
|
---|
2538 | AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_INTERNAL_ERROR);
|
---|
2539 | }
|
---|
2540 |
|
---|
2541 | /*
|
---|
2542 | * Any threshold stuff to be done here?
|
---|
2543 | */
|
---|
2544 |
|
---|
2545 | return rc;
|
---|
2546 | }
|
---|
2547 |
|
---|
2548 |
|
---|
2549 | /**
|
---|
2550 | * Free one or more pages.
|
---|
2551 | *
|
---|
2552 | * This is typically used at reset time or power off.
|
---|
2553 | *
|
---|
2554 | * @returns VBox status code:
|
---|
2555 | * @retval xxx
|
---|
2556 | *
|
---|
2557 | * @param pVM Pointer to the shared VM structure.
|
---|
2558 | * @param idCpu VCPU id
|
---|
2559 | * @param cPages The number of pages to allocate.
|
---|
2560 | * @param paPages Pointer to the page descriptors containing the Page IDs for each page.
|
---|
2561 | * @param enmAccount The account this relates to.
|
---|
2562 | * @thread EMT.
|
---|
2563 | */
|
---|
2564 | GMMR0DECL(int) GMMR0FreePages(PVM pVM, VMCPUID idCpu, uint32_t cPages, PGMMFREEPAGEDESC paPages, GMMACCOUNT enmAccount)
|
---|
2565 | {
|
---|
2566 | LogFlow(("GMMR0FreePages: pVM=%p cPages=%#x paPages=%p enmAccount=%d\n", pVM, cPages, paPages, enmAccount));
|
---|
2567 |
|
---|
2568 | /*
|
---|
2569 | * Validate input and get the basics.
|
---|
2570 | */
|
---|
2571 | PGMM pGMM;
|
---|
2572 | GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
|
---|
2573 | PGVM pGVM;
|
---|
2574 | int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
|
---|
2575 | if (RT_FAILURE(rc))
|
---|
2576 | return rc;
|
---|
2577 |
|
---|
2578 | AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
|
---|
2579 | AssertMsgReturn(enmAccount > GMMACCOUNT_INVALID && enmAccount < GMMACCOUNT_END, ("%d\n", enmAccount), VERR_INVALID_PARAMETER);
|
---|
2580 | AssertMsgReturn(cPages > 0 && cPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cPages), VERR_INVALID_PARAMETER);
|
---|
2581 |
|
---|
2582 | for (unsigned iPage = 0; iPage < cPages; iPage++)
|
---|
2583 | AssertMsgReturn( paPages[iPage].idPage <= GMM_PAGEID_LAST
|
---|
2584 | /*|| paPages[iPage].idPage == NIL_GMM_PAGEID*/,
|
---|
2585 | ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
|
---|
2586 |
|
---|
2587 | /*
|
---|
2588 | * Take the semaphore and call the worker function.
|
---|
2589 | */
|
---|
2590 | rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
2591 | AssertRC(rc);
|
---|
2592 | if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
2593 | {
|
---|
2594 | rc = gmmR0FreePages(pGMM, pGVM, cPages, paPages, enmAccount);
|
---|
2595 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
2596 | }
|
---|
2597 | else
|
---|
2598 | rc = VERR_INTERNAL_ERROR_5;
|
---|
2599 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
2600 | LogFlow(("GMMR0FreePages: returns %Rrc\n", rc));
|
---|
2601 | return rc;
|
---|
2602 | }
|
---|
2603 |
|
---|
2604 |
|
---|
2605 | /**
|
---|
2606 | * VMMR0 request wrapper for GMMR0FreePages.
|
---|
2607 | *
|
---|
2608 | * @returns see GMMR0FreePages.
|
---|
2609 | * @param pVM Pointer to the shared VM structure.
|
---|
2610 | * @param idCpu VCPU id
|
---|
2611 | * @param pReq The request packet.
|
---|
2612 | */
|
---|
2613 | GMMR0DECL(int) GMMR0FreePagesReq(PVM pVM, VMCPUID idCpu, PGMMFREEPAGESREQ pReq)
|
---|
2614 | {
|
---|
2615 | /*
|
---|
2616 | * Validate input and pass it on.
|
---|
2617 | */
|
---|
2618 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
2619 | AssertPtrReturn(pReq, VERR_INVALID_POINTER);
|
---|
2620 | AssertMsgReturn(pReq->Hdr.cbReq >= RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[0]),
|
---|
2621 | ("%#x < %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[0])),
|
---|
2622 | VERR_INVALID_PARAMETER);
|
---|
2623 | AssertMsgReturn(pReq->Hdr.cbReq == RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[pReq->cPages]),
|
---|
2624 | ("%#x != %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[pReq->cPages])),
|
---|
2625 | VERR_INVALID_PARAMETER);
|
---|
2626 |
|
---|
2627 | return GMMR0FreePages(pVM, idCpu, pReq->cPages, &pReq->aPages[0], pReq->enmAccount);
|
---|
2628 | }
|
---|
2629 |
|
---|
2630 |
|
---|
2631 | /**
|
---|
2632 | * Report back on a memory ballooning request.
|
---|
2633 | *
|
---|
2634 | * The request may or may not have been initiated by the GMM. If it was initiated
|
---|
2635 | * by the GMM it is important that this function is called even if no pages was
|
---|
2636 | * ballooned.
|
---|
2637 | *
|
---|
2638 | * Since the whole purpose of ballooning is to free up guest RAM pages, this API
|
---|
2639 | * may also be given a set of related pages to be freed. These pages are assumed
|
---|
2640 | * to be on the base account.
|
---|
2641 | *
|
---|
2642 | * @returns VBox status code:
|
---|
2643 | * @retval xxx
|
---|
2644 | *
|
---|
2645 | * @param pVM Pointer to the shared VM structure.
|
---|
2646 | * @param idCpu VCPU id
|
---|
2647 | * @param cBalloonedPages The number of pages that was ballooned.
|
---|
2648 | * @param cPagesToFree The number of pages to be freed.
|
---|
2649 | * @param paPages Pointer to the page descriptors for the pages that's to be freed.
|
---|
2650 | * @param fCompleted Indicates whether the ballooning request was completed (true) or
|
---|
2651 | * if there is more pages to come (false). If the ballooning was not
|
---|
2652 | * not triggered by the GMM, don't set this.
|
---|
2653 | * @thread EMT.
|
---|
2654 | */
|
---|
2655 | GMMR0DECL(int) GMMR0BalloonedPages(PVM pVM, VMCPUID idCpu, uint32_t cBalloonedPages, uint32_t cPagesToFree, PGMMFREEPAGEDESC paPages, bool fCompleted)
|
---|
2656 | {
|
---|
2657 | LogFlow(("GMMR0BalloonedPages: pVM=%p cBalloonedPages=%#x cPagestoFree=%#x paPages=%p enmAccount=%d fCompleted=%RTbool\n",
|
---|
2658 | pVM, cBalloonedPages, cPagesToFree, paPages, fCompleted));
|
---|
2659 |
|
---|
2660 | /*
|
---|
2661 | * Validate input and get the basics.
|
---|
2662 | */
|
---|
2663 | PGMM pGMM;
|
---|
2664 | GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
|
---|
2665 | PGVM pGVM;
|
---|
2666 | int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
|
---|
2667 | if (RT_FAILURE(rc))
|
---|
2668 | return rc;
|
---|
2669 |
|
---|
2670 | AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
|
---|
2671 | AssertMsgReturn(cBalloonedPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cBalloonedPages), VERR_INVALID_PARAMETER);
|
---|
2672 | AssertMsgReturn(cPagesToFree <= cBalloonedPages, ("%#x\n", cPagesToFree), VERR_INVALID_PARAMETER);
|
---|
2673 |
|
---|
2674 | for (unsigned iPage = 0; iPage < cPagesToFree; iPage++)
|
---|
2675 | AssertMsgReturn( paPages[iPage].idPage <= GMM_PAGEID_LAST
|
---|
2676 | /*|| paPages[iPage].idPage == NIL_GMM_PAGEID*/,
|
---|
2677 | ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
|
---|
2678 |
|
---|
2679 | /*
|
---|
2680 | * Take the sempahore and do some more validations.
|
---|
2681 | */
|
---|
2682 | rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
2683 | AssertRC(rc);
|
---|
2684 | if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
2685 | {
|
---|
2686 |
|
---|
2687 | if (pGVM->gmm.s.Allocated.cBasePages >= cPagesToFree)
|
---|
2688 | {
|
---|
2689 | /*
|
---|
2690 | * Record the ballooned memory.
|
---|
2691 | */
|
---|
2692 | pGMM->cBalloonedPages += cBalloonedPages;
|
---|
2693 | if (pGVM->gmm.s.cReqBalloonedPages)
|
---|
2694 | {
|
---|
2695 | pGVM->gmm.s.cBalloonedPages += cBalloonedPages;
|
---|
2696 | pGVM->gmm.s.cReqActuallyBalloonedPages += cBalloonedPages;
|
---|
2697 | if (fCompleted)
|
---|
2698 | {
|
---|
2699 | Log(("GMMR0BalloonedPages: +%#x - Global=%#llx; / VM: Total=%#llx Req=%#llx Actual=%#llx (completed)\n", cBalloonedPages,
|
---|
2700 | pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages, pGVM->gmm.s.cReqBalloonedPages, pGVM->gmm.s.cReqActuallyBalloonedPages));
|
---|
2701 |
|
---|
2702 | /*
|
---|
2703 | * Anything we need to do here now when the request has been completed?
|
---|
2704 | */
|
---|
2705 | pGVM->gmm.s.cReqBalloonedPages = 0;
|
---|
2706 | }
|
---|
2707 | else
|
---|
2708 | Log(("GMMR0BalloonedPages: +%#x - Global=%#llx / VM: Total=%#llx Req=%#llx Actual=%#llx (pending)\n", cBalloonedPages,
|
---|
2709 | pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages, pGVM->gmm.s.cReqBalloonedPages, pGVM->gmm.s.cReqActuallyBalloonedPages));
|
---|
2710 | }
|
---|
2711 | else
|
---|
2712 | {
|
---|
2713 | pGVM->gmm.s.cBalloonedPages += cBalloonedPages;
|
---|
2714 | Log(("GMMR0BalloonedPages: +%#x - Global=%#llx / VM: Total=%#llx (user)\n",
|
---|
2715 | cBalloonedPages, pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages));
|
---|
2716 | }
|
---|
2717 |
|
---|
2718 | /*
|
---|
2719 | * Any pages to free?
|
---|
2720 | */
|
---|
2721 | if (cPagesToFree)
|
---|
2722 | rc = gmmR0FreePages(pGMM, pGVM, cPagesToFree, paPages, GMMACCOUNT_BASE);
|
---|
2723 | }
|
---|
2724 | else
|
---|
2725 | rc = VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
|
---|
2726 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
2727 | }
|
---|
2728 | else
|
---|
2729 | rc = VERR_INTERNAL_ERROR_5;
|
---|
2730 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
2731 | LogFlow(("GMMR0BalloonedPages: returns %Rrc\n", rc));
|
---|
2732 | return rc;
|
---|
2733 | }
|
---|
2734 |
|
---|
2735 |
|
---|
2736 | /**
|
---|
2737 | * VMMR0 request wrapper for GMMR0BalloonedPages.
|
---|
2738 | *
|
---|
2739 | * @returns see GMMR0BalloonedPages.
|
---|
2740 | * @param pVM Pointer to the shared VM structure.
|
---|
2741 | * @param idCpu VCPU id
|
---|
2742 | * @param pReq The request packet.
|
---|
2743 | */
|
---|
2744 | GMMR0DECL(int) GMMR0BalloonedPagesReq(PVM pVM, VMCPUID idCpu, PGMMBALLOONEDPAGESREQ pReq)
|
---|
2745 | {
|
---|
2746 | /*
|
---|
2747 | * Validate input and pass it on.
|
---|
2748 | */
|
---|
2749 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
2750 | AssertPtrReturn(pReq, VERR_INVALID_POINTER);
|
---|
2751 | AssertMsgReturn(pReq->Hdr.cbReq >= RT_UOFFSETOF(GMMBALLOONEDPAGESREQ, aPages[0]),
|
---|
2752 | ("%#x < %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMBALLOONEDPAGESREQ, aPages[0])),
|
---|
2753 | VERR_INVALID_PARAMETER);
|
---|
2754 | AssertMsgReturn(pReq->Hdr.cbReq == RT_UOFFSETOF(GMMBALLOONEDPAGESREQ, aPages[pReq->cPagesToFree]),
|
---|
2755 | ("%#x != %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMBALLOONEDPAGESREQ, aPages[pReq->cPagesToFree])),
|
---|
2756 | VERR_INVALID_PARAMETER);
|
---|
2757 |
|
---|
2758 | return GMMR0BalloonedPages(pVM, idCpu, pReq->cBalloonedPages, pReq->cPagesToFree, &pReq->aPages[0], pReq->fCompleted);
|
---|
2759 | }
|
---|
2760 |
|
---|
2761 |
|
---|
2762 | /**
|
---|
2763 | * Report balloon deflating.
|
---|
2764 | *
|
---|
2765 | * @returns VBox status code:
|
---|
2766 | * @retval xxx
|
---|
2767 | *
|
---|
2768 | * @param pVM Pointer to the shared VM structure.
|
---|
2769 | * @param idCpu VCPU id
|
---|
2770 | * @param cPages The number of pages that was let out of the balloon.
|
---|
2771 | * @thread EMT.
|
---|
2772 | */
|
---|
2773 | GMMR0DECL(int) GMMR0DeflatedBalloon(PVM pVM, VMCPUID idCpu, uint32_t cPages)
|
---|
2774 | {
|
---|
2775 | LogFlow(("GMMR0DeflatedBalloon: pVM=%p cPages=%#x\n", pVM, cPages));
|
---|
2776 |
|
---|
2777 | /*
|
---|
2778 | * Validate input and get the basics.
|
---|
2779 | */
|
---|
2780 | PGMM pGMM;
|
---|
2781 | GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
|
---|
2782 | PGVM pGVM;
|
---|
2783 | int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
|
---|
2784 | if (RT_FAILURE(rc))
|
---|
2785 | return rc;
|
---|
2786 |
|
---|
2787 | AssertMsgReturn(cPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cPages), VERR_INVALID_PARAMETER);
|
---|
2788 |
|
---|
2789 | /*
|
---|
2790 | * Take the sempahore and do some more validations.
|
---|
2791 | */
|
---|
2792 | rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
2793 | AssertRC(rc);
|
---|
2794 | if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
2795 | {
|
---|
2796 |
|
---|
2797 | if (pGVM->gmm.s.cBalloonedPages < cPages)
|
---|
2798 | {
|
---|
2799 | Assert(pGMM->cBalloonedPages >= pGVM->gmm.s.cBalloonedPages);
|
---|
2800 |
|
---|
2801 | /*
|
---|
2802 | * Record it.
|
---|
2803 | */
|
---|
2804 | pGMM->cBalloonedPages -= cPages;
|
---|
2805 | pGVM->gmm.s.cBalloonedPages -= cPages;
|
---|
2806 | if (pGVM->gmm.s.cReqDeflatePages)
|
---|
2807 | {
|
---|
2808 | Log(("GMMR0BalloonedPages: -%#x - Global=%#llx / VM: Total=%#llx Req=%#llx\n", cPages,
|
---|
2809 | pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages, pGVM->gmm.s.cReqDeflatePages));
|
---|
2810 |
|
---|
2811 | /*
|
---|
2812 | * Anything we need to do here now when the request has been completed?
|
---|
2813 | */
|
---|
2814 | pGVM->gmm.s.cReqDeflatePages = 0;
|
---|
2815 | }
|
---|
2816 | else
|
---|
2817 | Log(("GMMR0BalloonedPages: -%#x - Global=%#llx / VM: Total=%#llx\n", cPages,
|
---|
2818 | pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages));
|
---|
2819 | }
|
---|
2820 | else
|
---|
2821 | {
|
---|
2822 | Log(("GMMR0DeflatedBalloon: cBalloonedPages=%#llx cPages=%#x\n", pGVM->gmm.s.cBalloonedPages, cPages));
|
---|
2823 | rc = VERR_GMM_ATTEMPT_TO_DEFLATE_TOO_MUCH;
|
---|
2824 | }
|
---|
2825 |
|
---|
2826 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
2827 | }
|
---|
2828 | else
|
---|
2829 | rc = VERR_INTERNAL_ERROR_5;
|
---|
2830 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
2831 | LogFlow(("GMMR0BalloonedPages: returns %Rrc\n", rc));
|
---|
2832 | return rc;
|
---|
2833 | }
|
---|
2834 |
|
---|
2835 |
|
---|
2836 | /**
|
---|
2837 | * Unmaps a chunk previously mapped into the address space of the current process.
|
---|
2838 | *
|
---|
2839 | * @returns VBox status code.
|
---|
2840 | * @param pGMM Pointer to the GMM instance data.
|
---|
2841 | * @param pGVM Pointer to the Global VM structure.
|
---|
2842 | * @param pChunk Pointer to the chunk to be unmapped.
|
---|
2843 | */
|
---|
2844 | static int gmmR0UnmapChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk)
|
---|
2845 | {
|
---|
2846 | if (!pGMM->fLegacyAllocationMode)
|
---|
2847 | {
|
---|
2848 | /*
|
---|
2849 | * Find the mapping and try unmapping it.
|
---|
2850 | */
|
---|
2851 | for (uint32_t i = 0; i < pChunk->cMappings; i++)
|
---|
2852 | {
|
---|
2853 | Assert(pChunk->paMappings[i].pGVM && pChunk->paMappings[i].MapObj != NIL_RTR0MEMOBJ);
|
---|
2854 | if (pChunk->paMappings[i].pGVM == pGVM)
|
---|
2855 | {
|
---|
2856 | /* unmap */
|
---|
2857 | int rc = RTR0MemObjFree(pChunk->paMappings[i].MapObj, false /* fFreeMappings (NA) */);
|
---|
2858 | if (RT_SUCCESS(rc))
|
---|
2859 | {
|
---|
2860 | /* update the record. */
|
---|
2861 | pChunk->cMappings--;
|
---|
2862 | if (i < pChunk->cMappings)
|
---|
2863 | pChunk->paMappings[i] = pChunk->paMappings[pChunk->cMappings];
|
---|
2864 | pChunk->paMappings[pChunk->cMappings].MapObj = NIL_RTR0MEMOBJ;
|
---|
2865 | pChunk->paMappings[pChunk->cMappings].pGVM = NULL;
|
---|
2866 | }
|
---|
2867 | return rc;
|
---|
2868 | }
|
---|
2869 | }
|
---|
2870 | }
|
---|
2871 | else if (pChunk->hGVM == pGVM->hSelf)
|
---|
2872 | return VINF_SUCCESS;
|
---|
2873 |
|
---|
2874 | Log(("gmmR0MapChunk: Chunk %#x is not mapped into pGVM=%p/%#x\n", pChunk->Core.Key, pGVM, pGVM->hSelf));
|
---|
2875 | return VERR_GMM_CHUNK_NOT_MAPPED;
|
---|
2876 | }
|
---|
2877 |
|
---|
2878 |
|
---|
2879 | /**
|
---|
2880 | * Maps a chunk into the user address space of the current process.
|
---|
2881 | *
|
---|
2882 | * @returns VBox status code.
|
---|
2883 | * @param pGMM Pointer to the GMM instance data.
|
---|
2884 | * @param pGVM Pointer to the Global VM structure.
|
---|
2885 | * @param pChunk Pointer to the chunk to be mapped.
|
---|
2886 | * @param ppvR3 Where to store the ring-3 address of the mapping.
|
---|
2887 | * In the VERR_GMM_CHUNK_ALREADY_MAPPED case, this will be
|
---|
2888 | * contain the address of the existing mapping.
|
---|
2889 | */
|
---|
2890 | static int gmmR0MapChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, PRTR3PTR ppvR3)
|
---|
2891 | {
|
---|
2892 | /*
|
---|
2893 | * If we're in legacy mode this is simple.
|
---|
2894 | */
|
---|
2895 | if (pGMM->fLegacyAllocationMode)
|
---|
2896 | {
|
---|
2897 | if (pChunk->hGVM != pGVM->hSelf)
|
---|
2898 | {
|
---|
2899 | Log(("gmmR0MapChunk: chunk %#x is already mapped at %p!\n", pChunk->Core.Key, *ppvR3));
|
---|
2900 | return VERR_GMM_CHUNK_NOT_FOUND;
|
---|
2901 | }
|
---|
2902 |
|
---|
2903 | *ppvR3 = RTR0MemObjAddressR3(pChunk->MemObj);
|
---|
2904 | return VINF_SUCCESS;
|
---|
2905 | }
|
---|
2906 |
|
---|
2907 | /*
|
---|
2908 | * Check to see if the chunk is already mapped.
|
---|
2909 | */
|
---|
2910 | for (uint32_t i = 0; i < pChunk->cMappings; i++)
|
---|
2911 | {
|
---|
2912 | Assert(pChunk->paMappings[i].pGVM && pChunk->paMappings[i].MapObj != NIL_RTR0MEMOBJ);
|
---|
2913 | if (pChunk->paMappings[i].pGVM == pGVM)
|
---|
2914 | {
|
---|
2915 | *ppvR3 = RTR0MemObjAddressR3(pChunk->paMappings[i].MapObj);
|
---|
2916 | Log(("gmmR0MapChunk: chunk %#x is already mapped at %p!\n", pChunk->Core.Key, *ppvR3));
|
---|
2917 | return VERR_GMM_CHUNK_ALREADY_MAPPED;
|
---|
2918 | }
|
---|
2919 | }
|
---|
2920 |
|
---|
2921 | /*
|
---|
2922 | * Do the mapping.
|
---|
2923 | */
|
---|
2924 | RTR0MEMOBJ MapObj;
|
---|
2925 | int rc = RTR0MemObjMapUser(&MapObj, pChunk->MemObj, (RTR3PTR)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
|
---|
2926 | if (RT_SUCCESS(rc))
|
---|
2927 | {
|
---|
2928 | /* reallocate the array? */
|
---|
2929 | if ((pChunk->cMappings & 1 /*7*/) == 0)
|
---|
2930 | {
|
---|
2931 | void *pvMappings = RTMemRealloc(pChunk->paMappings, (pChunk->cMappings + 2 /*8*/) * sizeof(pChunk->paMappings[0]));
|
---|
2932 | if (RT_UNLIKELY(!pvMappings))
|
---|
2933 | {
|
---|
2934 | rc = RTR0MemObjFree(MapObj, false /* fFreeMappings (NA) */);
|
---|
2935 | AssertRC(rc);
|
---|
2936 | return VERR_NO_MEMORY;
|
---|
2937 | }
|
---|
2938 | pChunk->paMappings = (PGMMCHUNKMAP)pvMappings;
|
---|
2939 | }
|
---|
2940 |
|
---|
2941 | /* insert new entry */
|
---|
2942 | pChunk->paMappings[pChunk->cMappings].MapObj = MapObj;
|
---|
2943 | pChunk->paMappings[pChunk->cMappings].pGVM = pGVM;
|
---|
2944 | pChunk->cMappings++;
|
---|
2945 |
|
---|
2946 | *ppvR3 = RTR0MemObjAddressR3(MapObj);
|
---|
2947 | }
|
---|
2948 |
|
---|
2949 | return rc;
|
---|
2950 | }
|
---|
2951 |
|
---|
2952 |
|
---|
2953 | /**
|
---|
2954 | * Map a chunk and/or unmap another chunk.
|
---|
2955 | *
|
---|
2956 | * The mapping and unmapping applies to the current process.
|
---|
2957 | *
|
---|
2958 | * This API does two things because it saves a kernel call per mapping when
|
---|
2959 | * when the ring-3 mapping cache is full.
|
---|
2960 | *
|
---|
2961 | * @returns VBox status code.
|
---|
2962 | * @param pVM The VM.
|
---|
2963 | * @param idCpu VCPU id
|
---|
2964 | * @param idChunkMap The chunk to map. NIL_GMM_CHUNKID if nothing to map.
|
---|
2965 | * @param idChunkUnmap The chunk to unmap. NIL_GMM_CHUNKID if nothing to unmap.
|
---|
2966 | * @param ppvR3 Where to store the address of the mapped chunk. NULL is ok if nothing to map.
|
---|
2967 | * @thread EMT
|
---|
2968 | */
|
---|
2969 | GMMR0DECL(int) GMMR0MapUnmapChunk(PVM pVM, VMCPUID idCpu, uint32_t idChunkMap, uint32_t idChunkUnmap, PRTR3PTR ppvR3)
|
---|
2970 | {
|
---|
2971 | LogFlow(("GMMR0MapUnmapChunk: pVM=%p idChunkMap=%#x idChunkUnmap=%#x ppvR3=%p\n",
|
---|
2972 | pVM, idChunkMap, idChunkUnmap, ppvR3));
|
---|
2973 |
|
---|
2974 | /*
|
---|
2975 | * Validate input and get the basics.
|
---|
2976 | */
|
---|
2977 | PGMM pGMM;
|
---|
2978 | GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
|
---|
2979 | PGVM pGVM;
|
---|
2980 | int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
|
---|
2981 | if (RT_FAILURE(rc))
|
---|
2982 | return rc;
|
---|
2983 |
|
---|
2984 | AssertCompile(NIL_GMM_CHUNKID == 0);
|
---|
2985 | AssertMsgReturn(idChunkMap <= GMM_CHUNKID_LAST, ("%#x\n", idChunkMap), VERR_INVALID_PARAMETER);
|
---|
2986 | AssertMsgReturn(idChunkUnmap <= GMM_CHUNKID_LAST, ("%#x\n", idChunkUnmap), VERR_INVALID_PARAMETER);
|
---|
2987 |
|
---|
2988 | if ( idChunkMap == NIL_GMM_CHUNKID
|
---|
2989 | && idChunkUnmap == NIL_GMM_CHUNKID)
|
---|
2990 | return VERR_INVALID_PARAMETER;
|
---|
2991 |
|
---|
2992 | if (idChunkMap != NIL_GMM_CHUNKID)
|
---|
2993 | {
|
---|
2994 | AssertPtrReturn(ppvR3, VERR_INVALID_POINTER);
|
---|
2995 | *ppvR3 = NIL_RTR3PTR;
|
---|
2996 | }
|
---|
2997 |
|
---|
2998 | /*
|
---|
2999 | * Take the semaphore and do the work.
|
---|
3000 | *
|
---|
3001 | * The unmapping is done last since it's easier to undo a mapping than
|
---|
3002 | * undoing an unmapping. The ring-3 mapping cache cannot not be so big
|
---|
3003 | * that it pushes the user virtual address space to within a chunk of
|
---|
3004 | * it it's limits, so, no problem here.
|
---|
3005 | */
|
---|
3006 | rc = RTSemFastMutexRequest(pGMM->Mtx);
|
---|
3007 | AssertRC(rc);
|
---|
3008 | if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
|
---|
3009 | {
|
---|
3010 | PGMMCHUNK pMap = NULL;
|
---|
3011 | if (idChunkMap != NIL_GVM_HANDLE)
|
---|
3012 | {
|
---|
3013 | pMap = gmmR0GetChunk(pGMM, idChunkMap);
|
---|
3014 | if (RT_LIKELY(pMap))
|
---|
3015 | rc = gmmR0MapChunk(pGMM, pGVM, pMap, ppvR3);
|
---|
3016 | else
|
---|
3017 | {
|
---|
3018 | Log(("GMMR0MapUnmapChunk: idChunkMap=%#x\n", idChunkMap));
|
---|
3019 | rc = VERR_GMM_CHUNK_NOT_FOUND;
|
---|
3020 | }
|
---|
3021 | }
|
---|
3022 |
|
---|
3023 | if ( idChunkUnmap != NIL_GMM_CHUNKID
|
---|
3024 | && RT_SUCCESS(rc))
|
---|
3025 | {
|
---|
3026 | PGMMCHUNK pUnmap = gmmR0GetChunk(pGMM, idChunkUnmap);
|
---|
3027 | if (RT_LIKELY(pUnmap))
|
---|
3028 | rc = gmmR0UnmapChunk(pGMM, pGVM, pUnmap);
|
---|
3029 | else
|
---|
3030 | {
|
---|
3031 | Log(("GMMR0MapUnmapChunk: idChunkUnmap=%#x\n", idChunkUnmap));
|
---|
3032 | rc = VERR_GMM_CHUNK_NOT_FOUND;
|
---|
3033 | }
|
---|
3034 |
|
---|
3035 | if (RT_FAILURE(rc) && pMap)
|
---|
3036 | gmmR0UnmapChunk(pGMM, pGVM, pMap);
|
---|
3037 | }
|
---|
3038 |
|
---|
3039 | GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
|
---|
3040 | }
|
---|
3041 | else
|
---|
3042 | rc = VERR_INTERNAL_ERROR_5;
|
---|
3043 | RTSemFastMutexRelease(pGMM->Mtx);
|
---|
3044 |
|
---|
3045 | LogFlow(("GMMR0MapUnmapChunk: returns %Rrc\n", rc));
|
---|
3046 | return rc;
|
---|
3047 | }
|
---|
3048 |
|
---|
3049 |
|
---|
3050 | /**
|
---|
3051 | * VMMR0 request wrapper for GMMR0MapUnmapChunk.
|
---|
3052 | *
|
---|
3053 | * @returns see GMMR0MapUnmapChunk.
|
---|
3054 | * @param pVM Pointer to the shared VM structure.
|
---|
3055 | * @param idCpu VCPU id
|
---|
3056 | * @param pReq The request packet.
|
---|
3057 | */
|
---|
3058 | GMMR0DECL(int) GMMR0MapUnmapChunkReq(PVM pVM, VMCPUID idCpu, PGMMMAPUNMAPCHUNKREQ pReq)
|
---|
3059 | {
|
---|
3060 | /*
|
---|
3061 | * Validate input and pass it on.
|
---|
3062 | */
|
---|
3063 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
3064 | AssertPtrReturn(pReq, VERR_INVALID_POINTER);
|
---|
3065 | AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
|
---|
3066 |
|
---|
3067 | return GMMR0MapUnmapChunk(pVM, idCpu, pReq->idChunkMap, pReq->idChunkUnmap, &pReq->pvR3);
|
---|
3068 | }
|
---|
3069 |
|
---|
3070 |
|
---|
3071 | /**
|
---|
3072 | * Legacy mode API for supplying pages.
|
---|
3073 | *
|
---|
3074 | * The specified user address points to a allocation chunk sized block that
|
---|
3075 | * will be locked down and used by the GMM when the GM asks for pages.
|
---|
3076 | *
|
---|
3077 | * @returns VBox status code.
|
---|
3078 | * @param pVM The VM.
|
---|
3079 | * @param idCpu VCPU id
|
---|
3080 | * @param pvR3 Pointer to the chunk size memory block to lock down.
|
---|
3081 | */
|
---|
3082 | GMMR0DECL(int) GMMR0SeedChunk(PVM pVM, VMCPUID idCpu, RTR3PTR pvR3)
|
---|
3083 | {
|
---|
3084 | /*
|
---|
3085 | * Validate input and get the basics.
|
---|
3086 | */
|
---|
3087 | PGMM pGMM;
|
---|
3088 | GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
|
---|
3089 | PGVM pGVM;
|
---|
3090 | int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
|
---|
3091 | if (RT_FAILURE(rc))
|
---|
3092 | return rc;
|
---|
3093 |
|
---|
3094 | AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
|
---|
3095 | AssertReturn(!(PAGE_OFFSET_MASK & pvR3), VERR_INVALID_POINTER);
|
---|
3096 |
|
---|
3097 | if (!pGMM->fLegacyAllocationMode)
|
---|
3098 | {
|
---|
3099 | Log(("GMMR0SeedChunk: not in legacy allocation mode!\n"));
|
---|
3100 | return VERR_NOT_SUPPORTED;
|
---|
3101 | }
|
---|
3102 |
|
---|
3103 | /*
|
---|
3104 | * Lock the memory before taking the semaphore.
|
---|
3105 | */
|
---|
3106 | RTR0MEMOBJ MemObj;
|
---|
3107 | rc = RTR0MemObjLockUser(&MemObj, pvR3, GMM_CHUNK_SIZE, NIL_RTR0PROCESS);
|
---|
3108 | if (RT_SUCCESS(rc))
|
---|
3109 | {
|
---|
3110 | /*
|
---|
3111 | * Add a new chunk with our hGVM.
|
---|
3112 | */
|
---|
3113 | rc = gmmR0RegisterChunk(pGMM, &pGMM->Private, MemObj, pGVM->hSelf);
|
---|
3114 | if (RT_FAILURE(rc))
|
---|
3115 | RTR0MemObjFree(MemObj, false /* fFreeMappings */);
|
---|
3116 | }
|
---|
3117 |
|
---|
3118 | LogFlow(("GMMR0SeedChunk: rc=%d (pvR3=%p)\n", rc, pvR3));
|
---|
3119 | return rc;
|
---|
3120 | }
|
---|
3121 |
|
---|