1 | /* $Id: tstInt.cpp 98103 2023-01-17 14:15:46Z 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 |
|
---|
60 | int main(int argc, char **argv)
|
---|
61 | {
|
---|
62 | int rcRet = 0;
|
---|
63 | int i;
|
---|
64 | int rc;
|
---|
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 | rc = SUPR3Init(&pSession);
|
---|
75 | rcRet += rc != 0;
|
---|
76 | RTPrintf("tstInt: SUPR3Init -> rc=%Rrc\n", rc);
|
---|
77 | char szFile[RTPATH_MAX];
|
---|
78 | if (!rc)
|
---|
79 | {
|
---|
80 | rc = RTPathExecDir(szFile, sizeof(szFile) - sizeof("/VMMR0.r0"));
|
---|
81 | }
|
---|
82 | char szAbsFile[RTPATH_MAX];
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | {
|
---|
85 | strcat(szFile, "/VMMR0.r0");
|
---|
86 | rc = RTPathAbs(szFile, szAbsFile, sizeof(szAbsFile));
|
---|
87 | }
|
---|
88 | if (RT_SUCCESS(rc))
|
---|
89 | {
|
---|
90 | /*
|
---|
91 | * Load VMM code.
|
---|
92 | */
|
---|
93 | RTERRINFOSTATIC ErrInfo;
|
---|
94 | rc = SUPR3LoadVMM(szAbsFile, RTErrInfoInitStatic(&ErrInfo));
|
---|
95 | if (RT_SUCCESS(rc))
|
---|
96 | {
|
---|
97 | /*
|
---|
98 | * Create a tiny dummy VM so we can do NOP calls into it using the fast I/O control path.
|
---|
99 | */
|
---|
100 | GVMMCREATEVMREQ CreateVMReq;
|
---|
101 | CreateVMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
102 | CreateVMReq.Hdr.cbReq = sizeof(CreateVMReq);
|
---|
103 | CreateVMReq.pSession = pSession;
|
---|
104 | CreateVMReq.pVMR0 = NIL_RTR0PTR;
|
---|
105 | CreateVMReq.pVMR3 = NULL;
|
---|
106 | CreateVMReq.cCpus = 1;
|
---|
107 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_GVMM_CREATE_VM, 0, &CreateVMReq.Hdr);
|
---|
108 | if (RT_SUCCESS(rc))
|
---|
109 | {
|
---|
110 | PVM pVM = CreateVMReq.pVMR3;
|
---|
111 | AssertRelease(RT_VALID_PTR(pVM));
|
---|
112 | AssertRelease(pVM->pVMR0ForCall == CreateVMReq.pVMR0);
|
---|
113 | AssertRelease(pVM->pSession == pSession);
|
---|
114 | AssertRelease(pVM->cCpus == 1);
|
---|
115 | pVM->enmVMState = VMSTATE_CREATED;
|
---|
116 | PVMR0 const pVMR0 = CreateVMReq.pVMR0;
|
---|
117 |
|
---|
118 | rc = SUPR3SetVMForFastIOCtl(pVMR0);
|
---|
119 | if (!rc)
|
---|
120 | {
|
---|
121 | /*
|
---|
122 | * Call VMM code with invalid function.
|
---|
123 | */
|
---|
124 | for (i = cIterations; i > 0; i--)
|
---|
125 | {
|
---|
126 | rc = SUPR3CallVMMR0(pVMR0, NIL_VMCPUID, VMMR0_DO_SLOW_NOP, NULL);
|
---|
127 | if (rc != VINF_SUCCESS)
|
---|
128 | {
|
---|
129 | RTPrintf("tstInt: SUPR3CallVMMR0 -> rc=%Rrc i=%d Expected VINF_SUCCESS!\n", rc, i);
|
---|
130 | rcRet++;
|
---|
131 | break;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | RTPrintf("tstInt: Performed SUPR3CallVMMR0 %d times (rc=%Rrc)\n", cIterations, rc);
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * The fast path.
|
---|
138 | */
|
---|
139 | if (rc == VINF_SUCCESS)
|
---|
140 | {
|
---|
141 | RTTimeNanoTS();
|
---|
142 | uint64_t StartTS = RTTimeNanoTS();
|
---|
143 | uint64_t StartTick = ASMReadTSC();
|
---|
144 | uint64_t MinTicks = UINT64_MAX;
|
---|
145 | for (i = 0; i < 1000000; i++)
|
---|
146 | {
|
---|
147 | uint64_t OneStartTick = ASMReadTSC();
|
---|
148 | rc = SUPR3CallVMMR0Fast(pVMR0, VMMR0_DO_NOP, 0);
|
---|
149 | uint64_t Ticks = ASMReadTSC() - OneStartTick;
|
---|
150 | if (Ticks < MinTicks)
|
---|
151 | MinTicks = Ticks;
|
---|
152 |
|
---|
153 | if (RT_UNLIKELY(rc != VINF_SUCCESS))
|
---|
154 | {
|
---|
155 | RTPrintf("tstInt: SUPR3CallVMMR0Fast -> rc=%Rrc i=%d Expected VINF_SUCCESS!\n", rc, i);
|
---|
156 | rcRet++;
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | uint64_t Ticks = ASMReadTSC() - StartTick;
|
---|
161 | uint64_t NanoSecs = RTTimeNanoTS() - StartTS;
|
---|
162 |
|
---|
163 | RTPrintf("tstInt: SUPR3CallVMMR0Fast - %d iterations in %llu ns / %llu ticks. %llu ns / %#llu ticks per iteration. Min %llu ticks.\n",
|
---|
164 | i, NanoSecs, Ticks, NanoSecs / i, Ticks / i, MinTicks);
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * The ordinary path.
|
---|
168 | */
|
---|
169 | RTTimeNanoTS();
|
---|
170 | StartTS = RTTimeNanoTS();
|
---|
171 | StartTick = ASMReadTSC();
|
---|
172 | MinTicks = UINT64_MAX;
|
---|
173 | for (i = 0; i < 1000000; i++)
|
---|
174 | {
|
---|
175 | uint64_t OneStartTick = ASMReadTSC();
|
---|
176 | rc = SUPR3CallVMMR0Ex(pVMR0, NIL_VMCPUID, VMMR0_DO_SLOW_NOP, 0, NULL);
|
---|
177 | uint64_t OneTicks = ASMReadTSC() - OneStartTick;
|
---|
178 | if (OneTicks < MinTicks)
|
---|
179 | MinTicks = OneTicks;
|
---|
180 |
|
---|
181 | if (RT_UNLIKELY(rc != VINF_SUCCESS))
|
---|
182 | {
|
---|
183 | RTPrintf("tstInt: SUPR3CallVMMR0Ex -> rc=%Rrc i=%d Expected VINF_SUCCESS!\n", rc, i);
|
---|
184 | rcRet++;
|
---|
185 | break;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | Ticks = ASMReadTSC() - StartTick;
|
---|
189 | NanoSecs = RTTimeNanoTS() - StartTS;
|
---|
190 |
|
---|
191 | RTPrintf("tstInt: SUPR3CallVMMR0Ex - %d iterations in %llu ns / %llu ticks. %llu ns / %#llu ticks per iteration. Min %llu ticks.\n",
|
---|
192 | i, NanoSecs, Ticks, NanoSecs / i, Ticks / i, MinTicks);
|
---|
193 | }
|
---|
194 | }
|
---|
195 | else
|
---|
196 | {
|
---|
197 | RTPrintf("tstInt: SUPR3SetVMForFastIOCtl failed: %Rrc\n", rc);
|
---|
198 | rcRet++;
|
---|
199 | }
|
---|
200 |
|
---|
201 | rc = SUPR3CallVMMR0Ex(pVMR0, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
|
---|
202 | if (RT_FAILURE(rc))
|
---|
203 | {
|
---|
204 | RTPrintf("tstInt: VMMR0_DO_GVMM_DESTROY_VM failed: %Rrc\n", rc);
|
---|
205 | rcRet++;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | else
|
---|
209 | {
|
---|
210 | RTPrintf("tstInt: VMMR0_DO_GVMM_CREATE_VM failed\n");
|
---|
211 | rcRet++;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * Unload VMM.
|
---|
216 | */
|
---|
217 | rc = SUPR3UnloadVMM();
|
---|
218 | if (rc)
|
---|
219 | {
|
---|
220 | RTPrintf("tstInt: SUPR3UnloadVMM failed with rc=%Rrc\n", rc);
|
---|
221 | rcRet++;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | RTPrintf("tstInt: SUPR3LoadVMM failed with rc=%Rrc%#RTeim\n", rc, &ErrInfo.Core);
|
---|
227 | rcRet++;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Terminate.
|
---|
232 | */
|
---|
233 | rc = SUPR3Term(false /*fForced*/);
|
---|
234 | rcRet += rc != 0;
|
---|
235 | RTPrintf("tstInt: SUPR3Term -> rc=%Rrc\n", rc);
|
---|
236 | }
|
---|
237 |
|
---|
238 | return !!rc;
|
---|
239 | }
|
---|
240 |
|
---|