-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathHMR0VMX-x86.cpp
More file actions
7135 lines (6373 loc) · 286 KB
/
HMR0VMX-x86.cpp
File metadata and controls
7135 lines (6373 loc) · 286 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: HMR0VMX-x86.cpp 112403 2026-01-11 19:29:08Z knut.osmundsen@oracle.com $ */
/** @file
* HM VMX (Intel VT-x) - Host Context Ring-0.
*/
/*
* Copyright (C) 2012-2026 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_HM
#define VMCPU_INCL_CPUM_GST_CTX
#include <iprt/x86.h>
#include <iprt/asm-amd64-x86.h>
#include <iprt/thread.h>
#include <iprt/mem.h>
#include <iprt/mp.h>
#include <VBox/vmm/pdmapi.h>
#include <VBox/vmm/dbgf.h>
#include <VBox/vmm/iem.h>
#include <VBox/vmm/iom.h>
#include <VBox/vmm/tm.h>
#include <VBox/vmm/em.h>
#include <VBox/vmm/gcm.h>
#include <VBox/vmm/gim.h>
#include <VBox/vmm/pdmapic.h>
#include "HMInternal.h"
#include <VBox/vmm/vmcc.h>
#include <VBox/vmm/hmvmxinline.h>
#include "HMR0VMX-x86.h"
#include "VMXInternal.h"
#include "dtrace/VBoxVMM.h"
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
#ifdef DEBUG_ramshankar
# define HMVMX_ALWAYS_SAVE_GUEST_RFLAGS
# define HMVMX_ALWAYS_SAVE_RO_GUEST_STATE
# define HMVMX_ALWAYS_SAVE_FULL_GUEST_STATE
# define HMVMX_ALWAYS_SYNC_FULL_GUEST_STATE
# define HMVMX_ALWAYS_CLEAN_TRANSIENT
# define HMVMX_ALWAYS_CHECK_GUEST_STATE
# define HMVMX_ALWAYS_TRAP_ALL_XCPTS
# define HMVMX_ALWAYS_TRAP_PF
# define HMVMX_ALWAYS_FLUSH_TLB
# define HMVMX_ALWAYS_SWAP_EFER
# define HMVMX_VERIFY_VMCS_MAGIC
# define HMVMX_GST_VMCS_MAGIC 0xacce55edc01df00d
# define HMVMX_NST_GST_VMCS_MAGIC 0xde7ec7edbaddecaf
#endif
/** Sync relevant VMCS cache fields for nested-guests (slight performance hit). */
#define HMVMX_ALWAYS_SYNC_VMCS_CACHE
/** Enables the fAlwaysInterceptMovDRx related code. */
#define VMX_WITH_MAYBE_ALWAYS_INTERCEPT_MOV_DRX 1
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* VMX page allocation information.
*/
typedef struct
{
uint32_t fValid; /**< Whether to allocate this page (e.g, based on a CPU feature). */
uint32_t uPadding0; /**< Padding to ensure array of these structs are aligned to a multiple of 8. */
PRTHCPHYS pHCPhys; /**< Where to store the host-physical address of the allocation. */
PRTR0PTR ppVirt; /**< Where to store the host-virtual address of the allocation. */
} VMXPAGEALLOCINFO;
/** Pointer to VMX page-allocation info. */
typedef VMXPAGEALLOCINFO *PVMXPAGEALLOCINFO;
/** Pointer to a const VMX page-allocation info. */
typedef const VMXPAGEALLOCINFO *PCVMXPAGEALLOCINFO;
AssertCompileSizeAlignment(VMXPAGEALLOCINFO, 8);
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
static bool hmR0VmxShouldSwapEferMsr(PCVMCPUCC pVCpu, PCVMXTRANSIENT pVmxTransient);
static int hmR0VmxExitHostNmi(PVMCPUCC pVCpu, PCVMXVMCSINFO pVmcsInfo);
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** The DR6 value after writing zero to the register.
* Set by VMXR0GlobalInit(). */
static uint64_t g_fDr6Zeroed = 0;
/**
* Checks if the given MSR is part of the lastbranch-from-IP MSR stack.
* @returns @c true if it's part of LBR stack, @c false otherwise.
*
* @param pVM The cross context VM structure.
* @param idMsr The MSR.
* @param pidxMsr Where to store the index of the MSR in the LBR MSR array.
* Optional, can be NULL.
*
* @remarks Must only be called when LBR is enabled.
*/
DECL_FORCE_INLINE(bool) hmR0VmxIsLbrBranchFromMsr(PCVMCC pVM, uint32_t idMsr, uint32_t *pidxMsr)
{
Assert(pVM->hmr0.s.vmx.fLbr);
Assert(pVM->hmr0.s.vmx.idLbrFromIpMsrFirst);
uint32_t const cLbrStack = pVM->hmr0.s.vmx.idLbrFromIpMsrLast - pVM->hmr0.s.vmx.idLbrFromIpMsrFirst + 1;
uint32_t const idxMsr = idMsr - pVM->hmr0.s.vmx.idLbrFromIpMsrFirst;
if (idxMsr < cLbrStack)
{
if (pidxMsr)
*pidxMsr = idxMsr;
return true;
}
return false;
}
/**
* Checks if the given MSR is part of the lastbranch-to-IP MSR stack.
* @returns @c true if it's part of LBR stack, @c false otherwise.
*
* @param pVM The cross context VM structure.
* @param idMsr The MSR.
* @param pidxMsr Where to store the index of the MSR in the LBR MSR array.
* Optional, can be NULL.
*
* @remarks Must only be called when LBR is enabled and when lastbranch-to-IP MSRs
* are supported by the CPU (see hmR0VmxSetupLbrMsrRange).
*/
DECL_FORCE_INLINE(bool) hmR0VmxIsLbrBranchToMsr(PCVMCC pVM, uint32_t idMsr, uint32_t *pidxMsr)
{
Assert(pVM->hmr0.s.vmx.fLbr);
if (pVM->hmr0.s.vmx.idLbrToIpMsrFirst)
{
uint32_t const cLbrStack = pVM->hmr0.s.vmx.idLbrToIpMsrLast - pVM->hmr0.s.vmx.idLbrToIpMsrFirst + 1;
uint32_t const idxMsr = idMsr - pVM->hmr0.s.vmx.idLbrToIpMsrFirst;
if (idxMsr < cLbrStack)
{
if (pidxMsr)
*pidxMsr = idxMsr;
return true;
}
}
return false;
}
/**
* Gets the active (in use) VMCS info. object for the specified VCPU.
*
* This is either the guest or nested-guest VMCS info. and need not necessarily
* pertain to the "current" VMCS (in the VMX definition of the term). For instance,
* if the VM-entry failed due to an invalid-guest state, we may have "cleared" the
* current VMCS while returning to ring-3. However, the VMCS info. object for that
* VMCS would still be active and returned here so that we could dump the VMCS
* fields to ring-3 for diagnostics. This function is thus only used to
* distinguish between the nested-guest or guest VMCS.
*
* @returns The active VMCS information.
* @param pVCpu The cross context virtual CPU structure.
*
* @thread EMT.
* @remarks This function may be called with preemption or interrupts disabled!
*/
DECLINLINE(PVMXVMCSINFO) hmGetVmxActiveVmcsInfo(PVMCPUCC pVCpu)
{
if (!pVCpu->hmr0.s.vmx.fSwitchedToNstGstVmcs)
return &pVCpu->hmr0.s.vmx.VmcsInfo;
return &pVCpu->hmr0.s.vmx.VmcsInfoNstGst;
}
#ifdef HMVMX_VERIFY_VMCS_MAGIC
/**
* Returns whether the VMCS magic field matches its expected value.
*
* @returns VBox status code.
* @param fIsNstGstVmcs Whether this is a nested-guest VMCS.
*/
static int hmR0VmxCheckVmcsMagic(bool fIsNstGstVmcs)
{
uint64_t uVmcsMagic = 0;
uint64_t const uExpectedMagic = fIsNstGstVmcs ? HMVMX_NST_GST_VMCS_MAGIC : HMVMX_GST_VMCS_MAGIC;
uint32_t const uVmcsField = VMX_VMCS64_CTRL_EXEC_VMCS_PTR_FULL;
int const rc = VMXReadVmcs64(uVmcsField, &uVmcsMagic);
if (RT_SUCCESS(rc))
{
if (uVmcsMagic == uExpectedMagic)
return VINF_SUCCESS;
return VERR_INVALID_MAGIC;
}
return rc;
}
#endif
/**
* Returns whether the VM-exit MSR-store area differs from the VM-exit MSR-load
* area.
*
* @returns @c true if it's different, @c false otherwise.
* @param pVmcsInfo The VMCS info. object.
*/
DECL_FORCE_INLINE(bool) hmR0VmxIsSeparateExitMsrStoreAreaVmcs(PCVMXVMCSINFO pVmcsInfo)
{
return RT_BOOL( pVmcsInfo->pvGuestMsrStore != pVmcsInfo->pvGuestMsrLoad
&& pVmcsInfo->pvGuestMsrStore);
}
/**
* Sets the given Processor-based VM-execution controls.
*
* @param pVmxTransient The VMX-transient structure.
* @param uProcCtls The Processor-based VM-execution controls to set.
*/
static void hmR0VmxSetProcCtlsVmcs(PVMXTRANSIENT pVmxTransient, uint32_t uProcCtls)
{
PVMXVMCSINFO pVmcsInfo = pVmxTransient->pVmcsInfo;
if ((pVmcsInfo->u32ProcCtls & uProcCtls) != uProcCtls)
{
pVmcsInfo->u32ProcCtls |= uProcCtls;
int rc = VMXWriteVmcs32(VMX_VMCS32_CTRL_PROC_EXEC, pVmcsInfo->u32ProcCtls);
AssertRC(rc);
}
}
/**
* Removes the given Processor-based VM-execution controls.
*
* @param pVCpu The cross context virtual CPU structure.
* @param pVmxTransient The VMX-transient structure.
* @param uProcCtls The Processor-based VM-execution controls to remove.
*
* @remarks When executing a nested-guest, this will not remove any of the specified
* controls if the nested hypervisor has set any one of them.
*/
static void hmR0VmxRemoveProcCtlsVmcs(PVMCPUCC pVCpu, PVMXTRANSIENT pVmxTransient, uint32_t uProcCtls)
{
PVMXVMCSINFO pVmcsInfo = pVmxTransient->pVmcsInfo;
if (pVmcsInfo->u32ProcCtls & uProcCtls)
{
#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
if ( !pVmxTransient->fIsNestedGuest
|| !CPUMIsGuestVmxProcCtlsSet(&pVCpu->cpum.GstCtx, uProcCtls))
#else
NOREF(pVCpu);
if (!pVmxTransient->fIsNestedGuest)
#endif
{
pVmcsInfo->u32ProcCtls &= ~uProcCtls;
int rc = VMXWriteVmcs32(VMX_VMCS32_CTRL_PROC_EXEC, pVmcsInfo->u32ProcCtls);
AssertRC(rc);
}
}
}
/**
* Sets the TSC offset for the current VMCS.
*
* @param uTscOffset The TSC offset to set.
* @param pVmcsInfo The VMCS info. object.
*/
static void hmR0VmxSetTscOffsetVmcs(PVMXVMCSINFO pVmcsInfo, uint64_t uTscOffset)
{
if (pVmcsInfo->u64TscOffset != uTscOffset)
{
int rc = VMXWriteVmcs64(VMX_VMCS64_CTRL_TSC_OFFSET_FULL, uTscOffset);
AssertRC(rc);
pVmcsInfo->u64TscOffset = uTscOffset;
}
}
/**
* Loads the VMCS specified by the VMCS info. object.
*
* @returns VBox status code.
* @param pVmcsInfo The VMCS info. object.
*
* @remarks Can be called with interrupts disabled.
*/
static int hmR0VmxLoadVmcs(PVMXVMCSINFO pVmcsInfo)
{
Assert(pVmcsInfo->HCPhysVmcs != 0 && pVmcsInfo->HCPhysVmcs != NIL_RTHCPHYS);
Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
return VMXLoadVmcs(pVmcsInfo->HCPhysVmcs);
}
/**
* Clears the VMCS specified by the VMCS info. object.
*
* @returns VBox status code.
* @param pVmcsInfo The VMCS info. object.
*
* @remarks Can be called with interrupts disabled.
*/
static int hmR0VmxClearVmcs(PVMXVMCSINFO pVmcsInfo)
{
Assert(pVmcsInfo->HCPhysVmcs != 0 && pVmcsInfo->HCPhysVmcs != NIL_RTHCPHYS);
Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
int rc = VMXClearVmcs(pVmcsInfo->HCPhysVmcs);
if (RT_SUCCESS(rc))
pVmcsInfo->fVmcsState = VMX_V_VMCS_LAUNCH_STATE_CLEAR;
return rc;
}
/**
* Checks whether the MSR belongs to the set of guest MSRs that we restore
* lazily while leaving VT-x.
*
* @returns true if it does, false otherwise.
* @param pVCpu The cross context virtual CPU structure.
* @param idMsr The MSR to check.
*/
static bool hmR0VmxIsLazyGuestMsr(PCVMCPUCC pVCpu, uint32_t idMsr)
{
if (pVCpu->CTX_SUFF(pVM)->hmr0.s.fAllow64BitGuests)
{
switch (idMsr)
{
case MSR_K8_LSTAR:
case MSR_K6_STAR:
case MSR_K8_SF_MASK:
case MSR_K8_KERNEL_GS_BASE:
return true;
}
}
return false;
}
/**
* Loads a set of guests MSRs to allow read/passthru to the guest.
*
* The name of this function is slightly confusing. This function does NOT
* postpone loading, but loads the MSR right now. "hmR0VmxLazy" is simply a
* common prefix for functions dealing with "lazy restoration" of the shared
* MSRs.
*
* @param pVCpu The cross context virtual CPU structure.
*
* @remarks No-long-jump zone!!!
*/
static void hmR0VmxLazyLoadGuestMsrs(PVMCPUCC pVCpu)
{
Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
Assert(!VMMRZCallRing3IsEnabled(pVCpu));
Assert(pVCpu->hmr0.s.vmx.fLazyMsrs & VMX_LAZY_MSRS_SAVED_HOST);
if (pVCpu->CTX_SUFF(pVM)->hmr0.s.fAllow64BitGuests)
{
/*
* If the guest MSRs are not loaded -and- if all the guest MSRs are identical
* to the MSRs on the CPU (which are the saved host MSRs, see assertion above) then
* we can skip a few MSR writes.
*
* Otherwise, it implies either 1. they're not loaded, or 2. they're loaded but the
* guest MSR values in the guest-CPU context might be different to what's currently
* loaded in the CPU. In either case, we need to write the new guest MSR values to the
* CPU, see @bugref{8728}.
*/
PCCPUMCTX pCtx = &pVCpu->cpum.GstCtx;
if ( !(pVCpu->hmr0.s.vmx.fLazyMsrs & VMX_LAZY_MSRS_LOADED_GUEST)
&& pCtx->msrKERNELGSBASE == pVCpu->hmr0.s.vmx.u64HostMsrKernelGsBase
&& pCtx->msrLSTAR == pVCpu->hmr0.s.vmx.u64HostMsrLStar
&& pCtx->msrSTAR == pVCpu->hmr0.s.vmx.u64HostMsrStar
&& pCtx->msrSFMASK == pVCpu->hmr0.s.vmx.u64HostMsrSfMask)
{
#ifdef VBOX_STRICT
Assert(ASMRdMsr(MSR_K8_KERNEL_GS_BASE) == pCtx->msrKERNELGSBASE);
Assert(ASMRdMsr(MSR_K8_LSTAR) == pCtx->msrLSTAR);
Assert(ASMRdMsr(MSR_K6_STAR) == pCtx->msrSTAR);
Assert(ASMRdMsr(MSR_K8_SF_MASK) == pCtx->msrSFMASK);
#endif
}
else
{
/* Avoid raising #GP caused by writing illegal values to these MSRs. */
if ( X86_IS_CANONICAL(pCtx->msrKERNELGSBASE)
&& X86_IS_CANONICAL(pCtx->msrLSTAR))
{
ASMWrMsr(MSR_K8_KERNEL_GS_BASE, pCtx->msrKERNELGSBASE);
ASMWrMsr(MSR_K8_LSTAR, pCtx->msrLSTAR);
ASMWrMsr(MSR_K6_STAR, pCtx->msrSTAR);
/* The system call flag mask register isn't as benign and accepting of all
values as the above, so mask it to avoid #GP'ing on corrupted input. */
Assert(!(pCtx->msrSFMASK & ~(uint64_t)UINT32_MAX));
ASMWrMsr(MSR_K8_SF_MASK, pCtx->msrSFMASK & UINT32_MAX);
}
else
AssertMsgFailed(("Incompatible lazily-loaded guest MSR values\n"));
}
}
pVCpu->hmr0.s.vmx.fLazyMsrs |= VMX_LAZY_MSRS_LOADED_GUEST;
}
/**
* Checks if the specified guest MSR is part of the VM-entry MSR-load area.
*
* @returns @c true if found, @c false otherwise.
* @param pVmcsInfo The VMCS info. object.
* @param idMsr The MSR to find.
*/
static bool hmR0VmxIsAutoLoadGuestMsr(PCVMXVMCSINFO pVmcsInfo, uint32_t idMsr)
{
PCVMXAUTOMSR pMsrs = (PCVMXAUTOMSR)pVmcsInfo->pvGuestMsrLoad;
uint32_t const cMsrs = pVmcsInfo->cEntryMsrLoad;
Assert(pMsrs);
Assert(sizeof(*pMsrs) * cMsrs <= X86_PAGE_4K_SIZE);
for (uint32_t i = 0; i < cMsrs; i++)
{
if (pMsrs[i].u32Msr == idMsr)
return true;
}
return false;
}
/**
* Performs lazy restoration of the set of host MSRs if they were previously
* loaded with guest MSR values.
*
* @param pVCpu The cross context virtual CPU structure.
*
* @remarks No-long-jump zone!!!
* @remarks The guest MSRs should have been saved back into the guest-CPU
* context by vmxHCImportGuestState()!!!
*/
static void hmR0VmxLazyRestoreHostMsrs(PVMCPUCC pVCpu)
{
Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
Assert(!VMMRZCallRing3IsEnabled(pVCpu));
if (pVCpu->hmr0.s.vmx.fLazyMsrs & VMX_LAZY_MSRS_LOADED_GUEST)
{
Assert(pVCpu->hmr0.s.vmx.fLazyMsrs & VMX_LAZY_MSRS_SAVED_HOST);
if (pVCpu->CTX_SUFF(pVM)->hmr0.s.fAllow64BitGuests)
{
ASMWrMsr(MSR_K8_LSTAR, pVCpu->hmr0.s.vmx.u64HostMsrLStar);
ASMWrMsr(MSR_K6_STAR, pVCpu->hmr0.s.vmx.u64HostMsrStar);
ASMWrMsr(MSR_K8_SF_MASK, pVCpu->hmr0.s.vmx.u64HostMsrSfMask);
ASMWrMsr(MSR_K8_KERNEL_GS_BASE, pVCpu->hmr0.s.vmx.u64HostMsrKernelGsBase);
}
}
pVCpu->hmr0.s.vmx.fLazyMsrs &= ~(VMX_LAZY_MSRS_LOADED_GUEST | VMX_LAZY_MSRS_SAVED_HOST);
}
/**
* Sets pfnStartVm to the best suited variant.
*
* This must be called whenever anything changes relative to the hmR0VmXStartVm
* variant selection:
* - pVCpu->hm.s.fLoadSaveGuestXcr0
* - HM_WSF_IBPB_ENTRY in pVCpu->hmr0.s.fWorldSwitcher
* - HM_WSF_IBPB_EXIT in pVCpu->hmr0.s.fWorldSwitcher
* - Perhaps: CPUMIsGuestFPUStateActive() (windows only)
* - Perhaps: CPUMCTX.fXStateMask (windows only)
*
* We currently ASSUME that neither HM_WSF_IBPB_ENTRY nor HM_WSF_IBPB_EXIT
* cannot be changed at runtime.
*/
static void hmR0VmxUpdateStartVmFunction(PVMCPUCC pVCpu)
{
static const struct CLANGWORKAROUND { PFNHMVMXSTARTVM pfn; } s_aHmR0VmxStartVmFunctions[] =
{
{ hmR0VmxStartVm_SansXcr0_SansIbpbEntry_SansL1dEntry_SansMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_WithXcr0_SansIbpbEntry_SansL1dEntry_SansMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_SansXcr0_WithIbpbEntry_SansL1dEntry_SansMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_WithXcr0_WithIbpbEntry_SansL1dEntry_SansMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_SansXcr0_SansIbpbEntry_WithL1dEntry_SansMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_WithXcr0_SansIbpbEntry_WithL1dEntry_SansMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_SansXcr0_WithIbpbEntry_WithL1dEntry_SansMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_WithXcr0_WithIbpbEntry_WithL1dEntry_SansMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_SansXcr0_SansIbpbEntry_SansL1dEntry_WithMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_WithXcr0_SansIbpbEntry_SansL1dEntry_WithMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_SansXcr0_WithIbpbEntry_SansL1dEntry_WithMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_WithXcr0_WithIbpbEntry_SansL1dEntry_WithMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_SansXcr0_SansIbpbEntry_WithL1dEntry_WithMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_WithXcr0_SansIbpbEntry_WithL1dEntry_WithMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_SansXcr0_WithIbpbEntry_WithL1dEntry_WithMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_WithXcr0_WithIbpbEntry_WithL1dEntry_WithMdsEntry_SansIbpbExit },
{ hmR0VmxStartVm_SansXcr0_SansIbpbEntry_SansL1dEntry_SansMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_WithXcr0_SansIbpbEntry_SansL1dEntry_SansMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_SansXcr0_WithIbpbEntry_SansL1dEntry_SansMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_WithXcr0_WithIbpbEntry_SansL1dEntry_SansMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_SansXcr0_SansIbpbEntry_WithL1dEntry_SansMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_WithXcr0_SansIbpbEntry_WithL1dEntry_SansMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_SansXcr0_WithIbpbEntry_WithL1dEntry_SansMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_WithXcr0_WithIbpbEntry_WithL1dEntry_SansMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_SansXcr0_SansIbpbEntry_SansL1dEntry_WithMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_WithXcr0_SansIbpbEntry_SansL1dEntry_WithMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_SansXcr0_WithIbpbEntry_SansL1dEntry_WithMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_WithXcr0_WithIbpbEntry_SansL1dEntry_WithMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_SansXcr0_SansIbpbEntry_WithL1dEntry_WithMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_WithXcr0_SansIbpbEntry_WithL1dEntry_WithMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_SansXcr0_WithIbpbEntry_WithL1dEntry_WithMdsEntry_WithIbpbExit },
{ hmR0VmxStartVm_WithXcr0_WithIbpbEntry_WithL1dEntry_WithMdsEntry_WithIbpbExit },
};
uintptr_t const idx = (pVCpu->hmr0.s.fLoadSaveGuestXcr0 ? 1 : 0)
| (pVCpu->hmr0.s.fWorldSwitcher & HM_WSF_IBPB_ENTRY ? 2 : 0)
| (pVCpu->hmr0.s.fWorldSwitcher & HM_WSF_L1D_ENTRY ? 4 : 0)
| (pVCpu->hmr0.s.fWorldSwitcher & HM_WSF_MDS_ENTRY ? 8 : 0)
| (pVCpu->hmr0.s.fWorldSwitcher & HM_WSF_IBPB_EXIT ? 16 : 0);
PFNHMVMXSTARTVM const pfnStartVm = s_aHmR0VmxStartVmFunctions[idx].pfn;
if (pVCpu->hmr0.s.vmx.pfnStartVm != pfnStartVm)
pVCpu->hmr0.s.vmx.pfnStartVm = pfnStartVm;
}
/**
* Pushes a 2-byte value onto the real-mode (in virtual-8086 mode) guest's
* stack.
*
* @returns Strict VBox status code (i.e. informational status codes too).
* @retval VINF_EM_RESET if pushing a value to the stack caused a triple-fault.
* @param pVCpu The cross context virtual CPU structure.
* @param uValue The value to push to the guest stack.
*/
static VBOXSTRICTRC hmR0VmxRealModeGuestStackPush(PVMCPUCC pVCpu, uint16_t uValue)
{
/*
* The stack limit is 0xffff in real-on-virtual 8086 mode. Real-mode with weird stack limits cannot be run in
* virtual 8086 mode in VT-x. See Intel spec. 26.3.1.2 "Checks on Guest Segment Registers".
* See Intel Instruction reference for PUSH and Intel spec. 22.33.1 "Segment Wraparound".
*/
PCPUMCTX pCtx = &pVCpu->cpum.GstCtx;
if (pCtx->sp == 1)
return VINF_EM_RESET;
pCtx->sp -= sizeof(uint16_t); /* May wrap around which is expected behaviour. */
int rc = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), pCtx->ss.u64Base + pCtx->sp, &uValue, sizeof(uint16_t));
AssertRC(rc);
return rc;
}
/**
* Wrapper around VMXWriteVmcs16 taking a pVCpu parameter so VCC doesn't complain about
* unreferenced local parameters in the template code...
*/
DECL_FORCE_INLINE(int) hmR0VmxWriteVmcs16(PCVMCPUCC pVCpu, uint32_t uFieldEnc, uint16_t u16Val)
{
RT_NOREF(pVCpu);
return VMXWriteVmcs16(uFieldEnc, u16Val);
}
/**
* Wrapper around VMXWriteVmcs32 taking a pVCpu parameter so VCC doesn't complain about
* unreferenced local parameters in the template code...
*/
DECL_FORCE_INLINE(int) hmR0VmxWriteVmcs32(PCVMCPUCC pVCpu, uint32_t uFieldEnc, uint32_t u32Val)
{
RT_NOREF(pVCpu);
return VMXWriteVmcs32(uFieldEnc, u32Val);
}
/**
* Wrapper around VMXWriteVmcs64 taking a pVCpu parameter so VCC doesn't complain about
* unreferenced local parameters in the template code...
*/
DECL_FORCE_INLINE(int) hmR0VmxWriteVmcs64(PCVMCPUCC pVCpu, uint32_t uFieldEnc, uint64_t u64Val)
{
RT_NOREF(pVCpu);
return VMXWriteVmcs64(uFieldEnc, u64Val);
}
/**
* Wrapper around VMXReadVmcs16 taking a pVCpu parameter so VCC doesn't complain about
* unreferenced local parameters in the template code...
*/
DECL_FORCE_INLINE(int) hmR0VmxReadVmcs16(PCVMCPUCC pVCpu, uint32_t uFieldEnc, uint16_t *pu16Val)
{
RT_NOREF(pVCpu);
return VMXReadVmcs16(uFieldEnc, pu16Val);
}
/**
* Wrapper around VMXReadVmcs32 taking a pVCpu parameter so VCC doesn't complain about
* unreferenced local parameters in the template code...
*/
DECL_FORCE_INLINE(int) hmR0VmxReadVmcs32(PCVMCPUCC pVCpu, uint32_t uFieldEnc, uint32_t *pu32Val)
{
RT_NOREF(pVCpu);
return VMXReadVmcs32(uFieldEnc, pu32Val);
}
/**
* Wrapper around VMXReadVmcs64 taking a pVCpu parameter so VCC doesn't complain about
* unreferenced local parameters in the template code...
*/
DECL_FORCE_INLINE(int) hmR0VmxReadVmcs64(PCVMCPUCC pVCpu, uint32_t uFieldEnc, uint64_t *pu64Val)
{
RT_NOREF(pVCpu);
return VMXReadVmcs64(uFieldEnc, pu64Val);
}
/*
* Instantiate the code we share with the NEM darwin backend.
*/
#define VCPU_2_VMXSTATE(a_pVCpu) (a_pVCpu)->hm.s
#define VCPU_2_VMXSTATS(a_pVCpu) (a_pVCpu)->hm.s
#define VM_IS_VMX_UNRESTRICTED_GUEST(a_pVM) (a_pVM)->hmr0.s.vmx.fUnrestrictedGuest
#define VM_IS_VMX_NESTED_PAGING(a_pVM) (a_pVM)->hmr0.s.fNestedPaging
#define VM_IS_VMX_PREEMPT_TIMER_USED(a_pVM) (a_pVM)->hmr0.s.vmx.fUsePreemptTimer
#define VM_IS_VMX_LBR(a_pVM) (a_pVM)->hmr0.s.vmx.fLbr
#define VMX_VMCS_WRITE_16(a_pVCpu, a_FieldEnc, a_Val) hmR0VmxWriteVmcs16((a_pVCpu), (a_FieldEnc), (a_Val))
#define VMX_VMCS_WRITE_32(a_pVCpu, a_FieldEnc, a_Val) hmR0VmxWriteVmcs32((a_pVCpu), (a_FieldEnc), (a_Val))
#define VMX_VMCS_WRITE_64(a_pVCpu, a_FieldEnc, a_Val) hmR0VmxWriteVmcs64((a_pVCpu), (a_FieldEnc), (a_Val))
#define VMX_VMCS_WRITE_NW(a_pVCpu, a_FieldEnc, a_Val) hmR0VmxWriteVmcs64((a_pVCpu), (a_FieldEnc), (a_Val))
#define VMX_VMCS_READ_16(a_pVCpu, a_FieldEnc, a_pVal) hmR0VmxReadVmcs16((a_pVCpu), (a_FieldEnc), (a_pVal))
#define VMX_VMCS_READ_32(a_pVCpu, a_FieldEnc, a_pVal) hmR0VmxReadVmcs32((a_pVCpu), (a_FieldEnc), (a_pVal))
#define VMX_VMCS_READ_64(a_pVCpu, a_FieldEnc, a_pVal) hmR0VmxReadVmcs64((a_pVCpu), (a_FieldEnc), (a_pVal))
#define VMX_VMCS_READ_NW(a_pVCpu, a_FieldEnc, a_pVal) hmR0VmxReadVmcs64((a_pVCpu), (a_FieldEnc), (a_pVal))
#include "../VMMAll/VMXAllTemplate.cpp.h"
#undef VMX_VMCS_WRITE_16
#undef VMX_VMCS_WRITE_32
#undef VMX_VMCS_WRITE_64
#undef VMX_VMCS_WRITE_NW
#undef VMX_VMCS_READ_16
#undef VMX_VMCS_READ_32
#undef VMX_VMCS_READ_64
#undef VMX_VMCS_READ_NW
#undef VM_IS_VMX_PREEMPT_TIMER_USED
#undef VM_IS_VMX_NESTED_PAGING
#undef VM_IS_VMX_UNRESTRICTED_GUEST
#undef VCPU_2_VMXSTATS
#undef VCPU_2_VMXSTATE
/**
* Updates the VM's last error record.
*
* If there was a VMX instruction error, reads the error data from the VMCS and
* updates VCPU's last error record as well.
*
* @param pVCpu The cross context virtual CPU structure of the calling EMT.
* Can be NULL if @a rc is not VERR_VMX_UNABLE_TO_START_VM or
* VERR_VMX_INVALID_VMCS_FIELD.
* @param rc The error code.
*/
static void hmR0VmxUpdateErrorRecord(PVMCPUCC pVCpu, int rc)
{
if ( rc == VERR_VMX_INVALID_VMCS_FIELD
|| rc == VERR_VMX_UNABLE_TO_START_VM)
{
AssertPtrReturnVoid(pVCpu);
VMXReadVmcs32(VMX_VMCS32_RO_VM_INSTR_ERROR, &pVCpu->hm.s.vmx.LastError.u32InstrError);
}
pVCpu->CTX_SUFF(pVM)->hm.s.ForR3.rcInit = rc;
}
/**
* Enters VMX root mode operation on the current CPU.
*
* @returns VBox status code.
* @param pHostCpu The HM physical-CPU structure.
* @param pVM The cross context VM structure. Can be
* NULL, after a resume.
* @param HCPhysCpuPage Physical address of the VMXON region.
* @param pvCpuPage Pointer to the VMXON region.
*/
static int hmR0VmxEnterRootMode(PHMPHYSCPU pHostCpu, PVMCC pVM, RTHCPHYS HCPhysCpuPage, void *pvCpuPage)
{
Assert(pHostCpu);
Assert(HCPhysCpuPage && HCPhysCpuPage != NIL_RTHCPHYS);
Assert(RT_ALIGN_T(HCPhysCpuPage, _4K, RTHCPHYS) == HCPhysCpuPage);
Assert(pvCpuPage);
Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
if (pVM)
{
/* Write the VMCS revision identifier to the VMXON region. */
*(uint32_t *)pvCpuPage = RT_BF_GET(g_HmMsrs.u.vmx.u64Basic, VMX_BF_BASIC_VMCS_ID);
}
/* Paranoid: Disable interrupts as, in theory, interrupt handlers might mess with CR4. */
RTCCUINTREG const fEFlags = ASMIntDisableFlags();
/* Enable the VMX bit in CR4 if necessary. */
RTCCUINTREG const uOldCr4 = SUPR0ChangeCR4(X86_CR4_VMXE, RTCCUINTREG_MAX);
/* Record whether VMXE was already prior to us enabling it above. */
pHostCpu->fVmxeAlreadyEnabled = RT_BOOL(uOldCr4 & X86_CR4_VMXE);
/* Enter VMX root mode. */
int rc = VMXEnable(HCPhysCpuPage);
if (RT_FAILURE(rc))
{
/* Restore CR4.VMXE if it was not set prior to our attempt to set it above. */
if (!pHostCpu->fVmxeAlreadyEnabled)
SUPR0ChangeCR4(0 /* fOrMask */, ~(uint64_t)X86_CR4_VMXE);
if (pVM)
pVM->hm.s.ForR3.vmx.HCPhysVmxEnableError = HCPhysCpuPage;
}
/* Restore interrupts. */
ASMSetFlags(fEFlags);
return rc;
}
/**
* Exits VMX root mode operation on the current CPU.
*
* @returns VBox status code.
* @param pHostCpu The HM physical-CPU structure.
*/
static int hmR0VmxLeaveRootMode(PHMPHYSCPU pHostCpu)
{
Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
/* Paranoid: Disable interrupts as, in theory, interrupts handlers might mess with CR4. */
RTCCUINTREG const fEFlags = ASMIntDisableFlags();
/* If we're for some reason not in VMX root mode, then don't leave it. */
RTCCUINTREG const uHostCr4 = ASMGetCR4();
int rc;
if (uHostCr4 & X86_CR4_VMXE)
{
/* Exit VMX root mode and clear the VMX bit in CR4. */
VMXDisable();
/* Clear CR4.VMXE only if it was clear prior to use setting it. */
if (!pHostCpu->fVmxeAlreadyEnabled)
SUPR0ChangeCR4(0 /* fOrMask */, ~(uint64_t)X86_CR4_VMXE);
rc = VINF_SUCCESS;
}
else
rc = VERR_VMX_NOT_IN_VMX_ROOT_MODE;
/* Restore interrupts. */
ASMSetFlags(fEFlags);
return rc;
}
/**
* Allocates pages specified as specified by an array of VMX page allocation info
* objects.
*
* The pages contents are zero'd after allocation.
*
* @returns VBox status code.
* @param phMemObj Where to return the handle to the allocation.
* @param paAllocInfo The pointer to the first element of the VMX
* page-allocation info object array.
* @param cEntries The number of elements in the @a paAllocInfo array.
*/
static int hmR0VmxPagesAllocZ(PRTR0MEMOBJ phMemObj, PVMXPAGEALLOCINFO paAllocInfo, uint32_t cEntries)
{
*phMemObj = NIL_RTR0MEMOBJ;
/* Figure out how many pages to allocate. */
uint32_t cPages = 0;
for (uint32_t iPage = 0; iPage < cEntries; iPage++)
cPages += !!paAllocInfo[iPage].fValid;
/* Allocate the pages. */
if (cPages)
{
size_t const cbPages = cPages << HOST_PAGE_SHIFT;
int rc = RTR0MemObjAllocPage(phMemObj, cbPages, false /* fExecutable */);
if (RT_FAILURE(rc))
return rc;
/* Zero the contents and assign each page to the corresponding VMX page-allocation entry. */
void *pvFirstPage = RTR0MemObjAddress(*phMemObj);
RT_BZERO(pvFirstPage, cbPages);
uint32_t iPage = 0;
for (uint32_t i = 0; i < cEntries; i++)
if (paAllocInfo[i].fValid)
{
RTHCPHYS const HCPhysPage = RTR0MemObjGetPagePhysAddr(*phMemObj, iPage);
void *pvPage = (void *)((uintptr_t)pvFirstPage + (iPage << X86_PAGE_4K_SHIFT));
Assert(HCPhysPage && HCPhysPage != NIL_RTHCPHYS);
AssertPtr(pvPage);
Assert(paAllocInfo[iPage].pHCPhys);
Assert(paAllocInfo[iPage].ppVirt);
*paAllocInfo[iPage].pHCPhys = HCPhysPage;
*paAllocInfo[iPage].ppVirt = pvPage;
/* Move to next page. */
++iPage;
}
/* Make sure all valid (requested) pages have been assigned. */
Assert(iPage == cPages);
}
return VINF_SUCCESS;
}
/**
* Frees pages allocated using hmR0VmxPagesAllocZ.
*
* @param phMemObj Pointer to the memory object handle. Will be set to
* NIL.
*/
DECL_FORCE_INLINE(void) hmR0VmxPagesFree(PRTR0MEMOBJ phMemObj)
{
/* We can cleanup wholesale since it's all one allocation. */
if (*phMemObj != NIL_RTR0MEMOBJ)
{
RTR0MemObjFree(*phMemObj, true /* fFreeMappings */);
*phMemObj = NIL_RTR0MEMOBJ;
}
}
/**
* Initializes a VMCS info. object.
*
* @param pVmcsInfo The VMCS info. object.
* @param pVmcsInfoShared The VMCS info. object shared with ring-3.
*/
static void hmR0VmxVmcsInfoInit(PVMXVMCSINFO pVmcsInfo, PVMXVMCSINFOSHARED pVmcsInfoShared)
{
RT_ZERO(*pVmcsInfo);
RT_ZERO(*pVmcsInfoShared);
pVmcsInfo->pShared = pVmcsInfoShared;
Assert(pVmcsInfo->hMemObj == NIL_RTR0MEMOBJ);
pVmcsInfo->HCPhysVmcs = NIL_RTHCPHYS;
pVmcsInfo->HCPhysShadowVmcs = NIL_RTHCPHYS;
pVmcsInfo->HCPhysMsrBitmap = NIL_RTHCPHYS;
pVmcsInfo->HCPhysGuestMsrLoad = NIL_RTHCPHYS;
pVmcsInfo->HCPhysGuestMsrStore = NIL_RTHCPHYS;
pVmcsInfo->HCPhysHostMsrLoad = NIL_RTHCPHYS;
pVmcsInfo->HCPhysVirtApic = NIL_RTHCPHYS;
pVmcsInfo->HCPhysEPTP = NIL_RTHCPHYS;
pVmcsInfo->u64VmcsLinkPtr = NIL_RTHCPHYS;
pVmcsInfo->idHostCpuState = NIL_RTCPUID;
pVmcsInfo->idHostCpuExec = NIL_RTCPUID;
}
/**
* Frees the VT-x structures for a VMCS info. object.
*
* @param pVmcsInfo The VMCS info. object.
* @param pVmcsInfoShared The VMCS info. object shared with ring-3.
*/
static void hmR0VmxVmcsInfoFree(PVMXVMCSINFO pVmcsInfo, PVMXVMCSINFOSHARED pVmcsInfoShared)
{
hmR0VmxPagesFree(&pVmcsInfo->hMemObj);
hmR0VmxVmcsInfoInit(pVmcsInfo, pVmcsInfoShared);
}
/**
* Allocates the VT-x structures for a VMCS info. object.
*
* @returns VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param pVmcsInfo The VMCS info. object.
* @param fIsNstGstVmcs Whether this is a nested-guest VMCS.
*
* @remarks The caller is expected to take care of any and all allocation failures.
* This function will not perform any cleanup for failures half-way
* through.
*/
static int hmR0VmxAllocVmcsInfo(PVMCPUCC pVCpu, PVMXVMCSINFO pVmcsInfo, bool fIsNstGstVmcs)
{
PVMCC pVM = pVCpu->CTX_SUFF(pVM);
bool const fMsrBitmaps = RT_BOOL(g_HmMsrs.u.vmx.ProcCtls.n.allowed1 & VMX_PROC_CTLS_USE_MSR_BITMAPS);
bool const fShadowVmcs = !fIsNstGstVmcs ? pVM->hmr0.s.vmx.fUseVmcsShadowing : pVM->cpum.ro.GuestFeatures.fVmxVmcsShadowing;
Assert(!pVM->cpum.ro.GuestFeatures.fVmxVmcsShadowing); /* VMCS shadowing is not yet exposed to the guest. */
VMXPAGEALLOCINFO aAllocInfo[] =
{
{ true, 0 /* Unused */, &pVmcsInfo->HCPhysVmcs, &pVmcsInfo->pvVmcs },
{ true, 0 /* Unused */, &pVmcsInfo->HCPhysGuestMsrLoad, &pVmcsInfo->pvGuestMsrLoad },
{ true, 0 /* Unused */, &pVmcsInfo->HCPhysHostMsrLoad, &pVmcsInfo->pvHostMsrLoad },
{ fMsrBitmaps, 0 /* Unused */, &pVmcsInfo->HCPhysMsrBitmap, &pVmcsInfo->pvMsrBitmap },
{ fShadowVmcs, 0 /* Unused */, &pVmcsInfo->HCPhysShadowVmcs, &pVmcsInfo->pvShadowVmcs },
};
int rc = hmR0VmxPagesAllocZ(&pVmcsInfo->hMemObj, &aAllocInfo[0], RT_ELEMENTS(aAllocInfo));
if (RT_FAILURE(rc))
return rc;
/*
* We use the same page for VM-entry MSR-load and VM-exit MSR store areas.
* Because they contain a symmetric list of guest MSRs to load on VM-entry and store on VM-exit.
*/
AssertCompile(RT_ELEMENTS(aAllocInfo) > 0);
Assert(pVmcsInfo->HCPhysGuestMsrLoad != NIL_RTHCPHYS);
pVmcsInfo->pvGuestMsrStore = pVmcsInfo->pvGuestMsrLoad;
pVmcsInfo->HCPhysGuestMsrStore = pVmcsInfo->HCPhysGuestMsrLoad;
/*
* Get the virtual-APIC page rather than allocating them again.
*/
if (g_HmMsrs.u.vmx.ProcCtls.n.allowed1 & VMX_PROC_CTLS_USE_TPR_SHADOW)
{
if (!fIsNstGstVmcs)
{
if (PDMHasApic(pVM))
{
rc = PDMR0ApicGetApicPageForCpu(pVCpu, &pVmcsInfo->HCPhysVirtApic, (PRTR0PTR)&pVmcsInfo->pbVirtApic, NULL /*pR3Ptr*/);
if (RT_FAILURE(rc))
return rc;
Assert(pVmcsInfo->pbVirtApic);
Assert(pVmcsInfo->HCPhysVirtApic && pVmcsInfo->HCPhysVirtApic != NIL_RTHCPHYS);
}
}
else
{
/* These are setup later while marging the nested-guest VMCS. */
Assert(pVmcsInfo->pbVirtApic == NULL);
Assert(pVmcsInfo->HCPhysVirtApic == NIL_RTHCPHYS);
}
}
return VINF_SUCCESS;
}
/**
* Free all VT-x structures for the VM.
*
* @param pVM The cross context VM structure.
*/
static void hmR0VmxStructsFree(PVMCC pVM)
{
hmR0VmxPagesFree(&pVM->hmr0.s.vmx.hMemObj);
#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
if (pVM->hmr0.s.vmx.fUseVmcsShadowing)
{
RTMemFree(pVM->hmr0.s.vmx.paShadowVmcsFields);
pVM->hmr0.s.vmx.paShadowVmcsFields = NULL;
RTMemFree(pVM->hmr0.s.vmx.paShadowVmcsRoFields);
pVM->hmr0.s.vmx.paShadowVmcsRoFields = NULL;
}
#endif
for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
{
PVMCPUCC pVCpu = VMCC_GET_CPU(pVM, idCpu);
hmR0VmxVmcsInfoFree(&pVCpu->hmr0.s.vmx.VmcsInfo, &pVCpu->hm.s.vmx.VmcsInfo);
#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
if (pVM->cpum.ro.GuestFeatures.fVmx)
hmR0VmxVmcsInfoFree(&pVCpu->hmr0.s.vmx.VmcsInfoNstGst, &pVCpu->hm.s.vmx.VmcsInfoNstGst);
#endif
}
}