VirtualBox

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

Last change on this file since 7692 was 6841, checked in by vboxsync, 17 years ago

forgot LOG_GROUP.

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