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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
|
/** @file
* HM - VMX Structures and Definitions. (VMM)
*/
/*
* Copyright (C) 2006-2023 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>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
#ifndef VBOX_INCLUDED_vmm_hmvmxinline_h
#define VBOX_INCLUDED_vmm_hmvmxinline_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <VBox/vmm/hm_vmx.h>
#include <VBox/err.h>
/* In Visual C++ versions prior to 2012, the vmx intrinsics are only available
when targeting AMD64. */
#if RT_INLINE_ASM_USES_INTRIN >= RT_MSC_VER_VS2010 && defined(RT_ARCH_AMD64)
# include <iprt/sanitized/intrin.h>
/* We always want them as intrinsics, no functions. */
# pragma intrinsic(__vmx_on)
# pragma intrinsic(__vmx_off)
# pragma intrinsic(__vmx_vmclear)
# pragma intrinsic(__vmx_vmptrld)
# pragma intrinsic(__vmx_vmread)
# pragma intrinsic(__vmx_vmwrite)
# define VMX_USE_MSC_INTRINSICS 1
#else
# define VMX_USE_MSC_INTRINSICS 0
#endif
/**
* Whether we think the assembler supports VMX instructions.
*
* Guess that GCC 5 should have sufficient recent enough binutils.
*/
#if RT_INLINE_ASM_GNU_STYLE && RT_GNUC_PREREQ(5,0)
# define VMX_USE_GNU_STYLE_INLINE_VMX_INSTRUCTIONS 1
#else
# define VMX_USE_GNU_STYLE_INLINE_VMX_INSTRUCTIONS 0
#endif
/** Whether we can use the subsection trick to put error handling code
* elsewhere. */
#if VMX_USE_GNU_STYLE_INLINE_VMX_INSTRUCTIONS && defined(__ELF__)
# define VMX_USE_GNU_STYLE_INLINE_SECTION_TRICK 1
#else
# define VMX_USE_GNU_STYLE_INLINE_SECTION_TRICK 0
#endif
/* Skip checking VMREAD/VMWRITE failures on non-strict builds. */
#ifndef VBOX_STRICT
# define VBOX_WITH_VMREAD_VMWRITE_NOCHECK
#endif
/** @defgroup grp_hm_vmx_inline VMX Inline Helpers
* @ingroup grp_hm_vmx
* @{
*/
/**
* Gets the effective width of a VMCS field given it's encoding adjusted for
* HIGH/FULL access for 64-bit fields.
*
* @returns The effective VMCS field width.
* @param uFieldEnc The VMCS field encoding.
*
* @remarks Warning! This function does not verify the encoding is for a valid and
* supported VMCS field.
*/
DECLINLINE(uint8_t) VMXGetVmcsFieldWidthEff(uint32_t uFieldEnc)
{
/* Only the "HIGH" parts of all 64-bit fields have bit 0 set. */
if (uFieldEnc & RT_BIT(0))
return VMXVMCSFIELDWIDTH_32BIT;
/* Bits 13:14 contains the width of the VMCS field, see VMXVMCSFIELDWIDTH_XXX. */
return (uFieldEnc >> 13) & 0x3;
}
/**
* Returns whether the given VMCS field is a read-only VMCS field or not.
*
* @returns @c true if it's a read-only field, @c false otherwise.
* @param uFieldEnc The VMCS field encoding.
*
* @remarks Warning! This function does not verify that the encoding is for a valid
* and/or supported VMCS field.
*/
DECLINLINE(bool) VMXIsVmcsFieldReadOnly(uint32_t uFieldEnc)
{
/* See Intel spec. B.4.2 "Natural-Width Read-Only Data Fields". */
return (RT_BF_GET(uFieldEnc, VMX_BF_VMCSFIELD_TYPE) == VMXVMCSFIELDTYPE_VMEXIT_INFO);
}
/**
* Returns whether the given VM-entry interruption-information type is valid or not.
*
* @returns @c true if it's a valid type, @c false otherwise.
* @param fSupportsMTF Whether the Monitor-Trap Flag CPU feature is supported.
* @param uType The VM-entry interruption-information type.
*/
DECLINLINE(bool) VMXIsEntryIntInfoTypeValid(bool fSupportsMTF, uint8_t uType)
{
/* See Intel spec. 26.2.1.3 "VM-Entry Control Fields". */
switch (uType)
{
case VMX_ENTRY_INT_INFO_TYPE_EXT_INT:
case VMX_ENTRY_INT_INFO_TYPE_NMI:
case VMX_ENTRY_INT_INFO_TYPE_HW_XCPT:
case VMX_ENTRY_INT_INFO_TYPE_SW_INT:
case VMX_ENTRY_INT_INFO_TYPE_PRIV_SW_XCPT:
case VMX_ENTRY_INT_INFO_TYPE_SW_XCPT: return true;
case VMX_ENTRY_INT_INFO_TYPE_OTHER_EVENT: return fSupportsMTF;
default:
return false;
}
}
/**
* Returns whether the given VM-entry interruption-information vector and type
* combination is valid or not.
*
* @returns @c true if it's a valid vector/type combination, @c false otherwise.
* @param uVector The VM-entry interruption-information vector.
* @param uType The VM-entry interruption-information type.
*
* @remarks Warning! This function does not validate the type field individually.
* Use it after verifying type is valid using HMVmxIsEntryIntInfoTypeValid.
*/
DECLINLINE(bool) VMXIsEntryIntInfoVectorValid(uint8_t uVector, uint8_t uType)
{
/* See Intel spec. 26.2.1.3 "VM-Entry Control Fields". */
if ( uType == VMX_ENTRY_INT_INFO_TYPE_NMI
&& uVector != X86_XCPT_NMI)
return false;
if ( uType == VMX_ENTRY_INT_INFO_TYPE_HW_XCPT
&& uVector > X86_XCPT_LAST)
return false;
if ( uType == VMX_ENTRY_INT_INFO_TYPE_OTHER_EVENT
&& uVector != VMX_ENTRY_INT_INFO_VECTOR_MTF)
return false;
return true;
}
/**
* Returns whether or not the VM-exit is trap-like or fault-like.
*
* @returns @c true if it's a trap-like VM-exit, @c false otherwise.
* @param uExitReason The VM-exit reason.
*
* @remarks Warning! This does not validate the VM-exit reason.
*/
DECLINLINE(bool) VMXIsVmexitTrapLike(uint32_t uExitReason)
{
/*
* Trap-like VM-exits - The instruction causing the VM-exit completes before the
* VM-exit occurs.
*
* Fault-like VM-exits - The instruction causing the VM-exit is not completed before
* the VM-exit occurs.
*
* See Intel spec. 25.5.2 "Monitor Trap Flag".
* See Intel spec. 29.1.4 "EOI Virtualization".
* See Intel spec. 29.4.3.3 "APIC-Write VM Exits".
* See Intel spec. 29.1.2 "TPR Virtualization".
*/
/** @todo NSTVMX: r=ramshankar: What about VM-exits due to debug traps (single-step,
* I/O breakpoints, data breakpoints), debug exceptions (data breakpoint)
* delayed by MovSS blocking, machine-check exceptions. */
switch (uExitReason)
{
case VMX_EXIT_MTF:
case VMX_EXIT_VIRTUALIZED_EOI:
case VMX_EXIT_APIC_WRITE:
case VMX_EXIT_TPR_BELOW_THRESHOLD:
return true;
}
return false;
}
/**
* Returns whether the VM-entry is vectoring or not given the VM-entry interruption
* information field.
*
* @returns @c true if the VM-entry is vectoring, @c false otherwise.
* @param uEntryIntInfo The VM-entry interruption information field.
* @param pEntryIntInfoType The VM-entry interruption information type field.
* Optional, can be NULL. Only updated when this
* function returns @c true.
*/
DECLINLINE(bool) VMXIsVmentryVectoring(uint32_t uEntryIntInfo, uint8_t *pEntryIntInfoType)
{
/*
* The definition of what is a vectoring VM-entry is taken
* from Intel spec. 26.6 "Special Features of VM Entry".
*/
if (!VMX_ENTRY_INT_INFO_IS_VALID(uEntryIntInfo))
return false;
/* Scope and keep variable defines on top to satisy archaic c89 nonsense. */
{
uint8_t const uType = VMX_ENTRY_INT_INFO_TYPE(uEntryIntInfo);
switch (uType)
{
case VMX_ENTRY_INT_INFO_TYPE_EXT_INT:
case VMX_ENTRY_INT_INFO_TYPE_NMI:
case VMX_ENTRY_INT_INFO_TYPE_HW_XCPT:
case VMX_ENTRY_INT_INFO_TYPE_SW_INT:
case VMX_ENTRY_INT_INFO_TYPE_PRIV_SW_XCPT:
case VMX_ENTRY_INT_INFO_TYPE_SW_XCPT:
{
if (pEntryIntInfoType)
*pEntryIntInfoType = uType;
return true;
}
}
}
return false;
}
/**
* Gets the description for a VMX abort reason.
*
* @returns The descriptive string.
* @param enmAbort The VMX abort reason.
*/
DECLINLINE(const char *) VMXGetAbortDesc(VMXABORT enmAbort)
{
switch (enmAbort)
{
case VMXABORT_NONE: return "VMXABORT_NONE";
case VMXABORT_SAVE_GUEST_MSRS: return "VMXABORT_SAVE_GUEST_MSRS";
case VMXBOART_HOST_PDPTE: return "VMXBOART_HOST_PDPTE";
case VMXABORT_CURRENT_VMCS_CORRUPT: return "VMXABORT_CURRENT_VMCS_CORRUPT";
case VMXABORT_LOAD_HOST_MSR: return "VMXABORT_LOAD_HOST_MSR";
case VMXABORT_MACHINE_CHECK_XCPT: return "VMXABORT_MACHINE_CHECK_XCPT";
case VMXABORT_HOST_NOT_IN_LONG_MODE: return "VMXABORT_HOST_NOT_IN_LONG_MODE";
default:
break;
}
return "Unknown/invalid";
}
/**
* Gets the description for a virtual VMCS state.
*
* @returns The descriptive string.
* @param fVmcsState The virtual-VMCS state.
*/
DECLINLINE(const char *) VMXGetVmcsStateDesc(uint8_t fVmcsState)
{
switch (fVmcsState)
{
case VMX_V_VMCS_LAUNCH_STATE_CLEAR: return "Clear";
case VMX_V_VMCS_LAUNCH_STATE_LAUNCHED: return "Launched";
default: return "Unknown";
}
}
/**
* Gets the description for a VM-entry interruption information event type.
*
* @returns The descriptive string.
* @param uType The event type.
*/
DECLINLINE(const char *) VMXGetEntryIntInfoTypeDesc(uint8_t uType)
{
switch (uType)
{
case VMX_ENTRY_INT_INFO_TYPE_EXT_INT: return "External Interrupt";
case VMX_ENTRY_INT_INFO_TYPE_NMI: return "NMI";
case VMX_ENTRY_INT_INFO_TYPE_HW_XCPT: return "Hardware Exception";
case VMX_ENTRY_INT_INFO_TYPE_SW_INT: return "Software Interrupt";
case VMX_ENTRY_INT_INFO_TYPE_PRIV_SW_XCPT: return "Priv. Software Exception";
case VMX_ENTRY_INT_INFO_TYPE_SW_XCPT: return "Software Exception";
case VMX_ENTRY_INT_INFO_TYPE_OTHER_EVENT: return "Other Event";
default:
break;
}
return "Unknown/invalid";
}
/**
* Gets the description for a VM-exit interruption information event type.
*
* @returns The descriptive string.
* @param uType The event type.
*/
DECLINLINE(const char *) VMXGetExitIntInfoTypeDesc(uint8_t uType)
{
switch (uType)
{
case VMX_EXIT_INT_INFO_TYPE_EXT_INT: return "External Interrupt";
case VMX_EXIT_INT_INFO_TYPE_NMI: return "NMI";
case VMX_EXIT_INT_INFO_TYPE_HW_XCPT: return "Hardware Exception";
case VMX_EXIT_INT_INFO_TYPE_SW_INT: return "Software Interrupt";
case VMX_EXIT_INT_INFO_TYPE_PRIV_SW_XCPT: return "Priv. Software Exception";
case VMX_EXIT_INT_INFO_TYPE_SW_XCPT: return "Software Exception";
default:
break;
}
return "Unknown/invalid";
}
/**
* Gets the description for an IDT-vectoring information event type.
*
* @returns The descriptive string.
* @param uType The event type.
*/
DECLINLINE(const char *) VMXGetIdtVectoringInfoTypeDesc(uint8_t uType)
{
switch (uType)
{
case VMX_IDT_VECTORING_INFO_TYPE_EXT_INT: return "External Interrupt";
case VMX_IDT_VECTORING_INFO_TYPE_NMI: return "NMI";
case VMX_IDT_VECTORING_INFO_TYPE_HW_XCPT: return "Hardware Exception";
case VMX_IDT_VECTORING_INFO_TYPE_SW_INT: return "Software Interrupt";
case VMX_IDT_VECTORING_INFO_TYPE_PRIV_SW_XCPT: return "Priv. Software Exception";
case VMX_IDT_VECTORING_INFO_TYPE_SW_XCPT: return "Software Exception";
default:
break;
}
return "Unknown/invalid";
}
/** @} */
/** @defgroup grp_hm_vmx_asm VMX Assembly Helpers
* @{
*/
#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
/**
* Dispatches an NMI to the host.
*/
DECLASM(int) VMXDispatchHostNmi(void);
/**
* Executes VMXON.
*
* @returns VBox status code.
* @param HCPhysVmxOn Physical address of VMXON structure.
*/
#if RT_INLINE_ASM_EXTERNAL && !VMX_USE_MSC_INTRINSICS
DECLASM(int) VMXEnable(RTHCPHYS HCPhysVmxOn);
#else
DECLINLINE(int) VMXEnable(RTHCPHYS HCPhysVmxOn)
{
# if VMX_USE_MSC_INTRINSICS
unsigned char rcMsc = __vmx_on(&HCPhysVmxOn);
if (RT_LIKELY(rcMsc == 0))
return VINF_SUCCESS;
return rcMsc == 2 ? VERR_VMX_INVALID_VMXON_PTR : VERR_VMX_VMXON_FAILED;
# elif RT_INLINE_ASM_GNU_STYLE
# ifdef RT_ARCH_AMD64
int rc;
__asm__ __volatile__ (
"pushq %2 \n\t"
".byte 0xf3, 0x0f, 0xc7, 0x34, 0x24 # VMXON [esp] \n\t"
"ja 2f \n\t"
"je 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMXON_PTR)", %0 \n\t"
"jmp 2f \n\t"
"1: \n\t"
"movl $" RT_XSTR(VERR_VMX_VMXON_FAILED)", %0 \n\t"
"2: \n\t"
"add $8, %%rsp \n\t"
:"=rm"(rc)
:"0"(VINF_SUCCESS),
"ir"(HCPhysVmxOn) /* don't allow direct memory reference here, */
/* this would not work with -fomit-frame-pointer */
:"memory"
);
return rc;
# else
int rc;
__asm__ __volatile__ (
"push %3 \n\t"
"push %2 \n\t"
".byte 0xf3, 0x0f, 0xc7, 0x34, 0x24 # VMXON [esp] \n\t"
"ja 2f \n\t"
"je 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMXON_PTR)", %0 \n\t"
"jmp 2f \n\t"
"1: \n\t"
"movl $" RT_XSTR(VERR_VMX_VMXON_FAILED)", %0 \n\t"
"2: \n\t"
"add $8, %%esp \n\t"
:"=rm"(rc)
:"0"(VINF_SUCCESS),
"ir"((uint32_t)HCPhysVmxOn), /* don't allow direct memory reference here, */
"ir"((uint32_t)(HCPhysVmxOn >> 32)) /* this would not work with -fomit-frame-pointer */
:"memory"
);
return rc;
# endif
# elif defined(RT_ARCH_X86)
int rc = VINF_SUCCESS;
__asm
{
push dword ptr [HCPhysVmxOn + 4]
push dword ptr [HCPhysVmxOn]
_emit 0xf3
_emit 0x0f
_emit 0xc7
_emit 0x34
_emit 0x24 /* VMXON [esp] */
jnc vmxon_good
mov dword ptr [rc], VERR_VMX_INVALID_VMXON_PTR
jmp the_end
vmxon_good:
jnz the_end
mov dword ptr [rc], VERR_VMX_VMXON_FAILED
the_end:
add esp, 8
}
return rc;
# else
# error "Shouldn't be here..."
# endif
}
#endif
/**
* Executes VMXOFF.
*/
#if RT_INLINE_ASM_EXTERNAL && !VMX_USE_MSC_INTRINSICS
DECLASM(void) VMXDisable(void);
#else
DECLINLINE(void) VMXDisable(void)
{
# if VMX_USE_MSC_INTRINSICS
__vmx_off();
# elif RT_INLINE_ASM_GNU_STYLE
__asm__ __volatile__ (
".byte 0x0f, 0x01, 0xc4 # VMXOFF \n\t"
);
# elif defined(RT_ARCH_X86)
__asm
{
_emit 0x0f
_emit 0x01
_emit 0xc4 /* VMXOFF */
}
# else
# error "Shouldn't be here..."
# endif
}
#endif
/**
* Executes VMCLEAR.
*
* @returns VBox status code.
* @param HCPhysVmcs Physical address of VM control structure.
*/
#if RT_INLINE_ASM_EXTERNAL && !VMX_USE_MSC_INTRINSICS
DECLASM(int) VMXClearVmcs(RTHCPHYS HCPhysVmcs);
#else
DECLINLINE(int) VMXClearVmcs(RTHCPHYS HCPhysVmcs)
{
# if VMX_USE_MSC_INTRINSICS
unsigned char rcMsc = __vmx_vmclear(&HCPhysVmcs);
if (RT_LIKELY(rcMsc == 0))
return VINF_SUCCESS;
return VERR_VMX_INVALID_VMCS_PTR;
# elif RT_INLINE_ASM_GNU_STYLE
# ifdef RT_ARCH_AMD64
int rc;
__asm__ __volatile__ (
"pushq %2 \n\t"
".byte 0x66, 0x0f, 0xc7, 0x34, 0x24 # VMCLEAR [esp] \n\t"
"jnc 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_PTR)", %0 \n\t"
"1: \n\t"
"add $8, %%rsp \n\t"
:"=rm"(rc)
:"0"(VINF_SUCCESS),
"ir"(HCPhysVmcs) /* don't allow direct memory reference here, */
/* this would not work with -fomit-frame-pointer */
:"memory"
);
return rc;
# else
int rc;
__asm__ __volatile__ (
"push %3 \n\t"
"push %2 \n\t"
".byte 0x66, 0x0f, 0xc7, 0x34, 0x24 # VMCLEAR [esp] \n\t"
"jnc 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_PTR)", %0 \n\t"
"1: \n\t"
"add $8, %%esp \n\t"
:"=rm"(rc)
:"0"(VINF_SUCCESS),
"ir"((uint32_t)HCPhysVmcs), /* don't allow direct memory reference here, */
"ir"((uint32_t)(HCPhysVmcs >> 32)) /* this would not work with -fomit-frame-pointer */
:"memory"
);
return rc;
# endif
# elif defined(RT_ARCH_X86)
int rc = VINF_SUCCESS;
__asm
{
push dword ptr [HCPhysVmcs + 4]
push dword ptr [HCPhysVmcs]
_emit 0x66
_emit 0x0f
_emit 0xc7
_emit 0x34
_emit 0x24 /* VMCLEAR [esp] */
jnc success
mov dword ptr [rc], VERR_VMX_INVALID_VMCS_PTR
success:
add esp, 8
}
return rc;
# else
# error "Shouldn't be here..."
# endif
}
#endif
/**
* Executes VMPTRLD.
*
* @returns VBox status code.
* @param HCPhysVmcs Physical address of VMCS structure.
*/
#if RT_INLINE_ASM_EXTERNAL && !VMX_USE_MSC_INTRINSICS
DECLASM(int) VMXLoadVmcs(RTHCPHYS HCPhysVmcs);
#else
DECLINLINE(int) VMXLoadVmcs(RTHCPHYS HCPhysVmcs)
{
# if VMX_USE_MSC_INTRINSICS
unsigned char rcMsc = __vmx_vmptrld(&HCPhysVmcs);
if (RT_LIKELY(rcMsc == 0))
return VINF_SUCCESS;
return VERR_VMX_INVALID_VMCS_PTR;
# elif RT_INLINE_ASM_GNU_STYLE
# ifdef RT_ARCH_AMD64
int rc;
__asm__ __volatile__ (
"pushq %2 \n\t"
".byte 0x0f, 0xc7, 0x34, 0x24 # VMPTRLD [esp] \n\t"
"jnc 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_PTR)", %0 \n\t"
"1: \n\t"
"add $8, %%rsp \n\t"
:"=rm"(rc)
:"0"(VINF_SUCCESS),
"ir"(HCPhysVmcs) /* don't allow direct memory reference here, */
/* this will not work with -fomit-frame-pointer */
:"memory"
);
return rc;
# else
int rc;
__asm__ __volatile__ (
"push %3 \n\t"
"push %2 \n\t"
".byte 0x0f, 0xc7, 0x34, 0x24 # VMPTRLD [esp] \n\t"
"jnc 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_PTR)", %0 \n\t"
"1: \n\t"
"add $8, %%esp \n\t"
:"=rm"(rc)
:"0"(VINF_SUCCESS),
"ir"((uint32_t)HCPhysVmcs), /* don't allow direct memory reference here, */
"ir"((uint32_t)(HCPhysVmcs >> 32)) /* this will not work with -fomit-frame-pointer */
:"memory"
);
return rc;
# endif
# elif defined(RT_ARCH_X86)
int rc = VINF_SUCCESS;
__asm
{
push dword ptr [HCPhysVmcs + 4]
push dword ptr [HCPhysVmcs]
_emit 0x0f
_emit 0xc7
_emit 0x34
_emit 0x24 /* VMPTRLD [esp] */
jnc success
mov dword ptr [rc], VERR_VMX_INVALID_VMCS_PTR
success:
add esp, 8
}
return rc;
# else
# error "Shouldn't be here..."
# endif
}
#endif
/**
* Executes VMPTRST.
*
* @returns VBox status code.
* @param pHCPhysVmcs Where to store the physical address of the current
* VMCS.
*/
DECLASM(int) VMXGetCurrentVmcs(RTHCPHYS *pHCPhysVmcs);
/**
* Executes VMWRITE for a 32-bit field.
*
* @returns VBox status code.
* @retval VINF_SUCCESS.
* @retval VERR_VMX_INVALID_VMCS_PTR.
* @retval VERR_VMX_INVALID_VMCS_FIELD.
*
* @param uFieldEnc VMCS field encoding.
* @param u32Val The 32-bit value to set.
*
* @remarks The values of the two status codes can be OR'ed together, the result
* will be VERR_VMX_INVALID_VMCS_PTR.
*/
#if RT_INLINE_ASM_EXTERNAL && !VMX_USE_MSC_INTRINSICS
DECLASM(int) VMXWriteVmcs32(uint32_t uFieldEnc, uint32_t u32Val);
#else
DECLINLINE(int) VMXWriteVmcs32(uint32_t uFieldEnc, uint32_t u32Val)
{
# if VMX_USE_MSC_INTRINSICS
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
__vmx_vmwrite(uFieldEnc, u32Val);
return VINF_SUCCESS;
# else
unsigned char rcMsc = __vmx_vmwrite(uFieldEnc, u32Val);
if (RT_LIKELY(rcMsc == 0))
return VINF_SUCCESS;
return rcMsc == 2 ? VERR_VMX_INVALID_VMCS_PTR : VERR_VMX_INVALID_VMCS_FIELD;
# endif
# elif RT_INLINE_ASM_GNU_STYLE
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
__asm__ __volatile__ (
".byte 0x0f, 0x79, 0xc2 # VMWRITE eax, edx \n\t"
:
:"a"(uFieldEnc),
"d"(u32Val)
);
return VINF_SUCCESS;
# else
int rc;
__asm__ __volatile__ (
".byte 0x0f, 0x79, 0xc2 # VMWRITE eax, edx \n\t"
"ja 2f \n\t"
"je 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_PTR)", %0 \n\t"
"jmp 2f \n\t"
"1: \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_FIELD)", %0 \n\t"
"2: \n\t"
:"=rm"(rc)
:"0"(VINF_SUCCESS),
"a"(uFieldEnc),
"d"(u32Val)
);
return rc;
# endif
# elif defined(RT_ARCH_X86)
int rc = VINF_SUCCESS;
__asm
{
push dword ptr [u32Val]
mov eax, [uFieldEnc]
_emit 0x0f
_emit 0x79
_emit 0x04
_emit 0x24 /* VMWRITE eax, [esp] */
jnc valid_vmcs
mov dword ptr [rc], VERR_VMX_INVALID_VMCS_PTR
jmp the_end
valid_vmcs:
jnz the_end
mov dword ptr [rc], VERR_VMX_INVALID_VMCS_FIELD
the_end:
add esp, 4
}
return rc;
# else
# error "Shouldn't be here..."
# endif
}
#endif
/**
* Executes VMWRITE for a 64-bit field.
*
* @returns VBox status code.
* @retval VINF_SUCCESS.
* @retval VERR_VMX_INVALID_VMCS_PTR.
* @retval VERR_VMX_INVALID_VMCS_FIELD.
*
* @param uFieldEnc The VMCS field encoding.
* @param u64Val The 16, 32 or 64-bit value to set.
*
* @remarks The values of the two status codes can be OR'ed together, the result
* will be VERR_VMX_INVALID_VMCS_PTR.
*/
#if defined(RT_ARCH_X86) || (RT_INLINE_ASM_EXTERNAL && !VMX_USE_MSC_INTRINSICS)
DECLASM(int) VMXWriteVmcs64(uint32_t uFieldEnc, uint64_t u64Val);
#else
DECLINLINE(int) VMXWriteVmcs64(uint32_t uFieldEnc, uint64_t u64Val)
{
# if VMX_USE_MSC_INTRINSICS
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
__vmx_vmwrite(uFieldEnc, u64Val);
return VINF_SUCCESS;
# else
unsigned char rcMsc = __vmx_vmwrite(uFieldEnc, u64Val);
if (RT_LIKELY(rcMsc == 0))
return VINF_SUCCESS;
return rcMsc == 2 ? VERR_VMX_INVALID_VMCS_PTR : VERR_VMX_INVALID_VMCS_FIELD;
# endif
# elif RT_INLINE_ASM_GNU_STYLE
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
__asm__ __volatile__ (
".byte 0x0f, 0x79, 0xc2 # VMWRITE eax, edx \n\t"
:
:"a"(uFieldEnc),
"d"(u64Val)
);
return VINF_SUCCESS;
# else
int rc;
__asm__ __volatile__ (
".byte 0x0f, 0x79, 0xc2 # VMWRITE eax, edx \n\t"
"ja 2f \n\t"
"je 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_PTR)", %0 \n\t"
"jmp 2f \n\t"
"1: \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_FIELD)", %0 \n\t"
"2: \n\t"
:"=rm"(rc)
:"0"(VINF_SUCCESS),
"a"(uFieldEnc),
"d"(u64Val)
);
return rc;
# endif
# else
# error "Shouldn't be here..."
# endif
}
#endif
/**
* Executes VMWRITE for a 16-bit VMCS field.
*
* @returns VBox status code.
* @retval VINF_SUCCESS.
* @retval VERR_VMX_INVALID_VMCS_PTR.
* @retval VERR_VMX_INVALID_VMCS_FIELD.
*
* @param uVmcsField The VMCS field.
* @param u16Val The 16-bit value to set.
*
* @remarks The values of the two status codes can be OR'ed together, the result
* will be VERR_VMX_INVALID_VMCS_PTR.
*/
DECLINLINE(int) VMXWriteVmcs16(uint32_t uVmcsField, uint16_t u16Val)
{
AssertMsg(RT_BF_GET(uVmcsField, VMX_BF_VMCSFIELD_WIDTH) == VMX_VMCSFIELD_WIDTH_16BIT, ("%#RX32\n", uVmcsField));
return VMXWriteVmcs32(uVmcsField, u16Val);
}
/**
* Executes VMWRITE for a natural-width VMCS field.
*/
#ifdef RT_ARCH_AMD64
# define VMXWriteVmcsNw VMXWriteVmcs64
#else
# define VMXWriteVmcsNw VMXWriteVmcs32
#endif
/**
* Invalidate a page using INVEPT.
*
* @returns VBox status code.
* @param enmFlush Type of flush.
* @param pDescriptor Pointer to the descriptor.
*/
DECLASM(int) VMXR0InvEPT(VMXTLBFLUSHEPT enmFlush, uint64_t *pDescriptor);
/**
* Invalidate a page using INVVPID.
*
* @returns VBox status code.
* @param enmFlush Type of flush.
* @param pDescriptor Pointer to the descriptor.
*/
DECLASM(int) VMXR0InvVPID(VMXTLBFLUSHVPID enmFlush, uint64_t *pDescriptor);
/**
* Executes VMREAD for a 32-bit field.
*
* @returns VBox status code.
* @retval VINF_SUCCESS.
* @retval VERR_VMX_INVALID_VMCS_PTR.
* @retval VERR_VMX_INVALID_VMCS_FIELD.
*
* @param uFieldEnc The VMCS field encoding.
* @param pData Where to store VMCS field value.
*
* @remarks The values of the two status codes can be OR'ed together, the result
* will be VERR_VMX_INVALID_VMCS_PTR.
*/
#if RT_INLINE_ASM_EXTERNAL && !VMX_USE_MSC_INTRINSICS
DECLASM(int) VMXReadVmcs32(uint32_t uFieldEnc, uint32_t *pData);
#else
DECLINLINE(int) VMXReadVmcs32(uint32_t uFieldEnc, uint32_t *pData)
{
# if VMX_USE_MSC_INTRINSICS
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
uint64_t u64Tmp = 0;
__vmx_vmread(uFieldEnc, &u64Tmp);
*pData = (uint32_t)u64Tmp;
return VINF_SUCCESS;
# else
unsigned char rcMsc;
uint64_t u64Tmp;
rcMsc = __vmx_vmread(uFieldEnc, &u64Tmp);
*pData = (uint32_t)u64Tmp;
if (RT_LIKELY(rcMsc == 0))
return VINF_SUCCESS;
return rcMsc == 2 ? VERR_VMX_INVALID_VMCS_PTR : VERR_VMX_INVALID_VMCS_FIELD;
# endif
# elif VMX_USE_GNU_STYLE_INLINE_VMX_INSTRUCTIONS
RTCCUINTREG uTmp = 0;
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
__asm__ __volatile__("vmread %[uField],%[uDst]"
: [uDst] "=mr" (uTmp)
: [uField] "r" ((RTCCUINTREG)uFieldEnc));
*pData = (uint32_t)uTmp;
return VINF_SUCCESS;
# else
#if 0
int rc;
__asm__ __volatile__("vmread %[uField],%[uDst]\n\t"
"movl %[rcSuccess],%[rc]\n\t"
# if VMX_USE_GNU_STYLE_INLINE_SECTION_TRICK
"jna 1f\n\t"
".section .text.vmread_failures, \"ax?\"\n\t"
"1:\n\t"
"movl %[rcInvalidVmcsPtr],%[rc]\n\t"
"jnz 2f\n\t"
"movl %[rcInvalidVmcsField],%[rc]\n\t"
"2:\n\t"
"jmp 3f\n\t"
".previous\n\t"
"3:\n\t"
# else
"ja 1f\n\t"
"movl %[rcInvalidVmcsPtr],%[rc]\n\t"
"jnz 1f\n\t"
"movl %[rcInvalidVmcsField],%[rc]\n\t"
"1:\n\t"
# endif
: [uDst] "=mr" (uTmp)
, [rc] "=r" (rc)
: [uField] "r" ((RTCCUINTREG)uFieldEnc)
, [rcSuccess] "i" (VINF_SUCCESS)
, [rcInvalidVmcsPtr] "i" (VERR_VMX_INVALID_VMCS_PTR)
, [rcInvalidVmcsField] "i" (VERR_VMX_INVALID_VMCS_FIELD));
*pData = uTmp;
return rc;
#else
int fSuccess, fFieldError;
__asm__ __volatile__("vmread %[uField],%[uDst]"
: [uDst] "=mr" (uTmp)
, "=@cca" (fSuccess)
, "=@ccnc" (fFieldError)
: [uField] "r" ((RTCCUINTREG)uFieldEnc));
*pData = uTmp;
return RT_LIKELY(fSuccess) ? VINF_SUCCESS : fFieldError ? VERR_VMX_INVALID_VMCS_FIELD : VERR_VMX_INVALID_VMCS_PTR;
#endif
# endif
# elif RT_INLINE_ASM_GNU_STYLE
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
__asm__ __volatile__ (
".byte 0x0f, 0x78, 0xc2 # VMREAD eax, edx \n\t"
:"=d"(*pData)
:"a"(uFieldEnc),
"d"(0)
);
return VINF_SUCCESS;
# else
int rc;
__asm__ __volatile__ (
"movl $" RT_XSTR(VINF_SUCCESS)", %0 \n\t"
".byte 0x0f, 0x78, 0xc2 # VMREAD eax, edx \n\t"
"ja 2f \n\t"
"je 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_PTR)", %0 \n\t"
"jmp 2f \n\t"
"1: \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_FIELD)", %0 \n\t"
"2: \n\t"
:"=&r"(rc),
"=d"(*pData)
:"a"(uFieldEnc),
"d"(0)
);
return rc;
# endif
# elif defined(RT_ARCH_X86)
int rc = VINF_SUCCESS;
__asm
{
sub esp, 4
mov dword ptr [esp], 0
mov eax, [uFieldEnc]
_emit 0x0f
_emit 0x78
_emit 0x04
_emit 0x24 /* VMREAD eax, [esp] */
mov edx, pData
pop dword ptr [edx]
jnc valid_vmcs
mov dword ptr [rc], VERR_VMX_INVALID_VMCS_PTR
jmp the_end
valid_vmcs:
jnz the_end
mov dword ptr [rc], VERR_VMX_INVALID_VMCS_FIELD
the_end:
}
return rc;
# else
# error "Shouldn't be here..."
# endif
}
#endif
/**
* Executes VMREAD for a 64-bit field.
*
* @returns VBox status code.
* @retval VINF_SUCCESS.
* @retval VERR_VMX_INVALID_VMCS_PTR.
* @retval VERR_VMX_INVALID_VMCS_FIELD.
*
* @param uFieldEnc The VMCS field encoding.
* @param pData Where to store VMCS field value.
*
* @remarks The values of the two status codes can be OR'ed together, the result
* will be VERR_VMX_INVALID_VMCS_PTR.
*/
#if defined(RT_ARCH_X86) || (RT_INLINE_ASM_EXTERNAL && !VMX_USE_MSC_INTRINSICS)
DECLASM(int) VMXReadVmcs64(uint32_t uFieldEnc, uint64_t *pData);
#else
DECLINLINE(int) VMXReadVmcs64(uint32_t uFieldEnc, uint64_t *pData)
{
# if VMX_USE_MSC_INTRINSICS
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
__vmx_vmread(uFieldEnc, pData);
return VINF_SUCCESS;
# else
unsigned char rcMsc;
rcMsc = __vmx_vmread(uFieldEnc, pData);
if (RT_LIKELY(rcMsc == 0))
return VINF_SUCCESS;
return rcMsc == 2 ? VERR_VMX_INVALID_VMCS_PTR : VERR_VMX_INVALID_VMCS_FIELD;
# endif
# elif VMX_USE_GNU_STYLE_INLINE_VMX_INSTRUCTIONS
uint64_t uTmp = 0;
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
__asm__ __volatile__("vmreadq %[uField],%[uDst]"
: [uDst] "=m" (uTmp)
: [uField] "r" ((uint64_t)uFieldEnc));
*pData = uTmp;
return VINF_SUCCESS;
# elif 0
int rc;
__asm__ __volatile__("vmreadq %[uField],%[uDst]\n\t"
"movl %[rcSuccess],%[rc]\n\t"
# if VMX_USE_GNU_STYLE_INLINE_SECTION_TRICK
"jna 1f\n\t"
".section .text.vmread_failures, \"ax?\"\n\t"
"1:\n\t"
"movl %[rcInvalidVmcsPtr],%[rc]\n\t"
"jnz 2f\n\t"
"movl %[rcInvalidVmcsField],%[rc]\n\t"
"2:\n\t"
"jmp 3f\n\t"
".previous\n\t"
"3:\n\t"
# else
"ja 1f\n\t"
"movl %[rcInvalidVmcsPtr],%[rc]\n\t"
"jnz 1f\n\t"
"movl %[rcInvalidVmcsField],%[rc]\n\t"
"1:\n\t"
# endif
: [uDst] "=mr" (uTmp)
, [rc] "=r" (rc)
: [uField] "r" ((uint64_t)uFieldEnc)
, [rcSuccess] "i" (VINF_SUCCESS)
, [rcInvalidVmcsPtr] "i" (VERR_VMX_INVALID_VMCS_PTR)
, [rcInvalidVmcsField] "i" (VERR_VMX_INVALID_VMCS_FIELD)
);
*pData = uTmp;
return rc;
# else
int fSuccess, fFieldError;
__asm__ __volatile__("vmread %[uField],%[uDst]"
: [uDst] "=mr" (uTmp)
, "=@cca" (fSuccess)
, "=@ccnc" (fFieldError)
: [uField] "r" ((RTCCUINTREG)uFieldEnc));
*pData = uTmp;
return RT_LIKELY(fSuccess) ? VINF_SUCCESS : fFieldError ? VERR_VMX_INVALID_VMCS_FIELD : VERR_VMX_INVALID_VMCS_PTR;
# endif
# elif RT_INLINE_ASM_GNU_STYLE
# ifdef VBOX_WITH_VMREAD_VMWRITE_NOCHECK
__asm__ __volatile__ (
".byte 0x0f, 0x78, 0xc2 # VMREAD eax, edx \n\t"
:"=d"(*pData)
:"a"(uFieldEnc),
"d"(0)
);
return VINF_SUCCESS;
# else
int rc;
__asm__ __volatile__ (
"movl $" RT_XSTR(VINF_SUCCESS)", %0 \n\t"
".byte 0x0f, 0x78, 0xc2 # VMREAD eax, edx \n\t"
"ja 2f \n\t"
"je 1f \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_PTR)", %0 \n\t"
"jmp 2f \n\t"
"1: \n\t"
"movl $" RT_XSTR(VERR_VMX_INVALID_VMCS_FIELD)", %0 \n\t"
"2: \n\t"
:"=&r"(rc),
"=d"(*pData)
:"a"(uFieldEnc),
"d"(0)
);
return rc;
# endif
# else
# error "Shouldn't be here..."
# endif
}
#endif
/**
* Executes VMREAD for a 16-bit field.
*
* @returns VBox status code.
* @retval VINF_SUCCESS.
* @retval VERR_VMX_INVALID_VMCS_PTR.
* @retval VERR_VMX_INVALID_VMCS_FIELD.
*
* @param uVmcsField The VMCS field.
* @param pData Where to store VMCS field value.
*
* @remarks The values of the two status codes can be OR'ed together, the result
* will be VERR_VMX_INVALID_VMCS_PTR.
*/
DECLINLINE(int) VMXReadVmcs16(uint32_t uVmcsField, uint16_t *pData)
{
uint32_t u32Tmp;
int rc;
AssertMsg(RT_BF_GET(uVmcsField, VMX_BF_VMCSFIELD_WIDTH) == VMX_VMCSFIELD_WIDTH_16BIT, ("%#RX32\n", uVmcsField));
rc = VMXReadVmcs32(uVmcsField, &u32Tmp);
*pData = (uint16_t)u32Tmp;
return rc;
}
/**
* Executes VMREAD for a natural-width VMCS field.
*/
#ifdef RT_ARCH_AMD64
# define VMXReadVmcsNw VMXReadVmcs64
#else
# define VMXReadVmcsNw VMXReadVmcs32
#endif
#endif /* RT_ARCH_AMD64 || RT_ARCH_X86 */
/** @} */
#endif /* !VBOX_INCLUDED_vmm_hmvmxinline_h */
|