VirtualBox

source: vbox/trunk/src/VBox/Devices/VMMDev/VMMDevTesting.cpp@ 53625

Last change on this file since 53625 was 53625, checked in by vboxsync, 10 years ago

Test I/O and MMIO performance when we need to go to ring-3 to service the request.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.1 KB
Line 
1/* $Id: VMMDevTesting.cpp 53625 2014-12-31 15:33:55Z vboxsync $ */
2/** @file
3 * VMMDev - Testing Extensions.
4 *
5 * To enable: VBoxManage setextradata vmname VBoxInternal/Devices/VMMDev/0/Config/TestingEnabled 1
6 */
7
8/*
9 * Copyright (C) 2010-2013 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*******************************************************************************
22* Header Files *
23*******************************************************************************/
24#define LOG_GROUP LOG_GROUP_DEV_VMM
25#include <VBox/VMMDev.h>
26#include <VBox/vmm/vmapi.h>
27#include <VBox/log.h>
28#include <VBox/err.h>
29
30#include <iprt/asm.h>
31#include <iprt/assert.h>
32#include <iprt/string.h>
33#include <iprt/time.h>
34#include <iprt/test.h>
35
36#include "VMMDevState.h"
37#include "VMMDevTesting.h"
38
39
40#ifndef VBOX_WITHOUT_TESTING_FEATURES
41
42#define VMMDEV_TESTING_OUTPUT(a) \
43 do \
44 { \
45 LogAlways(a);\
46 LogRel(a);\
47 } while (0)
48
49/**
50 * @callback_method_impl{FNIOMMMIOWRITE}
51 */
52PDMBOTHCBDECL(int) vmmdevTestingMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
53{
54 switch (GCPhysAddr)
55 {
56 case VMMDEV_TESTING_MMIO_NOP:
57 switch (cb)
58 {
59 case 8:
60 case 4:
61 case 2:
62 case 1:
63 break;
64 default:
65 AssertFailed();
66 return VERR_INTERNAL_ERROR_5;
67 }
68 return VINF_SUCCESS;
69
70 case VMMDEV_TESTING_MMIO_NOP_R3:
71 switch (cb)
72 {
73 case 8:
74 case 4:
75 case 2:
76 case 1:
77#ifndef IN_RING3
78 return VINF_IOM_R3_MMIO_READ_WRITE;
79#else
80 return VINF_SUCCESS;
81#endif
82 default:
83 AssertFailed();
84 return VERR_INTERNAL_ERROR_5;
85 }
86
87 default:
88 break;
89 }
90 return VINF_SUCCESS;
91}
92
93
94/**
95 * @callback_method_impl{FNIOMMMIOREAD}
96 */
97PDMBOTHCBDECL(int) vmmdevTestingMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
98{
99 switch (GCPhysAddr)
100 {
101 case VMMDEV_TESTING_MMIO_NOP_R3:
102#ifndef IN_RING3
103 switch (cb)
104 {
105 case 8:
106 case 4:
107 case 2:
108 case 1:
109 return VINF_IOM_R3_MMIO_READ;
110 }
111#endif
112 /* fall thru. */
113 case VMMDEV_TESTING_MMIO_NOP:
114 switch (cb)
115 {
116 case 8:
117 *(uint64_t *)pv = VMMDEV_TESTING_NOP_RET | ((uint64_t)VMMDEV_TESTING_NOP_RET << 32);
118 break;
119 case 4:
120 *(uint32_t *)pv = VMMDEV_TESTING_NOP_RET;
121 break;
122 case 2:
123 *(uint16_t *)pv = (uint16_t)VMMDEV_TESTING_NOP_RET;
124 break;
125 case 1:
126 *(uint8_t *)pv = (uint8_t)VMMDEV_TESTING_NOP_RET;
127 break;
128 default:
129 AssertFailed();
130 return VERR_INTERNAL_ERROR_5;
131 }
132 return VINF_SUCCESS;
133
134 default:
135 break;
136 }
137
138 return VINF_IOM_MMIO_UNUSED_FF;
139}
140
141#ifdef IN_RING3
142
143/**
144 * Executes the VMMDEV_TESTING_CMD_VALUE_REG command when the data is ready.
145 *
146 * @param pDevIns The PDM device instance.
147 * @param pThis The instance VMMDev data.
148 */
149static void vmmdevTestingCmdExec_ValueReg(PPDMDEVINS pDevIns, VMMDevState *pThis)
150{
151 char *pszRegNm = strchr(pThis->TestingData.String.sz, ':');
152 if (pszRegNm)
153 {
154 *pszRegNm++ = '\0';
155 pszRegNm = RTStrStrip(pszRegNm);
156 }
157 char *pszValueNm = RTStrStrip(pThis->TestingData.String.sz);
158 size_t const cchValueNm = strlen(pszValueNm);
159 if (cchValueNm && pszRegNm && *pszRegNm)
160 {
161 PUVM pUVM = PDMDevHlpGetUVM(pDevIns);
162 PVM pVM = PDMDevHlpGetVM(pDevIns);
163 VMCPUID idCpu = VMMGetCpuId(pVM);
164 uint64_t u64Value;
165 int rc2 = DBGFR3RegNmQueryU64(pUVM, idCpu, pszRegNm, &u64Value);
166 if (RT_SUCCESS(rc2))
167 {
168 const char *pszWarn = rc2 == VINF_DBGF_TRUNCATED_REGISTER ? " truncated" : "";
169#if 1 /*!RTTestValue format*/
170 char szFormat[128], szValue[128];
171 RTStrPrintf(szFormat, sizeof(szFormat), "%%VR{%s}", pszRegNm);
172 rc2 = DBGFR3RegPrintf(pUVM, idCpu, szValue, sizeof(szValue), szFormat);
173 if (RT_SUCCESS(rc2))
174 VMMDEV_TESTING_OUTPUT(("testing: VALUE '%s'%*s: %16s {reg=%s}%s\n",
175 pszValueNm,
176 (ssize_t)cchValueNm - 12 > 48 ? 0 : 48 - ((ssize_t)cchValueNm - 12), "",
177 szValue, pszRegNm, pszWarn));
178 else
179#endif
180 VMMDEV_TESTING_OUTPUT(("testing: VALUE '%s'%*s: %'9llu (%#llx) [0] {reg=%s}%s\n",
181 pszValueNm,
182 (ssize_t)cchValueNm - 12 > 48 ? 0 : 48 - ((ssize_t)cchValueNm - 12), "",
183 u64Value, u64Value, pszRegNm, pszWarn));
184 }
185 else
186 VMMDEV_TESTING_OUTPUT(("testing: error querying register '%s' for value '%s': %Rrc\n",
187 pszRegNm, pszValueNm, rc2));
188 }
189 else
190 VMMDEV_TESTING_OUTPUT(("testing: malformed register value '%s'/'%s'\n", pszValueNm, pszRegNm));
191}
192
193#endif /* IN_RING3 */
194
195/**
196 * @callback_method_impl{FNIOMIOPORTOUT}
197 */
198PDMBOTHCBDECL(int) vmmdevTestingIoWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
199{
200 VMMDevState *pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
201
202 switch (Port)
203 {
204 /*
205 * The NOP I/O ports are used for performance measurements.
206 */
207 case VMMDEV_TESTING_IOPORT_NOP:
208 switch (cb)
209 {
210 case 4:
211 case 2:
212 case 1:
213 break;
214 default:
215 AssertFailed();
216 return VERR_INTERNAL_ERROR_2;
217 }
218 return VINF_SUCCESS;
219
220 case VMMDEV_TESTING_IOPORT_NOP_R3:
221 switch (cb)
222 {
223 case 4:
224 case 2:
225 case 1:
226#ifndef IN_RING3
227 return VINF_IOM_R3_IOPORT_WRITE;
228#else
229 return VINF_SUCCESS;
230#endif
231 default:
232 AssertFailed();
233 return VERR_INTERNAL_ERROR_2;
234 }
235
236 /* The timestamp I/O ports are read-only. */
237 case VMMDEV_TESTING_IOPORT_TS_LOW:
238 case VMMDEV_TESTING_IOPORT_TS_HIGH:
239 break;
240
241 /*
242 * The command port (DWORD write only).
243 */
244 case VMMDEV_TESTING_IOPORT_CMD:
245 if (cb == 4)
246 {
247 pThis->u32TestingCmd = u32;
248 pThis->offTestingData = 0;
249 RT_ZERO(pThis->TestingData);
250 return VINF_SUCCESS;
251 }
252 break;
253
254 /*
255 * The data port. Used of providing data for a command.
256 */
257 case VMMDEV_TESTING_IOPORT_DATA:
258 {
259 uint32_t uCmd = pThis->u32TestingCmd;
260 uint32_t off = pThis->offTestingData;
261 switch (uCmd)
262 {
263 case VMMDEV_TESTING_CMD_INIT:
264 case VMMDEV_TESTING_CMD_SUB_NEW:
265 case VMMDEV_TESTING_CMD_FAILED:
266 case VMMDEV_TESTING_CMD_SKIPPED:
267 if ( off < sizeof(pThis->TestingData.String.sz) - 1
268 && cb == 1)
269 {
270 if (u32)
271 {
272 pThis->TestingData.String.sz[off] = u32;
273 pThis->offTestingData = off + 1;
274 }
275 else
276 {
277#ifdef IN_RING3
278 pThis->TestingData.String.sz[off] = '\0';
279 switch (uCmd)
280 {
281 case VMMDEV_TESTING_CMD_INIT:
282 VMMDEV_TESTING_OUTPUT(("testing: INIT '%s'\n", pThis->TestingData.String.sz));
283 if (pThis->hTestingTest != NIL_RTTEST)
284 {
285 RTTestChangeName(pThis->hTestingTest, pThis->TestingData.String.sz);
286 RTTestBanner(pThis->hTestingTest);
287 }
288 break;
289 case VMMDEV_TESTING_CMD_SUB_NEW:
290 VMMDEV_TESTING_OUTPUT(("testing: SUB_NEW '%s'\n", pThis->TestingData.String.sz));
291 if (pThis->hTestingTest != NIL_RTTEST)
292 RTTestSub(pThis->hTestingTest, pThis->TestingData.String.sz);
293 break;
294 case VMMDEV_TESTING_CMD_FAILED:
295 if (pThis->hTestingTest != NIL_RTTEST)
296 RTTestFailed(pThis->hTestingTest, "%s", pThis->TestingData.String.sz);
297 VMMDEV_TESTING_OUTPUT(("testing: FAILED '%s'\n", pThis->TestingData.String.sz));
298 break;
299 case VMMDEV_TESTING_CMD_SKIPPED:
300 if (pThis->hTestingTest != NIL_RTTEST)
301 {
302 if (off)
303 RTTestSkipped(pThis->hTestingTest, "%s", pThis->TestingData.String.sz);
304 else
305 RTTestSkipped(pThis->hTestingTest, NULL);
306 }
307 VMMDEV_TESTING_OUTPUT(("testing: SKIPPED '%s'\n", pThis->TestingData.String.sz));
308 break;
309 }
310#else
311 return VINF_IOM_R3_IOPORT_WRITE;
312#endif
313 }
314 return VINF_SUCCESS;
315 }
316 break;
317
318 case VMMDEV_TESTING_CMD_TERM:
319 case VMMDEV_TESTING_CMD_SUB_DONE:
320 if ( off == 0
321 && cb == 4)
322 {
323#ifdef IN_RING3
324 pThis->TestingData.Error.c = u32;
325 if (uCmd == VMMDEV_TESTING_CMD_TERM)
326 {
327 if (pThis->hTestingTest != NIL_RTTEST)
328 {
329 while (RTTestErrorCount(pThis->hTestingTest) < u32)
330 RTTestErrorInc(pThis->hTestingTest); /* A bit stupid, but does the trick. */
331 RTTestSubDone(pThis->hTestingTest);
332 RTTestSummaryAndDestroy(pThis->hTestingTest);
333 pThis->hTestingTest = NIL_RTTEST;
334 }
335 VMMDEV_TESTING_OUTPUT(("testing: TERM - %u errors\n", u32));
336 }
337 else
338 {
339 if (pThis->hTestingTest != NIL_RTTEST)
340 {
341 while (RTTestSubErrorCount(pThis->hTestingTest) < u32)
342 RTTestErrorInc(pThis->hTestingTest); /* A bit stupid, but does the trick. */
343 RTTestSubDone(pThis->hTestingTest);
344 }
345 VMMDEV_TESTING_OUTPUT(("testing: SUB_DONE - %u errors\n", u32));
346 }
347 return VINF_SUCCESS;
348#else
349 return VINF_IOM_R3_IOPORT_WRITE;
350#endif
351 }
352 break;
353
354 case VMMDEV_TESTING_CMD_VALUE:
355 if (cb == 4)
356 {
357 if (off == 0)
358 pThis->TestingData.Value.u64Value.s.Lo = u32;
359 else if (off == 4)
360 pThis->TestingData.Value.u64Value.s.Hi = u32;
361 else if (off == 8)
362 pThis->TestingData.Value.u32Unit = u32;
363 else
364 break;
365 pThis->offTestingData = off + 4;
366 return VINF_SUCCESS;
367 }
368 if ( off >= 12
369 && cb == 1
370 && off - 12 < sizeof(pThis->TestingData.Value.szName) - 1)
371 {
372 if (u32)
373 {
374 pThis->TestingData.Value.szName[off - 12] = u32;
375 pThis->offTestingData = off + 1;
376 }
377 else
378 {
379#ifdef IN_RING3
380 pThis->TestingData.Value.szName[off - 12] = '\0';
381
382 RTTESTUNIT enmUnit = (RTTESTUNIT)pThis->TestingData.Value.u32Unit;
383 if (enmUnit <= RTTESTUNIT_INVALID || enmUnit >= RTTESTUNIT_END)
384 {
385 VMMDEV_TESTING_OUTPUT(("Invalid log value unit %#x\n", pThis->TestingData.Value.u32Unit));
386 enmUnit = RTTESTUNIT_NONE;
387 }
388 if (pThis->hTestingTest != NIL_RTTEST)
389 RTTestValue(pThis->hTestingTest, pThis->TestingData.Value.szName,
390 pThis->TestingData.Value.u64Value.u, enmUnit);
391
392 VMMDEV_TESTING_OUTPUT(("testing: VALUE '%s'%*s: %'9llu (%#llx) [%u]\n",
393 pThis->TestingData.Value.szName,
394 off - 12 > 48 ? 0 : 48 - (off - 12), "",
395 pThis->TestingData.Value.u64Value.u, pThis->TestingData.Value.u64Value.u,
396 pThis->TestingData.Value.u32Unit));
397#else
398 return VINF_IOM_R3_IOPORT_WRITE;
399#endif
400 }
401 return VINF_SUCCESS;
402 }
403 break;
404
405
406 /*
407 * RTTestValue with the return from DBGFR3RegNmQuery.
408 */
409 case VMMDEV_TESTING_CMD_VALUE_REG:
410 {
411 if ( off < sizeof(pThis->TestingData.String.sz) - 1
412 && cb == 1)
413 {
414 pThis->TestingData.String.sz[off] = u32;
415 if (u32)
416 pThis->offTestingData = off + 1;
417 else
418#ifdef IN_RING3
419 vmmdevTestingCmdExec_ValueReg(pDevIns, pThis);
420#else
421 return VINF_IOM_R3_IOPORT_WRITE;
422#endif
423 return VINF_SUCCESS;
424 }
425 break;
426
427 }
428
429 default:
430 break;
431 }
432 Log(("VMMDEV_TESTING_IOPORT_CMD: bad access; cmd=%#x off=%#x cb=%#x u32=%#x\n", uCmd, off, cb, u32));
433 return VINF_SUCCESS;
434 }
435
436 default:
437 break;
438 }
439
440 return VERR_IOM_IOPORT_UNUSED;
441}
442
443
444/**
445 * @callback_method_impl{FNIOMIOPORTIN}
446 */
447PDMBOTHCBDECL(int) vmmdevTestingIoRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
448{
449 VMMDevState *pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
450
451 switch (Port)
452 {
453 /*
454 * The NOP I/O ports are used for performance measurements.
455 */
456 case VMMDEV_TESTING_IOPORT_NOP:
457 switch (cb)
458 {
459 case 4:
460 case 2:
461 case 1:
462 break;
463 default:
464 AssertFailed();
465 return VERR_INTERNAL_ERROR_2;
466 }
467 *pu32 = VMMDEV_TESTING_NOP_RET;
468 return VINF_SUCCESS;
469
470 case VMMDEV_TESTING_IOPORT_NOP_R3:
471 switch (cb)
472 {
473 case 4:
474 case 2:
475 case 1:
476#ifndef IN_RING3
477 return VINF_IOM_R3_IOPORT_READ;
478#else
479 *pu32 = VMMDEV_TESTING_NOP_RET;
480 return VINF_SUCCESS;
481#endif
482 default:
483 AssertFailed();
484 return VERR_INTERNAL_ERROR_2;
485 }
486
487 /*
488 * The timestamp I/O ports are obviously used for getting a good fix
489 * on the current time (as seen by the host?).
490 *
491 * The high word is latched when reading the low, so reading low + high
492 * gives you a 64-bit timestamp value.
493 */
494 case VMMDEV_TESTING_IOPORT_TS_LOW:
495 if (cb == 4)
496 {
497 uint64_t NowTS = RTTimeNanoTS();
498 *pu32 = (uint32_t)NowTS;
499 pThis->u32TestingHighTimestamp = (uint32_t)(NowTS >> 32);
500 return VINF_SUCCESS;
501 }
502 break;
503
504 case VMMDEV_TESTING_IOPORT_TS_HIGH:
505 if (cb == 4)
506 {
507 *pu32 = pThis->u32TestingHighTimestamp;
508 return VINF_SUCCESS;
509 }
510 break;
511
512 /*
513 * The command and data registers are write-only.
514 */
515 case VMMDEV_TESTING_IOPORT_CMD:
516 case VMMDEV_TESTING_IOPORT_DATA:
517 break;
518
519 default:
520 break;
521 }
522
523 return VERR_IOM_IOPORT_UNUSED;
524}
525
526
527#ifdef IN_RING3
528
529/**
530 * Initializes the testing part of the VMMDev if enabled.
531 *
532 * @returns VBox status code.
533 * @param pDevIns The VMMDev device instance.
534 */
535void vmmdevTestingTerminate(PPDMDEVINS pDevIns)
536{
537 VMMDevState *pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
538 if (!pThis->fTestingEnabled)
539 return;
540
541 if (pThis->hTestingTest != NIL_RTTEST)
542 {
543 RTTestFailed(pThis->hTestingTest, "Still open at vmmdev destruction.");
544 RTTestSummaryAndDestroy(pThis->hTestingTest);
545 pThis->hTestingTest = NIL_RTTEST;
546 }
547}
548
549
550/**
551 * Initializes the testing part of the VMMDev if enabled.
552 *
553 * @returns VBox status code.
554 * @param pDevIns The VMMDev device instance.
555 */
556int vmmdevTestingInitialize(PPDMDEVINS pDevIns)
557{
558 VMMDevState *pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
559 int rc;
560
561 if (!pThis->fTestingEnabled)
562 return VINF_SUCCESS;
563
564 if (pThis->fTestingMMIO)
565 {
566 /*
567 * Register a chunk of MMIO memory that we'll use for various
568 * tests interfaces. Optional, needs to be explicitly enabled.
569 */
570 rc = PDMDevHlpMMIORegister(pDevIns, VMMDEV_TESTING_MMIO_BASE, VMMDEV_TESTING_MMIO_SIZE, NULL /*pvUser*/,
571 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
572 vmmdevTestingMmioWrite, vmmdevTestingMmioRead, "VMMDev Testing");
573 AssertRCReturn(rc, rc);
574 if (pThis->fRZEnabled)
575 {
576 rc = PDMDevHlpMMIORegisterR0(pDevIns, VMMDEV_TESTING_MMIO_BASE, VMMDEV_TESTING_MMIO_SIZE, NIL_RTR0PTR /*pvUser*/,
577 "vmmdevTestingMmioWrite", "vmmdevTestingMmioRead");
578 AssertRCReturn(rc, rc);
579 rc = PDMDevHlpMMIORegisterRC(pDevIns, VMMDEV_TESTING_MMIO_BASE, VMMDEV_TESTING_MMIO_SIZE, NIL_RTRCPTR /*pvUser*/,
580 "vmmdevTestingMmioWrite", "vmmdevTestingMmioRead");
581 AssertRCReturn(rc, rc);
582 }
583 }
584
585
586 /*
587 * Register the I/O ports used for testing.
588 */
589 rc = PDMDevHlpIOPortRegister(pDevIns, VMMDEV_TESTING_IOPORT_BASE, VMMDEV_TESTING_IOPORT_COUNT, NULL,
590 vmmdevTestingIoWrite,
591 vmmdevTestingIoRead,
592 NULL /*pfnOutStr*/,
593 NULL /*pfnInStr*/,
594 "VMMDev Testing");
595 AssertRCReturn(rc, rc);
596 if (pThis->fRZEnabled)
597 {
598 rc = PDMDevHlpIOPortRegisterR0(pDevIns, VMMDEV_TESTING_IOPORT_BASE, VMMDEV_TESTING_IOPORT_COUNT, NIL_RTR0PTR /*pvUser*/,
599 "vmmdevTestingIoWrite",
600 "vmmdevTestingIoRead",
601 NULL /*pszOutStr*/,
602 NULL /*pszInStr*/,
603 "VMMDev Testing");
604 AssertRCReturn(rc, rc);
605 rc = PDMDevHlpIOPortRegisterRC(pDevIns, VMMDEV_TESTING_IOPORT_BASE, VMMDEV_TESTING_IOPORT_COUNT, NIL_RTRCPTR /*pvUser*/,
606 "vmmdevTestingIoWrite",
607 "vmmdevTestingIoRead",
608 NULL /*pszOutStr*/,
609 NULL /*pszInStr*/,
610 "VMMDev Testing");
611 AssertRCReturn(rc, rc);
612 }
613
614 /*
615 * Open the XML output file(/pipe/whatever) if specfied.
616 */
617 rc = RTTestCreateEx("VMMDevTesting", RTTEST_C_USE_ENV | RTTEST_C_NO_TLS | RTTEST_C_XML_DELAY_TOP_TEST,
618 RTTESTLVL_INVALID, -1 /*iNativeTestPipe*/, pThis->pszTestingXmlOutput, &pThis->hTestingTest);
619 if (RT_FAILURE(rc))
620 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS, "Error creating testing instance");
621
622 return VINF_SUCCESS;
623}
624
625#endif /* IN_RING3 */
626#endif /* !VBOX_WITHOUT_TESTING_FEATURES */
627
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