VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstMMHyperHeap.cpp@ 82930

Last change on this file since 82930 was 82930, checked in by vboxsync, 5 years ago

tstMMHyperHeap: some tweaking. [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.9 KB
Line 
1/* $Id: tstMMHyperHeap.cpp 82930 2020-01-30 14:00:01Z vboxsync $ */
2/** @file
3 * MM Hypervisor Heap testcase.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
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/vmm/mm.h>
23#include <VBox/vmm/stam.h>
24#include <VBox/vmm/vm.h>
25#include <VBox/vmm/uvm.h>
26#include <VBox/vmm/gvm.h>
27#include <VBox/sup.h>
28#include <VBox/param.h>
29#include <iprt/errcore.h>
30
31#include <VBox/log.h>
32#include <iprt/initterm.h>
33#include <iprt/mem.h>
34#include <iprt/assert.h>
35#include <iprt/stream.h>
36#include <iprt/string.h>
37
38#define NUM_CPUS 16
39
40#define OUTPUT(a) do { Log(a); RTPrintf a; } while (0)
41
42/**
43 * Entry point.
44 */
45extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
46{
47 RT_NOREF1(envp);
48
49 /*
50 * Init runtime.
51 */
52 int rc = RTR3InitExe(argc, &argv, 0);
53 AssertRCReturn(rc, RTEXITCODE_INIT);
54
55 /*
56 * Create empty VM structure and call MMR3Init().
57 */
58 void *pvVM = NULL;
59 RTR0PTR pvR0 = NIL_RTR0PTR;
60 SUPPAGE aPages[(sizeof(GVM) + NUM_CPUS * sizeof(GVMCPU)) >> PAGE_SHIFT];
61 rc = SUPR3Init(NULL);
62 if (RT_FAILURE(rc))
63 {
64 RTPrintf("Fatal error: SUP failure! rc=%Rrc\n", rc);
65 return RTEXITCODE_FAILURE;
66 }
67 rc = SUPR3PageAllocEx(RT_ELEMENTS(aPages), 0, &pvVM, &pvR0, &aPages[0]);
68 if (RT_FAILURE(rc))
69 {
70 RTPrintf("Fatal error: Allocation failure! rc=%Rrc\n", rc);
71 return RTEXITCODE_FAILURE;
72 }
73 RT_BZERO(pvVM, RT_ELEMENTS(aPages) * PAGE_SIZE); /* SUPR3PageAllocEx doesn't necessarily zero the memory. */
74 PVM pVM = (PVM)pvVM;
75 pVM->paVMPagesR3 = aPages;
76 pVM->pVMR0ForCall = pvR0;
77
78 PUVM pUVM = (PUVM)RTMemPageAllocZ(RT_ALIGN_Z(sizeof(*pUVM), PAGE_SIZE));
79 if (!pUVM)
80 {
81 RTPrintf("Fatal error: RTMEmPageAllocZ failed\n");
82 return RTEXITCODE_FAILURE;
83 }
84 pUVM->u32Magic = UVM_MAGIC;
85 pUVM->pVM = pVM;
86 pVM->pUVM = pUVM;
87
88 pVM->cCpus = NUM_CPUS;
89 pVM->cbSelf = sizeof(VM);
90 pVM->cbVCpu = sizeof(VMCPU);
91 PVMCPU pVCpu = (PVMCPU)((uintptr_t)pVM + sizeof(GVM));
92 for (VMCPUID idCpu = 0; idCpu < NUM_CPUS; idCpu++)
93 {
94 pVM->apCpusR3[idCpu] = pVCpu;
95 pVCpu = (PVMCPU)((uintptr_t)pVCpu + sizeof(GVMCPU));
96 }
97
98 rc = STAMR3InitUVM(pUVM);
99 if (RT_FAILURE(rc))
100 {
101 RTPrintf("FAILURE: STAMR3Init failed. rc=%Rrc\n", rc);
102 return 1;
103 }
104
105 rc = MMR3InitUVM(pUVM);
106 if (RT_FAILURE(rc))
107 {
108 RTPrintf("FAILURE: STAMR3Init failed. rc=%Rrc\n", rc);
109 return 1;
110 }
111
112 rc = CFGMR3Init(pVM, NULL, NULL);
113 if (RT_FAILURE(rc))
114 {
115 RTPrintf("FAILURE: CFGMR3Init failed. rc=%Rrc\n", rc);
116 return 1;
117 }
118
119 rc = MMR3Init(pVM);
120 if (RT_FAILURE(rc))
121 {
122 RTPrintf("Fatal error: MMR3Init failed! rc=%Rrc\n", rc);
123 return 1;
124 }
125
126 /*
127 * Try allocate.
128 */
129 static struct
130 {
131 size_t cb;
132 unsigned uAlignment;
133 void *pvAlloc;
134 unsigned iFreeOrder;
135 } aOps[] =
136 {
137 { 16, 0, NULL, 0 },
138 { 16, 4, NULL, 1 },
139 { 16, 8, NULL, 2 },
140 { 16, 16, NULL, 5 },
141 { 16, 32, NULL, 4 },
142 { 32, 0, NULL, 3 },
143 { 31, 0, NULL, 6 },
144 { 1024, 0, NULL, 8 },
145 { 1024, 32, NULL, 10 },
146 { 1024, 32, NULL, 12 },
147 { PAGE_SIZE, PAGE_SIZE, NULL, 13 },
148 { 1024, 32, NULL, 9 },
149 { PAGE_SIZE, 32, NULL, 11 },
150 { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
151 { 16, 0, NULL, 15 },
152 { 9, 0, NULL, 7 },
153 { 16, 0, NULL, 7 },
154 { 36, 0, NULL, 7 },
155 { 16, 0, NULL, 7 },
156 { 12344, 0, NULL, 7 },
157 { 50, 0, NULL, 7 },
158 { 16, 0, NULL, 7 },
159 };
160 unsigned i;
161#ifdef DEBUG
162 MMHyperHeapDump(pVM);
163#endif
164 size_t cbBefore = MMHyperHeapGetFreeSize(pVM);
165 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
166
167 /* allocate */
168 for (i = 0; i < RT_ELEMENTS(aOps); i++)
169 {
170 rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM, &aOps[i].pvAlloc);
171 if (RT_FAILURE(rc))
172 {
173 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
174 return 1;
175 }
176 memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
177 if (RT_ALIGN_P(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
178 {
179 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %p, invalid alignment!\n", aOps[i].cb, aOps[i].uAlignment, aOps[i].pvAlloc);
180 return 1;
181 }
182 }
183
184 /* free and allocate the same node again. */
185#ifdef DEBUG
186 MMHyperHeapDump(pVM);
187#endif
188 for (i = 0; i < RT_ELEMENTS(aOps); i++)
189 {
190 if ( !aOps[i].pvAlloc
191 || aOps[i].uAlignment == PAGE_SIZE)
192 continue;
193 size_t cbBeforeSub = MMHyperHeapGetFreeSize(pVM);
194 rc = MMHyperFree(pVM, aOps[i].pvAlloc);
195 if (RT_FAILURE(rc))
196 {
197 RTPrintf("Failure: MMHyperFree(, %p,) -> %d i=%d\n", aOps[i].pvAlloc, rc, i);
198 return 1;
199 }
200 size_t const cbFreed = MMHyperHeapGetFreeSize(pVM);
201 void *pv;
202 rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM_REQ, &pv);
203 if (RT_FAILURE(rc))
204 {
205 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
206 return 1;
207 }
208 if (pv != aOps[i].pvAlloc)
209 {
210 RTPrintf("Failure: Free+Alloc returned different address. new=%p old=%p i=%d (doesn't work with delayed free)\n", pv, aOps[i].pvAlloc, i);
211 //return 1;
212 }
213 aOps[i].pvAlloc = pv;
214 OUTPUT(("debug: i=%02d cbBeforeSub=%d cbFreed=%d now=%d\n", i, cbBeforeSub, cbFreed, MMHyperHeapGetFreeSize(pVM)));
215#if 0 /* won't work :/ */
216 size_t cbAfterSub = MMHyperHeapGetFreeSize(pVM);
217 if (cbBeforeSub != cbAfterSub)
218 {
219 RTPrintf("Failure: cbBeforeSub=%d cbAfterSub=%d. i=%d\n", cbBeforeSub, cbAfterSub, i);
220 return 1;
221 }
222#endif
223 }
224
225 /* free it in a specific order. */
226 int cFreed = 0;
227 for (i = 0; i < RT_ELEMENTS(aOps); i++)
228 {
229 unsigned j;
230 for (j = 0; j < RT_ELEMENTS(aOps); j++)
231 {
232 if ( aOps[j].iFreeOrder != i
233 || !aOps[j].pvAlloc)
234 continue;
235 OUTPUT(("j=%02d i=%02d free=%d cb=%5u pv=%p\n", j, i, MMHyperHeapGetFreeSize(pVM), aOps[j].cb, aOps[j].pvAlloc));
236 if (aOps[j].uAlignment == PAGE_SIZE)
237 cbBefore -= aOps[j].cb;
238 else
239 {
240 rc = MMHyperFree(pVM, aOps[j].pvAlloc);
241 if (RT_FAILURE(rc))
242 {
243 RTPrintf("Failure: MMHyperFree(, %p,) -> %d j=%d i=%d\n", aOps[j].pvAlloc, rc, i, j);
244 return 1;
245 }
246 }
247 aOps[j].pvAlloc = NULL;
248 cFreed++;
249 }
250 }
251 Assert(cFreed == RT_ELEMENTS(aOps));
252 OUTPUT(("i=done free=%d\n", MMHyperHeapGetFreeSize(pVM)));
253
254 /* check that we're back at the right amount of free memory. */
255 size_t cbAfter = MMHyperHeapGetFreeSize(pVM);
256 if (cbBefore != cbAfter)
257 {
258 OUTPUT(("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
259 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter));
260#ifdef DEBUG
261 MMHyperHeapDump(pVM);
262#endif
263 }
264
265 RTPrintf("tstMMHyperHeap: Success\n");
266#ifdef LOG_ENABLED
267 RTLogFlush(NULL);
268#endif
269 SUPR3PageFreeEx(pVM, RT_ELEMENTS(aPages));
270 RTMemPageFree(pUVM, RT_ALIGN_Z(sizeof(*pUVM), PAGE_SIZE));
271 return 0;
272}
273
274
275#if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
276/**
277 * Main entry point.
278 */
279int main(int argc, char **argv, char **envp)
280{
281 return TrustedMain(argc, argv, envp);
282}
283#endif
284
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