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
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
|
/* $Id: EMAll.cpp $ */
/** @file
* EM - Execution Monitor(/Manager) - All contexts
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_EM
#include <VBox/vmm/em.h>
#include <VBox/vmm/mm.h>
#include <VBox/vmm/selm.h>
#include <VBox/vmm/patm.h>
#include <VBox/vmm/pgm.h>
#include <VBox/vmm/iem.h>
#include <VBox/vmm/iom.h>
#include <VBox/vmm/hm.h>
#include <VBox/vmm/pdmapi.h>
#include <VBox/vmm/vmm.h>
#include <VBox/vmm/stam.h>
#include "EMInternal.h"
#include <VBox/vmm/vm.h>
#include <VBox/param.h>
#include <VBox/err.h>
#include <VBox/dis.h>
#include <VBox/disopcode.h>
#include <VBox/log.h>
#include <iprt/assert.h>
#include <iprt/string.h>
/**
* Get the current execution manager status.
*
* @returns Current status.
* @param pVCpu The cross context virtual CPU structure.
*/
VMM_INT_DECL(EMSTATE) EMGetState(PVMCPU pVCpu)
{
return pVCpu->em.s.enmState;
}
/**
* Sets the current execution manager status. (use only when you know what you're doing!)
*
* @param pVCpu The cross context virtual CPU structure.
* @param enmNewState The new state, EMSTATE_WAIT_SIPI or EMSTATE_HALTED.
*/
VMM_INT_DECL(void) EMSetState(PVMCPU pVCpu, EMSTATE enmNewState)
{
/* Only allowed combination: */
Assert(pVCpu->em.s.enmState == EMSTATE_WAIT_SIPI && enmNewState == EMSTATE_HALTED);
pVCpu->em.s.enmState = enmNewState;
}
/**
* Sets the PC for which interrupts should be inhibited.
*
* @param pVCpu The cross context virtual CPU structure.
* @param PC The PC.
*/
VMMDECL(void) EMSetInhibitInterruptsPC(PVMCPU pVCpu, RTGCUINTPTR PC)
{
pVCpu->em.s.GCPtrInhibitInterrupts = PC;
VMCPU_FF_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
}
/**
* Gets the PC for which interrupts should be inhibited.
*
* There are a few instructions which inhibits or delays interrupts
* for the instruction following them. These instructions are:
* - STI
* - MOV SS, r/m16
* - POP SS
*
* @returns The PC for which interrupts should be inhibited.
* @param pVCpu The cross context virtual CPU structure.
*
*/
VMMDECL(RTGCUINTPTR) EMGetInhibitInterruptsPC(PVMCPU pVCpu)
{
return pVCpu->em.s.GCPtrInhibitInterrupts;
}
/**
* Checks if interrupt inhibiting is enabled for the current instruction.
*
* @returns true if interrupts are inhibited, false if not.
* @param pVCpu The cross context virtual CPU structure.
*/
VMMDECL(bool) EMIsInhibitInterruptsActive(PVMCPU pVCpu)
{
if (!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
return false;
if (pVCpu->em.s.GCPtrInhibitInterrupts == CPUMGetGuestRIP(pVCpu))
return true;
VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
return false;
}
/**
* Enables / disable hypercall instructions.
*
* This interface is used by GIM to tell the execution monitors whether the
* hypercall instruction (VMMCALL & VMCALL) are allowed or should \#UD.
*
* @param pVCpu The cross context virtual CPU structure this applies to.
* @param fEnabled Whether hypercall instructions are enabled (true) or not.
*/
VMMDECL(void) EMSetHypercallInstructionsEnabled(PVMCPU pVCpu, bool fEnabled)
{
pVCpu->em.s.fHypercallEnabled = fEnabled;
}
/**
* Checks if hypercall instructions (VMMCALL & VMCALL) are enabled or not.
*
* @returns true if enabled, false if not.
* @param pVCpu The cross context virtual CPU structure.
*
* @note If this call becomes a performance factor, we can make the data
* field available thru a read-only view in VMCPU. See VM::cpum.ro.
*/
VMMDECL(bool) EMAreHypercallInstructionsEnabled(PVMCPU pVCpu)
{
return pVCpu->em.s.fHypercallEnabled;
}
/**
* Prepare an MWAIT - essentials of the MONITOR instruction.
*
* @returns VINF_SUCCESS
* @param pVCpu The cross context virtual CPU structure of the calling EMT.
* @param rax The content of RAX.
* @param rcx The content of RCX.
* @param rdx The content of RDX.
* @param GCPhys The physical address corresponding to rax.
*/
VMM_INT_DECL(int) EMMonitorWaitPrepare(PVMCPU pVCpu, uint64_t rax, uint64_t rcx, uint64_t rdx, RTGCPHYS GCPhys)
{
pVCpu->em.s.MWait.uMonitorRAX = rax;
pVCpu->em.s.MWait.uMonitorRCX = rcx;
pVCpu->em.s.MWait.uMonitorRDX = rdx;
pVCpu->em.s.MWait.fWait |= EMMWAIT_FLAG_MONITOR_ACTIVE;
/** @todo Make use of GCPhys. */
NOREF(GCPhys);
/** @todo Complete MONITOR implementation. */
return VINF_SUCCESS;
}
/**
* Checks if the monitor hardware is armed / active.
*
* @returns true if armed, false otherwise.
* @param pVCpu The cross context virtual CPU structure of the calling EMT.
*/
VMM_INT_DECL(bool) EMMonitorIsArmed(PVMCPU pVCpu)
{
return RT_BOOL(pVCpu->em.s.MWait.fWait & EMMWAIT_FLAG_MONITOR_ACTIVE);
}
/**
* Checks if we're in a MWAIT.
*
* @retval 1 if regular,
* @retval > 1 if MWAIT with EMMWAIT_FLAG_BREAKIRQIF0
* @retval 0 if not armed
* @param pVCpu The cross context virtual CPU structure of the calling EMT.
*/
VMM_INT_DECL(unsigned) EMMonitorWaitIsActive(PVMCPU pVCpu)
{
uint32_t fWait = pVCpu->em.s.MWait.fWait;
AssertCompile(EMMWAIT_FLAG_ACTIVE == 1);
AssertCompile(EMMWAIT_FLAG_BREAKIRQIF0 == 2);
AssertCompile((EMMWAIT_FLAG_ACTIVE << 1) == EMMWAIT_FLAG_BREAKIRQIF0);
return fWait & (EMMWAIT_FLAG_ACTIVE | ((fWait & EMMWAIT_FLAG_ACTIVE) << 1));
}
/**
* Performs an MWAIT.
*
* @returns VINF_SUCCESS
* @param pVCpu The cross context virtual CPU structure of the calling EMT.
* @param rax The content of RAX.
* @param rcx The content of RCX.
*/
VMM_INT_DECL(int) EMMonitorWaitPerform(PVMCPU pVCpu, uint64_t rax, uint64_t rcx)
{
pVCpu->em.s.MWait.uMWaitRAX = rax;
pVCpu->em.s.MWait.uMWaitRCX = rcx;
pVCpu->em.s.MWait.fWait |= EMMWAIT_FLAG_ACTIVE;
if (rcx)
pVCpu->em.s.MWait.fWait |= EMMWAIT_FLAG_BREAKIRQIF0;
else
pVCpu->em.s.MWait.fWait &= ~EMMWAIT_FLAG_BREAKIRQIF0;
/** @todo not completely correct?? */
return VINF_EM_HALT;
}
/**
* Clears any address-range monitoring that is active.
*
* @param pVCpu The cross context virtual CPU structure of the calling EMT.
*/
VMM_INT_DECL(void) EMMonitorWaitClear(PVMCPU pVCpu)
{
LogFlowFunc(("Clearing MWAIT\n"));
pVCpu->em.s.MWait.fWait &= ~(EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0);
}
/**
* Determine if we should continue execution in HM after encountering an mwait
* instruction.
*
* Clears MWAIT flags if returning @c true.
*
* @returns true if we should continue, false if we should halt.
* @param pVCpu The cross context virtual CPU structure.
* @param pCtx Current CPU context.
*/
VMM_INT_DECL(bool) EMMonitorWaitShouldContinue(PVMCPU pVCpu, PCPUMCTX pCtx)
{
if (CPUMGetGuestGif(pCtx))
{
if ( CPUMIsGuestPhysIntrEnabled(pVCpu)
|| ( CPUMIsGuestInNestedHwvirtMode(pCtx)
&& CPUMIsGuestVirtIntrEnabled(pVCpu))
|| ( (pVCpu->em.s.MWait.fWait & (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0))
== (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0)) )
{
if (VMCPU_FF_IS_ANY_SET(pVCpu, ( VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC
| VMCPU_FF_INTERRUPT_NESTED_GUEST)))
{
pVCpu->em.s.MWait.fWait &= ~(EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0);
return true;
}
}
}
return false;
}
/**
* Determine if we should continue execution in HM after encountering a hlt
* instruction.
*
* @returns true if we should continue, false if we should halt.
* @param pVCpu The cross context virtual CPU structure.
* @param pCtx Current CPU context.
*/
VMM_INT_DECL(bool) EMShouldContinueAfterHalt(PVMCPU pVCpu, PCPUMCTX pCtx)
{
if (CPUMGetGuestGif(pCtx))
{
if (CPUMIsGuestPhysIntrEnabled(pVCpu))
return VMCPU_FF_IS_ANY_SET(pVCpu, (VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC));
if ( CPUMIsGuestInNestedHwvirtMode(pCtx)
&& CPUMIsGuestVirtIntrEnabled(pVCpu))
return VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST);
}
return false;
}
/**
* Unhalts and wakes up the given CPU.
*
* This is an API for assisting the KVM hypercall API in implementing KICK_CPU.
* It sets VMCPU_FF_UNHALT for @a pVCpuDst and makes sure it is woken up. If
* the CPU isn't currently in a halt, the next HLT instruction it executes will
* be affected.
*
* @returns GVMMR0SchedWakeUpEx result or VINF_SUCCESS depending on context.
* @param pVM The cross context VM structure.
* @param pVCpuDst The cross context virtual CPU structure of the
* CPU to unhalt and wake up. This is usually not the
* same as the caller.
* @thread EMT
*/
VMM_INT_DECL(int) EMUnhaltAndWakeUp(PVM pVM, PVMCPU pVCpuDst)
{
/*
* Flag the current(/next) HLT to unhalt immediately.
*/
VMCPU_FF_SET(pVCpuDst, VMCPU_FF_UNHALT);
/*
* Wake up the EMT (technically should be abstracted by VMM/VMEmt, but
* just do it here for now).
*/
#ifdef IN_RING0
/* We might be here with preemption disabled or enabled (i.e. depending on
thread-context hooks being used), so don't try obtaining the GVMMR0 used
lock here. See @bugref{7270#c148}. */
int rc = GVMMR0SchedWakeUpNoGVMNoLock(pVM, pVCpuDst->idCpu);
AssertRC(rc);
#elif defined(IN_RING3)
int rc = SUPR3CallVMMR0(pVM->pVMR0, pVCpuDst->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, NULL /* pvArg */);
AssertRC(rc);
#else
/* Nothing to do for raw-mode, shouldn't really be used by raw-mode guests anyway. */
Assert(pVM->cCpus == 1); NOREF(pVM);
int rc = VINF_SUCCESS;
#endif
return rc;
}
#ifndef IN_RING3
/**
* Makes an I/O port write pending for ring-3 processing.
*
* @returns VINF_EM_PENDING_R3_IOPORT_READ
* @param pVCpu The cross context virtual CPU structure.
* @param uPort The I/O port.
* @param cbInstr The instruction length (for RIP updating).
* @param cbValue The write size.
* @param uValue The value being written.
* @sa emR3ExecutePendingIoPortWrite
*
* @note Must not be used when I/O port breakpoints are pending or when single stepping.
*/
VMMRZ_INT_DECL(VBOXSTRICTRC)
EMRZSetPendingIoPortWrite(PVMCPU pVCpu, RTIOPORT uPort, uint8_t cbInstr, uint8_t cbValue, uint32_t uValue)
{
Assert(pVCpu->em.s.PendingIoPortAccess.cbValue == 0);
pVCpu->em.s.PendingIoPortAccess.uPort = uPort;
pVCpu->em.s.PendingIoPortAccess.cbValue = cbValue;
pVCpu->em.s.PendingIoPortAccess.cbInstr = cbInstr;
pVCpu->em.s.PendingIoPortAccess.uValue = uValue;
return VINF_EM_PENDING_R3_IOPORT_WRITE;
}
/**
* Makes an I/O port read pending for ring-3 processing.
*
* @returns VINF_EM_PENDING_R3_IOPORT_READ
* @param pVCpu The cross context virtual CPU structure.
* @param uPort The I/O port.
* @param cbInstr The instruction length (for RIP updating).
* @param cbValue The read size.
* @sa emR3ExecutePendingIoPortRead
*
* @note Must not be used when I/O port breakpoints are pending or when single stepping.
*/
VMMRZ_INT_DECL(VBOXSTRICTRC)
EMRZSetPendingIoPortRead(PVMCPU pVCpu, RTIOPORT uPort, uint8_t cbInstr, uint8_t cbValue)
{
Assert(pVCpu->em.s.PendingIoPortAccess.cbValue == 0);
pVCpu->em.s.PendingIoPortAccess.uPort = uPort;
pVCpu->em.s.PendingIoPortAccess.cbValue = cbValue;
pVCpu->em.s.PendingIoPortAccess.cbInstr = cbInstr;
pVCpu->em.s.PendingIoPortAccess.uValue = UINT32_C(0x52454144); /* 'READ' */
return VINF_EM_PENDING_R3_IOPORT_READ;
}
#endif /* IN_RING3 */
/**
* Worker for EMHistoryExec that checks for ring-3 returns and flags
* continuation of the EMHistoryExec run there.
*/
DECL_FORCE_INLINE(void) emHistoryExecSetContinueExitRecIdx(PVMCPU pVCpu, VBOXSTRICTRC rcStrict, PCEMEXITREC pExitRec)
{
pVCpu->em.s.idxContinueExitRec = UINT16_MAX;
#ifdef IN_RING3
RT_NOREF_PV(rcStrict); RT_NOREF_PV(pExitRec);
#else
switch (VBOXSTRICTRC_VAL(rcStrict))
{
case VINF_SUCCESS:
default:
break;
/*
* Only status codes that EMHandleRCTmpl.h will resume EMHistoryExec with.
*/
case VINF_IOM_R3_IOPORT_READ: /* -> emR3ExecuteIOInstruction */
case VINF_IOM_R3_IOPORT_WRITE: /* -> emR3ExecuteIOInstruction */
case VINF_IOM_R3_IOPORT_COMMIT_WRITE: /* -> VMCPU_FF_IOM -> VINF_EM_RESUME_R3_HISTORY_EXEC -> emR3ExecuteIOInstruction */
case VINF_IOM_R3_MMIO_READ: /* -> emR3ExecuteInstruction */
case VINF_IOM_R3_MMIO_WRITE: /* -> emR3ExecuteInstruction */
case VINF_IOM_R3_MMIO_READ_WRITE: /* -> emR3ExecuteInstruction */
case VINF_IOM_R3_MMIO_COMMIT_WRITE: /* -> VMCPU_FF_IOM -> VINF_EM_RESUME_R3_HISTORY_EXEC -> emR3ExecuteIOInstruction */
case VINF_CPUM_R3_MSR_READ: /* -> emR3ExecuteInstruction */
case VINF_CPUM_R3_MSR_WRITE: /* -> emR3ExecuteInstruction */
case VINF_GIM_R3_HYPERCALL: /* -> emR3ExecuteInstruction */
pVCpu->em.s.idxContinueExitRec = (uint16_t)(pExitRec - &pVCpu->em.s.aExitRecords[0]);
break;
}
#endif /* !IN_RING3 */
}
#ifndef IN_RC
/**
* Execute using history.
*
* This function will be called when EMHistoryAddExit() and friends returns a
* non-NULL result. This happens in response to probing or when probing has
* uncovered adjacent exits which can more effectively be reached by using IEM
* than restarting execution using the main execution engine and fielding an
* regular exit.
*
* @returns VBox strict status code, see IEMExecForExits.
* @param pVCpu The cross context virtual CPU structure.
* @param pExitRec The exit record return by a previous history add
* or update call.
* @param fWillExit Flags indicating to IEM what will cause exits, TBD.
*/
VMM_INT_DECL(VBOXSTRICTRC) EMHistoryExec(PVMCPU pVCpu, PCEMEXITREC pExitRec, uint32_t fWillExit)
{
Assert(pExitRec);
VMCPU_ASSERT_EMT(pVCpu);
IEMEXECFOREXITSTATS ExecStats;
switch (pExitRec->enmAction)
{
/*
* Executes multiple instruction stopping only when we've gone a given
* number without perceived exits.
*/
case EMEXITACTION_EXEC_WITH_MAX:
{
STAM_REL_PROFILE_START(&pVCpu->em.s.StatHistoryExec, a);
LogFlow(("EMHistoryExec/EXEC_WITH_MAX: %RX64, max %u\n", pExitRec->uFlatPC, pExitRec->cMaxInstructionsWithoutExit));
VBOXSTRICTRC rcStrict = IEMExecForExits(pVCpu, fWillExit,
pExitRec->cMaxInstructionsWithoutExit /* cMinInstructions*/,
pVCpu->em.s.cHistoryExecMaxInstructions,
pExitRec->cMaxInstructionsWithoutExit,
&ExecStats);
LogFlow(("EMHistoryExec/EXEC_WITH_MAX: %Rrc cExits=%u cMaxExitDistance=%u cInstructions=%u\n",
VBOXSTRICTRC_VAL(rcStrict), ExecStats.cExits, ExecStats.cMaxExitDistance, ExecStats.cInstructions));
emHistoryExecSetContinueExitRecIdx(pVCpu, rcStrict, pExitRec);
/* Ignore instructions IEM doesn't know about. */
if ( ( rcStrict != VERR_IEM_INSTR_NOT_IMPLEMENTED
&& rcStrict != VERR_IEM_ASPECT_NOT_IMPLEMENTED)
|| ExecStats.cInstructions == 0)
{ /* likely */ }
else
rcStrict = VINF_SUCCESS;
if (ExecStats.cExits > 1)
STAM_REL_COUNTER_ADD(&pVCpu->em.s.StatHistoryExecSavedExits, ExecStats.cExits - 1);
STAM_REL_COUNTER_ADD(&pVCpu->em.s.StatHistoryExecInstructions, ExecStats.cInstructions);
STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatHistoryExec, a);
return rcStrict;
}
/*
* Probe a exit for close by exits.
*/
case EMEXITACTION_EXEC_PROBE:
{
STAM_REL_PROFILE_START(&pVCpu->em.s.StatHistoryProbe, b);
LogFlow(("EMHistoryExec/EXEC_PROBE: %RX64\n", pExitRec->uFlatPC));
PEMEXITREC pExitRecUnconst = (PEMEXITREC)pExitRec;
VBOXSTRICTRC rcStrict = IEMExecForExits(pVCpu, fWillExit,
pVCpu->em.s.cHistoryProbeMinInstructions,
pVCpu->em.s.cHistoryExecMaxInstructions,
pVCpu->em.s.cHistoryProbeMaxInstructionsWithoutExit,
&ExecStats);
LogFlow(("EMHistoryExec/EXEC_PROBE: %Rrc cExits=%u cMaxExitDistance=%u cInstructions=%u\n",
VBOXSTRICTRC_VAL(rcStrict), ExecStats.cExits, ExecStats.cMaxExitDistance, ExecStats.cInstructions));
emHistoryExecSetContinueExitRecIdx(pVCpu, rcStrict, pExitRecUnconst);
if ( ExecStats.cExits >= 2
&& RT_SUCCESS(rcStrict))
{
Assert(ExecStats.cMaxExitDistance > 0 && ExecStats.cMaxExitDistance <= 32);
pExitRecUnconst->cMaxInstructionsWithoutExit = ExecStats.cMaxExitDistance;
pExitRecUnconst->enmAction = EMEXITACTION_EXEC_WITH_MAX;
LogFlow(("EMHistoryExec/EXEC_PROBE: -> EXEC_WITH_MAX %u\n", ExecStats.cMaxExitDistance));
STAM_REL_COUNTER_INC(&pVCpu->em.s.StatHistoryProbedExecWithMax);
}
#ifndef IN_RING3
else if ( pVCpu->em.s.idxContinueExitRec != UINT16_MAX
&& RT_SUCCESS(rcStrict))
{
STAM_REL_COUNTER_INC(&pVCpu->em.s.StatHistoryProbedToRing3);
LogFlow(("EMHistoryExec/EXEC_PROBE: -> ring-3\n"));
}
#endif
else
{
pExitRecUnconst->enmAction = EMEXITACTION_NORMAL_PROBED;
pVCpu->em.s.idxContinueExitRec = UINT16_MAX;
LogFlow(("EMHistoryExec/EXEC_PROBE: -> PROBED\n"));
STAM_REL_COUNTER_INC(&pVCpu->em.s.StatHistoryProbedNormal);
if ( rcStrict == VERR_IEM_INSTR_NOT_IMPLEMENTED
|| rcStrict == VERR_IEM_ASPECT_NOT_IMPLEMENTED)
rcStrict = VINF_SUCCESS;
}
STAM_REL_COUNTER_ADD(&pVCpu->em.s.StatHistoryProbeInstructions, ExecStats.cInstructions);
STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatHistoryProbe, b);
return rcStrict;
}
/* We shouldn't ever see these here! */
case EMEXITACTION_FREE_RECORD:
case EMEXITACTION_NORMAL:
case EMEXITACTION_NORMAL_PROBED:
break;
/* No default case, want compiler warnings. */
}
AssertLogRelFailedReturn(VERR_EM_INTERNAL_ERROR);
}
/**
* Worker for emHistoryAddOrUpdateRecord.
*/
DECL_FORCE_INLINE(PCEMEXITREC) emHistoryRecordInit(PEMEXITREC pExitRec, uint64_t uFlatPC, uint32_t uFlagsAndType, uint64_t uExitNo)
{
pExitRec->uFlatPC = uFlatPC;
pExitRec->uFlagsAndType = uFlagsAndType;
pExitRec->enmAction = EMEXITACTION_NORMAL;
pExitRec->bUnused = 0;
pExitRec->cMaxInstructionsWithoutExit = 64;
pExitRec->uLastExitNo = uExitNo;
pExitRec->cHits = 1;
return NULL;
}
/**
* Worker for emHistoryAddOrUpdateRecord.
*/
DECL_FORCE_INLINE(PCEMEXITREC) emHistoryRecordInitNew(PVMCPU pVCpu, PEMEXITENTRY pHistEntry, uintptr_t idxSlot,
PEMEXITREC pExitRec, uint64_t uFlatPC,
uint32_t uFlagsAndType, uint64_t uExitNo)
{
pHistEntry->idxSlot = (uint32_t)idxSlot;
pVCpu->em.s.cExitRecordUsed++;
LogFlow(("emHistoryRecordInitNew: [%#x] = %#07x %016RX64; (%u of %u used)\n", idxSlot, uFlagsAndType, uFlatPC,
pVCpu->em.s.cExitRecordUsed, RT_ELEMENTS(pVCpu->em.s.aExitRecords) ));
return emHistoryRecordInit(pExitRec, uFlatPC, uFlagsAndType, uExitNo);
}
/**
* Worker for emHistoryAddOrUpdateRecord.
*/
DECL_FORCE_INLINE(PCEMEXITREC) emHistoryRecordInitReplacement(PEMEXITENTRY pHistEntry, uintptr_t idxSlot,
PEMEXITREC pExitRec, uint64_t uFlatPC,
uint32_t uFlagsAndType, uint64_t uExitNo)
{
pHistEntry->idxSlot = (uint32_t)idxSlot;
LogFlow(("emHistoryRecordInitReplacement: [%#x] = %#07x %016RX64 replacing %#07x %016RX64 with %u hits, %u exits old\n",
idxSlot, uFlagsAndType, uFlatPC, pExitRec->uFlagsAndType, pExitRec->uFlatPC, pExitRec->cHits,
uExitNo - pExitRec->uLastExitNo));
return emHistoryRecordInit(pExitRec, uFlatPC, uFlagsAndType, uExitNo);
}
/**
* Adds or updates the EMEXITREC for this PC/type and decide on an action.
*
* @returns Pointer to an exit record if special action should be taken using
* EMHistoryExec(). Take normal exit action when NULL.
*
* @param pVCpu The cross context virtual CPU structure.
* @param uFlagsAndType Combined flags and type, EMEXIT_F_KIND_EM set and
* both EMEXIT_F_CS_EIP and EMEXIT_F_UNFLATTENED_PC are clear.
* @param uFlatPC The flattened program counter.
* @param pHistEntry The exit history entry.
* @param uExitNo The current exit number.
*/
static PCEMEXITREC emHistoryAddOrUpdateRecord(PVMCPU pVCpu, uint64_t uFlagsAndType, uint64_t uFlatPC,
PEMEXITENTRY pHistEntry, uint64_t uExitNo)
{
# ifdef IN_RING0
/* Disregard the hm flag. */
uFlagsAndType &= ~EMEXIT_F_HM;
# endif
/*
* Work the hash table.
*/
AssertCompile(RT_ELEMENTS(pVCpu->em.s.aExitRecords) == 1024);
# define EM_EXIT_RECORDS_IDX_MASK 0x3ff
uintptr_t idxSlot = ((uintptr_t)uFlatPC >> 1) & EM_EXIT_RECORDS_IDX_MASK;
PEMEXITREC pExitRec = &pVCpu->em.s.aExitRecords[idxSlot];
if (pExitRec->uFlatPC == uFlatPC)
{
Assert(pExitRec->enmAction != EMEXITACTION_FREE_RECORD);
pHistEntry->idxSlot = (uint32_t)idxSlot;
if (pExitRec->uFlagsAndType == uFlagsAndType)
{
pExitRec->uLastExitNo = uExitNo;
STAM_REL_COUNTER_INC(&pVCpu->em.s.aStatHistoryRecHits[0]);
}
else
{
STAM_REL_COUNTER_INC(&pVCpu->em.s.aStatHistoryRecTypeChanged[0]);
return emHistoryRecordInit(pExitRec, uFlatPC, uFlagsAndType, uExitNo);
}
}
else if (pExitRec->enmAction == EMEXITACTION_FREE_RECORD)
{
STAM_REL_COUNTER_INC(&pVCpu->em.s.aStatHistoryRecNew[0]);
return emHistoryRecordInitNew(pVCpu, pHistEntry, idxSlot, pExitRec, uFlatPC, uFlagsAndType, uExitNo);
}
else
{
/*
* Collision. We calculate a new hash for stepping away from the first,
* doing up to 8 steps away before replacing the least recently used record.
*/
uintptr_t idxOldest = idxSlot;
uint64_t uOldestExitNo = pExitRec->uLastExitNo;
unsigned iOldestStep = 0;
unsigned iStep = 1;
uintptr_t const idxAdd = (uintptr_t)(uFlatPC >> 11) & (EM_EXIT_RECORDS_IDX_MASK / 4);
for (;;)
{
Assert(iStep < RT_ELEMENTS(pVCpu->em.s.aStatHistoryRecHits));
AssertCompile(RT_ELEMENTS(pVCpu->em.s.aStatHistoryRecNew) == RT_ELEMENTS(pVCpu->em.s.aStatHistoryRecHits));
AssertCompile(RT_ELEMENTS(pVCpu->em.s.aStatHistoryRecReplaced) == RT_ELEMENTS(pVCpu->em.s.aStatHistoryRecHits));
AssertCompile(RT_ELEMENTS(pVCpu->em.s.aStatHistoryRecTypeChanged) == RT_ELEMENTS(pVCpu->em.s.aStatHistoryRecHits));
/* Step to the next slot. */
idxSlot += idxAdd;
idxSlot &= EM_EXIT_RECORDS_IDX_MASK;
pExitRec = &pVCpu->em.s.aExitRecords[idxSlot];
/* Does it match? */
if (pExitRec->uFlatPC == uFlatPC)
{
Assert(pExitRec->enmAction != EMEXITACTION_FREE_RECORD);
pHistEntry->idxSlot = (uint32_t)idxSlot;
if (pExitRec->uFlagsAndType == uFlagsAndType)
{
pExitRec->uLastExitNo = uExitNo;
STAM_REL_COUNTER_INC(&pVCpu->em.s.aStatHistoryRecHits[iStep]);
break;
}
STAM_REL_COUNTER_INC(&pVCpu->em.s.aStatHistoryRecTypeChanged[iStep]);
return emHistoryRecordInit(pExitRec, uFlatPC, uFlagsAndType, uExitNo);
}
/* Is it free? */
if (pExitRec->enmAction == EMEXITACTION_FREE_RECORD)
{
STAM_REL_COUNTER_INC(&pVCpu->em.s.aStatHistoryRecNew[iStep]);
return emHistoryRecordInitNew(pVCpu, pHistEntry, idxSlot, pExitRec, uFlatPC, uFlagsAndType, uExitNo);
}
/* Is it the least recently used one? */
if (pExitRec->uLastExitNo < uOldestExitNo)
{
uOldestExitNo = pExitRec->uLastExitNo;
idxOldest = idxSlot;
iOldestStep = iStep;
}
/* Next iteration? */
iStep++;
Assert(iStep < RT_ELEMENTS(pVCpu->em.s.aStatHistoryRecReplaced));
if (RT_LIKELY(iStep < 8 + 1))
{ /* likely */ }
else
{
/* Replace the least recently used slot. */
STAM_REL_COUNTER_INC(&pVCpu->em.s.aStatHistoryRecReplaced[iOldestStep]);
pExitRec = &pVCpu->em.s.aExitRecords[idxOldest];
return emHistoryRecordInitReplacement(pHistEntry, idxOldest, pExitRec, uFlatPC, uFlagsAndType, uExitNo);
}
}
}
/*
* Found an existing record.
*/
switch (pExitRec->enmAction)
{
case EMEXITACTION_NORMAL:
{
uint64_t const cHits = ++pExitRec->cHits;
if (cHits < 256)
return NULL;
LogFlow(("emHistoryAddOrUpdateRecord: [%#x] %#07x %16RX64: -> EXEC_PROBE\n", idxSlot, uFlagsAndType, uFlatPC));
pExitRec->enmAction = EMEXITACTION_EXEC_PROBE;
return pExitRec;
}
case EMEXITACTION_NORMAL_PROBED:
pExitRec->cHits += 1;
return NULL;
default:
pExitRec->cHits += 1;
return pExitRec;
/* This will happen if the caller ignores or cannot serve the probe
request (forced to ring-3, whatever). We retry this 256 times. */
case EMEXITACTION_EXEC_PROBE:
{
uint64_t const cHits = ++pExitRec->cHits;
if (cHits < 512)
return pExitRec;
pExitRec->enmAction = EMEXITACTION_NORMAL_PROBED;
LogFlow(("emHistoryAddOrUpdateRecord: [%#x] %#07x %16RX64: -> PROBED\n", idxSlot, uFlagsAndType, uFlatPC));
return NULL;
}
}
}
#endif /* !IN_RC */
/**
* Adds an exit to the history for this CPU.
*
* @returns Pointer to an exit record if special action should be taken using
* EMHistoryExec(). Take normal exit action when NULL.
*
* @param pVCpu The cross context virtual CPU structure.
* @param uFlagsAndType Combined flags and type (see EMEXIT_MAKE_FLAGS_AND_TYPE).
* @param uFlatPC The flattened program counter (RIP). UINT64_MAX if not available.
* @param uTimestamp The TSC value for the exit, 0 if not available.
* @thread EMT(pVCpu)
*/
VMM_INT_DECL(PCEMEXITREC) EMHistoryAddExit(PVMCPU pVCpu, uint32_t uFlagsAndType, uint64_t uFlatPC, uint64_t uTimestamp)
{
VMCPU_ASSERT_EMT(pVCpu);
/*
* Add the exit history entry.
*/
AssertCompile(RT_ELEMENTS(pVCpu->em.s.aExitHistory) == 256);
uint64_t uExitNo = pVCpu->em.s.iNextExit++;
PEMEXITENTRY pHistEntry = &pVCpu->em.s.aExitHistory[(uintptr_t)uExitNo & 0xff];
pHistEntry->uFlatPC = uFlatPC;
pHistEntry->uTimestamp = uTimestamp;
pHistEntry->uFlagsAndType = uFlagsAndType;
pHistEntry->idxSlot = UINT32_MAX;
#ifndef IN_RC
/*
* If common exit type, we will insert/update the exit into the exit record hash table.
*/
if ( (uFlagsAndType & (EMEXIT_F_KIND_MASK | EMEXIT_F_CS_EIP | EMEXIT_F_UNFLATTENED_PC)) == EMEXIT_F_KIND_EM
# ifdef IN_RING0
&& pVCpu->em.s.fExitOptimizationEnabledR0
&& ( !(uFlagsAndType & EMEXIT_F_HM) || pVCpu->em.s.fExitOptimizationEnabledR0PreemptDisabled)
# else
&& pVCpu->em.s.fExitOptimizationEnabled
# endif
&& uFlatPC != UINT64_MAX
)
return emHistoryAddOrUpdateRecord(pVCpu, uFlagsAndType, uFlatPC, pHistEntry, uExitNo);
#endif
return NULL;
}
#ifdef IN_RC
/**
* Special raw-mode interface for adding an exit to the history.
*
* Currently this is only for recording, not optimizing, so no return value. If
* we start seriously caring about raw-mode again, we may extend it.
*
* @param pVCpu The cross context virtual CPU structure.
* @param uFlagsAndType Combined flags and type (see EMEXIT_MAKE_FLAGS_AND_TYPE).
* @param uCs The CS.
* @param uEip The EIP.
* @param uTimestamp The TSC value for the exit, 0 if not available.
* @thread EMT(0)
*/
VMMRC_INT_DECL(void) EMRCHistoryAddExitCsEip(PVMCPU pVCpu, uint32_t uFlagsAndType, uint16_t uCs, uint32_t uEip, uint64_t uTimestamp)
{
AssertCompile(RT_ELEMENTS(pVCpu->em.s.aExitHistory) == 256);
PEMEXITENTRY pHistEntry = &pVCpu->em.s.aExitHistory[(uintptr_t)(pVCpu->em.s.iNextExit++) & 0xff];
pHistEntry->uFlatPC = ((uint64_t)uCs << 32) | uEip;
pHistEntry->uTimestamp = uTimestamp;
pHistEntry->uFlagsAndType = uFlagsAndType | EMEXIT_F_CS_EIP;
pHistEntry->idxSlot = UINT32_MAX;
}
#endif
#ifdef IN_RING0
/**
* Interface that VT-x uses to supply the PC of an exit when CS:RIP is being read.
*
* @param pVCpu The cross context virtual CPU structure.
* @param uFlatPC The flattened program counter (RIP).
* @param fFlattened Set if RIP was subjected to CS.BASE, clear if not.
*/
VMMR0_INT_DECL(void) EMR0HistoryUpdatePC(PVMCPU pVCpu, uint64_t uFlatPC, bool fFlattened)
{
AssertCompile(RT_ELEMENTS(pVCpu->em.s.aExitHistory) == 256);
uint64_t uExitNo = pVCpu->em.s.iNextExit - 1;
PEMEXITENTRY pHistEntry = &pVCpu->em.s.aExitHistory[(uintptr_t)uExitNo & 0xff];
pHistEntry->uFlatPC = uFlatPC;
if (fFlattened)
pHistEntry->uFlagsAndType &= ~EMEXIT_F_UNFLATTENED_PC;
else
pHistEntry->uFlagsAndType |= EMEXIT_F_UNFLATTENED_PC;
}
#endif
/**
* Interface for convering a engine specific exit to a generic one and get guidance.
*
* @returns Pointer to an exit record if special action should be taken using
* EMHistoryExec(). Take normal exit action when NULL.
*
* @param pVCpu The cross context virtual CPU structure.
* @param uFlagsAndType Combined flags and type (see EMEXIT_MAKE_FLAGS_AND_TYPE).
* @thread EMT(pVCpu)
*/
VMM_INT_DECL(PCEMEXITREC) EMHistoryUpdateFlagsAndType(PVMCPU pVCpu, uint32_t uFlagsAndType)
{
VMCPU_ASSERT_EMT(pVCpu);
/*
* Do the updating.
*/
AssertCompile(RT_ELEMENTS(pVCpu->em.s.aExitHistory) == 256);
uint64_t uExitNo = pVCpu->em.s.iNextExit - 1;
PEMEXITENTRY pHistEntry = &pVCpu->em.s.aExitHistory[(uintptr_t)uExitNo & 0xff];
pHistEntry->uFlagsAndType = uFlagsAndType | (pHistEntry->uFlagsAndType & (EMEXIT_F_CS_EIP | EMEXIT_F_UNFLATTENED_PC));
#ifndef IN_RC
/*
* If common exit type, we will insert/update the exit into the exit record hash table.
*/
if ( (uFlagsAndType & (EMEXIT_F_KIND_MASK | EMEXIT_F_CS_EIP | EMEXIT_F_UNFLATTENED_PC)) == EMEXIT_F_KIND_EM
# ifdef IN_RING0
&& pVCpu->em.s.fExitOptimizationEnabledR0
&& ( !(uFlagsAndType & EMEXIT_F_HM) || pVCpu->em.s.fExitOptimizationEnabledR0PreemptDisabled)
# else
&& pVCpu->em.s.fExitOptimizationEnabled
# endif
&& pHistEntry->uFlatPC != UINT64_MAX
)
return emHistoryAddOrUpdateRecord(pVCpu, uFlagsAndType, pHistEntry->uFlatPC, pHistEntry, uExitNo);
#endif
return NULL;
}
/**
* Interface for convering a engine specific exit to a generic one and get
* guidance, supplying flattened PC too.
*
* @returns Pointer to an exit record if special action should be taken using
* EMHistoryExec(). Take normal exit action when NULL.
*
* @param pVCpu The cross context virtual CPU structure.
* @param uFlagsAndType Combined flags and type (see EMEXIT_MAKE_FLAGS_AND_TYPE).
* @param uFlatPC The flattened program counter (RIP).
* @thread EMT(pVCpu)
*/
VMM_INT_DECL(PCEMEXITREC) EMHistoryUpdateFlagsAndTypeAndPC(PVMCPU pVCpu, uint32_t uFlagsAndType, uint64_t uFlatPC)
{
VMCPU_ASSERT_EMT(pVCpu);
Assert(uFlatPC != UINT64_MAX);
/*
* Do the updating.
*/
AssertCompile(RT_ELEMENTS(pVCpu->em.s.aExitHistory) == 256);
uint64_t uExitNo = pVCpu->em.s.iNextExit - 1;
PEMEXITENTRY pHistEntry = &pVCpu->em.s.aExitHistory[(uintptr_t)uExitNo & 0xff];
pHistEntry->uFlagsAndType = uFlagsAndType;
pHistEntry->uFlatPC = uFlatPC;
#ifndef IN_RC
/*
* If common exit type, we will insert/update the exit into the exit record hash table.
*/
if ( (uFlagsAndType & (EMEXIT_F_KIND_MASK | EMEXIT_F_CS_EIP | EMEXIT_F_UNFLATTENED_PC)) == EMEXIT_F_KIND_EM
# ifdef IN_RING0
&& pVCpu->em.s.fExitOptimizationEnabledR0
&& ( !(uFlagsAndType & EMEXIT_F_HM) || pVCpu->em.s.fExitOptimizationEnabledR0PreemptDisabled)
# else
&& pVCpu->em.s.fExitOptimizationEnabled
# endif
)
return emHistoryAddOrUpdateRecord(pVCpu, uFlagsAndType, uFlatPC, pHistEntry, uExitNo);
#endif
return NULL;
}
/**
* Locks REM execution to a single VCPU.
*
* @param pVM The cross context VM structure.
*/
VMMDECL(void) EMRemLock(PVM pVM)
{
#ifdef VBOX_WITH_REM
if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
return; /* early init */
Assert(!PGMIsLockOwner(pVM));
Assert(!IOMIsLockWriteOwner(pVM));
int rc = PDMCritSectEnter(&pVM->em.s.CritSectREM, VERR_SEM_BUSY);
AssertRCSuccess(rc);
#else
RT_NOREF(pVM);
#endif
}
/**
* Unlocks REM execution
*
* @param pVM The cross context VM structure.
*/
VMMDECL(void) EMRemUnlock(PVM pVM)
{
#ifdef VBOX_WITH_REM
if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
return; /* early init */
PDMCritSectLeave(&pVM->em.s.CritSectREM);
#else
RT_NOREF(pVM);
#endif
}
/**
* Check if this VCPU currently owns the REM lock.
*
* @returns bool owner/not owner
* @param pVM The cross context VM structure.
*/
VMMDECL(bool) EMRemIsLockOwner(PVM pVM)
{
#ifdef VBOX_WITH_REM
if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
return true; /* early init */
return PDMCritSectIsOwner(&pVM->em.s.CritSectREM);
#else
RT_NOREF(pVM);
return true;
#endif
}
/**
* Try to acquire the REM lock.
*
* @returns VBox status code
* @param pVM The cross context VM structure.
*/
VMM_INT_DECL(int) EMRemTryLock(PVM pVM)
{
#ifdef VBOX_WITH_REM
if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
return VINF_SUCCESS; /* early init */
return PDMCritSectTryEnter(&pVM->em.s.CritSectREM);
#else
RT_NOREF(pVM);
return VINF_SUCCESS;
#endif
}
/**
* @callback_method_impl{FNDISREADBYTES}
*/
static DECLCALLBACK(int) emReadBytes(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
{
PVMCPU pVCpu = (PVMCPU)pDis->pvUser;
#if defined(VBOX_WITH_RAW_MODE) && (defined(IN_RC) || defined(IN_RING3))
PVM pVM = pVCpu->CTX_SUFF(pVM);
#endif
RTUINTPTR uSrcAddr = pDis->uInstrAddr + offInstr;
int rc;
/*
* Figure how much we can or must read.
*/
size_t cbToRead = PAGE_SIZE - (uSrcAddr & PAGE_OFFSET_MASK);
if (cbToRead > cbMaxRead)
cbToRead = cbMaxRead;
else if (cbToRead < cbMinRead)
cbToRead = cbMinRead;
#if defined(VBOX_WITH_RAW_MODE) && (defined(IN_RC) || defined(IN_RING3))
/*
* We might be called upon to interpret an instruction in a patch.
*/
if (PATMIsPatchGCAddr(pVM, uSrcAddr))
{
# ifdef IN_RC
memcpy(&pDis->abInstr[offInstr], (void *)(uintptr_t)uSrcAddr, cbToRead);
# else
memcpy(&pDis->abInstr[offInstr], PATMR3GCPtrToHCPtr(pVM, uSrcAddr), cbToRead);
# endif
rc = VINF_SUCCESS;
}
else
#endif
{
# ifdef IN_RC
/*
* Try access it thru the shadow page tables first. Fall back on the
* slower PGM method if it fails because the TLB or page table was
* modified recently.
*/
rc = MMGCRamRead(pVCpu->pVMRC, &pDis->abInstr[offInstr], (void *)(uintptr_t)uSrcAddr, cbToRead);
if (rc == VERR_ACCESS_DENIED && cbToRead > cbMinRead)
{
cbToRead = cbMinRead;
rc = MMGCRamRead(pVCpu->pVMRC, &pDis->abInstr[offInstr], (void *)(uintptr_t)uSrcAddr, cbToRead);
}
if (rc == VERR_ACCESS_DENIED)
#endif
{
rc = PGMPhysSimpleReadGCPtr(pVCpu, &pDis->abInstr[offInstr], uSrcAddr, cbToRead);
if (RT_FAILURE(rc))
{
if (cbToRead > cbMinRead)
{
cbToRead = cbMinRead;
rc = PGMPhysSimpleReadGCPtr(pVCpu, &pDis->abInstr[offInstr], uSrcAddr, cbToRead);
}
if (RT_FAILURE(rc))
{
#ifndef IN_RC
/*
* If we fail to find the page via the guest's page tables
* we invalidate the page in the host TLB (pertaining to
* the guest in the NestedPaging case). See @bugref{6043}.
*/
if (rc == VERR_PAGE_TABLE_NOT_PRESENT || rc == VERR_PAGE_NOT_PRESENT)
{
HMInvalidatePage(pVCpu, uSrcAddr);
if (((uSrcAddr + cbToRead - 1) >> PAGE_SHIFT) != (uSrcAddr >> PAGE_SHIFT))
HMInvalidatePage(pVCpu, uSrcAddr + cbToRead - 1);
}
#endif
}
}
}
}
pDis->cbCachedInstr = offInstr + (uint8_t)cbToRead;
return rc;
}
/**
* Disassembles the current instruction.
*
* @returns VBox status code, see SELMToFlatEx and EMInterpretDisasOneEx for
* details.
*
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param pDis Where to return the parsed instruction info.
* @param pcbInstr Where to return the instruction size. (optional)
*/
VMM_INT_DECL(int) EMInterpretDisasCurrent(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, unsigned *pcbInstr)
{
PCPUMCTXCORE pCtxCore = CPUMCTX2CORE(CPUMQueryGuestCtxPtr(pVCpu));
RTGCPTR GCPtrInstr;
#if 0
int rc = SELMToFlatEx(pVCpu, DISSELREG_CS, pCtxCore, pCtxCore->rip, 0, &GCPtrInstr);
#else
/** @todo Get the CPU mode as well while we're at it! */
int rc = SELMValidateAndConvertCSAddr(pVCpu, pCtxCore->eflags, pCtxCore->ss.Sel, pCtxCore->cs.Sel, &pCtxCore->cs,
pCtxCore->rip, &GCPtrInstr);
#endif
if (RT_FAILURE(rc))
{
Log(("EMInterpretDisasOne: Failed to convert %RTsel:%RGv (cpl=%d) - rc=%Rrc !!\n",
pCtxCore->cs.Sel, (RTGCPTR)pCtxCore->rip, pCtxCore->ss.Sel & X86_SEL_RPL, rc));
return rc;
}
return EMInterpretDisasOneEx(pVM, pVCpu, (RTGCUINTPTR)GCPtrInstr, pCtxCore, pDis, pcbInstr);
}
/**
* Disassembles one instruction.
*
* This is used by internally by the interpreter and by trap/access handlers.
*
* @returns VBox status code.
*
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param GCPtrInstr The flat address of the instruction.
* @param pCtxCore The context core (used to determine the cpu mode).
* @param pDis Where to return the parsed instruction info.
* @param pcbInstr Where to return the instruction size. (optional)
*/
VMM_INT_DECL(int) EMInterpretDisasOneEx(PVM pVM, PVMCPU pVCpu, RTGCUINTPTR GCPtrInstr, PCCPUMCTXCORE pCtxCore,
PDISCPUSTATE pDis, unsigned *pcbInstr)
{
NOREF(pVM);
Assert(pCtxCore == CPUMGetGuestCtxCore(pVCpu)); NOREF(pCtxCore);
DISCPUMODE enmCpuMode = CPUMGetGuestDisMode(pVCpu);
/** @todo Deal with too long instruction (=> \#GP), opcode read errors (=>
* \#PF, \#GP, \#??), undefined opcodes (=> \#UD), and such. */
int rc = DISInstrWithReader(GCPtrInstr, enmCpuMode, emReadBytes, pVCpu, pDis, pcbInstr);
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
AssertMsg(rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("DISCoreOne failed to GCPtrInstr=%RGv rc=%Rrc\n", GCPtrInstr, rc));
return rc;
}
/**
* Interprets the current instruction.
*
* @returns VBox status code.
* @retval VINF_* Scheduling instructions.
* @retval VERR_EM_INTERPRETER Something we can't cope with.
* @retval VERR_* Fatal errors.
*
* @param pVCpu The cross context virtual CPU structure.
* @param pRegFrame The register frame.
* Updates the EIP if an instruction was executed successfully.
* @param pvFault The fault address (CR2).
*
* @remark Invalid opcode exceptions have a higher priority than GP (see Intel
* Architecture System Developers Manual, Vol 3, 5.5) so we don't need
* to worry about e.g. invalid modrm combinations (!)
*/
VMM_INT_DECL(VBOXSTRICTRC) EMInterpretInstruction(PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault)
{
Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
LogFlow(("EMInterpretInstruction %RGv fault %RGv\n", (RTGCPTR)pRegFrame->rip, pvFault));
NOREF(pvFault);
VBOXSTRICTRC rc = IEMExecOneBypassEx(pVCpu, pRegFrame, NULL);
if (RT_UNLIKELY( rc == VERR_IEM_ASPECT_NOT_IMPLEMENTED
|| rc == VERR_IEM_INSTR_NOT_IMPLEMENTED))
rc = VERR_EM_INTERPRETER;
if (rc != VINF_SUCCESS)
Log(("EMInterpretInstruction: returns %Rrc\n", VBOXSTRICTRC_VAL(rc)));
return rc;
}
/**
* Interprets the current instruction.
*
* @returns VBox status code.
* @retval VINF_* Scheduling instructions.
* @retval VERR_EM_INTERPRETER Something we can't cope with.
* @retval VERR_* Fatal errors.
*
* @param pVCpu The cross context virtual CPU structure of the calling EMT.
* @param pRegFrame The register frame.
* Updates the EIP if an instruction was executed successfully.
* @param pvFault The fault address (CR2).
* @param pcbWritten Size of the write (if applicable).
*
* @remark Invalid opcode exceptions have a higher priority than GP (see Intel
* Architecture System Developers Manual, Vol 3, 5.5) so we don't need
* to worry about e.g. invalid modrm combinations (!)
*/
VMM_INT_DECL(VBOXSTRICTRC) EMInterpretInstructionEx(PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbWritten)
{
LogFlow(("EMInterpretInstructionEx %RGv fault %RGv\n", (RTGCPTR)pRegFrame->rip, pvFault));
Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
NOREF(pvFault);
VBOXSTRICTRC rc = IEMExecOneBypassEx(pVCpu, pRegFrame, pcbWritten);
if (RT_UNLIKELY( rc == VERR_IEM_ASPECT_NOT_IMPLEMENTED
|| rc == VERR_IEM_INSTR_NOT_IMPLEMENTED))
rc = VERR_EM_INTERPRETER;
if (rc != VINF_SUCCESS)
Log(("EMInterpretInstructionEx: returns %Rrc\n", VBOXSTRICTRC_VAL(rc)));
return rc;
}
/**
* Interprets the current instruction using the supplied DISCPUSTATE structure.
*
* IP/EIP/RIP *IS* updated!
*
* @returns VBox strict status code.
* @retval VINF_* Scheduling instructions. When these are returned, it
* starts to get a bit tricky to know whether code was
* executed or not... We'll address this when it becomes a problem.
* @retval VERR_EM_INTERPRETER Something we can't cope with.
* @retval VERR_* Fatal errors.
*
* @param pVCpu The cross context virtual CPU structure of the calling EMT.
* @param pDis The disassembler cpu state for the instruction to be
* interpreted.
* @param pRegFrame The register frame. IP/EIP/RIP *IS* changed!
* @param pvFault The fault address (CR2).
* @param enmCodeType Code type (user/supervisor)
*
* @remark Invalid opcode exceptions have a higher priority than GP (see Intel
* Architecture System Developers Manual, Vol 3, 5.5) so we don't need
* to worry about e.g. invalid modrm combinations (!)
*
* @todo At this time we do NOT check if the instruction overwrites vital information.
* Make sure this can't happen!! (will add some assertions/checks later)
*/
VMM_INT_DECL(VBOXSTRICTRC) EMInterpretInstructionDisasState(PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame,
RTGCPTR pvFault, EMCODETYPE enmCodeType)
{
LogFlow(("EMInterpretInstructionDisasState %RGv fault %RGv\n", (RTGCPTR)pRegFrame->rip, pvFault));
Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
NOREF(pDis); NOREF(pvFault); NOREF(enmCodeType);
VBOXSTRICTRC rc = IEMExecOneBypassWithPrefetchedByPC(pVCpu, pRegFrame, pRegFrame->rip, pDis->abInstr, pDis->cbCachedInstr);
if (RT_UNLIKELY( rc == VERR_IEM_ASPECT_NOT_IMPLEMENTED
|| rc == VERR_IEM_INSTR_NOT_IMPLEMENTED))
rc = VERR_EM_INTERPRETER;
if (rc != VINF_SUCCESS)
Log(("EMInterpretInstructionDisasState: returns %Rrc\n", VBOXSTRICTRC_VAL(rc)));
return rc;
}
#ifdef IN_RC
DECLINLINE(int) emRCStackRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCPTR GCPtrSrc, uint32_t cb)
{
int rc = MMGCRamRead(pVM, pvDst, (void *)(uintptr_t)GCPtrSrc, cb);
if (RT_LIKELY(rc != VERR_ACCESS_DENIED))
return rc;
return PGMPhysInterpretedReadNoHandlers(pVCpu, pCtxCore, pvDst, GCPtrSrc, cb, /*fMayTrap*/ false);
}
/**
* Interpret IRET (currently only to V86 code) - PATM only.
*
* @returns VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param pRegFrame The register frame.
*
*/
VMM_INT_DECL(int) EMInterpretIretV86ForPatm(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
{
RTGCUINTPTR pIretStack = (RTGCUINTPTR)pRegFrame->esp;
RTGCUINTPTR eip, cs, esp, ss, eflags, ds, es, fs, gs, uMask;
int rc;
Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
Assert(!CPUMIsGuestIn64BitCode(pVCpu));
/** @todo Rainy day: Test what happens when VERR_EM_INTERPRETER is returned by
* this function. Fear that it may guru on us, thus not converted to
* IEM. */
rc = emRCStackRead(pVM, pVCpu, pRegFrame, &eip, (RTGCPTR)pIretStack , 4);
rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &cs, (RTGCPTR)(pIretStack + 4), 4);
rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &eflags, (RTGCPTR)(pIretStack + 8), 4);
AssertRCReturn(rc, VERR_EM_INTERPRETER);
AssertReturn(eflags & X86_EFL_VM, VERR_EM_INTERPRETER);
rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &esp, (RTGCPTR)(pIretStack + 12), 4);
rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &ss, (RTGCPTR)(pIretStack + 16), 4);
rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &es, (RTGCPTR)(pIretStack + 20), 4);
rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &ds, (RTGCPTR)(pIretStack + 24), 4);
rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &fs, (RTGCPTR)(pIretStack + 28), 4);
rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &gs, (RTGCPTR)(pIretStack + 32), 4);
AssertRCReturn(rc, VERR_EM_INTERPRETER);
pRegFrame->eip = eip & 0xffff;
pRegFrame->cs.Sel = cs;
/* Mask away all reserved bits */
uMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT | X86_EFL_RF | X86_EFL_VM | X86_EFL_AC | X86_EFL_VIF | X86_EFL_VIP | X86_EFL_ID;
eflags &= uMask;
CPUMRawSetEFlags(pVCpu, eflags);
Assert((pRegFrame->eflags.u32 & (X86_EFL_IF|X86_EFL_IOPL)) == X86_EFL_IF);
pRegFrame->esp = esp;
pRegFrame->ss.Sel = ss;
pRegFrame->ds.Sel = ds;
pRegFrame->es.Sel = es;
pRegFrame->fs.Sel = fs;
pRegFrame->gs.Sel = gs;
return VINF_SUCCESS;
}
#endif /* IN_RC */
/*
*
* Old interpreter primitives used by HM, move/eliminate later.
* Old interpreter primitives used by HM, move/eliminate later.
* Old interpreter primitives used by HM, move/eliminate later.
* Old interpreter primitives used by HM, move/eliminate later.
* Old interpreter primitives used by HM, move/eliminate later.
*
*/
/**
* Interpret RDPMC.
*
* @returns VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param pRegFrame The register frame.
*
*/
VMM_INT_DECL(int) EMInterpretRdpmc(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
{
Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
uint32_t uCR4 = CPUMGetGuestCR4(pVCpu);
/* If X86_CR4_PCE is not set, then CPL must be zero. */
if ( !(uCR4 & X86_CR4_PCE)
&& CPUMGetGuestCPL(pVCpu) != 0)
{
Assert(CPUMGetGuestCR0(pVCpu) & X86_CR0_PE);
return VERR_EM_INTERPRETER; /* genuine #GP */
}
/* Just return zero here; rather tricky to properly emulate this, especially as the specs are a mess. */
pRegFrame->rax = 0;
pRegFrame->rdx = 0;
/** @todo We should trigger a \#GP here if the CPU doesn't support the index in
* ecx but see @bugref{3472}! */
NOREF(pVM);
return VINF_SUCCESS;
}
/**
* MWAIT Emulation.
*/
VMM_INT_DECL(VBOXSTRICTRC) EMInterpretMWait(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
{
Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
uint32_t u32Dummy, u32ExtFeatures, cpl, u32MWaitFeatures;
NOREF(pVM);
/* Get the current privilege level. */
cpl = CPUMGetGuestCPL(pVCpu);
if (cpl != 0)
return VERR_EM_INTERPRETER; /* supervisor only */
CPUMGetGuestCpuId(pVCpu, 1, 0, &u32Dummy, &u32Dummy, &u32ExtFeatures, &u32Dummy);
if (!(u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR))
return VERR_EM_INTERPRETER; /* not supported */
/*
* CPUID.05H.ECX[0] defines support for power management extensions (eax)
* CPUID.05H.ECX[1] defines support for interrupts as break events for mwait even when IF=0
*/
CPUMGetGuestCpuId(pVCpu, 5, 0, &u32Dummy, &u32Dummy, &u32MWaitFeatures, &u32Dummy);
if (pRegFrame->ecx > 1)
{
Log(("EMInterpretMWait: unexpected ecx value %x -> recompiler\n", pRegFrame->ecx));
return VERR_EM_INTERPRETER; /* illegal value. */
}
if (pRegFrame->ecx && !(u32MWaitFeatures & X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
{
Log(("EMInterpretMWait: unsupported X86_CPUID_MWAIT_ECX_BREAKIRQIF0 -> recompiler\n"));
return VERR_EM_INTERPRETER; /* illegal value. */
}
return EMMonitorWaitPerform(pVCpu, pRegFrame->rax, pRegFrame->rcx);
}
/**
* MONITOR Emulation.
*/
VMM_INT_DECL(int) EMInterpretMonitor(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
{
uint32_t u32Dummy, u32ExtFeatures, cpl;
Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
NOREF(pVM);
if (pRegFrame->ecx != 0)
{
Log(("emInterpretMonitor: unexpected ecx=%x -> recompiler!!\n", pRegFrame->ecx));
return VERR_EM_INTERPRETER; /* illegal value. */
}
/* Get the current privilege level. */
cpl = CPUMGetGuestCPL(pVCpu);
if (cpl != 0)
return VERR_EM_INTERPRETER; /* supervisor only */
CPUMGetGuestCpuId(pVCpu, 1, 0, &u32Dummy, &u32Dummy, &u32ExtFeatures, &u32Dummy);
if (!(u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR))
return VERR_EM_INTERPRETER; /* not supported */
EMMonitorWaitPrepare(pVCpu, pRegFrame->rax, pRegFrame->rcx, pRegFrame->rdx, NIL_RTGCPHYS);
return VINF_SUCCESS;
}
/* VT-x only: */
/**
* Interpret DRx write.
*
* @returns VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param pRegFrame The register frame.
* @param DestRegDrx DRx register index (USE_REG_DR*)
* @param SrcRegGen General purpose register index (USE_REG_E**))
*
*/
VMM_INT_DECL(int) EMInterpretDRxWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegDrx, uint32_t SrcRegGen)
{
Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
uint64_t uNewDrX;
int rc;
NOREF(pVM);
if (CPUMIsGuestIn64BitCode(pVCpu))
rc = DISFetchReg64(pRegFrame, SrcRegGen, &uNewDrX);
else
{
uint32_t val32;
rc = DISFetchReg32(pRegFrame, SrcRegGen, &val32);
uNewDrX = val32;
}
if (RT_SUCCESS(rc))
{
if (DestRegDrx == 6)
{
uNewDrX |= X86_DR6_RA1_MASK;
uNewDrX &= ~X86_DR6_RAZ_MASK;
}
else if (DestRegDrx == 7)
{
uNewDrX |= X86_DR7_RA1_MASK;
uNewDrX &= ~X86_DR7_RAZ_MASK;
}
/** @todo we don't fail if illegal bits are set/cleared for e.g. dr7 */
rc = CPUMSetGuestDRx(pVCpu, DestRegDrx, uNewDrX);
if (RT_SUCCESS(rc))
return rc;
AssertMsgFailed(("CPUMSetGuestDRx %d failed\n", DestRegDrx));
}
return VERR_EM_INTERPRETER;
}
/**
* Interpret DRx read.
*
* @returns VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param pRegFrame The register frame.
* @param DestRegGen General purpose register index (USE_REG_E**))
* @param SrcRegDrx DRx register index (USE_REG_DR*)
*/
VMM_INT_DECL(int) EMInterpretDRxRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegDrx)
{
uint64_t val64;
Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
NOREF(pVM);
int rc = CPUMGetGuestDRx(pVCpu, SrcRegDrx, &val64);
AssertMsgRCReturn(rc, ("CPUMGetGuestDRx %d failed\n", SrcRegDrx), VERR_EM_INTERPRETER);
if (CPUMIsGuestIn64BitCode(pVCpu))
rc = DISWriteReg64(pRegFrame, DestRegGen, val64);
else
rc = DISWriteReg32(pRegFrame, DestRegGen, (uint32_t)val64);
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
return VERR_EM_INTERPRETER;
}
|