VirtualBox

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

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

Hooked up the GMM code.

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