VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/GMM.cpp@ 102869

Last change on this file since 102869 was 99739, checked in by vboxsync, 19 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.6 KB
Line 
1/* $Id: GMM.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
2/** @file
3 * GMM - Global Memory Manager, ring-3 request wrappers.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_GMM
33#include <VBox/vmm/gmm.h>
34#include <VBox/vmm/vmm.h>
35#include <VBox/vmm/vmcc.h>
36#include <VBox/sup.h>
37#include <VBox/err.h>
38#include <VBox/param.h>
39
40#include <iprt/assert.h>
41#include <VBox/log.h>
42#include <iprt/mem.h>
43#include <iprt/string.h>
44
45
46/**
47 * @see GMMR0InitialReservation
48 */
49GMMR3DECL(int) GMMR3InitialReservation(PVM pVM, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages,
50 GMMOCPOLICY enmPolicy, GMMPRIORITY enmPriority)
51{
52 if (!SUPR3IsDriverless())
53 {
54 GMMINITIALRESERVATIONREQ Req;
55 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
56 Req.Hdr.cbReq = sizeof(Req);
57 Req.cBasePages = cBasePages;
58 Req.cShadowPages = cShadowPages;
59 Req.cFixedPages = cFixedPages;
60 Req.enmPolicy = enmPolicy;
61 Req.enmPriority = enmPriority;
62 return VMMR3CallR0(pVM, VMMR0_DO_GMM_INITIAL_RESERVATION, 0, &Req.Hdr);
63 }
64 return VINF_SUCCESS;
65}
66
67
68/**
69 * @see GMMR0UpdateReservation
70 */
71GMMR3DECL(int) GMMR3UpdateReservation(PVM pVM, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages)
72{
73 if (!SUPR3IsDriverless())
74 {
75 GMMUPDATERESERVATIONREQ Req;
76 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
77 Req.Hdr.cbReq = sizeof(Req);
78 Req.cBasePages = cBasePages;
79 Req.cShadowPages = cShadowPages;
80 Req.cFixedPages = cFixedPages;
81 return VMMR3CallR0(pVM, VMMR0_DO_GMM_UPDATE_RESERVATION, 0, &Req.Hdr);
82 }
83 return VINF_SUCCESS;
84}
85
86
87/**
88 * Prepares a GMMR0AllocatePages request.
89 *
90 * @returns VINF_SUCCESS or VERR_NO_TMP_MEMORY.
91 * @param pVM The cross context VM structure.
92 * @param[out] ppReq Where to store the pointer to the request packet.
93 * @param cPages The number of pages that's to be allocated.
94 * @param enmAccount The account to charge.
95 */
96GMMR3DECL(int) GMMR3AllocatePagesPrepare(PVM pVM, PGMMALLOCATEPAGESREQ *ppReq, uint32_t cPages, GMMACCOUNT enmAccount)
97{
98 uint32_t cb = RT_UOFFSETOF_DYN(GMMALLOCATEPAGESREQ, aPages[cPages]);
99 PGMMALLOCATEPAGESREQ pReq = (PGMMALLOCATEPAGESREQ)RTMemTmpAllocZ(cb);
100 if (!pReq)
101 return VERR_NO_TMP_MEMORY;
102
103 pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
104 pReq->Hdr.cbReq = cb;
105 pReq->enmAccount = enmAccount;
106 pReq->cPages = cPages;
107 NOREF(pVM);
108 *ppReq = pReq;
109 return VINF_SUCCESS;
110}
111
112
113/**
114 * Performs a GMMR0AllocatePages request.
115 *
116 * This will call VMSetError on failure.
117 *
118 * @returns VBox status code.
119 * @param pVM The cross context VM structure.
120 * @param pReq Pointer to the request (returned by GMMR3AllocatePagesPrepare).
121 */
122GMMR3DECL(int) GMMR3AllocatePagesPerform(PVM pVM, PGMMALLOCATEPAGESREQ pReq)
123{
124 int rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_ALLOCATE_PAGES, 0, &pReq->Hdr);
125 if (RT_SUCCESS(rc))
126 {
127#ifdef LOG_ENABLED
128 for (uint32_t iPage = 0; iPage < pReq->cPages; iPage++)
129 Log3(("GMMR3AllocatePagesPerform: idPage=%#x HCPhys=%RHp fZeroed=%d\n",
130 pReq->aPages[iPage].idPage, pReq->aPages[iPage].HCPhysGCPhys, pReq->aPages[iPage].fZeroed));
131#endif
132 return rc;
133 }
134 return VMSetError(pVM, rc, RT_SRC_POS, N_("GMMR0AllocatePages failed to allocate %u pages"), pReq->cPages);
135}
136
137
138/**
139 * Cleans up a GMMR0AllocatePages request.
140 * @param pReq Pointer to the request (returned by GMMR3AllocatePagesPrepare).
141 */
142GMMR3DECL(void) GMMR3AllocatePagesCleanup(PGMMALLOCATEPAGESREQ pReq)
143{
144 RTMemTmpFree(pReq);
145}
146
147
148/**
149 * Prepares a GMMR0FreePages request.
150 *
151 * @returns VINF_SUCCESS or VERR_NO_TMP_MEMORY.
152 * @param pVM The cross context VM structure.
153 * @param[out] ppReq Where to store the pointer to the request packet.
154 * @param cPages The number of pages that's to be freed.
155 * @param enmAccount The account to charge.
156 */
157GMMR3DECL(int) GMMR3FreePagesPrepare(PVM pVM, PGMMFREEPAGESREQ *ppReq, uint32_t cPages, GMMACCOUNT enmAccount)
158{
159 uint32_t cb = RT_UOFFSETOF_DYN(GMMFREEPAGESREQ, aPages[cPages]);
160 PGMMFREEPAGESREQ pReq = (PGMMFREEPAGESREQ)RTMemTmpAllocZ(cb);
161 if (!pReq)
162 return VERR_NO_TMP_MEMORY;
163
164 pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
165 pReq->Hdr.cbReq = cb;
166 pReq->enmAccount = enmAccount;
167 pReq->cPages = cPages;
168 NOREF(pVM);
169 *ppReq = pReq;
170 return VINF_SUCCESS;
171}
172
173
174/**
175 * Re-prepares a GMMR0FreePages request.
176 *
177 * @param pVM The cross context VM structure.
178 * @param pReq A request buffer previously returned by
179 * GMMR3FreePagesPrepare().
180 * @param cPages The number of pages originally passed to
181 * GMMR3FreePagesPrepare().
182 * @param enmAccount The account to charge.
183 */
184GMMR3DECL(void) GMMR3FreePagesRePrep(PVM pVM, PGMMFREEPAGESREQ pReq, uint32_t cPages, GMMACCOUNT enmAccount)
185{
186 Assert(pReq->Hdr.u32Magic == SUPVMMR0REQHDR_MAGIC);
187 pReq->Hdr.cbReq = RT_UOFFSETOF_DYN(GMMFREEPAGESREQ, aPages[cPages]);
188 pReq->enmAccount = enmAccount;
189 pReq->cPages = cPages;
190 NOREF(pVM);
191}
192
193
194/**
195 * Performs a GMMR0FreePages request.
196 * This will call VMSetError on failure.
197 *
198 * @returns VBox status code.
199 * @param pVM The cross context VM structure.
200 * @param pReq Pointer to the request (returned by GMMR3FreePagesPrepare).
201 * @param cActualPages The number of pages actually freed.
202 */
203GMMR3DECL(int) GMMR3FreePagesPerform(PVM pVM, PGMMFREEPAGESREQ pReq, uint32_t cActualPages)
204{
205 /*
206 * Adjust the request if we ended up with fewer pages than anticipated.
207 */
208 if (cActualPages != pReq->cPages)
209 {
210 AssertReturn(cActualPages < pReq->cPages, VERR_GMM_ACTUAL_PAGES_IPE);
211 if (!cActualPages)
212 return VINF_SUCCESS;
213 pReq->cPages = cActualPages;
214 pReq->Hdr.cbReq = RT_UOFFSETOF_DYN(GMMFREEPAGESREQ, aPages[cActualPages]);
215 }
216
217 /*
218 * Do the job.
219 */
220 int rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_FREE_PAGES, 0, &pReq->Hdr);
221 if (RT_SUCCESS(rc))
222 return rc;
223 AssertRC(rc);
224 return VMSetError(pVM, rc, RT_SRC_POS,
225 N_("GMMR0FreePages failed to free %u pages"),
226 pReq->cPages);
227}
228
229
230/**
231 * Cleans up a GMMR0FreePages request.
232 * @param pReq Pointer to the request (returned by GMMR3FreePagesPrepare).
233 */
234GMMR3DECL(void) GMMR3FreePagesCleanup(PGMMFREEPAGESREQ pReq)
235{
236 RTMemTmpFree(pReq);
237}
238
239
240/**
241 * Frees allocated pages, for bailing out on failure.
242 *
243 * This will not call VMSetError on failure but will use AssertLogRel instead.
244 *
245 * @param pVM The cross context VM structure.
246 * @param pAllocReq The allocation request to undo.
247 */
248GMMR3DECL(void) GMMR3FreeAllocatedPages(PVM pVM, GMMALLOCATEPAGESREQ const *pAllocReq)
249{
250 uint32_t cb = RT_UOFFSETOF_DYN(GMMFREEPAGESREQ, aPages[pAllocReq->cPages]);
251 PGMMFREEPAGESREQ pReq = (PGMMFREEPAGESREQ)RTMemTmpAllocZ(cb);
252 AssertLogRelReturnVoid(pReq);
253
254 pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
255 pReq->Hdr.cbReq = cb;
256 pReq->enmAccount = pAllocReq->enmAccount;
257 pReq->cPages = pAllocReq->cPages;
258 uint32_t iPage = pAllocReq->cPages;
259 while (iPage-- > 0)
260 {
261 Assert(pAllocReq->aPages[iPage].idPage != NIL_GMM_PAGEID);
262 pReq->aPages[iPage].idPage = pAllocReq->aPages[iPage].idPage;
263 }
264
265 int rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_FREE_PAGES, 0, &pReq->Hdr);
266 AssertLogRelRC(rc);
267
268 RTMemTmpFree(pReq);
269}
270
271
272/**
273 * @see GMMR0BalloonedPages
274 */
275GMMR3DECL(int) GMMR3BalloonedPages(PVM pVM, GMMBALLOONACTION enmAction, uint32_t cBalloonedPages)
276{
277 int rc;
278 if (!SUPR3IsDriverless())
279 {
280 GMMBALLOONEDPAGESREQ Req;
281 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
282 Req.Hdr.cbReq = sizeof(Req);
283 Req.enmAction = enmAction;
284 Req.cBalloonedPages = cBalloonedPages;
285
286 rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_BALLOONED_PAGES, 0, &Req.Hdr);
287 }
288 /*
289 * Ignore reset and fail all other requests.
290 */
291 else if (enmAction == GMMBALLOONACTION_RESET && cBalloonedPages == 0)
292 rc = VINF_SUCCESS;
293 else
294 rc = VERR_SUP_DRIVERLESS;
295 return rc;
296}
297
298
299/**
300 * @note Caller does the driverless check.
301 * @see GMMR0QueryVMMMemoryStatsReq
302 */
303GMMR3DECL(int) GMMR3QueryHypervisorMemoryStats(PVM pVM, uint64_t *pcTotalAllocPages, uint64_t *pcTotalFreePages, uint64_t *pcTotalBalloonPages, uint64_t *puTotalBalloonSize)
304{
305 GMMMEMSTATSREQ Req;
306 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
307 Req.Hdr.cbReq = sizeof(Req);
308 Req.cAllocPages = 0;
309 Req.cFreePages = 0;
310 Req.cBalloonedPages = 0;
311 Req.cSharedPages = 0;
312
313 *pcTotalAllocPages = 0;
314 *pcTotalFreePages = 0;
315 *pcTotalBalloonPages = 0;
316 *puTotalBalloonSize = 0;
317
318 /* Must be callable from any thread, so can't use VMMR3CallR0. */
319 int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID, VMMR0_DO_GMM_QUERY_HYPERVISOR_MEM_STATS, 0, &Req.Hdr);
320 if (rc == VINF_SUCCESS)
321 {
322 *pcTotalAllocPages = Req.cAllocPages;
323 *pcTotalFreePages = Req.cFreePages;
324 *pcTotalBalloonPages = Req.cBalloonedPages;
325 *puTotalBalloonSize = Req.cSharedPages;
326 }
327 return rc;
328}
329
330
331/**
332 * @see GMMR0QueryMemoryStatsReq
333 */
334GMMR3DECL(int) GMMR3QueryMemoryStats(PVM pVM, uint64_t *pcAllocPages, uint64_t *pcMaxPages, uint64_t *pcBalloonPages)
335{
336 GMMMEMSTATSREQ Req;
337 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
338 Req.Hdr.cbReq = sizeof(Req);
339 Req.cAllocPages = 0;
340 Req.cFreePages = 0;
341 Req.cBalloonedPages = 0;
342
343 *pcAllocPages = 0;
344 *pcMaxPages = 0;
345 *pcBalloonPages = 0;
346
347 int rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_QUERY_MEM_STATS, 0, &Req.Hdr);
348 if (rc == VINF_SUCCESS)
349 {
350 *pcAllocPages = Req.cAllocPages;
351 *pcMaxPages = Req.cMaxPages;
352 *pcBalloonPages = Req.cBalloonedPages;
353 }
354 return rc;
355}
356
357
358/**
359 * @see GMMR0MapUnmapChunk
360 */
361GMMR3DECL(int) GMMR3MapUnmapChunk(PVM pVM, uint32_t idChunkMap, uint32_t idChunkUnmap, PRTR3PTR ppvR3)
362{
363 GMMMAPUNMAPCHUNKREQ Req;
364 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
365 Req.Hdr.cbReq = sizeof(Req);
366 Req.idChunkMap = idChunkMap;
367 Req.idChunkUnmap = idChunkUnmap;
368 Req.pvR3 = NULL;
369 int rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_MAP_UNMAP_CHUNK, 0, &Req.Hdr);
370 if (RT_SUCCESS(rc) && ppvR3)
371 *ppvR3 = Req.pvR3;
372 return rc;
373}
374
375
376/**
377 * @see GMMR0FreeLargePage
378 */
379GMMR3DECL(int) GMMR3FreeLargePage(PVM pVM, uint32_t idPage)
380{
381 GMMFREELARGEPAGEREQ Req;
382 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
383 Req.Hdr.cbReq = sizeof(Req);
384 Req.idPage = idPage;
385 return VMMR3CallR0(pVM, VMMR0_DO_GMM_FREE_LARGE_PAGE, 0, &Req.Hdr);
386}
387
388
389/**
390 * @see GMMR0RegisterSharedModule
391 */
392GMMR3DECL(int) GMMR3RegisterSharedModule(PVM pVM, PGMMREGISTERSHAREDMODULEREQ pReq)
393{
394 pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
395 pReq->Hdr.cbReq = RT_UOFFSETOF_DYN(GMMREGISTERSHAREDMODULEREQ, aRegions[pReq->cRegions]);
396 int rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_REGISTER_SHARED_MODULE, 0, &pReq->Hdr);
397 if (rc == VINF_SUCCESS)
398 rc = pReq->rc;
399 return rc;
400}
401
402
403/**
404 * @see GMMR0RegisterSharedModule
405 */
406GMMR3DECL(int) GMMR3UnregisterSharedModule(PVM pVM, PGMMUNREGISTERSHAREDMODULEREQ pReq)
407{
408 pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
409 pReq->Hdr.cbReq = sizeof(*pReq);
410 return VMMR3CallR0(pVM, VMMR0_DO_GMM_UNREGISTER_SHARED_MODULE, 0, &pReq->Hdr);
411}
412
413
414/**
415 * @see GMMR0ResetSharedModules
416 */
417GMMR3DECL(int) GMMR3ResetSharedModules(PVM pVM)
418{
419 if (!SUPR3IsDriverless())
420 return VMMR3CallR0(pVM, VMMR0_DO_GMM_RESET_SHARED_MODULES, 0, NULL);
421 return VINF_SUCCESS;
422}
423
424
425/**
426 * @see GMMR0CheckSharedModules
427 */
428GMMR3DECL(int) GMMR3CheckSharedModules(PVM pVM)
429{
430 return VMMR3CallR0(pVM, VMMR0_DO_GMM_CHECK_SHARED_MODULES, 0, NULL);
431}
432
433
434#if defined(VBOX_STRICT) && HC_ARCH_BITS == 64
435/**
436 * @see GMMR0FindDuplicatePage
437 */
438GMMR3DECL(bool) GMMR3IsDuplicatePage(PVM pVM, uint32_t idPage)
439{
440 GMMFINDDUPLICATEPAGEREQ Req;
441 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
442 Req.Hdr.cbReq = sizeof(Req);
443 Req.idPage = idPage;
444 Req.fDuplicate = false;
445
446 /* Must be callable from any thread, so can't use VMMR3CallR0. */
447 int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID, VMMR0_DO_GMM_FIND_DUPLICATE_PAGE, 0, &Req.Hdr);
448 if (rc == VINF_SUCCESS)
449 return Req.fDuplicate;
450 return false;
451}
452#endif /* VBOX_STRICT && HC_ARCH_BITS == 64 */
453
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