1 | /* $Id: tstVMM.cpp 45664 2013-04-22 12:32:23Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM Testcase.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2013 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/vm.h>
|
---|
23 | #include <VBox/vmm/vmm.h>
|
---|
24 | #include <VBox/vmm/cpum.h>
|
---|
25 | #include <VBox/vmm/tm.h>
|
---|
26 | #include <VBox/vmm/pdmapi.h>
|
---|
27 | #include <VBox/err.h>
|
---|
28 | #include <VBox/log.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include <iprt/ctype.h>
|
---|
31 | #include <iprt/getopt.h>
|
---|
32 | #include <iprt/initterm.h>
|
---|
33 | #include <iprt/message.h>
|
---|
34 | #include <iprt/semaphore.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/test.h>
|
---|
38 | #include <iprt/thread.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Defined Constants And Macros *
|
---|
43 | *******************************************************************************/
|
---|
44 | #define TESTCASE "tstVMM"
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Global Variables *
|
---|
50 | *******************************************************************************/
|
---|
51 | static uint32_t g_cCpus = 1;
|
---|
52 |
|
---|
53 |
|
---|
54 | /*******************************************************************************
|
---|
55 | * Internal Functions *
|
---|
56 | *******************************************************************************/
|
---|
57 | VMMR3DECL(int) VMMDoTest(PVM pVM); /* Linked into VMM, see ../VMMTests.cpp. */
|
---|
58 |
|
---|
59 |
|
---|
60 | /** Dummy timer callback. */
|
---|
61 | static DECLCALLBACK(void) tstTMDummyCallback(PVM pVM, PTMTIMER pTimer, void *pvUser)
|
---|
62 | {
|
---|
63 | NOREF(pVM);
|
---|
64 | NOREF(pTimer);
|
---|
65 | NOREF(pvUser);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * This is called on each EMT and will beat TM.
|
---|
71 | *
|
---|
72 | * @returns VINF_SUCCESS, test failure is reported via RTTEST.
|
---|
73 | * @param pVM Pointer to the VM.
|
---|
74 | * @param hTest The test handle.
|
---|
75 | */
|
---|
76 | DECLCALLBACK(int) tstTMWorker(PVM pVM, RTTEST hTest)
|
---|
77 | {
|
---|
78 | VMCPUID idCpu = VMMGetCpuId(pVM);
|
---|
79 | RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "idCpu=%d STARTING\n", idCpu);
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Create the test set.
|
---|
83 | */
|
---|
84 | int rc;
|
---|
85 | PTMTIMER apTimers[5];
|
---|
86 | for (size_t i = 0; i < RT_ELEMENTS(apTimers); i++)
|
---|
87 | {
|
---|
88 | rc = TMR3TimerCreateInternal(pVM, i & 1 ? TMCLOCK_VIRTUAL : TMCLOCK_VIRTUAL_SYNC,
|
---|
89 | tstTMDummyCallback, NULL, "test timer", &apTimers[i]);
|
---|
90 | RTTEST_CHECK_RET(hTest, RT_SUCCESS(rc), rc);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * The run loop.
|
---|
95 | */
|
---|
96 | unsigned uPrevPct = 0;
|
---|
97 | uint32_t const cLoops = 100000;
|
---|
98 | for (uint32_t iLoop = 0; iLoop < cLoops; iLoop++)
|
---|
99 | {
|
---|
100 | size_t cLeft = RT_ELEMENTS(apTimers);
|
---|
101 | unsigned i = iLoop % RT_ELEMENTS(apTimers);
|
---|
102 | while (cLeft-- > 0)
|
---|
103 | {
|
---|
104 | PTMTIMER pTimer = apTimers[i];
|
---|
105 |
|
---|
106 | if ( cLeft == RT_ELEMENTS(apTimers) / 2
|
---|
107 | && TMTimerIsActive(pTimer))
|
---|
108 | {
|
---|
109 | rc = TMTimerStop(pTimer);
|
---|
110 | RTTEST_CHECK_MSG(hTest, RT_SUCCESS(rc), (hTest, "TMTimerStop: %Rrc\n", rc));
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | rc = TMTimerSetMicro(pTimer, 50 + cLeft);
|
---|
115 | RTTEST_CHECK_MSG(hTest, RT_SUCCESS(rc), (hTest, "TMTimerSetMicro: %Rrc\n", rc));
|
---|
116 | }
|
---|
117 |
|
---|
118 | /* next */
|
---|
119 | i = (i + 1) % RT_ELEMENTS(apTimers);
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (i % 3)
|
---|
123 | TMR3TimerQueuesDo(pVM);
|
---|
124 |
|
---|
125 | /* Progress report. */
|
---|
126 | unsigned uPct = (unsigned)(100.0 * iLoop / cLoops);
|
---|
127 | if (uPct != uPrevPct)
|
---|
128 | {
|
---|
129 | uPrevPct = uPct;
|
---|
130 | if (!(uPct % 10))
|
---|
131 | RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "idCpu=%d - %3u%%\n", idCpu, uPct);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "idCpu=%d DONE\n", idCpu);
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /** PDMR3LdrEnumModules callback, see FNPDMR3ENUM. */
|
---|
141 | static DECLCALLBACK(int)
|
---|
142 | tstVMMLdrEnum(PVM pVM, const char *pszFilename, const char *pszName, RTUINTPTR ImageBase, size_t cbImage, bool fGC, void *pvUser)
|
---|
143 | {
|
---|
144 | NOREF(pVM); NOREF(pszFilename); NOREF(fGC); NOREF(pvUser); NOREF(cbImage);
|
---|
145 | RTPrintf("tstVMM: %RTptr %s\n", ImageBase, pszName);
|
---|
146 | return VINF_SUCCESS;
|
---|
147 | }
|
---|
148 |
|
---|
149 | static DECLCALLBACK(int)
|
---|
150 | tstVMMConfigConstructor(PUVM pUVM, PVM pVM, void *pvUser)
|
---|
151 | {
|
---|
152 | NOREF(pvUser);
|
---|
153 | int rc = CFGMR3ConstructDefaultTree(pVM);
|
---|
154 | if (RT_SUCCESS(rc))
|
---|
155 | {
|
---|
156 | PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
|
---|
157 | if (g_cCpus < 2)
|
---|
158 | {
|
---|
159 | rc = CFGMR3InsertInteger(pRoot, "HMEnabled", false);
|
---|
160 | RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc),
|
---|
161 | ("CFGMR3InsertInteger(pRoot,\"HMEnabled\",) -> %Rrc\n", rc), rc);
|
---|
162 | }
|
---|
163 | else if (g_cCpus > 1)
|
---|
164 | {
|
---|
165 | CFGMR3RemoveValue(pRoot, "NumCPUs");
|
---|
166 | rc = CFGMR3InsertInteger(pRoot, "NumCPUs", g_cCpus);
|
---|
167 | RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc),
|
---|
168 | ("CFGMR3InsertInteger(pRoot,\"NumCPUs\",) -> %Rrc\n", rc), rc);
|
---|
169 |
|
---|
170 | CFGMR3RemoveValue(pRoot, "HwVirtExtForced");
|
---|
171 | rc = CFGMR3InsertInteger(pRoot, "HwVirtExtForced", true);
|
---|
172 | RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc),
|
---|
173 | ("CFGMR3InsertInteger(pRoot,\"HwVirtExtForced\",) -> %Rrc\n", rc), rc);
|
---|
174 | PCFGMNODE pHwVirtExt = CFGMR3GetChild(pRoot, "HWVirtExt");
|
---|
175 | CFGMR3RemoveNode(pHwVirtExt);
|
---|
176 | rc = CFGMR3InsertNode(pRoot, "HWVirtExt", &pHwVirtExt);
|
---|
177 | RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc),
|
---|
178 | ("CFGMR3InsertNode(pRoot,\"HWVirtExt\",) -> %Rrc\n", rc), rc);
|
---|
179 | rc = CFGMR3InsertInteger(pHwVirtExt, "Enabled", true);
|
---|
180 | RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc),
|
---|
181 | ("CFGMR3InsertInteger(pHwVirtExt,\"Enabled\",) -> %Rrc\n", rc), rc);
|
---|
182 | rc = CFGMR3InsertInteger(pHwVirtExt, "64bitEnabled", false);
|
---|
183 | RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc),
|
---|
184 | ("CFGMR3InsertInteger(pHwVirtExt,\"64bitEnabled\",) -> %Rrc\n", rc), rc);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | return rc;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | int main(int argc, char **argv)
|
---|
192 | {
|
---|
193 | /*
|
---|
194 | * Init runtime and the test environment.
|
---|
195 | */
|
---|
196 | int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
|
---|
197 | if (RT_FAILURE(rc))
|
---|
198 | return RTMsgInitFailure(rc);
|
---|
199 | RTTEST hTest;
|
---|
200 | rc = RTTestCreate("tstVMM", &hTest);
|
---|
201 | if (RT_FAILURE(rc))
|
---|
202 | {
|
---|
203 | RTPrintf("tstVMM: RTTestCreate failed: %Rrc\n", rc);
|
---|
204 | return 1;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Parse arguments.
|
---|
209 | */
|
---|
210 | static const RTGETOPTDEF s_aOptions[] =
|
---|
211 | {
|
---|
212 | { "--cpus", 'c', RTGETOPT_REQ_UINT8 },
|
---|
213 | { "--test", 't', RTGETOPT_REQ_STRING },
|
---|
214 | };
|
---|
215 | enum
|
---|
216 | {
|
---|
217 | kTstVMMTest_VMM, kTstVMMTest_TM
|
---|
218 | } enmTestOpt = kTstVMMTest_VMM;
|
---|
219 |
|
---|
220 | int ch;
|
---|
221 | RTGETOPTUNION ValueUnion;
|
---|
222 | RTGETOPTSTATE GetState;
|
---|
223 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
|
---|
224 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
225 | {
|
---|
226 | switch (ch)
|
---|
227 | {
|
---|
228 | case 'c':
|
---|
229 | g_cCpus = ValueUnion.u8;
|
---|
230 | break;
|
---|
231 |
|
---|
232 | case 't':
|
---|
233 | if (!strcmp("vmm", ValueUnion.psz))
|
---|
234 | enmTestOpt = kTstVMMTest_VMM;
|
---|
235 | else if (!strcmp("tm", ValueUnion.psz))
|
---|
236 | enmTestOpt = kTstVMMTest_TM;
|
---|
237 | else
|
---|
238 | {
|
---|
239 | RTPrintf("tstVMM: unknown test: '%s'\n", ValueUnion.psz);
|
---|
240 | return 1;
|
---|
241 | }
|
---|
242 | break;
|
---|
243 |
|
---|
244 | case 'h':
|
---|
245 | RTPrintf("usage: tstVMM [--cpus|-c cpus] [--test <vmm|tm>]\n");
|
---|
246 | return 1;
|
---|
247 |
|
---|
248 | case 'V':
|
---|
249 | RTPrintf("$Revision: 45664 $\n");
|
---|
250 | return 0;
|
---|
251 |
|
---|
252 | default:
|
---|
253 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Create the test VM.
|
---|
259 | */
|
---|
260 | RTPrintf(TESTCASE ": Initializing...\n");
|
---|
261 | PVM pVM;
|
---|
262 | PUVM pUVM;
|
---|
263 | rc = VMR3Create(g_cCpus, NULL, NULL, NULL, tstVMMConfigConstructor, NULL, &pVM, &pUVM);
|
---|
264 | if (RT_SUCCESS(rc))
|
---|
265 | {
|
---|
266 | PDMR3LdrEnumModules(pVM, tstVMMLdrEnum, NULL);
|
---|
267 | RTStrmFlush(g_pStdOut);
|
---|
268 | RTThreadSleep(256);
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * Do the requested testing.
|
---|
272 | */
|
---|
273 | switch (enmTestOpt)
|
---|
274 | {
|
---|
275 | case kTstVMMTest_VMM:
|
---|
276 | {
|
---|
277 | RTTestSub(hTest, "VMM");
|
---|
278 | rc = VMR3ReqCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)VMMDoTest, 1, pVM);
|
---|
279 | if (RT_FAILURE(rc))
|
---|
280 | RTTestFailed(hTest, "VMMDoTest failed: rc=%Rrc\n", rc);
|
---|
281 | break;
|
---|
282 | }
|
---|
283 |
|
---|
284 | case kTstVMMTest_TM:
|
---|
285 | {
|
---|
286 | RTTestSub(hTest, "TM");
|
---|
287 | for (VMCPUID idCpu = 1; idCpu < g_cCpus; idCpu++)
|
---|
288 | {
|
---|
289 | rc = VMR3ReqCallNoWaitU(pUVM, idCpu, (PFNRT)tstTMWorker, 2, pVM, hTest);
|
---|
290 | if (RT_FAILURE(rc))
|
---|
291 | RTTestFailed(hTest, "VMR3ReqCall failed: rc=%Rrc\n", rc);
|
---|
292 | }
|
---|
293 |
|
---|
294 | rc = VMR3ReqCallWaitU(pUVM, 0 /*idDstCpu*/, (PFNRT)tstTMWorker, 2, pVM, hTest);
|
---|
295 | if (RT_FAILURE(rc))
|
---|
296 | RTTestFailed(hTest, "VMMDoTest failed: rc=%Rrc\n", rc);
|
---|
297 | break;
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | STAMR3Dump(pUVM, "*");
|
---|
302 |
|
---|
303 | /*
|
---|
304 | * Cleanup.
|
---|
305 | */
|
---|
306 | rc = VMR3PowerOff(pUVM);
|
---|
307 | if (RT_FAILURE(rc))
|
---|
308 | RTTestFailed(hTest, "VMR3PowerOff failed: rc=%Rrc\n", rc);
|
---|
309 | rc = VMR3Destroy(pUVM);
|
---|
310 | if (RT_FAILURE(rc))
|
---|
311 | RTTestFailed(hTest, "VMR3Destroy failed: rc=%Rrc\n", rc);
|
---|
312 | VMR3ReleaseUVM(pUVM);
|
---|
313 | }
|
---|
314 | else
|
---|
315 | RTTestFailed(hTest, "VMR3Create failed: rc=%Rrc\n", rc);
|
---|
316 |
|
---|
317 | return RTTestSummaryAndDestroy(hTest);
|
---|
318 | }
|
---|