VirtualBox

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

Last change on this file since 31989 was 31989, checked in by vboxsync, 14 years ago

VMM: Removed the PGMR3DumpHierarchyGC/HC functions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.3 KB
Line 
1/* $Id: tstMicro.cpp 31989 2010-08-26 12:43:18Z vboxsync $ */
2/** @file
3 * Micro Testcase, profiling special CPU operations.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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* 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/pdmapi.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/initterm.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 (RT_FAILURE(rc))
85 RTPrintf(TESTCASE ": %-25s %10llu %10llu %10llu - %Rrc 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 (RT_FAILURE(rc))
116 RTPrintf(TESTCASE ": %-25s %10llu %10llu %10llu %10llu %10llu - %Rrc 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 RTRCPTR RCPtr = CPUMGetHyperGDTR(VMMGetCpu0(pVM), NULL);
160 PX86DESC paGDTEs = (PX86DESC)MMHyperRCToR3(pVM, RCPtr);
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 = 0xffff;
170 pGDTE->Gen.u4LimitHigh = 0xf;
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 = 0xffff;
182 pGDTE->Gen.u4LimitHigh = 0xf;
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 = PDMR3LdrLoadRC(pVM, NULL, "tstMicroRC.gc");
202 if (RT_FAILURE(rc))
203 {
204 RTPrintf(TESTCASE ": Failed to load tstMicroRC.gc, rc=%Rra\n", rc);
205 return rc;
206 }
207 RTRCPTR RCPtrEntry;
208 rc = PDMR3LdrGetSymbolRC(pVM, "tstMicroRC.gc", "tstMicroRC", &RCPtrEntry);
209 if (RT_FAILURE(rc))
210 {
211 RTPrintf(TESTCASE ": Failed to resolve the 'tstMicroRC' entry point in tstMicroRC.gc, rc=%Rra\n", rc);
212 return rc;
213 }
214 RTRCPTR RCPtrStart;
215 rc = PDMR3LdrGetSymbolRC(pVM, "tstMicroRC.gc", "tstMicroRCAsmStart", &RCPtrStart);
216 if (RT_FAILURE(rc))
217 {
218 RTPrintf(TESTCASE ": Failed to resolve the 'tstMicroRCAsmStart' entry point in tstMicroRC.gc, rc=%Rra\n", rc);
219 return rc;
220 }
221 RTRCPTR RCPtrEnd;
222 rc = PDMR3LdrGetSymbolRC(pVM, "tstMicroRC.gc", "tstMicroRCAsmEnd", &RCPtrEnd);
223 if (RT_FAILURE(rc))
224 {
225 RTPrintf(TESTCASE ": Failed to resolve the 'tstMicroRCAsmEnd' entry point in tstMicroRC.gc, rc=%Rra\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 (RT_FAILURE(rc))
235 {
236 RTPrintf(TESTCASE ": Failed to resolve allocate instance memory (%d bytes), rc=%Rra\n", sizeof(*pTst), rc);
237 return rc;
238 }
239 pTst->RCPtr = MMHyperR3ToRC(pVM, pTst);
240 pTst->RCPtrStack = MMHyperR3ToRC(pVM, &pTst->au8Stack[sizeof(pTst->au8Stack) - 32]);
241
242 /* the page must be writable from user mode */
243 rc = PGMMapModifyPage(pVM, pTst->RCPtr, sizeof(*pTst), X86_PTE_US | X86_PTE_RW, ~(uint64_t)(X86_PTE_US | X86_PTE_RW));
244 if (RT_FAILURE(rc))
245 {
246 RTPrintf(TESTCASE ": PGMMapModifyPage -> rc=%Rra\n", rc);
247 return rc;
248 }
249
250 /* all the code must be executable from R3. */
251 rc = PGMMapModifyPage(pVM, RCPtrStart, RCPtrEnd - RCPtrStart + PAGE_SIZE, X86_PTE_US, ~(uint64_t)X86_PTE_US);
252 if (RT_FAILURE(rc))
253 {
254 RTPrintf(TESTCASE ": PGMMapModifyPage -> rc=%Rra\n", rc);
255 return rc;
256 }
257 DBGFR3PagingDumpEx(pVM, 0 /*idCpu*/, DBGFPGDMP_FLAGS_CURRENT_CR3 | DBGFPGDMP_FLAGS_CURRENT_MODE
258 | DBGFPGDMP_FLAGS_SHADOW | DBGFPGDMP_FLAGS_HEADER | DBGFPGDMP_FLAGS_PRINT_CR3,
259 0 /*cr3*/, 0 /*u64FirstAddr*/, UINT64_MAX /*u64LastAddr*/, 99 /*cMaxDepth*/, NULL);
260
261#if 0
262 /*
263 * Disassemble the assembly...
264 */
265 RTGCPTR GCPtr = RCPtrStart;
266 while (GCPtr < RCPtrEnd)
267 {
268 size_t cb = 0;
269 char sz[256];
270 int rc = DBGFR3DisasInstrEx(pVM, CPUMGetHyperCS(pVM), GCPtr, 0, sz, sizeof(sz), &cb);
271 if (RT_SUCCESS(rc))
272 RTLogPrintf("%s\n", sz);
273 else
274 {
275 RTLogPrintf("%RGv rc=%Rrc\n", GCPtr, rc);
276 cb = 1;
277 }
278 GCPtr += cb;
279 }
280#endif
281
282 /*
283 * Do the profiling.
284 */
285 /* execute the instruction profiling tests */
286 PrintHeaderInstr();
287 int i;
288 for (i = TSTMICROTEST_OVERHEAD; i < TSTMICROTEST_TRAP_FIRST; i++)
289 {
290 TSTMICROTEST enmTest = (TSTMICROTEST)i;
291 uint64_t cMin = ~0;
292 uint64_t cMax = 0;
293 uint64_t cTotal = 0;
294 unsigned cSamples = 0;
295 rc = VINF_SUCCESS;
296 for (int c = 0; c < 100; c++)
297 {
298 int rc2 = VMMR3CallRC(pVM, RCPtrEntry, 2, pTst->RCPtr, enmTest);
299 if (RT_SUCCESS(rc2))
300 {
301 uint64_t u64 = pTst->aResults[enmTest].cTotalTicks;
302 if (cMin > u64)
303 cMin = u64;
304 if (cMax < u64)
305 cMax = u64;
306 cTotal += u64;
307 cSamples++;
308 }
309 else if (RT_SUCCESS(rc))
310 rc = rc2;
311 }
312 uint64_t cAvg = cTotal / (cSamples ? cSamples : 1);
313 pTst->aResults[enmTest].cTotalTicks = cAvg;
314 PrintResultInstr(pTst, enmTest, rc, cMin, cAvg, cMax);
315 /* store the overhead */
316 if (enmTest == TSTMICROTEST_OVERHEAD)
317 pTst->u64Overhead = cMin;
318 }
319
320
321 /* execute the trap/cycle profiling tests. */
322 RTPrintf("\n");
323 PrintHeaderTraps();
324 for (i = TSTMICROTEST_TRAP_FIRST; i < TSTMICROTEST_MAX; i++)
325 {
326 TSTMICROTEST enmTest = (TSTMICROTEST)i;
327 rc = VMMR3CallRC(pVM, RCPtrEntry, 2, pTst->RCPtr, enmTest);
328 PrintResultTrap(pTst, enmTest, rc);
329 }
330
331
332 RTPrintf(TESTCASE ": done!\n");
333 return VINF_SUCCESS;
334}
335
336
337int main(int argc, char **argv)
338{
339 int rcRet = 0; /* error count. */
340
341 RTR3InitAndSUPLib();
342
343 /*
344 * Create empty VM.
345 */
346 PVM pVM;
347 int rc = VMR3Create(1, NULL, NULL, NULL, NULL, &pVM);
348 if (RT_SUCCESS(rc))
349 {
350 /*
351 * Do testing.
352 */
353 rc = VMR3ReqCallVoidWait(pVM, VMCPUID_ANY, (PFNRT)doit, 1, pVM);
354 AssertRC(rc);
355 STAMR3Dump(pVM, "*");
356
357 /*
358 * Cleanup.
359 */
360 rc = VMR3Destroy(pVM);
361 if (!RT_SUCCESS(rc))
362 {
363 RTPrintf(TESTCASE ": error: failed to destroy vm! rc=%d\n", rc);
364 rcRet++;
365 }
366 }
367 else
368 {
369 RTPrintf(TESTCASE ": fatal error: failed to create vm! rc=%d\n", rc);
370 rcRet++;
371 }
372
373 return rcRet;
374}
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