VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstMicro.cpp@ 5999

Last change on this file since 5999 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.1 KB
Line 
1/* $Id: tstMicro.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
2/** @file
3 * Micro Testcase, profiling special CPU operations.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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/vm.h>
22#include <VBox/vmm.h>
23#include <VBox/mm.h>
24#include <VBox/cpum.h>
25#include <VBox/pdm.h>
26#include <VBox/dbgf.h>
27#include <VBox/pgm.h>
28#include <VBox/err.h>
29#include <VBox/param.h>
30
31#include <VBox/log.h>
32#include <iprt/assert.h>
33#include <iprt/runtime.h>
34#include <iprt/stream.h>
35#include <iprt/string.h>
36#include <iprt/semaphore.h>
37
38#include "tstMicro.h"
39
40
41/*******************************************************************************
42* Defined Constants And Macros *
43*******************************************************************************/
44#define TESTCASE "tstVMM"
45
46static const char *GetDescription(TSTMICROTEST enmTest)
47{
48 switch (enmTest)
49 {
50 case TSTMICROTEST_OVERHEAD: return "Overhead";
51 case TSTMICROTEST_INVLPG_0: return "invlpg [0]";
52 case TSTMICROTEST_INVLPG_EIP: return "invlpg [EIP]";
53 case TSTMICROTEST_INVLPG_ESP: return "invlpg [ESP]";
54 case TSTMICROTEST_CR3_RELOAD: return "cr3 reload";
55 case TSTMICROTEST_WP_DISABLE: return "CR0.WP <- 0";
56 case TSTMICROTEST_WP_ENABLE: return "CR0.WP <- 1";
57
58 case TSTMICROTEST_PF_R0: return "R0 #PG (NULL)";
59 case TSTMICROTEST_PF_R1: return "R1 #PG (NULL)";
60 case TSTMICROTEST_PF_R2: return "R2 #PG (NULL)";
61 case TSTMICROTEST_PF_R3: return "R3 #PG (NULL)";
62
63 default:
64 {
65 static char sz[64];
66 RTStrPrintf(sz, sizeof(sz), "%d?", enmTest);
67 return sz;
68 }
69 }
70}
71
72
73static void PrintHeaderInstr(void)
74{
75 RTPrintf(TESTCASE ": %-25s %10s %10s %10s\n",
76 "Test name",
77 "Min",
78 "Avg",
79 "Max");
80}
81
82static void PrintResultInstr(PTSTMICRO pTst, TSTMICROTEST enmTest, int rc, uint64_t cMinTicks, uint64_t cAvgTicks, uint64_t cMaxTicks)
83{
84 if (VBOX_FAILURE(rc))
85 RTPrintf(TESTCASE ": %-25s %10llu %10llu %10llu - %Vrc cr2=%x err=%x eip=%x!\n",
86 GetDescription(enmTest),
87 cMinTicks,
88 cAvgTicks,
89 cMaxTicks,
90 rc,
91 pTst->u32CR2,
92 pTst->u32ErrCd,
93 pTst->u32EIP);
94 else
95 RTPrintf(TESTCASE ": %-25s %10llu %10llu %10llu\n",
96 GetDescription(enmTest),
97 cMinTicks,
98 cAvgTicks,
99 cMaxTicks);
100}
101
102static void PrintHeaderTraps(void)
103{
104 RTPrintf(TESTCASE ": %-25s %10s %10s %10s %10s %10s\n",
105 "Test name",
106 "Total",
107 "ToRx",
108 "Trap",
109 "ToRxTrap",
110 "int42-done");
111}
112
113static void PrintResultTrap(PTSTMICRO pTst, TSTMICROTEST enmTest, int rc)
114{
115 if (VBOX_FAILURE(rc))
116 RTPrintf(TESTCASE ": %-25s %10llu %10llu %10llu %10llu %10llu - %Vrc cr2=%x err=%x eip=%x!\n",
117 GetDescription(enmTest),
118 pTst->aResults[enmTest].cTotalTicks,
119 pTst->aResults[enmTest].cToRxFirstTicks,
120 pTst->aResults[enmTest].cTrapTicks,
121 pTst->aResults[enmTest].cToRxTrapTicks,
122 pTst->aResults[enmTest].cToR0Ticks,
123 rc,
124 pTst->u32CR2,
125 pTst->u32ErrCd,
126 pTst->u32EIP);
127 else
128 RTPrintf(TESTCASE ": %-25s %10llu %10llu %10llu %10llu %10llu\n",
129 GetDescription(enmTest),
130 pTst->aResults[enmTest].cTotalTicks,
131 pTst->aResults[enmTest].cToRxFirstTicks,
132 pTst->aResults[enmTest].cTrapTicks,
133 pTst->aResults[enmTest].cToRxTrapTicks,
134 pTst->aResults[enmTest].cToR0Ticks);
135}
136
137
138/**
139 * 'Allocate' selectors for 32-bit code/data in rings 0-3.
140 *
141 * 0060 - r0 code
142 * 0068 - r0 data
143 *
144 * 1060 - r1 code
145 * 1068 - r1 data
146 *
147 * 2060 - r2 code
148 * 2068 - r2 data
149 *
150 * 3060 - r3 code
151 * 3068 - r3 data
152 *
153 */
154static void SetupSelectors(PVM pVM)
155{
156 /*
157 * Find the GDT - This is a HACK :-)
158 */
159 RTGCPTR GCPtr = CPUMGetHyperGDTR(pVM, NULL);
160 PX86DESC paGDTEs = (PX86DESC)MMHyperGC2HC(pVM, GCPtr);
161
162 for (unsigned i = 0; i <= 3; i++)
163 {
164 RTSEL Sel = (i << 12) + 0x60;
165
166 /* 32-bit code selector. */
167 PX86DESC pGDTE = &paGDTEs[Sel >> X86_SEL_SHIFT];
168 pGDTE->au32[0] = pGDTE->au32[1] = 0;
169 pGDTE->Gen.u16LimitLow = ~0;
170 pGDTE->Gen.u4LimitHigh = ~0;
171 pGDTE->Gen.u1Granularity= 1;
172 pGDTE->Gen.u1Present = 1;
173 pGDTE->Gen.u2Dpl = i;
174 pGDTE->Gen.u1DefBig = 1;
175 pGDTE->Gen.u1DescType = 1; /* !system */
176 pGDTE->Gen.u4Type = X86_SEL_TYPE_ER_ACC;
177
178 /* 32-bit data selector. */
179 pGDTE++;
180 pGDTE->au32[0] = pGDTE->au32[1] = 0;
181 pGDTE->Gen.u16LimitLow = ~0;
182 pGDTE->Gen.u4LimitHigh = ~0;
183 pGDTE->Gen.u1Granularity= 1;
184 pGDTE->Gen.u1Present = 1;
185 pGDTE->Gen.u2Dpl = i;
186 pGDTE->Gen.u1DefBig = 1;
187 pGDTE->Gen.u1DescType = 1; /* !system */
188 pGDTE->Gen.u4Type = X86_SEL_TYPE_RW_ACC;
189 }
190}
191
192
193static DECLCALLBACK(int) doit(PVM pVM)
194{
195 RTPrintf(TESTCASE ": testing...\n");
196 SetupSelectors(pVM);
197
198 /*
199 * Loading the module and resolve the entry point.
200 */
201 int rc = PDMR3LoadGC(pVM, NULL, "tstMicroGC.gc");
202 if (VBOX_FAILURE(rc))
203 {
204 RTPrintf(TESTCASE ": Failed to load tstMicroGC.gc, rc=%Vra\n", rc);
205 return rc;
206 }
207 RTGCPTR GCPtrEntry;
208 rc = PDMR3GetSymbolGC(pVM, "tstMicroGC.gc", "tstMicroGC", &GCPtrEntry);
209 if (VBOX_FAILURE(rc))
210 {
211 RTPrintf(TESTCASE ": Failed to resolve the 'tstMicroGC' entry point in tstMicroGC.gc, rc=%Vra\n", rc);
212 return rc;
213 }
214 RTGCPTR GCPtrStart;
215 rc = PDMR3GetSymbolGC(pVM, "tstMicroGC.gc", "tstMicroGCAsmStart", &GCPtrStart);
216 if (VBOX_FAILURE(rc))
217 {
218 RTPrintf(TESTCASE ": Failed to resolve the 'tstMicroGCAsmStart' entry point in tstMicroGC.gc, rc=%Vra\n", rc);
219 return rc;
220 }
221 RTGCPTR GCPtrEnd;
222 rc = PDMR3GetSymbolGC(pVM, "tstMicroGC.gc", "tstMicroGCAsmEnd", &GCPtrEnd);
223 if (VBOX_FAILURE(rc))
224 {
225 RTPrintf(TESTCASE ": Failed to resolve the 'tstMicroGCAsmEnd' entry point in tstMicroGC.gc, rc=%Vra\n", rc);
226 return rc;
227 }
228
229 /*
230 * Allocate and initialize the instance data.
231 */
232 PTSTMICRO pTst;
233 rc = MMHyperAlloc(pVM, RT_ALIGN_Z(sizeof(*pTst), PAGE_SIZE), PAGE_SIZE, MM_TAG_VM, (void **)&pTst);
234 if (VBOX_FAILURE(rc))
235 {
236 RTPrintf(TESTCASE ": Failed to resolve allocate instance memory (%d bytes), rc=%Vra\n", sizeof(*pTst), rc);
237 return rc;
238 }
239 pTst->GCPtr = MMHyperHC2GC(pVM, pTst);
240 pTst->GCPtrStack = MMHyperHC2GC(pVM, &pTst->au8Stack[sizeof(pTst->au8Stack) - 32]);
241
242 /* the page must be writable from user mode */
243 rc = PGMMapModifyPage(pVM, pTst->GCPtr, sizeof(*pTst), X86_PTE_US | X86_PTE_RW, ~(uint64_t)(X86_PTE_US | X86_PTE_RW));
244 if (VBOX_FAILURE(rc))
245 {
246 RTPrintf(TESTCASE ": PGMMapModifyPage -> rc=%Vra\n", rc);
247 return rc;
248 }
249
250 /* all the code must be executable from R3. */
251 rc = PGMMapModifyPage(pVM, GCPtrStart, GCPtrEnd - GCPtrStart + PAGE_SIZE, X86_PTE_US, ~(uint64_t)X86_PTE_US);
252 if (VBOX_FAILURE(rc))
253 {
254 RTPrintf(TESTCASE ": PGMMapModifyPage -> rc=%Vra\n", rc);
255 return rc;
256 }
257 PGMR3DumpHierarchyHC(pVM, PGMGetHyper32BitCR3(pVM), X86_CR4_PSE, false, 4, NULL);
258
259#if 0
260 /*
261 * Disassemble the assembly...
262 */
263 RTGCPTR GCPtr = GCPtrStart;
264 while (GCPtr < GCPtrEnd)
265 {
266 size_t cb = 0;
267 char sz[256];
268 int rc = DBGFR3DisasInstrEx(pVM, CPUMGetHyperCS(pVM), GCPtr, 0, sz, sizeof(sz), &cb);
269 if (VBOX_SUCCESS(rc))
270 RTLogPrintf("%s\n", sz);
271 else
272 {
273 RTLogPrintf("%VGv rc=%Vrc\n", GCPtr, rc);
274 cb = 1;
275 }
276 GCPtr += cb;
277 }
278#endif
279
280 /*
281 * Do the profiling.
282 */
283 /* execute the instruction profiling tests */
284 PrintHeaderInstr();
285 int i;
286 for (i = TSTMICROTEST_OVERHEAD; i < TSTMICROTEST_TRAP_FIRST; i++)
287 {
288 TSTMICROTEST enmTest = (TSTMICROTEST)i;
289 uint64_t cMin = ~0;
290 uint64_t cMax = 0;
291 uint64_t cTotal = 0;
292 unsigned cSamples = 0;
293 rc = VINF_SUCCESS;
294 for (int c = 0; c < 100; c++)
295 {
296 int rc2 = VMMR3CallGC(pVM, GCPtrEntry, 2, pTst->GCPtr, enmTest);
297 if (VBOX_SUCCESS(rc2))
298 {
299 uint64_t u64 = pTst->aResults[enmTest].cTotalTicks;
300 if (cMin > u64)
301 cMin = u64;
302 if (cMax < u64)
303 cMax = u64;
304 cTotal += u64;
305 cSamples++;
306 }
307 else if (VBOX_SUCCESS(rc))
308 rc = rc2;
309 }
310 uint64_t cAvg = cTotal / (cSamples ? cSamples : 1);
311 pTst->aResults[enmTest].cTotalTicks = cAvg;
312 PrintResultInstr(pTst, enmTest, rc, cMin, cAvg, cMax);
313 /* store the overhead */
314 if (enmTest == TSTMICROTEST_OVERHEAD)
315 pTst->u64Overhead = cMin;
316 }
317
318
319 /* execute the trap/cycle profiling tests. */
320 RTPrintf("\n");
321 PrintHeaderTraps();
322 for (i = TSTMICROTEST_TRAP_FIRST; i < TSTMICROTEST_MAX; i++)
323 {
324 TSTMICROTEST enmTest = (TSTMICROTEST)i;
325 rc = VMMR3CallGC(pVM, GCPtrEntry, 2, pTst->GCPtr, enmTest);
326 PrintResultTrap(pTst, enmTest, rc);
327 }
328
329
330 RTPrintf(TESTCASE ": done!\n");
331 return VINF_SUCCESS;
332}
333
334
335int main(int argc, char **argv)
336{
337 int rcRet = 0; /* error count. */
338
339 RTR3Init();
340
341 /*
342 * Create empty VM.
343 */
344 PVM pVM;
345 int rc = VMR3Create(NULL, NULL, NULL, NULL, &pVM);
346 if (VBOX_SUCCESS(rc))
347 {
348 /*
349 * Do testing.
350 */
351 PVMREQ pReq1 = NULL;
352 rc = VMR3ReqCallVoid(pVM, &pReq1, RT_INDEFINITE_WAIT, (PFNRT)doit, 1, pVM);
353 AssertRC(rc);
354 VMR3ReqFree(pReq1);
355
356 STAMR3Dump(pVM, "*");
357
358 /*
359 * Cleanup.
360 */
361 rc = VMR3Destroy(pVM);
362 if (!VBOX_SUCCESS(rc))
363 {
364 RTPrintf(TESTCASE ": error: failed to destroy vm! rc=%d\n", rc);
365 rcRet++;
366 }
367 }
368 else
369 {
370 RTPrintf(TESTCASE ": fatal error: failed to create vm! rc=%d\n", rc);
371 rcRet++;
372 }
373
374 return rcRet;
375}
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