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