VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstInt.cpp

Last change on this file was 107916, checked in by vboxsync, 3 months ago

VMM,HostDrivers/SUP: Moved tstInt from HostDrivers to the VMM and fixed the breakage caused by the recent GVMMCREATEVMREQ modifications. [scm fixes] jiraref:VBP-1470

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette