1 | /* $Id: tstMMHyperHeap.cpp 48657 2013-09-24 13:14:57Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MM Hypervisor Heap testcase.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 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 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #include <VBox/vmm/mm.h>
|
---|
22 | #include <VBox/vmm/stam.h>
|
---|
23 | #include <VBox/vmm/vm.h>
|
---|
24 | #include <VBox/vmm/uvm.h>
|
---|
25 | #include <VBox/sup.h>
|
---|
26 | #include <VBox/param.h>
|
---|
27 | #include <VBox/err.h>
|
---|
28 |
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #include <iprt/initterm.h>
|
---|
31 | #include <iprt/mem.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 |
|
---|
36 | /* does not work for more CPUs as SUPR3LowAlloc() would refuse to allocate more pages */
|
---|
37 | #define NUM_CPUS 16
|
---|
38 |
|
---|
39 |
|
---|
40 | int main(int argc, char **argv)
|
---|
41 | {
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * Init runtime.
|
---|
45 | */
|
---|
46 | RTR3InitExe(argc, &argv, 0);
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * Create empty VM structure and call MMR3Init().
|
---|
50 | */
|
---|
51 | PVM pVM;
|
---|
52 | RTR0PTR pvR0;
|
---|
53 | SUPPAGE aPages[RT_ALIGN_Z(sizeof(*pVM) + NUM_CPUS * sizeof(VMCPU), PAGE_SIZE) >> PAGE_SHIFT];
|
---|
54 | int rc = SUPR3Init(NULL);
|
---|
55 | if (RT_SUCCESS(rc))
|
---|
56 | rc = SUPR3LowAlloc(RT_ELEMENTS(aPages), (void **)&pVM, &pvR0, &aPages[0]);
|
---|
57 | if (RT_FAILURE(rc))
|
---|
58 | {
|
---|
59 | RTPrintf("Fatal error: SUP Failure! rc=%Rrc\n", rc);
|
---|
60 | return 1;
|
---|
61 | }
|
---|
62 | memset(pVM, 0, sizeof(*pVM)); /* wtf? */
|
---|
63 | pVM->paVMPagesR3 = aPages;
|
---|
64 | pVM->pVMR0 = pvR0;
|
---|
65 |
|
---|
66 | static UVM s_UVM;
|
---|
67 | PUVM pUVM = &s_UVM;
|
---|
68 | pUVM->pVM = pVM;
|
---|
69 | pVM->pUVM = pUVM;
|
---|
70 |
|
---|
71 | pVM->cCpus = NUM_CPUS;
|
---|
72 | pVM->cbSelf = RT_UOFFSETOF(VM, aCpus[pVM->cCpus]);
|
---|
73 |
|
---|
74 | rc = STAMR3InitUVM(pUVM);
|
---|
75 | if (RT_FAILURE(rc))
|
---|
76 | {
|
---|
77 | RTPrintf("FAILURE: STAMR3Init failed. rc=%Rrc\n", rc);
|
---|
78 | return 1;
|
---|
79 | }
|
---|
80 |
|
---|
81 | rc = MMR3InitUVM(pUVM);
|
---|
82 | if (RT_FAILURE(rc))
|
---|
83 | {
|
---|
84 | RTPrintf("FAILURE: STAMR3Init failed. rc=%Rrc\n", rc);
|
---|
85 | return 1;
|
---|
86 | }
|
---|
87 |
|
---|
88 | rc = CFGMR3Init(pVM, NULL, NULL);
|
---|
89 | if (RT_FAILURE(rc))
|
---|
90 | {
|
---|
91 | RTPrintf("FAILURE: CFGMR3Init failed. rc=%Rrc\n", rc);
|
---|
92 | return 1;
|
---|
93 | }
|
---|
94 |
|
---|
95 | rc = MMR3Init(pVM);
|
---|
96 | if (RT_FAILURE(rc))
|
---|
97 | {
|
---|
98 | RTPrintf("Fatal error: MMR3Init failed! rc=%Rrc\n", rc);
|
---|
99 | return 1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Try allocate.
|
---|
104 | */
|
---|
105 | static struct
|
---|
106 | {
|
---|
107 | size_t cb;
|
---|
108 | unsigned uAlignment;
|
---|
109 | void *pvAlloc;
|
---|
110 | unsigned iFreeOrder;
|
---|
111 | } aOps[] =
|
---|
112 | {
|
---|
113 | { 16, 0, NULL, 0 },
|
---|
114 | { 16, 4, NULL, 1 },
|
---|
115 | { 16, 8, NULL, 2 },
|
---|
116 | { 16, 16, NULL, 5 },
|
---|
117 | { 16, 32, NULL, 4 },
|
---|
118 | { 32, 0, NULL, 3 },
|
---|
119 | { 31, 0, NULL, 6 },
|
---|
120 | { 1024, 0, NULL, 8 },
|
---|
121 | { 1024, 32, NULL, 10 },
|
---|
122 | { 1024, 32, NULL, 12 },
|
---|
123 | { PAGE_SIZE, PAGE_SIZE, NULL, 13 },
|
---|
124 | { 1024, 32, NULL, 9 },
|
---|
125 | { PAGE_SIZE, 32, NULL, 11 },
|
---|
126 | { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
|
---|
127 | { 16, 0, NULL, 15 },
|
---|
128 | { 9, 0, NULL, 7 },
|
---|
129 | { 16, 0, NULL, 7 },
|
---|
130 | { 36, 0, NULL, 7 },
|
---|
131 | { 16, 0, NULL, 7 },
|
---|
132 | { 12344, 0, NULL, 7 },
|
---|
133 | { 50, 0, NULL, 7 },
|
---|
134 | { 16, 0, NULL, 7 },
|
---|
135 | };
|
---|
136 | unsigned i;
|
---|
137 | #ifdef DEBUG
|
---|
138 | MMHyperHeapDump(pVM);
|
---|
139 | #endif
|
---|
140 | size_t cbBefore = MMHyperHeapGetFreeSize(pVM);
|
---|
141 | static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
142 |
|
---|
143 | /* allocate */
|
---|
144 | for (i = 0; i < RT_ELEMENTS(aOps); i++)
|
---|
145 | {
|
---|
146 | rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM, &aOps[i].pvAlloc);
|
---|
147 | if (RT_FAILURE(rc))
|
---|
148 | {
|
---|
149 | RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
|
---|
150 | return 1;
|
---|
151 | }
|
---|
152 | memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
|
---|
153 | if (RT_ALIGN_P(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
|
---|
154 | {
|
---|
155 | RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %p, invalid alignment!\n", aOps[i].cb, aOps[i].uAlignment, aOps[i].pvAlloc);
|
---|
156 | return 1;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* free and allocate the same node again. */
|
---|
161 | for (i = 0; i < RT_ELEMENTS(aOps); i++)
|
---|
162 | {
|
---|
163 | if ( !aOps[i].pvAlloc
|
---|
164 | || aOps[i].uAlignment == PAGE_SIZE)
|
---|
165 | continue;
|
---|
166 | //size_t cbBeforeSub = MMHyperHeapGetFreeSize(pVM);
|
---|
167 | rc = MMHyperFree(pVM, aOps[i].pvAlloc);
|
---|
168 | if (RT_FAILURE(rc))
|
---|
169 | {
|
---|
170 | RTPrintf("Failure: MMHyperFree(, %p,) -> %d i=%d\n", aOps[i].pvAlloc, rc, i);
|
---|
171 | return 1;
|
---|
172 | }
|
---|
173 | //RTPrintf("debug: i=%d cbBeforeSub=%d now=%d\n", i, cbBeforeSub, MMHyperHeapGetFreeSize(pVM));
|
---|
174 | void *pv;
|
---|
175 | rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM_REQ, &pv);
|
---|
176 | if (RT_FAILURE(rc))
|
---|
177 | {
|
---|
178 | RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
|
---|
179 | return 1;
|
---|
180 | }
|
---|
181 | if (pv != aOps[i].pvAlloc)
|
---|
182 | {
|
---|
183 | 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);
|
---|
184 | //return 1;
|
---|
185 | }
|
---|
186 | aOps[i].pvAlloc = pv;
|
---|
187 | #if 0 /* won't work :/ */
|
---|
188 | size_t cbAfterSub = MMHyperHeapGetFreeSize(pVM);
|
---|
189 | if (cbBeforeSub != cbAfterSub)
|
---|
190 | {
|
---|
191 | RTPrintf("Failure: cbBeforeSub=%d cbAfterSub=%d. i=%d\n", cbBeforeSub, cbAfterSub, i);
|
---|
192 | return 1;
|
---|
193 | }
|
---|
194 | #endif
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* free it in a specific order. */
|
---|
198 | int cFreed = 0;
|
---|
199 | for (i = 0; i < RT_ELEMENTS(aOps); i++)
|
---|
200 | {
|
---|
201 | unsigned j;
|
---|
202 | for (j = 0; j < RT_ELEMENTS(aOps); j++)
|
---|
203 | {
|
---|
204 | if ( aOps[j].iFreeOrder != i
|
---|
205 | || !aOps[j].pvAlloc)
|
---|
206 | continue;
|
---|
207 | RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, MMHyperHeapGetFreeSize(pVM), aOps[j].cb, aOps[j].pvAlloc);
|
---|
208 | if (aOps[j].uAlignment == PAGE_SIZE)
|
---|
209 | cbBefore -= aOps[j].cb;
|
---|
210 | else
|
---|
211 | {
|
---|
212 | rc = MMHyperFree(pVM, aOps[j].pvAlloc);
|
---|
213 | if (RT_FAILURE(rc))
|
---|
214 | {
|
---|
215 | RTPrintf("Failure: MMHyperFree(, %p,) -> %d j=%d i=%d\n", aOps[j].pvAlloc, rc, i, j);
|
---|
216 | return 1;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | aOps[j].pvAlloc = NULL;
|
---|
220 | cFreed++;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | Assert(cFreed == RT_ELEMENTS(aOps));
|
---|
224 | RTPrintf("i=done free=%d\n", MMHyperHeapGetFreeSize(pVM));
|
---|
225 |
|
---|
226 | /* check that we're back at the right amount of free memory. */
|
---|
227 | size_t cbAfter = MMHyperHeapGetFreeSize(pVM);
|
---|
228 | if (cbBefore != cbAfter)
|
---|
229 | {
|
---|
230 | RTPrintf("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
|
---|
231 | " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
|
---|
232 | #ifdef DEBUG
|
---|
233 | MMHyperHeapDump(pVM);
|
---|
234 | #endif
|
---|
235 | }
|
---|
236 |
|
---|
237 | RTPrintf("tstMMHyperHeap: Success\n");
|
---|
238 | #ifdef LOG_ENABLED
|
---|
239 | RTLogFlush(NULL);
|
---|
240 | #endif
|
---|
241 | return 0;
|
---|
242 | }
|
---|