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