VirtualBox

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

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

tstMMHyperHeap: some tweaking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.9 KB
Line 
1/* $Id: tstMMHyperHeap.cpp 82928 2020-01-30 13:52:30Z 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. */
185MMHyperHeapDump(pVM);
186 for (i = 0; i < RT_ELEMENTS(aOps); i++)
187 {
188 if ( !aOps[i].pvAlloc
189 || aOps[i].uAlignment == PAGE_SIZE)
190 continue;
191 size_t cbBeforeSub = MMHyperHeapGetFreeSize(pVM);
192 rc = MMHyperFree(pVM, aOps[i].pvAlloc);
193 if (RT_FAILURE(rc))
194 {
195 RTPrintf("Failure: MMHyperFree(, %p,) -> %d i=%d\n", aOps[i].pvAlloc, rc, i);
196 return 1;
197 }
198 size_t const cbFreed = MMHyperHeapGetFreeSize(pVM);
199 void *pv;
200 rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM_REQ, &pv);
201 if (RT_FAILURE(rc))
202 {
203 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
204 return 1;
205 }
206 if (pv != aOps[i].pvAlloc)
207 {
208 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);
209 //return 1;
210 }
211 aOps[i].pvAlloc = pv;
212 OUTPUT(("debug: i=%02d cbBeforeSub=%d cbFreed=%d now=%d\n", i, cbBeforeSub, cbFreed, MMHyperHeapGetFreeSize(pVM)));
213#if 0 /* won't work :/ */
214 size_t cbAfterSub = MMHyperHeapGetFreeSize(pVM);
215 if (cbBeforeSub != cbAfterSub)
216 {
217 RTPrintf("Failure: cbBeforeSub=%d cbAfterSub=%d. i=%d\n", cbBeforeSub, cbAfterSub, i);
218 return 1;
219 }
220#endif
221 }
222
223 /* free it in a specific order. */
224 int cFreed = 0;
225 for (i = 0; i < RT_ELEMENTS(aOps); i++)
226 {
227 unsigned j;
228 for (j = 0; j < RT_ELEMENTS(aOps); j++)
229 {
230 if ( aOps[j].iFreeOrder != i
231 || !aOps[j].pvAlloc)
232 continue;
233 OUTPUT(("j=%02d i=%02d free=%d cb=%5u pv=%p\n", j, i, MMHyperHeapGetFreeSize(pVM), aOps[j].cb, aOps[j].pvAlloc));
234 if (aOps[j].uAlignment == PAGE_SIZE)
235 cbBefore -= aOps[j].cb;
236 else
237 {
238 rc = MMHyperFree(pVM, aOps[j].pvAlloc);
239 if (RT_FAILURE(rc))
240 {
241 RTPrintf("Failure: MMHyperFree(, %p,) -> %d j=%d i=%d\n", aOps[j].pvAlloc, rc, i, j);
242 return 1;
243 }
244 }
245 aOps[j].pvAlloc = NULL;
246 cFreed++;
247 }
248 }
249 Assert(cFreed == RT_ELEMENTS(aOps));
250 OUTPUT(("i=done free=%d\n", MMHyperHeapGetFreeSize(pVM)));
251
252 /* check that we're back at the right amount of free memory. */
253 size_t cbAfter = MMHyperHeapGetFreeSize(pVM);
254 if (cbBefore != cbAfter)
255 {
256 OUTPUT(("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
257 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter));
258#ifdef DEBUG
259 MMHyperHeapDump(pVM);
260#endif
261 }
262
263 RTPrintf("tstMMHyperHeap: Success\n");
264#ifdef LOG_ENABLED
265 RTLogFlush(NULL);
266#endif
267 SUPR3PageFreeEx(pVM, RT_ELEMENTS(aPages));
268 RTMemPageFree(pUVM, RT_ALIGN_Z(sizeof(*pUVM), PAGE_SIZE));
269 return 0;
270}
271
272
273#if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
274/**
275 * Main entry point.
276 */
277int main(int argc, char **argv, char **envp)
278{
279 return TrustedMain(argc, argv, envp);
280}
281#endif
282
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