1 | /* $Id: VMMTests.cpp 93725 2022-02-14 13:46:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM - The Virtual Machine Monitor Core, Tests.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 |
|
---|
18 | //#define NO_SUPCALLR0VMM
|
---|
19 |
|
---|
20 |
|
---|
21 | /*********************************************************************************************************************************
|
---|
22 | * Header Files *
|
---|
23 | *********************************************************************************************************************************/
|
---|
24 | #define LOG_GROUP LOG_GROUP_VMM
|
---|
25 | #include <VBox/vmm/vmm.h>
|
---|
26 | #include <VBox/vmm/pdmapi.h>
|
---|
27 | #include <VBox/vmm/cpum.h>
|
---|
28 | #include <VBox/dbg.h>
|
---|
29 | #include <VBox/vmm/hm.h>
|
---|
30 | #include <VBox/vmm/mm.h>
|
---|
31 | #include <VBox/vmm/trpm.h>
|
---|
32 | #include <VBox/vmm/selm.h>
|
---|
33 | #include "VMMInternal.h"
|
---|
34 | #include <VBox/vmm/vm.h>
|
---|
35 | #include <iprt/errcore.h>
|
---|
36 | #include <VBox/param.h>
|
---|
37 |
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/time.h>
|
---|
41 | #include <iprt/stream.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <iprt/x86.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | #define SYNC_SEL(pHyperCtx, reg) \
|
---|
47 | if (pHyperCtx->reg.Sel) \
|
---|
48 | { \
|
---|
49 | DBGFSELINFO selInfo; \
|
---|
50 | int rc2 = SELMR3GetShadowSelectorInfo(pVM, pHyperCtx->reg.Sel, &selInfo); \
|
---|
51 | AssertRC(rc2); \
|
---|
52 | \
|
---|
53 | pHyperCtx->reg.u64Base = selInfo.GCPtrBase; \
|
---|
54 | pHyperCtx->reg.u32Limit = selInfo.cbLimit; \
|
---|
55 | pHyperCtx->reg.Attr.n.u1Present = selInfo.u.Raw.Gen.u1Present; \
|
---|
56 | pHyperCtx->reg.Attr.n.u1DefBig = selInfo.u.Raw.Gen.u1DefBig; \
|
---|
57 | pHyperCtx->reg.Attr.n.u1Granularity = selInfo.u.Raw.Gen.u1Granularity; \
|
---|
58 | pHyperCtx->reg.Attr.n.u4Type = selInfo.u.Raw.Gen.u4Type; \
|
---|
59 | pHyperCtx->reg.Attr.n.u2Dpl = selInfo.u.Raw.Gen.u2Dpl; \
|
---|
60 | pHyperCtx->reg.Attr.n.u1DescType = selInfo.u.Raw.Gen.u1DescType; \
|
---|
61 | pHyperCtx->reg.Attr.n.u1Long = selInfo.u.Raw.Gen.u1Long; \
|
---|
62 | }
|
---|
63 |
|
---|
64 | /* execute the switch. */
|
---|
65 | VMMR3DECL(int) VMMDoHmTest(PVM pVM)
|
---|
66 | {
|
---|
67 | #if 1
|
---|
68 | RTPrintf("FIXME!\n");
|
---|
69 | RT_NOREF(pVM);
|
---|
70 | return 0;
|
---|
71 | #else
|
---|
72 |
|
---|
73 | uint32_t i;
|
---|
74 | int rc;
|
---|
75 | PCPUMCTX pHyperCtx, pGuestCtx;
|
---|
76 | RTGCPHYS CR3Phys = 0x0; /* fake address */
|
---|
77 | PVMCPU pVCpu = &pVM->aCpus[0];
|
---|
78 |
|
---|
79 | if (!HMIsEnabled(pVM))
|
---|
80 | {
|
---|
81 | RTPrintf("VMM: Hardware accelerated test not available!\n");
|
---|
82 | return VERR_ACCESS_DENIED;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* Enable mapping of the hypervisor into the shadow page table. */
|
---|
86 | uint32_t cb;
|
---|
87 | rc = PGMR3MappingsSize(pVM, &cb);
|
---|
88 | AssertRCReturn(rc, rc);
|
---|
89 |
|
---|
90 | /* Pretend the mappings are now fixed; to force a refresh of the reserved PDEs. */
|
---|
91 | rc = PGMR3MappingsFix(pVM, MM_HYPER_AREA_ADDRESS, cb);
|
---|
92 | AssertRCReturn(rc, rc);
|
---|
93 |
|
---|
94 | pHyperCtx = CPUMGetHyperCtxPtr(pVCpu);
|
---|
95 |
|
---|
96 | pHyperCtx->cr0 = X86_CR0_PE | X86_CR0_WP | X86_CR0_PG | X86_CR0_TS | X86_CR0_ET | X86_CR0_NE | X86_CR0_MP;
|
---|
97 | pHyperCtx->cr4 = X86_CR4_PGE | X86_CR4_OSFXSR | X86_CR4_OSXMMEEXCPT;
|
---|
98 | PGMChangeMode(pVCpu, pHyperCtx->cr0, pHyperCtx->cr4, pHyperCtx->msrEFER);
|
---|
99 | PGMSyncCR3(pVCpu, pHyperCtx->cr0, CR3Phys, pHyperCtx->cr4, true);
|
---|
100 |
|
---|
101 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TO_R3);
|
---|
102 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TIMER);
|
---|
103 | VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
|
---|
104 | VM_FF_CLEAR(pVM, VM_FF_REQUEST);
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Setup stack for calling VMMRCEntry().
|
---|
108 | */
|
---|
109 | RTRCPTR RCPtrEP;
|
---|
110 | rc = PDMR3LdrGetSymbolRC(pVM, VMMRC_MAIN_MODULE_NAME, "VMMRCEntry", &RCPtrEP);
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | RTPrintf("VMM: VMMRCEntry=%RRv\n", RCPtrEP);
|
---|
114 |
|
---|
115 | pHyperCtx = CPUMGetHyperCtxPtr(pVCpu);
|
---|
116 |
|
---|
117 | /* Fill in hidden selector registers for the hypervisor state. */
|
---|
118 | SYNC_SEL(pHyperCtx, cs);
|
---|
119 | SYNC_SEL(pHyperCtx, ds);
|
---|
120 | SYNC_SEL(pHyperCtx, es);
|
---|
121 | SYNC_SEL(pHyperCtx, fs);
|
---|
122 | SYNC_SEL(pHyperCtx, gs);
|
---|
123 | SYNC_SEL(pHyperCtx, ss);
|
---|
124 | SYNC_SEL(pHyperCtx, tr);
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Profile switching.
|
---|
128 | */
|
---|
129 | RTPrintf("VMM: profiling switcher...\n");
|
---|
130 | Log(("VMM: profiling switcher...\n"));
|
---|
131 | uint64_t TickMin = UINT64_MAX;
|
---|
132 | uint64_t tsBegin = RTTimeNanoTS();
|
---|
133 | uint64_t TickStart = ASMReadTSC();
|
---|
134 | for (i = 0; i < 1000000; i++)
|
---|
135 | {
|
---|
136 | CPUMSetHyperState(pVCpu, pVM->vmm.s.pfnCallTrampolineRC, pVCpu->vmm.s.pbEMTStackBottomRC, 0, 0);
|
---|
137 | CPUMPushHyper(pVCpu, 0);
|
---|
138 | CPUMPushHyper(pVCpu, VMMRC_DO_TESTCASE_HM_NOP);
|
---|
139 | CPUMPushHyper(pVCpu, pVM->pVMRC);
|
---|
140 | CPUMPushHyper(pVCpu, 3 * sizeof(RTRCPTR)); /* stack frame size */
|
---|
141 | CPUMPushHyper(pVCpu, RCPtrEP); /* what to call */
|
---|
142 |
|
---|
143 | pHyperCtx = CPUMGetHyperCtxPtr(pVCpu);
|
---|
144 | pGuestCtx = CPUMQueryGuestCtxPtr(pVCpu);
|
---|
145 |
|
---|
146 | /* Copy the hypervisor context to make sure we have a valid guest context. */
|
---|
147 | *pGuestCtx = *pHyperCtx;
|
---|
148 | pGuestCtx->cr3 = CR3Phys;
|
---|
149 |
|
---|
150 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TO_R3);
|
---|
151 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TIMER);
|
---|
152 | VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
|
---|
153 |
|
---|
154 | uint64_t TickThisStart = ASMReadTSC();
|
---|
155 | rc = SUPR3CallVMMR0Fast(pVM->pVMR0, VMMR0_DO_HM_RUN, 0);
|
---|
156 | uint64_t TickThisElapsed = ASMReadTSC() - TickThisStart;
|
---|
157 | if (RT_FAILURE(rc))
|
---|
158 | {
|
---|
159 | Log(("VMM: R0 returned fatal %Rrc in iteration %d\n", rc, i));
|
---|
160 | VMMR3FatalDump(pVM, pVCpu, rc);
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 | if (TickThisElapsed < TickMin)
|
---|
164 | TickMin = TickThisElapsed;
|
---|
165 | }
|
---|
166 | uint64_t TickEnd = ASMReadTSC();
|
---|
167 | uint64_t tsEnd = RTTimeNanoTS();
|
---|
168 |
|
---|
169 | uint64_t Elapsed = tsEnd - tsBegin;
|
---|
170 | uint64_t PerIteration = Elapsed / (uint64_t)i;
|
---|
171 | uint64_t cTicksElapsed = TickEnd - TickStart;
|
---|
172 | uint64_t cTicksPerIteration = cTicksElapsed / (uint64_t)i;
|
---|
173 |
|
---|
174 | RTPrintf("VMM: %8d cycles in %11llu ns (%11lld ticks), %10llu ns/iteration (%11lld ticks) Min %11lld ticks\n",
|
---|
175 | i, Elapsed, cTicksElapsed, PerIteration, cTicksPerIteration, TickMin);
|
---|
176 | Log(("VMM: %8d cycles in %11llu ns (%11lld ticks), %10llu ns/iteration (%11lld ticks) Min %11lld ticks\n",
|
---|
177 | i, Elapsed, cTicksElapsed, PerIteration, cTicksPerIteration, TickMin));
|
---|
178 |
|
---|
179 | rc = VINF_SUCCESS;
|
---|
180 | }
|
---|
181 | else
|
---|
182 | AssertMsgFailed(("Failed to resolved VMMRC.rc::VMMRCEntry(), rc=%Rrc\n", rc));
|
---|
183 |
|
---|
184 | return rc;
|
---|
185 | #endif
|
---|
186 | }
|
---|
187 |
|
---|