[23] | 1 | /* $Id: VMMTests.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[1313] | 3 | * VMM - The Virtual Machine Monitor Core, Tests.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[82968] | 7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
[1] | 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
|
---|
[5999] | 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.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
[1313] | 18 | //#define NO_SUPCALLR0VMM
|
---|
[1] | 19 |
|
---|
[57358] | 20 |
|
---|
| 21 | /*********************************************************************************************************************************
|
---|
| 22 | * Header Files *
|
---|
| 23 | *********************************************************************************************************************************/
|
---|
[1] | 24 | #define LOG_GROUP LOG_GROUP_VMM
|
---|
[29250] | 25 | #include <iprt/asm-amd64-x86.h> /* for SUPGetCpuHzFromGIP */
|
---|
[35346] | 26 | #include <VBox/vmm/vmm.h>
|
---|
| 27 | #include <VBox/vmm/pdmapi.h>
|
---|
| 28 | #include <VBox/vmm/cpum.h>
|
---|
[26152] | 29 | #include <VBox/dbg.h>
|
---|
[45618] | 30 | #include <VBox/vmm/hm.h>
|
---|
[35346] | 31 | #include <VBox/vmm/mm.h>
|
---|
| 32 | #include <VBox/vmm/trpm.h>
|
---|
| 33 | #include <VBox/vmm/selm.h>
|
---|
[1] | 34 | #include "VMMInternal.h"
|
---|
[35346] | 35 | #include <VBox/vmm/vm.h>
|
---|
[80011] | 36 | #include <iprt/errcore.h>
|
---|
[1] | 37 | #include <VBox/param.h>
|
---|
[1313] | 38 |
|
---|
[1] | 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>
|
---|
[37955] | 44 | #include <iprt/x86.h>
|
---|
[1] | 45 |
|
---|
[63429] | 46 |
|
---|
[1248] | 47 | #define SYNC_SEL(pHyperCtx, reg) \
|
---|
[41906] | 48 | if (pHyperCtx->reg.Sel) \
|
---|
[1248] | 49 | { \
|
---|
[19334] | 50 | DBGFSELINFO selInfo; \
|
---|
[41906] | 51 | int rc2 = SELMR3GetShadowSelectorInfo(pVM, pHyperCtx->reg.Sel, &selInfo); \
|
---|
[25239] | 52 | AssertRC(rc2); \
|
---|
[1248] | 53 | \
|
---|
[41906] | 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; \
|
---|
[1248] | 63 | }
|
---|
| 64 |
|
---|
[1210] | 65 | /* execute the switch. */
|
---|
[43391] | 66 | VMMR3DECL(int) VMMDoHmTest(PVM pVM)
|
---|
[1210] | 67 | {
|
---|
[80003] | 68 | #if 1
|
---|
| 69 | RTPrintf("FIXME!\n");
|
---|
| 70 | RT_NOREF(pVM);
|
---|
| 71 | return 0;
|
---|
| 72 | #else
|
---|
| 73 |
|
---|
[1210] | 74 | uint32_t i;
|
---|
[1257] | 75 | int rc;
|
---|
| 76 | PCPUMCTX pHyperCtx, pGuestCtx;
|
---|
[1269] | 77 | RTGCPHYS CR3Phys = 0x0; /* fake address */
|
---|
[18927] | 78 | PVMCPU pVCpu = &pVM->aCpus[0];
|
---|
[1210] | 79 |
|
---|
[45618] | 80 | if (!HMIsEnabled(pVM))
|
---|
[1210] | 81 | {
|
---|
[1239] | 82 | RTPrintf("VMM: Hardware accelerated test not available!\n");
|
---|
[1210] | 83 | return VERR_ACCESS_DENIED;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /* Enable mapping of the hypervisor into the shadow page table. */
|
---|
[16408] | 87 | uint32_t cb;
|
---|
| 88 | rc = PGMR3MappingsSize(pVM, &cb);
|
---|
| 89 | AssertRCReturn(rc, rc);
|
---|
[1210] | 90 |
|
---|
[16408] | 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 |
|
---|
[41931] | 95 | pHyperCtx = CPUMGetHyperCtxPtr(pVCpu);
|
---|
[1257] | 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;
|
---|
[54862] | 98 | pHyperCtx->cr4 = X86_CR4_PGE | X86_CR4_OSFXSR | X86_CR4_OSXMMEEXCPT;
|
---|
[18992] | 99 | PGMChangeMode(pVCpu, pHyperCtx->cr0, pHyperCtx->cr4, pHyperCtx->msrEFER);
|
---|
| 100 | PGMSyncCR3(pVCpu, pHyperCtx->cr0, CR3Phys, pHyperCtx->cr4, true);
|
---|
[1257] | 101 |
|
---|
[19141] | 102 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TO_R3);
|
---|
[19660] | 103 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TIMER);
|
---|
| 104 | VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
|
---|
[1269] | 105 | VM_FF_CLEAR(pVM, VM_FF_REQUEST);
|
---|
| 106 |
|
---|
[1210] | 107 | /*
|
---|
[56286] | 108 | * Setup stack for calling VMMRCEntry().
|
---|
[1210] | 109 | */
|
---|
[13813] | 110 | RTRCPTR RCPtrEP;
|
---|
[56286] | 111 | rc = PDMR3LdrGetSymbolRC(pVM, VMMRC_MAIN_MODULE_NAME, "VMMRCEntry", &RCPtrEP);
|
---|
[13816] | 112 | if (RT_SUCCESS(rc))
|
---|
[1210] | 113 | {
|
---|
[56286] | 114 | RTPrintf("VMM: VMMRCEntry=%RRv\n", RCPtrEP);
|
---|
[1248] | 115 |
|
---|
[41931] | 116 | pHyperCtx = CPUMGetHyperCtxPtr(pVCpu);
|
---|
[1248] | 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);
|
---|
[1249] | 125 | SYNC_SEL(pHyperCtx, tr);
|
---|
[1248] | 126 |
|
---|
[1210] | 127 | /*
|
---|
| 128 | * Profile switching.
|
---|
| 129 | */
|
---|
| 130 | RTPrintf("VMM: profiling switcher...\n");
|
---|
| 131 | Log(("VMM: profiling switcher...\n"));
|
---|
[62647] | 132 | uint64_t TickMin = UINT64_MAX;
|
---|
[1210] | 133 | uint64_t tsBegin = RTTimeNanoTS();
|
---|
| 134 | uint64_t TickStart = ASMReadTSC();
|
---|
| 135 | for (i = 0; i < 1000000; i++)
|
---|
| 136 | {
|
---|
[41985] | 137 | CPUMSetHyperState(pVCpu, pVM->vmm.s.pfnCallTrampolineRC, pVCpu->vmm.s.pbEMTStackBottomRC, 0, 0);
|
---|
[18927] | 138 | CPUMPushHyper(pVCpu, 0);
|
---|
[56286] | 139 | CPUMPushHyper(pVCpu, VMMRC_DO_TESTCASE_HM_NOP);
|
---|
[18927] | 140 | CPUMPushHyper(pVCpu, pVM->pVMRC);
|
---|
| 141 | CPUMPushHyper(pVCpu, 3 * sizeof(RTRCPTR)); /* stack frame size */
|
---|
| 142 | CPUMPushHyper(pVCpu, RCPtrEP); /* what to call */
|
---|
[1210] | 143 |
|
---|
[41931] | 144 | pHyperCtx = CPUMGetHyperCtxPtr(pVCpu);
|
---|
[18927] | 145 | pGuestCtx = CPUMQueryGuestCtxPtr(pVCpu);
|
---|
[1210] | 146 |
|
---|
| 147 | /* Copy the hypervisor context to make sure we have a valid guest context. */
|
---|
| 148 | *pGuestCtx = *pHyperCtx;
|
---|
[1269] | 149 | pGuestCtx->cr3 = CR3Phys;
|
---|
[1210] | 150 |
|
---|
[19141] | 151 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TO_R3);
|
---|
[19660] | 152 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TIMER);
|
---|
| 153 | VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
|
---|
[1248] | 154 |
|
---|
[1210] | 155 | uint64_t TickThisStart = ASMReadTSC();
|
---|
[43394] | 156 | rc = SUPR3CallVMMR0Fast(pVM->pVMR0, VMMR0_DO_HM_RUN, 0);
|
---|
[1210] | 157 | uint64_t TickThisElapsed = ASMReadTSC() - TickThisStart;
|
---|
[13816] | 158 | if (RT_FAILURE(rc))
|
---|
[1210] | 159 | {
|
---|
[13818] | 160 | Log(("VMM: R0 returned fatal %Rrc in iteration %d\n", rc, i));
|
---|
[18927] | 161 | VMMR3FatalDump(pVM, pVCpu, rc);
|
---|
[1210] | 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
|
---|
[56286] | 183 | AssertMsgFailed(("Failed to resolved VMMRC.rc::VMMRCEntry(), rc=%Rrc\n", rc));
|
---|
[1210] | 184 |
|
---|
| 185 | return rc;
|
---|
[49141] | 186 | #endif
|
---|
| 187 | }
|
---|
| 188 |
|
---|