1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox host drivers - Ring-0 support drivers - Testcases:
|
---|
4 | * Test the interrupt gate feature of the support library
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #include <VBox/sup.h>
|
---|
24 | #include <VBox/vm.h>
|
---|
25 | #include <VBox/vmm.h>
|
---|
26 | #include <VBox/err.h>
|
---|
27 | #include <VBox/param.h>
|
---|
28 | #include <iprt/runtime.h>
|
---|
29 | #include <iprt/stream.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Makes a path to a file in the executable directory.
|
---|
35 | */
|
---|
36 | static char *ExeDirFile(char *pszFile, const char *pszArgv0, const char *pszFilename)
|
---|
37 | {
|
---|
38 | char *psz;
|
---|
39 | char *psz2;
|
---|
40 |
|
---|
41 | strcpy(pszFile, pszArgv0);
|
---|
42 | psz = strrchr(pszFile, '/');
|
---|
43 | psz2 = strrchr(pszFile, '\\');
|
---|
44 | if (psz < psz2)
|
---|
45 | psz = psz2;
|
---|
46 | if (!psz)
|
---|
47 | psz = strrchr(pszFile, ':');
|
---|
48 | if (!psz)
|
---|
49 | {
|
---|
50 | strcpy(pszFile, "./");
|
---|
51 | psz = &pszFile[1];
|
---|
52 | }
|
---|
53 | strcpy(psz + 1, "VMMR0.r0");
|
---|
54 | return pszFile;
|
---|
55 | }
|
---|
56 |
|
---|
57 | int main(int argc, char **argv)
|
---|
58 | {
|
---|
59 | int rcRet = 0;
|
---|
60 | int i;
|
---|
61 | int rc;
|
---|
62 | int cIterations = argc > 1 ? RTStrToUInt32(argv[1]) : 32;
|
---|
63 | if (cIterations == 0)
|
---|
64 | cIterations = 64;
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Init.
|
---|
68 | */
|
---|
69 | RTR3Init();
|
---|
70 | PSUPDRVSESSION pSession;
|
---|
71 | rc = SUPInit(&pSession);
|
---|
72 | rcRet += rc != 0;
|
---|
73 | RTPrintf("tstInt: SUPInit -> rc=%Vrc\n", rc);
|
---|
74 | if (!rc)
|
---|
75 | {
|
---|
76 | /*
|
---|
77 | * Load VMM code.
|
---|
78 | */
|
---|
79 | char szFile[RTPATH_MAX];
|
---|
80 | rc = SUPLoadVMM(ExeDirFile(szFile, argv[0], "VMMR0.r0"));
|
---|
81 | if (!rc)
|
---|
82 | {
|
---|
83 | /*
|
---|
84 | * Create a fake 'VM'.
|
---|
85 | */
|
---|
86 | PVMR0 pVMR0 = NIL_RTR0PTR;
|
---|
87 | PVM pVM = NULL;
|
---|
88 | const unsigned cPages = RT_ALIGN_Z(sizeof(*pVM), PAGE_SIZE) >> PAGE_SHIFT;
|
---|
89 | PSUPPAGE paPages = (PSUPPAGE)RTMemAllocZ(cPages * sizeof(SUPPAGE));
|
---|
90 | if (paPages)
|
---|
91 | rc = SUPLowAlloc(cPages, (void **)&pVM, &pVMR0, &paPages[0]);
|
---|
92 | else
|
---|
93 | rc = VERR_NO_MEMORY;
|
---|
94 | if (VBOX_SUCCESS(rc))
|
---|
95 | {
|
---|
96 | pVM->pVMGC = 0;
|
---|
97 | pVM->pVMR3 = pVM;
|
---|
98 | pVM->pVMR0 = pVMR0;
|
---|
99 | pVM->paVMPagesR3 = paPages;
|
---|
100 | pVM->pSession = pSession;
|
---|
101 |
|
---|
102 | #ifdef VBOX_WITHOUT_IDT_PATCHING
|
---|
103 | rc = SUPSetVMForFastIOCtl(pVMR0);
|
---|
104 | #endif
|
---|
105 | if (!rc)
|
---|
106 | {
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Call VMM code with invalid function.
|
---|
110 | */
|
---|
111 | for (i = cIterations; i > 0; i--)
|
---|
112 | {
|
---|
113 | rc = SUPCallVMMR0(pVMR0, VMMR0_DO_NOP, NULL);
|
---|
114 | if (rc != VINF_SUCCESS)
|
---|
115 | {
|
---|
116 | RTPrintf("tstInt: SUPCallVMMR0 -> rc=%Vrc i=%d Expected VINF_SUCCESS!\n", rc, i);
|
---|
117 | rcRet++;
|
---|
118 | break;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | RTPrintf("tstInt: Performed SUPCallVMMR0 %d times (rc=%Vrc)\n", cIterations, rc);
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Profile it.
|
---|
125 | */
|
---|
126 | if (!rc)
|
---|
127 | {
|
---|
128 | RTTimeNanoTS();
|
---|
129 | uint64_t StartTS = RTTimeNanoTS();
|
---|
130 | uint64_t StartTick = ASMReadTSC();
|
---|
131 | uint64_t MinTicks = UINT64_MAX;
|
---|
132 | for (i = 0; i < 1000000; i++)
|
---|
133 | {
|
---|
134 | uint64_t OneStartTick = ASMReadTSC();
|
---|
135 | rc = SUPCallVMMR0(pVMR0, VMMR0_DO_NOP, NULL);
|
---|
136 | uint64_t Ticks = ASMReadTSC() - OneStartTick;
|
---|
137 | if (Ticks < MinTicks)
|
---|
138 | MinTicks = Ticks;
|
---|
139 |
|
---|
140 | if (RT_UNLIKELY(rc != VINF_SUCCESS))
|
---|
141 | {
|
---|
142 | RTPrintf("tstInt: SUPCallVMMR0 -> rc=%Vrc i=%d Expected VINF_SUCCESS!\n", rc, i);
|
---|
143 | rcRet++;
|
---|
144 | break;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | uint64_t Ticks = ASMReadTSC() - StartTick;
|
---|
148 | uint64_t NanoSecs = RTTimeNanoTS() - StartTS;
|
---|
149 |
|
---|
150 | RTPrintf("tstInt: %d iterations in %llu ns / %llu ticks. %llu ns / %#llu ticks per iteration. Min %llu ticks.\n",
|
---|
151 | i, NanoSecs, Ticks, NanoSecs / i, Ticks / i, MinTicks);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | RTPrintf("tstInt: SUPSetVMForFastIOCtl failed: %Vrc\n", rc);
|
---|
157 | rcRet++;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | RTPrintf("tstInt: SUPContAlloc2(%#zx,,) failed\n", sizeof(*pVM));
|
---|
163 | rcRet++;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * Unload VMM.
|
---|
168 | */
|
---|
169 | rc = SUPUnloadVMM();
|
---|
170 | if (rc)
|
---|
171 | {
|
---|
172 | RTPrintf("tstInt: SUPUnloadVMM failed with rc=%Vrc\n", rc);
|
---|
173 | rcRet++;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | else
|
---|
177 | {
|
---|
178 | RTPrintf("tstInt: SUPLoadVMM failed with rc=%Vrc\n", rc);
|
---|
179 | rcRet++;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * Terminate.
|
---|
184 | */
|
---|
185 | rc = SUPTerm();
|
---|
186 | rcRet += rc != 0;
|
---|
187 | RTPrintf("tstInt: SUPTerm -> rc=%Vrc\n", rc);
|
---|
188 | }
|
---|
189 |
|
---|
190 | return !!rc;
|
---|
191 | }
|
---|
192 |
|
---|