VirtualBox

source: vbox/trunk/include/VBox/gmm.h@ 5142

Last change on this file since 5142 was 5142, checked in by vboxsync, 18 years ago

More GMM code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.2 KB
Line 
1/** @file
2 * GMM - The Global Memory Manager.
3 */
4
5/*
6 * Copyright (C) 2007 InnoTek Systemberatung GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 */
17
18#ifndef ___VBox_gmm_h
19#define ___VBox_gmm_h
20
21#include <VBox/types.h>
22#include <VBox/gvmm.h>
23#include <VBox/sup.h>
24
25__BEGIN_DECLS
26
27/** @defgroup grp_gmm GMM - The Global Memory Manager
28 * @{
29 */
30
31/** @def IN_GMM_R0
32 * Used to indicate whether we're inside the same link module as the ring 0
33 * part of the Global Memory Manager or not.
34 */
35/** @def GMMR0DECL
36 * Ring 0 GMM export or import declaration.
37 * @param type The return type of the function declaration.
38 */
39#ifdef IN_GMM_R0
40# define GMMR0DECL(type) DECLEXPORT(type) VBOXCALL
41#else
42# define GMMR0DECL(type) DECLIMPORT(type) VBOXCALL
43#endif
44
45
46/** The chunk shift. (2^20 = 1 MB) */
47#define GMM_CHUNK_SHIFT 20
48/** The allocation chunk size. */
49#define GMM_CHUNK_SIZE (1U << GMM_CHUNK_SHIFT)
50/** The allocation chunk size in pages. */
51#define GMM_CHUNK_NUM_PAGES (1U << (GMM_CHUNK_SHIFT - PAGE_SHIFT))
52/** The shift factor for converting a page id into a chunk id. */
53#define GMM_CHUNKID_SHIFT (GMM_CHUNK_SHIFT - PAGE_SHIFT)
54/** The last valid Chunk ID value. */
55#define GMM_CHUNKID_LAST (GMM_PAGEID_LAST >> GMM_CHUNKID_SHIFT)
56/** The last valid Page ID value.
57 * The current limit is 2^28 - 1, or almost 1TB if you like.
58 * The constraints are currently dictated by PGMPAGE. */
59#define GMM_PAGEID_LAST (RT_BIT_32(28) - 1)
60/** The NIL Chunk ID value. */
61#define NIL_GMM_CHUNKID 0
62/** The NIL Page ID value. */
63#define NIL_GMM_PAGEID 0
64
65#if 0 /* wrong - these are guest page pfns and not page ids! */
66/** Special Page ID used by unassigned pages. */
67#define GMM_PAGEID_UNASSIGNED 0x0fffffffU
68/** Special Page ID used by unsharable pages.
69 * Like MMIO2, shadow and heap. This is for later, obviously. */
70#define GMM_PAGEID_UNSHARABLE 0x0ffffffeU
71/** The end of the valid Page IDs. This is the first special one. */
72#define GMM_PAGEID_END 0x0ffffff0U
73#endif
74
75
76/**
77 * Over-commitment policy.
78 */
79typedef enum GMMOCPOLICY
80{
81 /** The usual invalid 0 value. */
82 GMMOCPOLICY_INVALID = 0,
83 /** No over-commitment, fully backed.
84 * The GMM guarantees that it will be able to allocate all of the
85 * guest RAM for a VM with OC policy. */
86 GMMOCPOLICY_NO_OC,
87 /** to-be-determined. */
88 GMMOCPOLICY_TBD,
89 /** The end of the valid policy range. */
90 GMMOCPOLICY_END,
91 /** The usual 32-bit hack. */
92 GMMOCPOLICY_32BIT_HACK = 0x7fffffff
93} GMMOCPOLICY;
94
95/**
96 * VM / Memory priority.
97 */
98typedef enum GMMPRIORITY
99{
100 /** The usual invalid 0 value. */
101 GMMPRIORITY_INVALID = 0,
102 /** High.
103 * When ballooning, ask these VMs last.
104 * When running out of memory, try not to interrupt these VMs. */
105 GMMPRIORITY_HIGH,
106 /** Normal.
107 * When ballooning, don't wait to ask these.
108 * When running out of memory, pause, save and/or kill these VMs. */
109 GMMPRIORITY_NORMAL,
110 /** Low.
111 * When ballooning, maximize these first.
112 * When running out of memory, save or kill these VMs. */
113 GMMPRIORITY_LOW,
114 /** The end of the valid priority range. */
115 GMMPRIORITY_END = 0,
116 /** The custom 32-bit type blowup. */
117 GMMPRIORITY_32BIT_HACK = 0x7fffffff
118} GMMPRIORITY;
119
120
121/**
122 * GMM Memory Accounts.
123 */
124typedef enum GMMACCOUNT
125{
126 /** The customary invalid zero entry. */
127 GMMACCOUNT_INVALID = 0,
128 /** Account with the base allocations. */
129 GMMACCOUNT_BASE,
130 /** Account with the shadow allocations. */
131 GMMACCOUNT_SHADOW,
132 /** Account with the fixed allocations. */
133 GMMACCOUNT_FIXED,
134 /** The end of the valid values. */
135 GMMACCOUNT_END,
136 /** The usual 32-bit value to finish it off. */
137 GMMACCOUNT_32BIT_HACK = 0x7fffffff
138} GMMACCOUNT;
139
140
141/**
142 * A page descriptor for use when freeing pages.
143 * See GMMR0FreePages, GMMR0BalloonedPages.
144 */
145typedef struct GMMFREEPAGEDESC
146{
147 /** The Page ID of the page to be freed. */
148 uint32_t idPage;
149} GMMFREEPAGEDESC;
150/** Pointer to a page descriptor for freeing pages. */
151typedef GMMFREEPAGEDESC *PGMMFREEPAGEDESC;
152
153
154/**
155 * A page descriptor for use when updating and allocating pages.
156 *
157 * This is a bit complicated because we want to do as much as possible
158 * with the same structure.
159 */
160typedef struct GMMPAGEDESC
161{
162 /** The physical address of the page.
163 *
164 * @input GMMR0AllocateHandyPages expects the guest physical address
165 * to update the GMMPAGE structure with. Pass GMM_GCPHYS_UNSHAREABLE
166 * when appropriate and NIL_RTHCPHYS when the page wasn't used
167 * for any specific guest address.
168 *
169 * GMMR0AllocatePage expects the guest physical address to put in
170 * the GMMPAGE structure for the page it allocates for this entry.
171 * Pass NIL_RTHCPHYS and GMM_GCPHYS_UNSHAREABLE as above.
172 *
173 * @output The host physical address of the allocated page.
174 * NIL_RTHCPHYS on allocation failure.
175 *
176 * ASSUMES: sizeof(RTHCPHYS) >= sizeof(RTGCPHYS).
177 */
178 RTHCPHYS HCPhysGCPhys;
179
180 /** The Page ID.
181 *
182 * @intput GMMR0AllocateHandyPages expects the Page ID of the page to
183 * update here. NIL_GMM_PAGEID means no page should be updated.
184 *
185 * GMMR0AllocatePages requires this to be initialized to
186 * NIL_GMM_PAGEID currently.
187 *
188 * @output The ID of the page, NIL_GMM_PAGEID if the allocation failed.
189 */
190 uint32_t idPage;
191
192 /** The Page ID of the shared page was replaced by this page.
193 *
194 * @input GMMR0AllocateHandyPages expects this to indicate a shared
195 * page that has been replaced by this page and should have its
196 * reference counter decremented and perhaps be freed up. Use
197 * NIL_GMM_PAGEID if no shared page was involved.
198 *
199 * All other APIs expects NIL_GMM_PAGEID here.
200 *
201 * @output All APIs sets this to NIL_GMM_PAGEID.
202 */
203 uint32_t idSharedPage;
204} GMMPAGEDESC;
205AssertCompileSize(GMMPAGEDESC, 16);
206/** Pointer to a page allocation. */
207typedef GMMPAGEDESC *PGMMPAGEDESC;
208
209/** GMMPAGEDESC::HCPhysGCPhys value that indicates that the page is shared. */
210#define GMM_GCPHYS_UNSHAREABLE (RTHCPHYS)(0xfffffff0)
211
212GMMR0DECL(int) GMMR0Init(void);
213GMMR0DECL(void) GMMR0Term(void);
214GMMR0DECL(void) GMMR0InitPerVMData(PGVM pGVM);
215GMMR0DECL(void) GMMR0CleanupVM(PGVM pGVM);
216GMMR0DECL(int) GMMR0InitialReservation(PVM pVM, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages,
217 GMMOCPOLICY enmPolicy, GMMPRIORITY enmPriority);
218GMMR0DECL(int) GMMR0UpdateReservation(PVM pVM, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages);
219GMMR0DECL(int) GMMR0AllocateHandyPages(PVM pVM, uint32_t cPagesToUpdate, uint32_t cPagesToAlloc, PGMMPAGEDESC paPages);
220GMMR0DECL(int) GMMR0AllocatePages(PVM pVM, uint32_t cPages, PGMMPAGEDESC paPages, GMMACCOUNT enmAccount);
221GMMR0DECL(int) GMMR0FreePages(PVM pVM, uint32_t cPages, PGMMFREEPAGEDESC paPages, GMMACCOUNT enmAccount);
222GMMR0DECL(int) GMMR0BalloonedPages(PVM pVM, uint32_t cBalloonedPages, uint32_t cPagesToFree, PGMMFREEPAGEDESC paPages, bool fCompleted);
223GMMR0DECL(int) GMMR0DeflatedBalloon(PVM pVM, uint32_t cPages);
224GMMR0DECL(int) GMMR0MapUnmapChunk(PVM pVM, uint32_t idChunkMap, uint32_t idChunkUnmap, PRTR3PTR ppvR3);
225GMMR0DECL(int) GMMR0SeedChunk(PVM pVM, RTR3PTR pvR3);
226
227
228/**
229 * Request buffer for GMMR0InitialReservationReq / VMMR0_DO_GMM_INITIAL_RESERVATION.
230 * @see GMMR0InitialReservation
231 */
232typedef struct GMMINITIALRESERVATIONREQ
233{
234 /** The header. */
235 SUPVMMR0REQHDR Hdr;
236 uint64_t cBasePages; /**< @see GMMR0InitialReservation */
237 uint32_t cShadowPages; /**< @see GMMR0InitialReservation */
238 uint32_t cFixedPages; /**< @see GMMR0InitialReservation */
239 GMMOCPOLICY enmPolicy; /**< @see GMMR0InitialReservation */
240 GMMPRIORITY enmPriority; /**< @see GMMR0InitialReservation */
241} GMMINITIALRESERVATIONREQ;
242/** Pointer to a GMMR0InitialReservationReq / VMMR0_DO_GMM_INITIAL_RESERVATION request buffer. */
243typedef GMMINITIALRESERVATIONREQ *PGMMINITIALRESERVATIONREQ;
244
245GMMR0DECL(int) GMMR0InitialReservationReq(PVM pVM, PGMMINITIALRESERVATIONREQ pReq);
246
247
248/**
249 * Request buffer for GMMR0UpdateReservationReq / VMMR0_DO_GMM_UPDATE_RESERVATION.
250 * @see GMMR0UpdateReservation
251 */
252typedef struct GMMUPDATERESERVATIONREQ
253{
254 /** The header. */
255 SUPVMMR0REQHDR Hdr;
256 uint64_t cBasePages; /**< @see GMMR0UpdateReservation */
257 uint32_t cShadowPages; /**< @see GMMR0UpdateReservation */
258 uint32_t cFixedPages; /**< @see GMMR0UpdateReservation */
259} GMMUPDATERESERVATIONREQ;
260/** Pointer to a GMMR0InitialReservationReq / VMMR0_DO_GMM_INITIAL_RESERVATION request buffer. */
261typedef GMMUPDATERESERVATIONREQ *PGMMUPDATERESERVATIONREQ;
262
263GMMR0DECL(int) GMMR0UpdateReservationReq(PVM pVM, PGMMUPDATERESERVATIONREQ pReq);
264
265
266/**
267 * Request buffer for GMMR0AllocatePagesReq / VMMR0_DO_GMM_ALLOCATE_PAGES.
268 * @see GMMR0AllocatePages.
269 */
270typedef struct GMMALLOCATEPAGESREQ
271{
272 /** The header. */
273 SUPVMMR0REQHDR Hdr;
274 /** The account to charge the allocation to. */
275 GMMACCOUNT enmAccount;
276 /** The number of pages to allocate. */
277 uint32_t cPages;
278 /** Array of page descriptors. */
279 GMMPAGEDESC aPages[1];
280} GMMALLOCATEPAGESREQ;
281/** Pointer to a GMMR0AllocatePagesReq / VMMR0_DO_GMM_ALLOCATE_PAGES request buffer. */
282typedef GMMALLOCATEPAGESREQ *PGMMALLOCATEPAGESREQ;
283
284GMMR0DECL(int) GMMR0AllocatePagesReq(PVM pVM, PGMMALLOCATEPAGESREQ pReq);
285
286
287/**
288 * Request buffer for GMMR0FreePagesReq / VMMR0_DO_GMM_FREE_PAGES.
289 * @see GMMR0FreePages.
290 */
291typedef struct GMMFREEPAGESREQ
292{
293 /** The header. */
294 SUPVMMR0REQHDR Hdr;
295 /** The account this relates to. */
296 GMMACCOUNT enmAccount;
297 /** The number of pages to free. */
298 uint32_t cPages;
299 /** Array of free page descriptors. */
300 GMMFREEPAGEDESC aPages[1];
301} GMMFREEPAGESREQ;
302/** Pointer to a GMMR0FreePagesReq / VMMR0_DO_GMM_FREE_PAGES request buffer. */
303typedef GMMFREEPAGESREQ *PGMMFREEPAGESREQ;
304
305GMMR0DECL(int) GMMR0FreePagesReq(PVM pVM, PGMMFREEPAGESREQ pReq);
306
307
308/**
309 * Request buffer for GMMR0BalloonedPagesReq / VMMR0_DO_GMM_BALLOONED_PAGES.
310 * @see GMMR0BalloonedPages.
311 */
312typedef struct GMMBALLOONEDPAGESREQ
313{
314 /** The header. */
315 SUPVMMR0REQHDR Hdr;
316 /** The number of ballooned pages. */
317 uint32_t cBalloonedPages;
318 /** The number of pages to free. */
319 uint32_t cPagesToFree;
320 /** Whether the ballooning request is completed or more pages are still to come. */
321 bool fCompleted;
322 /** Array of free page descriptors. */
323 GMMFREEPAGEDESC aPages[1];
324} GMMBALLOONEDPAGESREQ;
325/** Pointer to a GMMR0BalloonedPagesReq / VMMR0_DO_GMM_BALLOONED_PAGES request buffer. */
326typedef GMMBALLOONEDPAGESREQ *PGMMBALLOONEDPAGESREQ;
327
328GMMR0DECL(int) GMMR0BalloonedPagesReq(PVM pVM, PGMMBALLOONEDPAGESREQ pReq);
329
330
331/**
332 * Request buffer for GMMR0MapUnmapChunkReq / VMMR0_DO_GMM_MAP_UNMAP_CHUNK.
333 * @see GMMR0MapUnmapChunk
334 */
335typedef struct GMMMAPUNMAPCHUNKREQ
336{
337 /** The header. */
338 SUPVMMR0REQHDR Hdr;
339 /** The chunk to map, UINT32_MAX if unmap only. (IN) */
340 uint32_t idChunkMap;
341 /** The chunk to unmap, UINT32_MAX if map only. (IN) */
342 uint32_t idChunkUnmap;
343 /** Where the mapping address is returned. (OUT) */
344 RTR3PTR pvR3;
345} GMMMAPUNMAPCHUNKREQ;
346/** Pointer to a GMMR0MapUnmapChunkReq / VMMR0_DO_GMM_MAP_UNMAP_CHUNK request buffer. */
347typedef GMMMAPUNMAPCHUNKREQ *PGMMMAPUNMAPCHUNKREQ;
348
349GMMR0DECL(int) GMMR0MapUnmapChunkReq(PVM pVM, PGMMMAPUNMAPCHUNKREQ pReq);
350
351
352/** @} */
353
354__END_DECLS
355
356#endif
357
Note: See TracBrowser for help on using the repository browser.

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