VirtualBox

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

Last change on this file since 90804 was 90804, checked in by vboxsync, 3 years ago

HostDrivers: More VALID_PTR -> RT_VALID_PTR/AssertPtr.

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