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