VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstVMM.cpp@ 49221

Last change on this file since 49221 was 49141, checked in by vboxsync, 11 years ago

tstVMM: Extended it with a 'msr' mode for dump the MSRs of the host CPU brute-force style.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette