VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/testcase/tstInt.cpp@ 103285

Last change on this file since 103285 was 103285, checked in by vboxsync, 4 months ago

Re-applied r161549 again (Got rid of a lot of deprecated strcpy / strcat calls; now using the IPRT pendants (found by Parfait)), left out some stuff which wasn't wanted, less bloated version of DrvAudio.cpp. bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1/* $Id: tstInt.cpp 103285 2024-02-08 15:27:12Z vboxsync $ */
2/** @file
3 * SUP Testcase - Test the interrupt gate feature of the support library.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <VBox/sup.h>
42#include <VBox/vmm/vmm.h>
43#include <VBox/vmm/gvmm.h>
44#include <VBox/vmm/vm.h>
45#include <iprt/errcore.h>
46#include <VBox/param.h>
47#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
48# include <iprt/asm-amd64-x86.h>
49#else
50# define ASMReadTSC RTTimeSystemNanoTS
51#endif
52#include <iprt/initterm.h>
53#include <iprt/stream.h>
54#include <iprt/string.h>
55#include <iprt/alloc.h>
56#include <iprt/time.h>
57#include <iprt/path.h>
58
59
60int main(int argc, char **argv)
61{
62 int rcRet = 0;
63 int i;
64 int cIterations = argc > 1 ? RTStrToUInt32(argv[1]) : 32;
65 if (cIterations == 0)
66 cIterations = 64;
67
68 /*
69 * Init.
70 */
71 RTR3InitExe(argc, &argv, 0);
72 PSUPDRVSESSION pSession;
73 int rc = SUPR3Init(&pSession);
74 rcRet += rc != 0;
75 RTPrintf("tstInt: SUPR3Init -> rc=%Rrc\n", rc);
76 char szFile[RTPATH_MAX];
77 if (RT_SUCCESS(rc))
78 rc = RTPathExecDir(szFile, sizeof(szFile));
79
80 char szAbsFile[RTPATH_MAX];
81 if (RT_SUCCESS(rc))
82 {
83 rc = RTPathAppend(szFile, sizeof(szFile), "VMMR0.r0");
84 if (RT_SUCCESS(rc))
85 rc = RTPathAbs(szFile, szAbsFile, sizeof(szAbsFile));
86 }
87 if (RT_SUCCESS(rc))
88 {
89 /*
90 * Load VMM code.
91 */
92 RTERRINFOSTATIC ErrInfo;
93 rc = SUPR3LoadVMM(szAbsFile, RTErrInfoInitStatic(&ErrInfo));
94 if (RT_SUCCESS(rc))
95 {
96 /*
97 * Create a tiny dummy VM so we can do NOP calls into it using the fast I/O control path.
98 */
99 GVMMCREATEVMREQ CreateVMReq;
100 CreateVMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
101 CreateVMReq.Hdr.cbReq = sizeof(CreateVMReq);
102 CreateVMReq.pSession = pSession;
103 CreateVMReq.pVMR0 = NIL_RTR0PTR;
104 CreateVMReq.pVMR3 = NULL;
105 CreateVMReq.cCpus = 1;
106 rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_GVMM_CREATE_VM, 0, &CreateVMReq.Hdr);
107 if (RT_SUCCESS(rc))
108 {
109 PVM pVM = CreateVMReq.pVMR3;
110 AssertRelease(RT_VALID_PTR(pVM));
111 AssertRelease(pVM->pVMR0ForCall == CreateVMReq.pVMR0);
112 AssertRelease(pVM->pSession == pSession);
113 AssertRelease(pVM->cCpus == 1);
114 pVM->enmVMState = VMSTATE_CREATED;
115 PVMR0 const pVMR0 = CreateVMReq.pVMR0;
116
117 rc = SUPR3SetVMForFastIOCtl(pVMR0);
118 if (!rc)
119 {
120 /*
121 * Call VMM code with invalid function.
122 */
123 for (i = cIterations; i > 0; i--)
124 {
125 rc = SUPR3CallVMMR0(pVMR0, NIL_VMCPUID, VMMR0_DO_SLOW_NOP, NULL);
126 if (rc != VINF_SUCCESS)
127 {
128 RTPrintf("tstInt: SUPR3CallVMMR0 -> rc=%Rrc i=%d Expected VINF_SUCCESS!\n", rc, i);
129 rcRet++;
130 break;
131 }
132 }
133 RTPrintf("tstInt: Performed SUPR3CallVMMR0 %d times (rc=%Rrc)\n", cIterations, rc);
134
135 /*
136 * The fast path.
137 */
138 if (rc == VINF_SUCCESS)
139 {
140 RTTimeNanoTS();
141 uint64_t StartTS = RTTimeNanoTS();
142 uint64_t StartTick = ASMReadTSC();
143 uint64_t MinTicks = UINT64_MAX;
144 for (i = 0; i < 1000000; i++)
145 {
146 uint64_t OneStartTick = ASMReadTSC();
147 rc = SUPR3CallVMMR0Fast(pVMR0, VMMR0_DO_NOP, 0);
148 uint64_t Ticks = ASMReadTSC() - OneStartTick;
149 if (Ticks < MinTicks)
150 MinTicks = Ticks;
151
152 if (RT_UNLIKELY(rc != VINF_SUCCESS))
153 {
154 RTPrintf("tstInt: SUPR3CallVMMR0Fast -> rc=%Rrc i=%d Expected VINF_SUCCESS!\n", rc, i);
155 rcRet++;
156 break;
157 }
158 }
159 uint64_t Ticks = ASMReadTSC() - StartTick;
160 uint64_t NanoSecs = RTTimeNanoTS() - StartTS;
161
162 RTPrintf("tstInt: SUPR3CallVMMR0Fast - %d iterations in %llu ns / %llu ticks. %llu ns / %#llu ticks per iteration. Min %llu ticks.\n",
163 i, NanoSecs, Ticks, NanoSecs / i, Ticks / i, MinTicks);
164
165 /*
166 * The ordinary path.
167 */
168 RTTimeNanoTS();
169 StartTS = RTTimeNanoTS();
170 StartTick = ASMReadTSC();
171 MinTicks = UINT64_MAX;
172 for (i = 0; i < 1000000; i++)
173 {
174 uint64_t OneStartTick = ASMReadTSC();
175 rc = SUPR3CallVMMR0Ex(pVMR0, NIL_VMCPUID, VMMR0_DO_SLOW_NOP, 0, NULL);
176 uint64_t OneTicks = ASMReadTSC() - OneStartTick;
177 if (OneTicks < MinTicks)
178 MinTicks = OneTicks;
179
180 if (RT_UNLIKELY(rc != VINF_SUCCESS))
181 {
182 RTPrintf("tstInt: SUPR3CallVMMR0Ex -> rc=%Rrc i=%d Expected VINF_SUCCESS!\n", rc, i);
183 rcRet++;
184 break;
185 }
186 }
187 Ticks = ASMReadTSC() - StartTick;
188 NanoSecs = RTTimeNanoTS() - StartTS;
189
190 RTPrintf("tstInt: SUPR3CallVMMR0Ex - %d iterations in %llu ns / %llu ticks. %llu ns / %#llu ticks per iteration. Min %llu ticks.\n",
191 i, NanoSecs, Ticks, NanoSecs / i, Ticks / i, MinTicks);
192 }
193 }
194 else
195 {
196 RTPrintf("tstInt: SUPR3SetVMForFastIOCtl failed: %Rrc\n", rc);
197 rcRet++;
198 }
199
200 rc = SUPR3CallVMMR0Ex(pVMR0, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
201 if (RT_FAILURE(rc))
202 {
203 RTPrintf("tstInt: VMMR0_DO_GVMM_DESTROY_VM failed: %Rrc\n", rc);
204 rcRet++;
205 }
206 }
207 else
208 {
209 RTPrintf("tstInt: VMMR0_DO_GVMM_CREATE_VM failed\n");
210 rcRet++;
211 }
212
213 /*
214 * Unload VMM.
215 */
216 rc = SUPR3UnloadVMM();
217 if (rc)
218 {
219 RTPrintf("tstInt: SUPR3UnloadVMM failed with rc=%Rrc\n", rc);
220 rcRet++;
221 }
222 }
223 else
224 {
225 RTPrintf("tstInt: SUPR3LoadVMM failed with rc=%Rrc%#RTeim\n", rc, &ErrInfo.Core);
226 rcRet++;
227 }
228
229 /*
230 * Terminate.
231 */
232 rc = SUPR3Term(false /*fForced*/);
233 rcRet += rc != 0;
234 RTPrintf("tstInt: SUPR3Term -> rc=%Rrc\n", rc);
235 }
236
237 if (RT_FAILURE(rc))
238 RTPrintf("Failed with rc=%Rrc\n", rc);
239
240 return !!rc;
241}
242
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use