1 | /* $Id: GMM.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * GMM - Global Memory Manager, ring-3 request wrappers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_GMM
|
---|
27 | #include <VBox/gmm.h>
|
---|
28 | #include <VBox/vmm.h>
|
---|
29 | #include <VBox/vm.h>
|
---|
30 | #include <VBox/sup.h>
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <VBox/param.h>
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 | #include <iprt/mem.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * @see GMMR0InitialReservation
|
---|
41 | */
|
---|
42 | GMMR3DECL(int) GMMR3InitialReservation(PVM pVM, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages,
|
---|
43 | GMMOCPOLICY enmPolicy, GMMPRIORITY enmPriority)
|
---|
44 | {
|
---|
45 | GMMINITIALRESERVATIONREQ Req;
|
---|
46 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
47 | Req.Hdr.cbReq = sizeof(Req);
|
---|
48 | Req.cBasePages = cBasePages;
|
---|
49 | Req.cShadowPages = cShadowPages;
|
---|
50 | Req.cFixedPages = cFixedPages;
|
---|
51 | Req.enmPolicy = enmPolicy;
|
---|
52 | Req.enmPriority = enmPriority;
|
---|
53 | return SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_INITIAL_RESERVATION, 0, &Req.Hdr);
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * @see GMMR0UpdateReservation
|
---|
59 | */
|
---|
60 | GMMR3DECL(int) GMMR3UpdateReservation(PVM pVM, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages)
|
---|
61 | {
|
---|
62 | GMMUPDATERESERVATIONREQ Req;
|
---|
63 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
64 | Req.Hdr.cbReq = sizeof(Req);
|
---|
65 | Req.cBasePages = cBasePages;
|
---|
66 | Req.cShadowPages = cShadowPages;
|
---|
67 | Req.cFixedPages = cFixedPages;
|
---|
68 | return SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_UPDATE_RESERVATION, 0, &Req.Hdr);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Prepares a GMMR0AllocatePages request.
|
---|
74 | *
|
---|
75 | * @returns VINF_SUCCESS or VERR_NO_TMP_MEMORY.
|
---|
76 | * @param pVM Pointer to the shared VM structure.
|
---|
77 | * @param[out] ppReq Where to store the pointer to the request packet.
|
---|
78 | * @param cPages The number of pages that's to be allocated.
|
---|
79 | * @param enmAccount The account to charge.
|
---|
80 | */
|
---|
81 | GMMR3DECL(int) GMMR3AllocatePagesPrepare(PVM pVM, PGMMALLOCATEPAGESREQ *ppReq, uint32_t cPages, GMMACCOUNT enmAccount)
|
---|
82 | {
|
---|
83 | uint32_t cb = RT_OFFSETOF(GMMALLOCATEPAGESREQ, aPages[cPages]);
|
---|
84 | PGMMALLOCATEPAGESREQ pReq = (PGMMALLOCATEPAGESREQ)RTMemTmpAllocZ(cb);
|
---|
85 | if (!pReq)
|
---|
86 | return VERR_NO_TMP_MEMORY;
|
---|
87 |
|
---|
88 | pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
89 | pReq->Hdr.cbReq = cb;
|
---|
90 | pReq->enmAccount = enmAccount;
|
---|
91 | pReq->cPages = cPages;
|
---|
92 | NOREF(pVM);
|
---|
93 | return VINF_SUCCESS;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Performs a GMMR0AllocatePages request.
|
---|
99 | * This will call VMSetError on failure.
|
---|
100 | *
|
---|
101 | * @returns VBox status code.
|
---|
102 | * @param pVM Pointer to the shared VM structure.
|
---|
103 | * @param pReq Pointer to the request (returned by GMMR3AllocatePagesPrepare).
|
---|
104 | */
|
---|
105 | GMMR3DECL(int) GMMR3AllocatePagesPerform(PVM pVM, PGMMALLOCATEPAGESREQ pReq)
|
---|
106 | {
|
---|
107 | for (unsigned i = 0; ; i++)
|
---|
108 | {
|
---|
109 | int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_ALLOCATE_PAGES, 0, &pReq->Hdr);
|
---|
110 | if (RT_SUCCESS(rc))
|
---|
111 | return rc;
|
---|
112 | if (rc != VERR_GMM_SEED_ME)
|
---|
113 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
114 | N_("GMMR0AllocatePages failed to allocate %u pages"),
|
---|
115 | pReq->cPages);
|
---|
116 | Assert(i < pReq->cPages);
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Seed another chunk.
|
---|
120 | */
|
---|
121 | void *pvChunk;
|
---|
122 | rc = SUPPageAlloc(GMM_CHUNK_SIZE >> PAGE_SHIFT, &pvChunk);
|
---|
123 | if (VBOX_FAILURE(rc))
|
---|
124 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
125 | N_("Out of memory (SUPPageAlloc) seeding a %u pages allocation request"),
|
---|
126 | pReq->cPages);
|
---|
127 |
|
---|
128 | rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_SEED_CHUNK, (uintptr_t)pvChunk, NULL);
|
---|
129 | if (RT_FAILURE(rc))
|
---|
130 | return VMSetError(pVM, rc, RT_SRC_POS, N_("GMM seeding failed"));
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Cleans up a GMMR0AllocatePages request.
|
---|
137 | * @param pReq Pointer to the request (returned by GMMR3AllocatePagesPrepare).
|
---|
138 | */
|
---|
139 | GMMR3DECL(void) GMMR3AllocatePagesCleanup(PGMMALLOCATEPAGESREQ pReq)
|
---|
140 | {
|
---|
141 | RTMemTmpFree(pReq);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Prepares a GMMR0FreePages request.
|
---|
147 | *
|
---|
148 | * @returns VINF_SUCCESS or VERR_NO_TMP_MEMORY.
|
---|
149 | * @param pVM Pointer to the shared VM structure.
|
---|
150 | * @param[out] ppReq Where to store the pointer to the request packet.
|
---|
151 | * @param cPages The number of pages that's to be freed.
|
---|
152 | * @param enmAccount The account to charge.
|
---|
153 | */
|
---|
154 | GMMR3DECL(int) GMMR3FreePagesPrepare(PVM pVM, PGMMFREEPAGESREQ *ppReq, uint32_t cPages, GMMACCOUNT enmAccount)
|
---|
155 | {
|
---|
156 | uint32_t cb = RT_OFFSETOF(GMMFREEPAGESREQ, aPages[cPages]);
|
---|
157 | PGMMFREEPAGESREQ pReq = (PGMMFREEPAGESREQ)RTMemTmpAllocZ(cb);
|
---|
158 | if (!pReq)
|
---|
159 | return VERR_NO_TMP_MEMORY;
|
---|
160 |
|
---|
161 | pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
162 | pReq->Hdr.cbReq = cb;
|
---|
163 | pReq->enmAccount = enmAccount;
|
---|
164 | pReq->cPages = cPages;
|
---|
165 | NOREF(pVM);
|
---|
166 | return VINF_SUCCESS;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Performs a GMMR0FreePages request.
|
---|
172 | * This will call VMSetError on failure.
|
---|
173 | *
|
---|
174 | * @returns VBox status code.
|
---|
175 | * @param pVM Pointer to the shared VM structure.
|
---|
176 | * @param pReq Pointer to the request (returned by GMMR3FreePagesPrepare).
|
---|
177 | */
|
---|
178 | GMMR3DECL(int) GMMR3FreePagesPerform(PVM pVM, PGMMFREEPAGESREQ pReq)
|
---|
179 | {
|
---|
180 | int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_FREE_PAGES, 0, &pReq->Hdr);
|
---|
181 | if (RT_SUCCESS(rc))
|
---|
182 | return rc;
|
---|
183 | AssertRC(rc);
|
---|
184 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
185 | N_("GMMR0FreePages failed to free %u pages"),
|
---|
186 | pReq->cPages);
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Cleans up a GMMR0FreePages request.
|
---|
192 | * @param pReq Pointer to the request (returned by GMMR3FreePagesPrepare).
|
---|
193 | */
|
---|
194 | GMMR3DECL(void) GMMR3FreePagesCleanup(PGMMFREEPAGESREQ pReq)
|
---|
195 | {
|
---|
196 | RTMemTmpFree(pReq);
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Frees allocated pages, for bailing out on failure.
|
---|
202 | *
|
---|
203 | * This will not call VMSetError on failure but will use AssertLogRel instead.
|
---|
204 | *
|
---|
205 | * @param pVM Pointer to the shared VM structure.
|
---|
206 | * @param pAllocReq The allocation request to undo.
|
---|
207 | */
|
---|
208 | GMMR3DECL(void) GMMR3FreeAllocatedPages(PVM pVM, GMMALLOCATEPAGESREQ const *pAllocReq)
|
---|
209 | {
|
---|
210 | uint32_t cb = RT_OFFSETOF(GMMFREEPAGESREQ, aPages[pAllocReq->cPages]);
|
---|
211 | PGMMFREEPAGESREQ pReq = (PGMMFREEPAGESREQ)RTMemTmpAllocZ(cb);
|
---|
212 | AssertLogRelReturnVoid(pReq);
|
---|
213 |
|
---|
214 | pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
215 | pReq->Hdr.cbReq = cb;
|
---|
216 | pReq->enmAccount = pAllocReq->enmAccount;
|
---|
217 | pReq->cPages = pAllocReq->cPages;
|
---|
218 | uint32_t iPage = pAllocReq->cPages;
|
---|
219 | while (iPage-- > 0)
|
---|
220 | {
|
---|
221 | Assert(pAllocReq->aPages[iPage].idPage != NIL_GMM_PAGEID);
|
---|
222 | pReq->aPages[iPage].idPage = pAllocReq->aPages[iPage].idPage;
|
---|
223 | }
|
---|
224 |
|
---|
225 | int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_FREE_PAGES, 0, &pReq->Hdr);
|
---|
226 | AssertLogRelRC(rc);
|
---|
227 |
|
---|
228 | RTMemTmpFree(pReq);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | #if 0 /* impractical */
|
---|
233 | GMMR3DECL(int) GMMR3BalloonedPages(PVM pVM, uint32_t cBalloonedPages, uint32_t cPagesToFree, PGMMFREEPAGEDESC paPages, bool fCompleted)
|
---|
234 | {
|
---|
235 | GMMBALLOONEDPAGESREQ Req;
|
---|
236 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
237 | Req.Hdr.cbReq = sizeof(Req);
|
---|
238 |
|
---|
239 | return SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_BALLOONED_PAGES, 0, &Req.Hdr);
|
---|
240 | }
|
---|
241 | #endif
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * @see GMMR0DeflatedBalloon
|
---|
246 | */
|
---|
247 | GMMR3DECL(int) GMMR3DeflatedBalloon(PVM pVM, uint32_t cPages)
|
---|
248 | {
|
---|
249 | return SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_DEFLATED_BALLOON, cPages, NULL);
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * @see GMMR0MapUnmapChunk
|
---|
255 | */
|
---|
256 | GMMR3DECL(int) GMMR3MapUnmapChunk(PVM pVM, uint32_t idChunkMap, uint32_t idChunkUnmap, PRTR3PTR ppvR3)
|
---|
257 | {
|
---|
258 | GMMMAPUNMAPCHUNKREQ Req;
|
---|
259 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
260 | Req.Hdr.cbReq = sizeof(Req);
|
---|
261 | Req.idChunkMap = idChunkMap;
|
---|
262 | Req.idChunkUnmap = idChunkUnmap;
|
---|
263 | Req.pvR3 = NULL;
|
---|
264 | int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_MAP_UNMAP_CHUNK, 0, &Req.Hdr);
|
---|
265 | if (RT_SUCCESS(rc) && ppvR3)
|
---|
266 | *ppvR3 = Req.pvR3;
|
---|
267 | return rc;
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * @see GMMR0SeedChunk
|
---|
273 | */
|
---|
274 | GMMR3DECL(int) GMMR3SeedChunk(PVM pVM, RTR3PTR pvR3)
|
---|
275 | {
|
---|
276 | return SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_SEED_CHUNK, (uintptr_t)pvR3, NULL);
|
---|
277 | }
|
---|
278 |
|
---|