VirtualBox

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

Last change on this file since 60404 was 60096, checked in by vboxsync, 8 years ago

VMMDev: Added VMMDEV_TESTING_CMD_PRINT for more output on the host.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.7 KB
Line 
1/* $Id: VMMDevTesting.cpp 60096 2016-03-18 13:13:44Z 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-2015 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 case VMMDEV_TESTING_CMD_PRINT:
268 if ( off < sizeof(pThis->TestingData.String.sz) - 1
269 && cb == 1)
270 {
271 if (u32)
272 {
273 pThis->TestingData.String.sz[off] = u32;
274 pThis->offTestingData = off + 1;
275 }
276 else
277 {
278#ifdef IN_RING3
279 pThis->TestingData.String.sz[off] = '\0';
280 switch (uCmd)
281 {
282 case VMMDEV_TESTING_CMD_INIT:
283 VMMDEV_TESTING_OUTPUT(("testing: INIT '%s'\n", pThis->TestingData.String.sz));
284 if (pThis->hTestingTest != NIL_RTTEST)
285 {
286 RTTestChangeName(pThis->hTestingTest, pThis->TestingData.String.sz);
287 RTTestBanner(pThis->hTestingTest);
288 }
289 break;
290 case VMMDEV_TESTING_CMD_SUB_NEW:
291 VMMDEV_TESTING_OUTPUT(("testing: SUB_NEW '%s'\n", pThis->TestingData.String.sz));
292 if (pThis->hTestingTest != NIL_RTTEST)
293 RTTestSub(pThis->hTestingTest, pThis->TestingData.String.sz);
294 break;
295 case VMMDEV_TESTING_CMD_FAILED:
296 if (pThis->hTestingTest != NIL_RTTEST)
297 RTTestFailed(pThis->hTestingTest, "%s", pThis->TestingData.String.sz);
298 VMMDEV_TESTING_OUTPUT(("testing: FAILED '%s'\n", pThis->TestingData.String.sz));
299 break;
300 case VMMDEV_TESTING_CMD_SKIPPED:
301 if (pThis->hTestingTest != NIL_RTTEST)
302 {
303 if (off)
304 RTTestSkipped(pThis->hTestingTest, "%s", pThis->TestingData.String.sz);
305 else
306 RTTestSkipped(pThis->hTestingTest, NULL);
307 }
308 VMMDEV_TESTING_OUTPUT(("testing: SKIPPED '%s'\n", pThis->TestingData.String.sz));
309 break;
310 case VMMDEV_TESTING_CMD_PRINT:
311 if (pThis->hTestingTest != NIL_RTTEST && off)
312 RTTestPrintf(pThis->hTestingTest, RTTESTLVL_ALWAYS, "%s", pThis->TestingData.String.sz);
313 VMMDEV_TESTING_OUTPUT(("testing: '%s'\n", pThis->TestingData.String.sz));
314 break;
315 }
316#else
317 return VINF_IOM_R3_IOPORT_WRITE;
318#endif
319 }
320 return VINF_SUCCESS;
321 }
322 break;
323
324 case VMMDEV_TESTING_CMD_TERM:
325 case VMMDEV_TESTING_CMD_SUB_DONE:
326 if ( off == 0
327 && cb == 4)
328 {
329#ifdef IN_RING3
330 pThis->TestingData.Error.c = u32;
331 if (uCmd == VMMDEV_TESTING_CMD_TERM)
332 {
333 if (pThis->hTestingTest != NIL_RTTEST)
334 {
335 while (RTTestErrorCount(pThis->hTestingTest) < u32)
336 RTTestErrorInc(pThis->hTestingTest); /* A bit stupid, but does the trick. */
337 RTTestSubDone(pThis->hTestingTest);
338 RTTestSummaryAndDestroy(pThis->hTestingTest);
339 pThis->hTestingTest = NIL_RTTEST;
340 }
341 VMMDEV_TESTING_OUTPUT(("testing: TERM - %u errors\n", u32));
342 }
343 else
344 {
345 if (pThis->hTestingTest != NIL_RTTEST)
346 {
347 while (RTTestSubErrorCount(pThis->hTestingTest) < u32)
348 RTTestErrorInc(pThis->hTestingTest); /* A bit stupid, but does the trick. */
349 RTTestSubDone(pThis->hTestingTest);
350 }
351 VMMDEV_TESTING_OUTPUT(("testing: SUB_DONE - %u errors\n", u32));
352 }
353 return VINF_SUCCESS;
354#else
355 return VINF_IOM_R3_IOPORT_WRITE;
356#endif
357 }
358 break;
359
360 case VMMDEV_TESTING_CMD_VALUE:
361 if (cb == 4)
362 {
363 if (off == 0)
364 pThis->TestingData.Value.u64Value.s.Lo = u32;
365 else if (off == 4)
366 pThis->TestingData.Value.u64Value.s.Hi = u32;
367 else if (off == 8)
368 pThis->TestingData.Value.u32Unit = u32;
369 else
370 break;
371 pThis->offTestingData = off + 4;
372 return VINF_SUCCESS;
373 }
374 if ( off >= 12
375 && cb == 1
376 && off - 12 < sizeof(pThis->TestingData.Value.szName) - 1)
377 {
378 if (u32)
379 {
380 pThis->TestingData.Value.szName[off - 12] = u32;
381 pThis->offTestingData = off + 1;
382 }
383 else
384 {
385#ifdef IN_RING3
386 pThis->TestingData.Value.szName[off - 12] = '\0';
387
388 RTTESTUNIT enmUnit = (RTTESTUNIT)pThis->TestingData.Value.u32Unit;
389 if (enmUnit <= RTTESTUNIT_INVALID || enmUnit >= RTTESTUNIT_END)
390 {
391 VMMDEV_TESTING_OUTPUT(("Invalid log value unit %#x\n", pThis->TestingData.Value.u32Unit));
392 enmUnit = RTTESTUNIT_NONE;
393 }
394 if (pThis->hTestingTest != NIL_RTTEST)
395 RTTestValue(pThis->hTestingTest, pThis->TestingData.Value.szName,
396 pThis->TestingData.Value.u64Value.u, enmUnit);
397
398 VMMDEV_TESTING_OUTPUT(("testing: VALUE '%s'%*s: %'9llu (%#llx) [%u]\n",
399 pThis->TestingData.Value.szName,
400 off - 12 > 48 ? 0 : 48 - (off - 12), "",
401 pThis->TestingData.Value.u64Value.u, pThis->TestingData.Value.u64Value.u,
402 pThis->TestingData.Value.u32Unit));
403#else
404 return VINF_IOM_R3_IOPORT_WRITE;
405#endif
406 }
407 return VINF_SUCCESS;
408 }
409 break;
410
411
412 /*
413 * RTTestValue with the output from DBGFR3RegNmQuery.
414 */
415 case VMMDEV_TESTING_CMD_VALUE_REG:
416 {
417 if ( off < sizeof(pThis->TestingData.String.sz) - 1
418 && cb == 1)
419 {
420 pThis->TestingData.String.sz[off] = u32;
421 if (u32)
422 pThis->offTestingData = off + 1;
423 else
424#ifdef IN_RING3
425 vmmdevTestingCmdExec_ValueReg(pDevIns, pThis);
426#else
427 return VINF_IOM_R3_IOPORT_WRITE;
428#endif
429 return VINF_SUCCESS;
430 }
431 break;
432 }
433
434 default:
435 break;
436 }
437 Log(("VMMDEV_TESTING_IOPORT_CMD: bad access; cmd=%#x off=%#x cb=%#x u32=%#x\n", uCmd, off, cb, u32));
438 return VINF_SUCCESS;
439 }
440
441 default:
442 break;
443 }
444
445 return VERR_IOM_IOPORT_UNUSED;
446}
447
448
449/**
450 * @callback_method_impl{FNIOMIOPORTIN}
451 */
452PDMBOTHCBDECL(int) vmmdevTestingIoRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
453{
454 VMMDevState *pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
455
456 switch (Port)
457 {
458 /*
459 * The NOP I/O ports are used for performance measurements.
460 */
461 case VMMDEV_TESTING_IOPORT_NOP:
462 switch (cb)
463 {
464 case 4:
465 case 2:
466 case 1:
467 break;
468 default:
469 AssertFailed();
470 return VERR_INTERNAL_ERROR_2;
471 }
472 *pu32 = VMMDEV_TESTING_NOP_RET;
473 return VINF_SUCCESS;
474
475 case VMMDEV_TESTING_IOPORT_NOP_R3:
476 switch (cb)
477 {
478 case 4:
479 case 2:
480 case 1:
481#ifndef IN_RING3
482 return VINF_IOM_R3_IOPORT_READ;
483#else
484 *pu32 = VMMDEV_TESTING_NOP_RET;
485 return VINF_SUCCESS;
486#endif
487 default:
488 AssertFailed();
489 return VERR_INTERNAL_ERROR_2;
490 }
491
492 /*
493 * The timestamp I/O ports are obviously used for getting a good fix
494 * on the current time (as seen by the host?).
495 *
496 * The high word is latched when reading the low, so reading low + high
497 * gives you a 64-bit timestamp value.
498 */
499 case VMMDEV_TESTING_IOPORT_TS_LOW:
500 if (cb == 4)
501 {
502 uint64_t NowTS = RTTimeNanoTS();
503 *pu32 = (uint32_t)NowTS;
504 pThis->u32TestingHighTimestamp = (uint32_t)(NowTS >> 32);
505 return VINF_SUCCESS;
506 }
507 break;
508
509 case VMMDEV_TESTING_IOPORT_TS_HIGH:
510 if (cb == 4)
511 {
512 *pu32 = pThis->u32TestingHighTimestamp;
513 return VINF_SUCCESS;
514 }
515 break;
516
517 /*
518 * The command and data registers are write-only.
519 */
520 case VMMDEV_TESTING_IOPORT_CMD:
521 case VMMDEV_TESTING_IOPORT_DATA:
522 break;
523
524 default:
525 break;
526 }
527
528 return VERR_IOM_IOPORT_UNUSED;
529}
530
531
532#ifdef IN_RING3
533
534/**
535 * Initializes the testing part of the VMMDev if enabled.
536 *
537 * @returns VBox status code.
538 * @param pDevIns The VMMDev device instance.
539 */
540void vmmdevTestingTerminate(PPDMDEVINS pDevIns)
541{
542 VMMDevState *pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
543 if (!pThis->fTestingEnabled)
544 return;
545
546 if (pThis->hTestingTest != NIL_RTTEST)
547 {
548 RTTestFailed(pThis->hTestingTest, "Still open at vmmdev destruction.");
549 RTTestSummaryAndDestroy(pThis->hTestingTest);
550 pThis->hTestingTest = NIL_RTTEST;
551 }
552}
553
554
555/**
556 * Initializes the testing part of the VMMDev if enabled.
557 *
558 * @returns VBox status code.
559 * @param pDevIns The VMMDev device instance.
560 */
561int vmmdevTestingInitialize(PPDMDEVINS pDevIns)
562{
563 VMMDevState *pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
564 int rc;
565
566 if (!pThis->fTestingEnabled)
567 return VINF_SUCCESS;
568
569 if (pThis->fTestingMMIO)
570 {
571 /*
572 * Register a chunk of MMIO memory that we'll use for various
573 * tests interfaces. Optional, needs to be explicitly enabled.
574 */
575 rc = PDMDevHlpMMIORegister(pDevIns, VMMDEV_TESTING_MMIO_BASE, VMMDEV_TESTING_MMIO_SIZE, NULL /*pvUser*/,
576 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
577 vmmdevTestingMmioWrite, vmmdevTestingMmioRead, "VMMDev Testing");
578 AssertRCReturn(rc, rc);
579 if (pThis->fRZEnabled)
580 {
581 rc = PDMDevHlpMMIORegisterR0(pDevIns, VMMDEV_TESTING_MMIO_BASE, VMMDEV_TESTING_MMIO_SIZE, NIL_RTR0PTR /*pvUser*/,
582 "vmmdevTestingMmioWrite", "vmmdevTestingMmioRead");
583 AssertRCReturn(rc, rc);
584 rc = PDMDevHlpMMIORegisterRC(pDevIns, VMMDEV_TESTING_MMIO_BASE, VMMDEV_TESTING_MMIO_SIZE, NIL_RTRCPTR /*pvUser*/,
585 "vmmdevTestingMmioWrite", "vmmdevTestingMmioRead");
586 AssertRCReturn(rc, rc);
587 }
588 }
589
590
591 /*
592 * Register the I/O ports used for testing.
593 */
594 rc = PDMDevHlpIOPortRegister(pDevIns, VMMDEV_TESTING_IOPORT_BASE, VMMDEV_TESTING_IOPORT_COUNT, NULL,
595 vmmdevTestingIoWrite,
596 vmmdevTestingIoRead,
597 NULL /*pfnOutStr*/,
598 NULL /*pfnInStr*/,
599 "VMMDev Testing");
600 AssertRCReturn(rc, rc);
601 if (pThis->fRZEnabled)
602 {
603 rc = PDMDevHlpIOPortRegisterR0(pDevIns, VMMDEV_TESTING_IOPORT_BASE, VMMDEV_TESTING_IOPORT_COUNT, NIL_RTR0PTR /*pvUser*/,
604 "vmmdevTestingIoWrite",
605 "vmmdevTestingIoRead",
606 NULL /*pszOutStr*/,
607 NULL /*pszInStr*/,
608 "VMMDev Testing");
609 AssertRCReturn(rc, rc);
610 rc = PDMDevHlpIOPortRegisterRC(pDevIns, VMMDEV_TESTING_IOPORT_BASE, VMMDEV_TESTING_IOPORT_COUNT, NIL_RTRCPTR /*pvUser*/,
611 "vmmdevTestingIoWrite",
612 "vmmdevTestingIoRead",
613 NULL /*pszOutStr*/,
614 NULL /*pszInStr*/,
615 "VMMDev Testing");
616 AssertRCReturn(rc, rc);
617 }
618
619 /*
620 * Open the XML output file(/pipe/whatever) if specfied.
621 */
622 rc = RTTestCreateEx("VMMDevTesting", RTTEST_C_USE_ENV | RTTEST_C_NO_TLS | RTTEST_C_XML_DELAY_TOP_TEST,
623 RTTESTLVL_INVALID, -1 /*iNativeTestPipe*/, pThis->pszTestingXmlOutput, &pThis->hTestingTest);
624 if (RT_FAILURE(rc))
625 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS, "Error creating testing instance");
626
627 return VINF_SUCCESS;
628}
629
630#endif /* IN_RING3 */
631#endif /* !VBOX_WITHOUT_TESTING_FEATURES */
632
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use