VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/GMMR0.cpp@ 5999

Last change on this file since 5999 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette