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
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
| diff --git a/README.md b/README.md
index c7ec76b..93c0a3b 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ Introduction
-------------
SimpleSvm is a minimalistic educational hypervisor for Windows on AMD processors.
-It aims to provide small and explanatory code to use Secure Virtual Machine (SVM),
+It aims to provide small and explanational code to use Secure Virtual Machine (SVM),
the AMD version of Intel VT-x, with Nested Page Tables (NPT) from a windows driver.
SimpleSvm is inspired by SimpleVisor, an Intel x64/EM64T VT-x specific hypervisor
@@ -14,8 +14,8 @@ for Windows, written by Alex Ionescu (@aionescu).
Supported Platforms
----------------------
-- Windows 10 and later (x64)
-- AMD processors with SVM and NPT support
+- Windows 10 x64 and Windows 7 x64
+- AMD Processors with SVM and NPT support
Resources
diff --git a/SimpleSvm/SimpleSvm.cpp b/SimpleSvm/SimpleSvm.cpp
index 77c32dd..98c250c 100644
--- a/SimpleSvm/SimpleSvm.cpp
+++ b/SimpleSvm/SimpleSvm.cpp
@@ -7,12 +7,82 @@
@copyright Copyright (c) 2017-2020, Satoshi Tanda. All rights reserved.
*/
+
+/*!
+
+* Additional comments and modifications related to HWID spoofing are not affiliated with the author.
+
+*/
+
#define POOL_NX_OPTIN 1
+
+#define KUSER_SPOOF
+
#include "SimpleSvm.hpp"
#include <intrin.h>
#include <ntifs.h>
+#include <ntddk.h>
#include <stdarg.h>
+#include "ia32.h"
+
+#ifdef KUSER_SPOOF
+
+#include "./amd.h"
+#include "./pte.h"
+#include "./global.h"
+#include "./runtime.h"
+#include "./other.h"
+#include "./utils.h"
+#include "./utils.cpp"
+
+EXTERN_C
+NTSTATUS
+NTAPI
+PsAcquireProcessExitSynchronization(
+ _In_ PEPROCESS Process
+);
+
+EXTERN_C
+VOID
+NTAPI
+PsReleaseProcessExitSynchronization(
+ _In_ PEPROCESS Process
+);
+
+#endif
+
+#define UMIP (1UL << 11)
+#define X86_FLAGS_TF (1U<<8)
+#define X86_FLAGS_IF (1U<<9)
+#define SingleStep (1U<<14)
+#define BranchSingleStep (1U<<1)
+
+UINT64 OrigLSTAR;
+void(*sysPtr);
+UINT64 Decoy;
+UINT64 Decoy2;
+UINT64 TempPTR;
+UINT64 TargetCR3;
+UINT64 TargetSysHandler;
+BOOLEAN UMIPSystem;
+UINT64 RegionBase;
+UINT64 RegionEnd;
+
+#ifdef KUSER_SPOOF
+
+PEPROCESS TargetProcess = nullptr;
+HANDLE TargetProcessId;
+PVOID NewKuserSharedData;
+UINT64 OriginalKuserPFN;
+BOOLEAN NotifyRoutineActive;
+BOOLEAN StopCounterThread = FALSE;
+BOOLEAN CounterInit = 0x0;
+HANDLE CounterThreadHandle = NULL;
+#define KUSER_SHARED_DATA_USERMODE 0x7FFE0000
+#define KUSER_SHARED_DATA_KERNELMODE 0xFFFFF78000000000
+
+#endif
EXTERN_C DRIVER_INITIALIZE DriverEntry;
static DRIVER_UNLOAD SvDriverUnload;
@@ -63,7 +133,7 @@ typedef struct _PML4_ENTRY_2MB
} Fields;
};
} PML4_ENTRY_2MB, *PPML4_ENTRY_2MB,
- PDPT_ENTRY_2MB, *PPDPT_ENTRY_2MB;
+ PDPT_ENTRY_2MB, * PPDPT_ENTRY_2MB;
static_assert(sizeof(PML4_ENTRY_2MB) == 8,
"PML4_ENTRY_1GB Size Mismatch");
@@ -171,7 +241,7 @@ typedef struct _PML4E_TREE
{
DECLSPEC_ALIGN(PAGE_SIZE) PDPT_ENTRY_2MB PdptEntries[512];
DECLSPEC_ALIGN(PAGE_SIZE) PD_ENTRY_2MB PdEntries[512][512];
-} PML4E_TREE, *PPML4E_TREE;
+} PML4E_TREE, * PPML4E_TREE;
typedef struct _SHARED_VIRTUAL_PROCESSOR_DATA
{
@@ -213,23 +283,23 @@ static_assert(sizeof(VIRTUAL_PROCESSOR_DATA) == KERNEL_STACK_SIZE + PAGE_SIZE *
typedef struct _GUEST_REGISTERS
{
- UINT64 R15;
- UINT64 R14;
- UINT64 R13;
- UINT64 R12;
- UINT64 R11;
- UINT64 R10;
- UINT64 R9;
- UINT64 R8;
- UINT64 Rdi;
- UINT64 Rsi;
- UINT64 Rbp;
- UINT64 Rsp;
- UINT64 Rbx;
- UINT64 Rdx;
- UINT64 Rcx;
UINT64 Rax;
-} GUEST_REGISTERS, *PGUEST_REGISTERS;
+ UINT64 Rcx;
+ UINT64 Rdx;
+ UINT64 Rbx;
+ UINT64 Rsp;
+ UINT64 Rbp;
+ UINT64 Rsi;
+ UINT64 Rdi;
+ UINT64 R8;
+ UINT64 R9;
+ UINT64 R10;
+ UINT64 R11;
+ UINT64 R12;
+ UINT64 R13;
+ UINT64 R14;
+ UINT64 R15;
+} GUEST_REGISTERS, * PGUEST_REGISTERS;
typedef struct _GUEST_CONTEXT
{
@@ -238,13 +308,30 @@ typedef struct _GUEST_CONTEXT
} GUEST_CONTEXT, *PGUEST_CONTEXT;
+typedef struct _CRDecodeAssist
+{
+ union
+ {
+ UINT64 AsUInt64;
+ struct
+ {
+ UINT64 GPR : 4; // [0:3]
+ UINT64 Zero : 59; // [4:62]
+ UINT64 Indicator : 1; // [63]
+ } Fields;
+ };
+} CRDecodeAssist, *PCRDecodeAssist;
+
+
//
// x86-64 defined constants.
//
-#define IA32_MSR_PAT 0x00000277
-#define IA32_MSR_EFER 0xc0000080
+#define IA32_MSR_PAT 0x00000277
+#define IA32_MSR_EFER 0xC0000080
+#define IA32_MSR_LSTAR 0xC0000082
+#define IA32_MSR_DEBUGCTL 0x000001D9
-#define EFER_SVME (1UL << 12)
+#define EFER_SVME (1UL << 12)
#define RPL_MASK 3
#define DPL_SYSTEM 0
@@ -299,8 +386,6 @@ static PVOID g_PowerCallbackRegistration;
@param[in] Format - The format string to print.
*/
-#pragma prefast(push)
-#pragma prefast(disable : 26826, "C-style variable arguments needed for DbgPrint.")
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
static
@@ -320,7 +405,6 @@ SvDebugPrint (
argList);
va_end(argList);
}
-#pragma prefast(pop)
/*!
@brief Allocates page aligned, zero filled physical memory.
@@ -356,7 +440,8 @@ SvAllocatePageAlingedPhysicalMemory (
//
NT_ASSERT(NumberOfBytes >= PAGE_SIZE);
- memory = ExAllocatePool2(POOL_FLAG_NON_PAGED, NumberOfBytes, 'MVSS');
+#pragma prefast(disable : 28118 __WARNING_ERROR, "FP due to POOL_NX_OPTIN.")
+ memory = ExAllocatePoolWithTag(NonPagedPool, NumberOfBytes, 'MVSS');
if (memory != nullptr)
{
NT_ASSERT(PAGE_ALIGN(memory) == memory);
@@ -412,12 +497,13 @@ SvAllocateContiguousMemory (
boundary.QuadPart = lowest.QuadPart = 0;
highest.QuadPart = -1;
- memory = MmAllocateContiguousNodeMemory(NumberOfBytes,
- lowest,
- highest,
- boundary,
- PAGE_READWRITE,
- MM_ANY_NODE_OK);
+#pragma prefast(disable : 30030, "No alternative API on Windows 7.")
+ memory = MmAllocateContiguousMemorySpecifyCacheNode(NumberOfBytes,
+ lowest,
+ highest,
+ boundary,
+ MmCached,
+ MM_ANY_NODE_OK);
if (memory != nullptr)
{
RtlZeroMemory(memory, NumberOfBytes);
@@ -444,146 +530,1005 @@ SvFreeContiguousMemory (
/*!
@brief Injects #GP with 0 of error code.
- @param[in,out] VpData - Per processor data.
- */
-_IRQL_requires_same_
-static
-VOID
-SvInjectGeneralProtectionException (
- _Inout_ PVIRTUAL_PROCESSOR_DATA VpData
- )
-{
- EVENTINJ event;
+ @param[in,out] VpData - Per processor data.
+ */
+_IRQL_requires_same_
+static
+VOID
+SvInjectGeneralProtectionException (
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData
+ )
+{
+ EVENTINJ event;
+
+ //
+ // Inject #GP(vector = 13, type = 3 = exception) with a valid error code.
+ // An error code are always zero. See "#GP-General-Protection Exception
+ // (Vector 13)" for details about the error code.
+ //
+ event.AsUInt64 = 0;
+ event.Fields.Vector = 13;
+ event.Fields.Type = 3;
+ event.Fields.ErrorCodeValid = 1;
+ event.Fields.Valid = 1;
+ VpData->GuestVmcb.ControlArea.EventInj = event.AsUInt64;
+}
+
+_IRQL_requires_same_
+static
+VOID
+SvInjectDbException(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData
+)
+{
+ EVENTINJ event;
+
+ // Inject #DB exception.
+
+ event.AsUInt64 = 0;
+ event.Fields.Vector = 1;
+ event.Fields.Type = 3;
+ event.Fields.ErrorCodeValid = 0;
+ event.Fields.Valid = 1;
+ VpData->GuestVmcb.ControlArea.EventInj = event.AsUInt64;
+}
+
+_IRQL_requires_same_
+static
+VOID
+SvInjectPFException(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData
+)
+{
+ EVENTINJ event;
+
+ // Inject #PF exception.
+
+ event.AsUInt64 = 0;
+ event.Fields.Vector = 14;
+ event.Fields.Type = 3;
+ event.Fields.ErrorCodeValid = 1;
+ event.Fields.Valid = 1;
+ event.Fields.ErrorCode = VpData->GuestVmcb.ControlArea.ExitInfo1;
+ VpData->GuestVmcb.ControlArea.EventInj = event.AsUInt64;
+}
+
+_IRQL_requires_same_
+static
+VOID
+SvInjectACException(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData
+)
+{
+ EVENTINJ event;
+
+ // Inject #AC exception.
+
+ event.AsUInt64 = 0;
+ event.Fields.Vector = 17;
+ event.Fields.Type = 3;
+ event.Fields.ErrorCodeValid = 1;
+ event.Fields.Valid = 1;
+ VpData->GuestVmcb.ControlArea.EventInj = event.AsUInt64;
+}
+
+_IRQL_requires_same_
+static
+VOID
+SvInjectSsException(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData
+)
+{
+ EVENTINJ event;
+
+ // Inject #SS exception.
+
+ event.AsUInt64 = 0;
+ event.Fields.Vector = 12;
+ event.Fields.Type = 3;
+ event.Fields.ErrorCodeValid = 1;
+ event.Fields.Valid = 1;
+ event.Fields.ErrorCode = VpData->GuestVmcb.ControlArea.ExitInfo1;
+ VpData->GuestVmcb.ControlArea.EventInj = event.AsUInt64;
+}
+
+static
+VOID
+SyscallHandler() //Later overwritten with actual code, creating space here for x64 inline assembly. Decoy code is never executed.
+{
+ Decoy = *(UINT64*)(0x7FFE0000);
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0002) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0013) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE001f) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE002a);
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0028) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE003c) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0030) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0042);
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0049) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0055) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE005f) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0062);
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0070) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0072) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE007a) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0280);
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0008) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0490) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0192) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0133);
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0175) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0144) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0318) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE01ac);
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0428) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0330) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE014c) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0120);
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0184) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0050) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0451) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0163);
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0227) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0342) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0181) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE03a0) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE01c8) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0120) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0321) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0204) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0451) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0223) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0041) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE043a) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE023c) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0111) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0238) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0040) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE031a) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0020) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE01ad) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0222) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0168) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE033f) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0078) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0312) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0128) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE01b3) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0098) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0300) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE01ad) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0426) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0237) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE004a) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE012c) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0327) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0201) - Decoy2;
+ Decoy2 = Decoy;
+ Decoy = *(UINT64*)(0x7FFE0452) - Decoy2;
+ Decoy2 = Decoy;
+}
+
+#ifdef KUSER_SPOOF
+
+// Single process tracking
+static HANDLE TrackedProcessId = nullptr;
+
+VOID ProcessNotifyRestoreFields(
+ IN HANDLE ParentId,
+ IN HANDLE ProcessId,
+ IN BOOLEAN Create
+) {
+ UNREFERENCED_PARAMETER(ParentId);
+
+ // Create == TRUE: Process creation
+ // Create == FALSE: Process termination
+ if (Create == FALSE && ProcessId == TrackedProcessId) {
+
+ if (CounterThreadHandle)
+ {
+ PETHREAD CounterThread;
+ ObReferenceObjectByHandle(CounterThreadHandle, NULL, *PsThreadType, KernelMode, (PVOID*)&CounterThread, NULL);
+ StopCounterThread = TRUE;
+ KeWaitForSingleObject(CounterThread, Executive, KernelMode, FALSE, NULL);
+ ObDereferenceObject(CounterThread);
+ ZwClose(CounterThreadHandle);
+ StopCounterThread = FALSE;
+ CounterThreadHandle = NULL;
+ CounterInit = 0x0;
+
+ auto hostcr3 = __readcr3();
+ __writecr3(TargetCR3);
+
+ auto TargetProcessKuserPte = Utils::GetPte((void*)0x7FFE0000, TargetCR3);
+
+ if (TargetProcessKuserPte)
+ {
+ TargetProcessKuserPte->PageFrameNumber = OriginalKuserPFN;
+
+ __invlpg((void*)0x7FFE0000);
+ }
+
+ __writecr3(hostcr3);
+
+ PsReleaseProcessExitSynchronization(TargetProcess);
+ ObDereferenceObject(TargetProcess);
+
+ if (NewKuserSharedData)
+ {
+ SvFreeContiguousMemory(NewKuserSharedData);
+ }
+
+ TargetProcessId = 0;
+ TrackedProcessId = nullptr;
+ TargetProcess = nullptr;
+ }
+
+ }
+}
+
+VOID CounterUpdater(PVOID Context)
+{
+ UNREFERENCED_PARAMETER(Context);
+
+ LARGE_INTEGER TimeToWait = { 0 };
+ TimeToWait.QuadPart = -10000LL; // relative 1ms
+
+ while (StopCounterThread == FALSE)
+ {
+ if (CounterInit)
+ {
+ if (!TrackedProcessId) {
+
+ NewKuserSharedData = SvAllocateContiguousMemory(PAGE_SIZE);
+
+ if (NewKuserSharedData)
+ {
+ MM_COPY_ADDRESS KUSER_SHARED_DATA;
+
+ SIZE_T size;
+
+ KUSER_SHARED_DATA.VirtualAddress = (PVOID)KUSER_SHARED_DATA_USERMODE;
+
+ MmCopyMemory(NewKuserSharedData, KUSER_SHARED_DATA, PAGE_SIZE, MM_COPY_MEMORY_VIRTUAL, &size);
+
+ UINT64 NewKusdAddress = (UINT64)NewKuserSharedData;
+
+ *(UINT32*)(NewKusdAddress + 0x2d6) = 0x00010034;
+ *(UINT32*)(NewKusdAddress + 0x2e8) = 0x00BF9C8F;
+ *(UINT32*)(NewKusdAddress + 0x3c0) = 0x00000010;
+ *(UINT32*)(NewKusdAddress + 0x288) = 0x01010101;
+ *(UINT32*)(NewKusdAddress + 0x268) = 0x00090001;
+ *(UINT32*)(NewKusdAddress + 0x2f4) = 0x0;
+ *(UINT32*)(NewKusdAddress + 0x264) = 0x1;
+ *(UINT32*)(NewKusdAddress + 0x2d0) = 0x00000310;
+ *(UINT32*)(NewKusdAddress + 0x260) = 0x00006658;
+ *(UINT32*)(NewKusdAddress + 0x26c) = 0xA;
+ *(UINT32*)(NewKusdAddress + 0x270) = 0x0;
+ }
+
+ TrackedProcessId = TargetProcessId;
+
+ auto hostcr3 = __readcr3();
+
+ UINT64 NewPFN = Utils::GetPte((void*)NewKuserSharedData, hostcr3)->PageFrameNumber;
+
+ ULONG_PTR GuestCr3 = TargetCR3;
+
+ __writecr3(GuestCr3);
+
+ auto TargetProcessKuserPte = Utils::GetPte((void*)0x7FFE0000, GuestCr3);
+
+ if (TargetProcessKuserPte) {
+
+ OriginalKuserPFN = TargetProcessKuserPte->PageFrameNumber;
+
+ TargetProcessKuserPte->PageFrameNumber = NewPFN;
+
+ __invlpg((void*)0x7FFE0000);
+ }
+
+ if (!NotifyRoutineActive) {
+ NTSTATUS status = PsSetCreateProcessNotifyRoutine(ProcessNotifyRestoreFields, FALSE);
+ if (NT_SUCCESS(status)) {
+ NotifyRoutineActive = TRUE;
+ }
+ }
+
+ __writecr3(hostcr3);
+
+ }
+
+ KeDelayExecutionThread(KernelMode, FALSE, &TimeToWait);
+
+ if (NewKuserSharedData)
+ {
+ CONST PKUSER_SHARED_DATA TargetSpoofedKuserSharedData = (PKUSER_SHARED_DATA)NewKuserSharedData;
+ PKUSER_SHARED_DATA KernelKuserSharedData = (PKUSER_SHARED_DATA)(KUSER_SHARED_DATA_KERNELMODE);
+
+ *(ULONG64*)&TargetSpoofedKuserSharedData->InterruptTime = *(ULONG64*)&KernelKuserSharedData->InterruptTime.LowPart;
+ TargetSpoofedKuserSharedData->InterruptTime.High2Time = TargetSpoofedKuserSharedData->InterruptTime.High1Time;
+
+ *(ULONG64*)&TargetSpoofedKuserSharedData->SystemTime = *(ULONG64*)&KernelKuserSharedData->SystemTime.LowPart;
+ TargetSpoofedKuserSharedData->SystemTime.High2Time = TargetSpoofedKuserSharedData->SystemTime.High1Time;
+
+ TargetSpoofedKuserSharedData->LastSystemRITEventTickCount = KernelKuserSharedData->LastSystemRITEventTickCount;
+
+ *(ULONG64*)&TargetSpoofedKuserSharedData->TickCount = *(ULONG64*)&KernelKuserSharedData->TickCount.LowPart;
+ TargetSpoofedKuserSharedData->TickCount.High2Time = TargetSpoofedKuserSharedData->TickCount.High1Time;
+
+ TargetSpoofedKuserSharedData->TimeUpdateLock = KernelKuserSharedData->TimeUpdateLock;
+
+ TargetSpoofedKuserSharedData->BaselineSystemTimeQpc = KernelKuserSharedData->BaselineSystemTimeQpc;
+ TargetSpoofedKuserSharedData->BaselineInterruptTimeQpc = TargetSpoofedKuserSharedData->BaselineSystemTimeQpc;
+ }
+
+ }
+
+ CounterInit = 0x1;
+ }
+
+ PsTerminateSystemThread(STATUS_SUCCESS);
+}
+
+#endif
+
+/*!
+ @brief Handles #VMEXIT due to execution of the CPUID instructions.
+
+ @param[in,out] VpData - Per processor data.
+ @param[in,out] GuestContext - Guest's GPRs.
+ */
+_IRQL_requires_same_
+static
+VOID
+SvHandleCpuid (
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData,
+ _Inout_ PGUEST_CONTEXT GuestContext
+ )
+{
+ int registers[4]; // EAX, EBX, ECX, and EDX
+ int leaf, subLeaf;
+ SEGMENT_ATTRIBUTE attribute;
+
+ //
+ // Execute CPUID as requested.
+ //
+
+ leaf = static_cast<int>(GuestContext->VpRegs->Rax);
+ subLeaf = static_cast<int>(GuestContext->VpRegs->Rcx);
+
+ if (VpData->GuestVmcb.StateSaveArea.Cpl == 0x3)
+ {
+ if (GuestContext->VpRegs->Rax == 0x69696969)
+ {
+ if (!TargetCR3 || !TrackedProcessId)
+ {
+ TargetCR3 = VpData->GuestVmcb.StateSaveArea.Cr3;
+ goto Exit;
+ }
+ }
+
+ if (VpData->GuestVmcb.StateSaveArea.Cr3 == TargetCR3)
+ {
+ if (GuestContext->VpRegs->Rax == 0x693369) //Limit cpuid spoofing to target region
+ {
+ RegionBase = GuestContext->VpRegs->Rcx;
+ RegionEnd = GuestContext->VpRegs->Rdx;
+ goto Exit;
+ }
+
+ if (leaf == 0x1 || leaf == 0x80000002 || leaf == 0x80000003 || leaf == 0x80000004)
+ {
+ if (RegionEnd && VpData->GuestVmcb.StateSaveArea.Rip >= RegionBase && VpData->GuestVmcb.StateSaveArea.Rip < RegionEnd)
+ {
+ GuestContext->VpRegs->Rax = 0x69696969;
+ GuestContext->VpRegs->Rbx = GuestContext->VpRegs->Rax;
+ GuestContext->VpRegs->Rcx = GuestContext->VpRegs->Rax;
+ GuestContext->VpRegs->Rdx = GuestContext->VpRegs->Rax;
+
+ goto Exit;
+ }
+
+ else
+ {
+ goto CPUIDEX;
+ }
+ }
+
+ if (GuestContext->VpRegs->Rax == 0x336933) //guest RCX should be == syscall handler address
+ {
+ TargetSysHandler = GuestContext->VpRegs->Rcx;
+ *(UINT64*)(TempPTR + 0x50) = TargetSysHandler; //SYSCALL HOOK
+ *(UINT64*)(TempPTR + 0x48) = TargetCR3;
+ goto Exit;
+ }
+
+#ifdef KUSER_SPOOF
+
+ if (GuestContext->VpRegs->Rax == 0x1337)
+ {
+ TargetProcessId = reinterpret_cast<HANDLE>(GuestContext->VpRegs->Rdx);
+
+ auto hostcr3 = __readcr3();
+
+ __writecr3(VpData->GuestVmcb.StateSaveArea.Cr3);
+
+ NTSTATUS status = PsLookupProcessByProcessId(TargetProcessId, &TargetProcess);
+
+ if (NT_SUCCESS(status)) {
+
+ status = PsAcquireProcessExitSynchronization(TargetProcess);
+
+ if (!NT_SUCCESS(status))
+ {
+ SvDebugPrint("Failed to AcquireProcessExitSynchronization, can not spoof KUSER only for the game process.");
+ }
+
+ if (NT_SUCCESS(PsCreateSystemThread(&CounterThreadHandle, 0, 0, 0, 0, CounterUpdater, NULL)) == FALSE)
+ {
+ SvDebugPrint("Failed to create CounterUpdater thread.");
+ }
+
+ }
+
+ __writecr3(hostcr3);
+
+ goto Exit;
+
+ }
+#endif
+ }
+
+ }
+
+ CPUIDEX:
+
+ __cpuidex(registers, leaf, subLeaf);
+
+ switch (leaf)
+ {
+ case CPUID_PROCESSOR_AND_PROCESSOR_FEATURE_IDENTIFIERS:
+ //
+ // Indicate presence of a hypervisor by setting the bit that are
+ // reserved for use by hypervisor to indicate guest status. See "CPUID
+ // Fn0000_0001_ECX Feature Identifiers".
+ //
+ registers[2] |= CPUID_FN0000_0001_ECX_HYPERVISOR_PRESENT;
+ break;
+ case CPUID_HV_VENDOR_AND_MAX_FUNCTIONS:
+ //
+ // Return a maximum supported hypervisor CPUID leaf range and a vendor
+ // ID signature as required by the spec.
+ //
+ registers[0] = CPUID_HV_MAX;
+ registers[1] = 'pmiS'; // "SimpleSvm "
+ registers[2] = 'vSel';
+ registers[3] = ' m';
+ break;
+ case CPUID_HV_INTERFACE:
+ //
+ // Return non Hv#1 value. This indicate that the SimpleSvm does NOT
+ // conform to the Microsoft hypervisor interface.
+ //
+ registers[0] = '0#vH'; // Hv#0
+ registers[1] = registers[2] = registers[3] = 0;
+ break;
+ case CPUID_UNLOAD_SIMPLE_SVM:
+ if (subLeaf == CPUID_UNLOAD_SIMPLE_SVM)
+ {
+ //
+ // Unload itself if the request is from the kernel mode.
+ //
+ attribute.AsUInt16 = VpData->GuestVmcb.StateSaveArea.SsAttrib;
+ if (attribute.Fields.Dpl == DPL_SYSTEM)
+ {
+ GuestContext->ExitVm = TRUE;
+ }
+ }
+ break;
+ default:
+ break;
+ }
+
+ //
+ // Update guest's GPRs with results.
+ //
+
+ GuestContext->VpRegs->Rax = registers[0];
+ GuestContext->VpRegs->Rbx = registers[1];
+ GuestContext->VpRegs->Rcx = registers[2];
+ GuestContext->VpRegs->Rdx = registers[3];
+
+
+ //
+ // Then, advance RIP to "complete" the instruction.
+ //
+
+Exit:
+
+ VpData->GuestVmcb.StateSaveArea.Rip = VpData->GuestVmcb.ControlArea.NRip;
+
+
+ if ((VpData->GuestVmcb.StateSaveArea.Rflags & X86_FLAGS_TF) != 0)
+
+ {
+
+ if ((__readmsr(IA32_MSR_DEBUGCTL) & BranchSingleStep) != 0)
+
+ {
+ __nop();
+ }
+
+ else
+
+ {
+
+ VpData->GuestVmcb.StateSaveArea.Dr6 = (VpData->GuestVmcb.StateSaveArea.Dr6 |= SingleStep);
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags & ~X86_FLAGS_TF);
+
+ SvInjectDbException(VpData);
+
+ }
+
+ }
+
+}
+
+/*!
+ SVM_INTERCEPT_MISC1_RDTSC bit == Used to preserve original RFLAGS.TF value
+ SVM_INTERCEPT_MISC1_RDPMC bit == Used to preserve original RFLAGS.IF value
+ SVM_INTERCEPT_MISC2_VMMCALL bit == Used to preserve original Debug-Control MSR Branch Single Step value
+ */
+
+_IRQL_requires_same_
+static
+VOID
+SvHandleDbException(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData,
+ _Inout_ PGUEST_CONTEXT GuestContext
+)
+{
+ DESCRIPTOR_TABLE_REGISTER CorrectGDTR;
+
+ if (VpData->GuestVmcb.StateSaveArea.Rip == UINT64(sysPtr))
+ {
+ VpData->GuestVmcb.StateSaveArea.Rip = OrigLSTAR;
+ }
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc1 & SVM_INTERCEPT_MISC1_GDTR_READ) != 0)
+
+ {
+ goto InjectDbException;
+ }
+
+ else
+
+ {
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 |= SVM_INTERCEPT_MISC1_GDTR_READ;
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_PF);
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_AC);
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_SS);
+
+ if (VpData->GuestVmcb.StateSaveArea.GdtrBase == 0xFFFFF69696900000)
+ {
+ _sgdt(&CorrectGDTR);
+ VpData->GuestVmcb.StateSaveArea.GdtrBase = CorrectGDTR.Base;
+ VpData->GuestVmcb.StateSaveArea.GdtrLimit = CorrectGDTR.Limit;
+ }
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc1 & SVM_INTERCEPT_MISC1_RDPMC) != 0)
+ {
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags |= X86_FLAGS_IF);
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 = (VpData->GuestVmcb.ControlArea.InterceptMisc1 & ~SVM_INTERCEPT_MISC1_RDPMC);
+ }
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc2 & SVM_INTERCEPT_MISC2_VMMCALL) != 0)
+ {
+ UINT64 MSR_DEBUGCTL = __readmsr(IA32_MSR_DEBUGCTL);
+ MSR_DEBUGCTL = (MSR_DEBUGCTL |= BranchSingleStep);
+ __writemsr(IA32_MSR_DEBUGCTL, MSR_DEBUGCTL);
+ }
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc1 & SVM_INTERCEPT_MISC1_RDTSC) != 0)
+ {
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 = (VpData->GuestVmcb.ControlArea.InterceptMisc1 & ~SVM_INTERCEPT_MISC1_RDTSC);
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc2 & SVM_INTERCEPT_MISC2_VMMCALL) != 0)
+ {
+ VpData->GuestVmcb.ControlArea.InterceptMisc2 = (VpData->GuestVmcb.ControlArea.InterceptMisc2 & ~SVM_INTERCEPT_MISC2_VMMCALL);
+ goto Exit;
+ }
+
+ }
+
+ else
+
+ {
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags & ~X86_FLAGS_TF);
+ VpData->GuestVmcb.ControlArea.InterceptMisc2 = (VpData->GuestVmcb.ControlArea.InterceptMisc2 & ~SVM_INTERCEPT_MISC2_VMMCALL);
+ goto Exit;
+ }
+
+ }
+
+
+ InjectDbException:
+
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags & ~X86_FLAGS_TF);
+ SvInjectDbException(VpData);
+
+ Exit:
+
+ __nop();
+
+}
+
+_IRQL_requires_same_
+static
+VOID
+SvHandleCR4Read(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData,
+ _Inout_ PGUEST_CONTEXT GuestContext
+)
+{
+ ULONG_PTR* gpr_array = (ULONG_PTR*)GuestContext->VpRegs;
+ CRDecodeAssist CRAssist;
+ UINT64 maskedCR4;
+
+ CRAssist.AsUInt64 = VpData->GuestVmcb.ControlArea.ExitInfo1;
+
+ maskedCR4 = (VpData->GuestVmcb.StateSaveArea.Cr4 |= UMIP);
+
+ gpr_array[CRAssist.Fields.GPR] = maskedCR4;
+
+ VpData->GuestVmcb.StateSaveArea.Rip = VpData->GuestVmcb.ControlArea.NRip;
+}
+
+
+_IRQL_requires_same_
+static
+VOID
+SvHandleCR4Write(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData,
+ _Inout_ PGUEST_CONTEXT GuestContext
+)
+{
+ ULONG_PTR* gpr_array = (ULONG_PTR*)GuestContext->VpRegs;
+ CRDecodeAssist CRAssist;
+ CR4 New;
+ CR4 Old;
+
+ CRAssist.AsUInt64 = VpData->GuestVmcb.ControlArea.ExitInfo1;
+
+ New.CR4 = gpr_array[CRAssist.Fields.GPR];
+ Old.CR4 = (VpData->GuestVmcb.StateSaveArea.Cr4 |= UMIP);
+
+ if (New.CR4 != Old.CR4)
+ {
+ New.CR4 = (New.CR4 & ~UMIP);
+
+ VpData->GuestVmcb.StateSaveArea.Cr4 = New.CR4;
+
+ if (Old.PageSizeExtensions != New.PageSizeExtensions)
+
+ {
+ goto FlushGuestTLB;
+ }
+
+ if (Old.PhysicalAddressExtension != New.PhysicalAddressExtension)
+
+ {
+ goto FlushGuestTLB;
+ }
+
+ if (Old.PageGlobalEnable != New.PageGlobalEnable)
+
+ {
+ goto FlushGuestTLB;
+ }
+
+ if (New.PcidEnable == 0x0 && Old.PcidEnable == 0x1)
+ {
+ goto FlushGuestTLB;
+ }
+
+ if (New.ProtectionKeyEnable == 0x1 && Old.ProtectionKeyEnable == 0x0)
+ {
+ goto FlushGuestTLB;
+ }
+
+ goto Exit;
+
+ }
+
+ goto Exit;
+
+FlushGuestTLB:
+
+ VpData->GuestVmcb.ControlArea.TlbControl = 0x3;
+
+Exit:
+
+ VpData->GuestVmcb.StateSaveArea.Rip = VpData->GuestVmcb.ControlArea.NRip;
+
+}
+
+
+_IRQL_requires_same_
+static
+VOID
+SvHandleSGDT(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData,
+ _Inout_ PGUEST_CONTEXT GuestContext
+)
+{
+
+ if (VpData->GuestVmcb.StateSaveArea.Cr3 != TargetCR3 && VpData->GuestVmcb.StateSaveArea.Cpl == 0x3 && UMIPSystem == 0x1)
+
+ {
+ SvInjectGeneralProtectionException(VpData);
+ }
+
+ else
+
+ {
+
+ if ((VpData->GuestVmcb.StateSaveArea.Rflags & X86_FLAGS_TF) != 0)
+
+ {
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 |= SVM_INTERCEPT_MISC1_RDTSC;
+ }
+
+ if ((VpData->GuestVmcb.StateSaveArea.Rflags & X86_FLAGS_IF) != 0)
+
+ {
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 |= SVM_INTERCEPT_MISC1_RDPMC;
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags & ~X86_FLAGS_IF);
+ }
+
+ UINT64 MSR_DEBUGCTL = __readmsr(IA32_MSR_DEBUGCTL);
+
+ if ((MSR_DEBUGCTL & BranchSingleStep) != 0)
+
+ {
+ VpData->GuestVmcb.ControlArea.InterceptMisc2 |= SVM_INTERCEPT_MISC2_VMMCALL;
+ MSR_DEBUGCTL = (MSR_DEBUGCTL & ~BranchSingleStep);
+ __writemsr(IA32_MSR_DEBUGCTL, MSR_DEBUGCTL);
+ }
+
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags |= X86_FLAGS_TF);
+
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 = (VpData->GuestVmcb.ControlArea.InterceptMisc1 & ~SVM_INTERCEPT_MISC1_GDTR_READ);
+
+ if (VpData->GuestVmcb.StateSaveArea.Cr3 == TargetCR3 && VpData->GuestVmcb.StateSaveArea.Cpl == 0x3)
+
+ {
+ VpData->GuestVmcb.StateSaveArea.GdtrBase = 0xFFFFF69696900000;
+ VpData->GuestVmcb.StateSaveArea.GdtrLimit = 0x7F;
+ VpData->GuestVmcb.ControlArea.InterceptException |= SVM_InterceptException_PF;
+ VpData->GuestVmcb.ControlArea.InterceptException |= SVM_InterceptException_AC;
+ VpData->GuestVmcb.ControlArea.InterceptException |= SVM_InterceptException_SS;
+ }
+
+ }
+
+}
+
+_IRQL_requires_same_
+static
+VOID
+SvHandlePFException(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData,
+ _Inout_ PGUEST_CONTEXT GuestContext
+)
+{
+ DESCRIPTOR_TABLE_REGISTER CorrectGDTR;
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc1 & SVM_INTERCEPT_MISC1_RDPMC) != 0)
+ {
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags |= X86_FLAGS_IF);
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 = (VpData->GuestVmcb.ControlArea.InterceptMisc1 & ~SVM_INTERCEPT_MISC1_RDPMC);
+ }
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc2 & SVM_INTERCEPT_MISC2_VMMCALL) != 0)
+ {
+ UINT64 MSR_DEBUGCTL = __readmsr(IA32_MSR_DEBUGCTL);
+ MSR_DEBUGCTL = (MSR_DEBUGCTL |= BranchSingleStep);
+ __writemsr(IA32_MSR_DEBUGCTL, MSR_DEBUGCTL);
+ VpData->GuestVmcb.ControlArea.InterceptMisc2 = (VpData->GuestVmcb.ControlArea.InterceptMisc2 & ~SVM_INTERCEPT_MISC2_VMMCALL);
+ }
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc1 & SVM_INTERCEPT_MISC1_RDTSC) != 0)
+ {
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 = (VpData->GuestVmcb.ControlArea.InterceptMisc1 & ~SVM_INTERCEPT_MISC1_RDTSC);
+ }
+
+ else
+
+ {
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags & ~X86_FLAGS_TF);
+ }
+
+ _sgdt(&CorrectGDTR);
+
+ VpData->GuestVmcb.StateSaveArea.GdtrBase = CorrectGDTR.Base;
+ VpData->GuestVmcb.StateSaveArea.GdtrLimit = CorrectGDTR.Limit;
+
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_PF);
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_AC);
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_SS);
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 |= SVM_INTERCEPT_MISC1_GDTR_READ;
+ VpData->GuestVmcb.StateSaveArea.Cr2 = VpData->GuestVmcb.ControlArea.ExitInfo2;
+
+ SvInjectPFException(VpData);
+}
+
+_IRQL_requires_same_
+static
+VOID
+SvHandleACException(
+ _Inout_ PVIRTUAL_PROCESSOR_DATA VpData,
+ _Inout_ PGUEST_CONTEXT GuestContext
+)
+{
+ DESCRIPTOR_TABLE_REGISTER CorrectGDTR;
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc1 & SVM_INTERCEPT_MISC1_RDPMC) != 0)
+ {
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags |= X86_FLAGS_IF);
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 = (VpData->GuestVmcb.ControlArea.InterceptMisc1 & ~SVM_INTERCEPT_MISC1_RDPMC);
+ }
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc2 & SVM_INTERCEPT_MISC2_VMMCALL) != 0)
+ {
+ UINT64 MSR_DEBUGCTL = __readmsr(IA32_MSR_DEBUGCTL);
+ MSR_DEBUGCTL = (MSR_DEBUGCTL |= BranchSingleStep);
+ __writemsr(IA32_MSR_DEBUGCTL, MSR_DEBUGCTL);
+ VpData->GuestVmcb.ControlArea.InterceptMisc2 = (VpData->GuestVmcb.ControlArea.InterceptMisc2 & ~SVM_INTERCEPT_MISC2_VMMCALL);
+ }
+
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc1 & SVM_INTERCEPT_MISC1_RDTSC) != 0)
+ {
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 = (VpData->GuestVmcb.ControlArea.InterceptMisc1 & ~SVM_INTERCEPT_MISC1_RDTSC);
+ }
+
+ else
+
+ {
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags & ~X86_FLAGS_TF);
+ }
- //
- // Inject #GP(vector = 13, type = 3 = exception) with a valid error code.
- // An error code are always zero. See "#GP-General-Protection Exception
- // (Vector 13)" for details about the error code.
- //
- event.AsUInt64 = 0;
- event.Fields.Vector = 13;
- event.Fields.Type = 3;
- event.Fields.ErrorCodeValid = 1;
- event.Fields.Valid = 1;
- VpData->GuestVmcb.ControlArea.EventInj = event.AsUInt64;
-}
+ _sgdt(&CorrectGDTR);
-/*!
- @brief Handles #VMEXIT due to execution of the CPUID instructions.
+ VpData->GuestVmcb.StateSaveArea.GdtrBase = CorrectGDTR.Base;
+ VpData->GuestVmcb.StateSaveArea.GdtrLimit = CorrectGDTR.Limit;
- @details This function returns unmodified results of the CPUID
- instruction, except for few cases to indicate presence of
- the hypervisor, and to process an unload request.
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_PF);
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_AC);
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_SS);
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 |= SVM_INTERCEPT_MISC1_GDTR_READ;
- CPUID leaf 0x40000000 and 0x40000001 return modified values
- to conform to the hypervisor interface to some extent. See
- "Requirements for implementing the Microsoft Hypervisor interface"
- https://msdn.microsoft.com/en-us/library/windows/hardware/Dn613994(v=vs.85).aspx
- for details of the interface.
+ SvInjectACException(VpData);
+}
- @param[in,out] VpData - Per processor data.
- @param[in,out] GuestContext - Guest's GPRs.
- */
_IRQL_requires_same_
static
VOID
-SvHandleCpuid (
+SvHandleSsException(
_Inout_ PVIRTUAL_PROCESSOR_DATA VpData,
_Inout_ PGUEST_CONTEXT GuestContext
- )
+)
{
- int registers[4]; // EAX, EBX, ECX, and EDX
- int leaf, subLeaf;
- SEGMENT_ATTRIBUTE attribute;
- //
- // Execute CPUID as requested.
- //
- leaf = static_cast<int>(GuestContext->VpRegs->Rax);
- subLeaf = static_cast<int>(GuestContext->VpRegs->Rcx);
- __cpuidex(registers, leaf, subLeaf);
+ DESCRIPTOR_TABLE_REGISTER CorrectGDTR;
- switch (leaf)
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc1 & SVM_INTERCEPT_MISC1_RDPMC) != 0)
{
- case CPUID_PROCESSOR_AND_PROCESSOR_FEATURE_IDENTIFIERS:
- //
- // Indicate presence of a hypervisor by setting the bit that are
- // reserved for use by hypervisor to indicate guest status. See "CPUID
- // Fn0000_0001_ECX Feature Identifiers".
- //
- registers[2] |= CPUID_FN0000_0001_ECX_HYPERVISOR_PRESENT;
- break;
- case CPUID_HV_VENDOR_AND_MAX_FUNCTIONS:
- //
- // Return a maximum supported hypervisor CPUID leaf range and a vendor
- // ID signature as required by the spec.
- //
- registers[0] = CPUID_HV_MAX;
- registers[1] = 'pmiS'; // "SimpleSvm "
- registers[2] = 'vSel';
- registers[3] = ' m';
- break;
- case CPUID_HV_INTERFACE:
- //
- // Return non Hv#1 value. This indicate that the SimpleSvm does NOT
- // conform to the Microsoft hypervisor interface.
- //
- registers[0] = '0#vH'; // Hv#0
- registers[1] = registers[2] = registers[3] = 0;
- break;
- case CPUID_UNLOAD_SIMPLE_SVM:
- if (subLeaf == CPUID_UNLOAD_SIMPLE_SVM)
- {
- //
- // Unload itself if the request is from the kernel mode.
- //
- attribute.AsUInt16 = VpData->GuestVmcb.StateSaveArea.SsAttrib;
- if (attribute.Fields.Dpl == DPL_SYSTEM)
- {
- GuestContext->ExitVm = TRUE;
- }
- }
- break;
- default:
- break;
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags |= X86_FLAGS_IF);
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 = (VpData->GuestVmcb.ControlArea.InterceptMisc1 & ~SVM_INTERCEPT_MISC1_RDPMC);
}
- //
- // Update guest's GPRs with results.
- //
- GuestContext->VpRegs->Rax = registers[0];
- GuestContext->VpRegs->Rbx = registers[1];
- GuestContext->VpRegs->Rcx = registers[2];
- GuestContext->VpRegs->Rdx = registers[3];
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc2 & SVM_INTERCEPT_MISC2_VMMCALL) != 0)
+ {
+ UINT64 MSR_DEBUGCTL = __readmsr(IA32_MSR_DEBUGCTL);
+ MSR_DEBUGCTL = (MSR_DEBUGCTL |= BranchSingleStep);
+ __writemsr(IA32_MSR_DEBUGCTL, MSR_DEBUGCTL);
+ VpData->GuestVmcb.ControlArea.InterceptMisc2 = (VpData->GuestVmcb.ControlArea.InterceptMisc2 & ~SVM_INTERCEPT_MISC2_VMMCALL);
+ }
- //
- // Debug prints results. Very important to note that any use of API from
- // the host context is unsafe and absolutely avoided, unless the API is
- // documented to be accessible on IRQL IPI_LEVEL+. This is because
- // interrupts are disabled when host code is running, and IPI is not going
- // to be delivered when it is issued.
- //
- // This code is not exception and violating this rule. The reasons for this
- // code are to demonstrate a bad example, and simply show that the SimpleSvm
- // is functioning for a test purpose.
- //
- if (KeGetCurrentIrql() <= DISPATCH_LEVEL)
+ if ((VpData->GuestVmcb.ControlArea.InterceptMisc1 & SVM_INTERCEPT_MISC1_RDTSC) != 0)
{
- SvDebugPrint("CPUID: %08x-%08x : %08x %08x %08x %08x\n",
- leaf,
- subLeaf,
- registers[0],
- registers[1],
- registers[2],
- registers[3]);
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 = (VpData->GuestVmcb.ControlArea.InterceptMisc1 & ~SVM_INTERCEPT_MISC1_RDTSC);
}
- //
- // Then, advance RIP to "complete" the instruction.
- //
- VpData->GuestVmcb.StateSaveArea.Rip = VpData->GuestVmcb.ControlArea.NRip;
+ else
+
+ {
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags & ~X86_FLAGS_TF);
+ }
+
+ _sgdt(&CorrectGDTR);
+
+ VpData->GuestVmcb.StateSaveArea.GdtrBase = CorrectGDTR.Base;
+ VpData->GuestVmcb.StateSaveArea.GdtrLimit = CorrectGDTR.Limit;
+
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_PF);
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_AC);
+ VpData->GuestVmcb.ControlArea.InterceptException = (VpData->GuestVmcb.ControlArea.InterceptException & ~SVM_InterceptException_SS);
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 |= SVM_INTERCEPT_MISC1_GDTR_READ;
+
+ SvInjectSsException(VpData);
}
+
/*!
@brief Handles #VMEXIT due to execution of the WRMSR and RDMSR
instructions.
@@ -604,6 +1549,7 @@ SvHandleMsrAccess (
)
{
ULARGE_INTEGER value;
+ ULARGE_INTEGER LSTARValue;
UINT32 msr;
BOOLEAN writeAccess;
@@ -631,51 +1577,56 @@ SvHandleMsrAccess (
// leads to undefined behavior.
//
SvInjectGeneralProtectionException(VpData);
- return;
}
- //
- // Otherwise, update the MSR as requested. Important to note that the value
- // should be checked not to allow any illegal values, and inject #GP as
- // needed. Otherwise, the hypervisor attempts to resume the guest with an
- // illegal EFER and immediately receives #VMEXIT due to VMEXIT_INVALID,
- // which in our case, results in a bug check. See "Extended Feature Enable
- // Register (EFER)" for what values are allowed.
- //
- // This code does not implement the check intentionally, for simplicity.
- //
VpData->GuestVmcb.StateSaveArea.Efer = value.QuadPart;
}
+
+ if (msr == IA32_MSR_LSTAR)
+ {
+ if (writeAccess != FALSE)
+ {
+ LSTARValue.LowPart = GuestContext->VpRegs->Rax & MAXUINT32;
+ LSTARValue.HighPart = GuestContext->VpRegs->Rdx & MAXUINT32;
+
+ if (LSTARValue.QuadPart != OrigLSTAR)
+
+ {
+ VpData->GuestVmcb.StateSaveArea.LStar = LSTARValue.QuadPart;
+ }
+
+ else
+
+ {
+ VpData->GuestVmcb.StateSaveArea.LStar = UINT64(sysPtr);
+ }
+ }
+
+ else
+
+ {
+ if (VpData->GuestVmcb.StateSaveArea.LStar != UINT64(sysPtr))
+ {
+ LSTARValue.QuadPart = VpData->GuestVmcb.StateSaveArea.LStar;
+ }
+
+ else
+
+ {
+ LSTARValue.QuadPart = OrigLSTAR;
+ }
+
+ GuestContext->VpRegs->Rax = LSTARValue.LowPart;
+ GuestContext->VpRegs->Rdx = LSTARValue.HighPart;
+ }
+ }
+
else
{
- //
- // If the MSR being accessed is not IA32_MSR_EFER, assert that #VMEXIT
- // can only occur on access to MSR outside the ranges controlled with
- // the MSR permissions map. This is true because the map is configured
- // not to intercept any MSR access but IA32_MSR_EFER. See
- // "MSR Ranges Covered by MSRPM" in "MSR Intercepts" for the MSR ranges
- // controlled by the map.
- //
- // Note that VMware Workstation has a bug that access to unimplemented
- // MSRs unconditionally causes #VMEXIT ignoring bits in the MSR
- // permissions map. This can be tested by reading MSR zero, for example.
- //
NT_ASSERT(((msr > 0x00001fff) && (msr < 0xc0000000)) ||
((msr > 0xc0001fff) && (msr < 0xc0010000)) ||
(msr > 0xc0011fff));
- //
- // Execute WRMSR or RDMSR on behalf of the guest. Important that this
- // can cause bug check when the guest tries to access unimplemented MSR
- // *even within the SEH block* because the below WRMSR or RDMSR raises
- // #GP and are not protected by the SEH block (or cannot be protected
- // either as this code run outside the thread stack region Windows
- // requires to proceed SEH). Hypervisors typically handle this by noop-ing
- // WRMSR and returning zero for RDMSR with non-architecturally defined
- // MSRs. Alternatively, one can probe which MSRs should cause #GP prior
- // to installation of a hypervisor and the hypervisor can emulate the
- // results.
- //
if (writeAccess != FALSE)
{
value.LowPart = GuestContext->VpRegs->Rax & MAXUINT32;
@@ -693,7 +1644,30 @@ SvHandleMsrAccess (
//
// Then, advance RIP to "complete" the instruction.
//
+
VpData->GuestVmcb.StateSaveArea.Rip = VpData->GuestVmcb.ControlArea.NRip;
+
+ if ((VpData->GuestVmcb.StateSaveArea.Rflags & X86_FLAGS_TF) != 0)
+
+ {
+
+ if ((__readmsr(IA32_MSR_DEBUGCTL) & BranchSingleStep) != 0)
+
+ {
+ __nop();
+ }
+
+ else
+
+ {
+
+ VpData->GuestVmcb.StateSaveArea.Dr6 = (VpData->GuestVmcb.StateSaveArea.Dr6 |= SingleStep);
+ VpData->GuestVmcb.StateSaveArea.Rflags = (VpData->GuestVmcb.StateSaveArea.Rflags & ~X86_FLAGS_TF);
+
+ SvInjectDbException(VpData);
+
+ }
+ }
}
/*!
@@ -747,7 +1721,6 @@ SvHandleVmExit (
)
{
GUEST_CONTEXT guestContext;
- KIRQL oldIrql;
guestContext.VpRegs = GuestRegisters;
guestContext.ExitVm = FALSE;
@@ -757,29 +1730,14 @@ SvHandleVmExit (
//
__svm_vmload(VpData->HostStackLayout.HostVmcbPa);
- NT_ASSERT(VpData->HostStackLayout.Reserved1 == MAXUINT64);
-
- //
- // Raise the IRQL to the DISPATCH_LEVEL level. This has no actual effect since
- // interrupts are disabled at #VMEXI but warrants bug check when some of
- // kernel API that are not usable on this context is called with Driver
- // Verifier. This protects developers from accidentally writing such #VMEXIT
- // handling code. This should actually raise IRQL to HIGH_LEVEL to represent
- // this running context better, but our Logger code is not designed to run at
- // that level unfortunately. Finally, note that this API is a thin wrapper
- // of mov-to-CR8 on x64 and safe to call on this context.
- //
- oldIrql = KeGetCurrentIrql();
- if (oldIrql < DISPATCH_LEVEL)
- {
- KeRaiseIrqlToDpcLevel();
- }
+ //NT_ASSERT(VpData->HostStackLayout.Reserved1 == MAXUINT64);
//
// Guest's RAX is overwritten by the host's value on #VMEXIT and saved in
// the VMCB instead. Reflect the guest RAX to the context.
//
GuestRegisters->Rax = VpData->GuestVmcb.StateSaveArea.Rax;
+ GuestRegisters->Rsp = VpData->GuestVmcb.StateSaveArea.Rsp;
//
// Update the _KTRAP_FRAME structure values in hypervisor stack, so that
@@ -797,28 +1755,39 @@ SvHandleVmExit (
case VMEXIT_CPUID:
SvHandleCpuid(VpData, &guestContext);
break;
+ case VMEXIT_CR4_READ:
+ SvHandleCR4Read(VpData, &guestContext);
+ break;
+ case VMEXIT_CR4_WRITE:
+ SvHandleCR4Write(VpData, &guestContext);
+ break;
+ case VMEXIT_EXCEPTION_DB:
+ SvHandleDbException(VpData, &guestContext);
+ break;
+ case VMEXIT_GDTR_READ:
+ SvHandleSGDT(VpData, &guestContext);
+ break;
case VMEXIT_MSR:
SvHandleMsrAccess(VpData, &guestContext);
break;
+ case VMEXIT_EXCEPTION_PF:
+ SvHandlePFException(VpData, &guestContext);
+ break;
+ case VMEXIT_EXCEPTION_AC:
+ SvHandleACException(VpData, &guestContext);
+ break;
+ case VMEXIT_EXCEPTION_SS:
+ SvHandleSsException(VpData, &guestContext);
+ break;
case VMEXIT_VMRUN:
SvHandleVmrun(VpData, &guestContext);
break;
default:
SV_DEBUG_BREAK();
-#pragma prefast(suppress : __WARNING_USE_OTHER_FUNCTION, "Unrecoverable path.")
+#pragma prefast(disable : __WARNING_USE_OTHER_FUNCTION, "Unrecoverble path.")
KeBugCheck(MANUALLY_INITIATED_CRASH);
}
- //
- // Again, no effect to change IRQL but restoring it here since a #VMEXIT
- // handler where the developers most likely call the kernel API inadvertently
- // is already executed.
- //
- if (oldIrql < DISPATCH_LEVEL)
- {
- KeLowerIrql(oldIrql);
- }
-
//
// Terminate the SimpleSvm hypervisor if requested.
//
@@ -826,6 +1795,16 @@ SvHandleVmExit (
{
NT_ASSERT(VpData->GuestVmcb.ControlArea.ExitCode == VMEXIT_CPUID);
+#ifdef KUSER_SPOOF
+
+ if (NotifyRoutineActive)
+ {
+ PsSetCreateProcessNotifyRoutine(ProcessNotifyRestoreFields, TRUE);
+ NotifyRoutineActive = 0x0;
+ }
+
+#endif
+
//
// Set return values of CPUID instruction as follows:
// RBX = An address to return
@@ -855,7 +1834,9 @@ SvHandleVmExit (
// Disable SVM, and restore the guest RFLAGS. This may enable interrupts.
// Some of arithmetic flags are destroyed by the subsequent code.
//
+
__writemsr(IA32_MSR_EFER, __readmsr(IA32_MSR_EFER) & ~EFER_SVME);
+ __writemsr(IA32_MSR_LSTAR, OrigLSTAR);
__writeeflags(VpData->GuestVmcb.StateSaveArea.Rflags);
goto Exit;
}
@@ -865,6 +1846,7 @@ SvHandleVmExit (
// RAX is loaded from VMCB on VMRUN.
//
VpData->GuestVmcb.StateSaveArea.Rax = guestContext.VpRegs->Rax;
+ VpData->GuestVmcb.StateSaveArea.Rsp = guestContext.VpRegs->Rsp;
Exit:
NT_ASSERT(VpData->HostStackLayout.Reserved1 == MAXUINT64);
@@ -874,19 +1856,6 @@ Exit:
/*!
@brief Returns attributes of a segment specified by the segment selector.
- @details This function locates a segment descriptor from the segment
- selector and the GDT base, extracts attributes of the segment,
- and returns it. The returned value is the same as what the "dg"
- command of Windbg shows as "Flags". Here is an example output
- with 0x18 of the selector:
- ----
- 0: kd> dg 18
- P Si Gr Pr Lo
- Sel Base Limit Type l ze an es ng Flags
- ---- ----------------- ----------------- ---------- - -- -- -- -- --------
- 0018 00000000`00000000 00000000`00000000 Data RW Ac 0 Bg By P Nl 00000493
- ----
-
@param[in] SegmentSelector - A segment selector to get attributes of a
corresponding descriptor.
@param[in] GdtBase - A base address of GDT.
@@ -932,15 +1901,6 @@ SvGetSegmentAccessRight (
/*!
@brief Tests whether the SimpleSvm hypervisor is installed.
- @details This function checks a result of CPUID leaf 40000000h, which
- should return a vendor name of the hypervisor if any of those
- who implement the Microsoft Hypervisor interface is installed.
- If the SimpleSvm hypervisor is installed, this should return
- "SimpleSvm", and if no hypervisor is installed, it the result of
- CPUID is undefined. For more details of the interface, see
- "Requirements for implementing the Microsoft Hypervisor interface"
- https://msdn.microsoft.com/en-us/library/windows/hardware/Dn613994(v=vs.85).aspx
-
@result TRUE when the SimpleSvm is installed; otherwise, FALSE.
*/
_IRQL_requires_max_(DISPATCH_LEVEL)
@@ -970,7 +1930,7 @@ SvIsSimpleSvmHypervisorInstalled (
}
/*!
- @brief Virtualizes the current processor.
+ @brief Virtualize the current processor.
@details This function enables SVM, initialize VMCB with the current
processor state, and enters the guest mode on the current
@@ -1008,19 +1968,22 @@ SvPrepareForVirtualization (
pml4BasePa = MmGetPhysicalAddress(&SharedVpData->Pml4Entries);
msrpmPa = MmGetPhysicalAddress(SharedVpData->MsrPermissionsMap);
- //
- // Configure to trigger #VMEXIT with CPUID and VMRUN instructions. CPUID is
- // intercepted to present existence of the SimpleSvm hypervisor and provide
- // an interface to ask it to unload itself.
- //
- // VMRUN is intercepted because it is required by the processor to enter the
- // guest mode; otherwise, #VMEXIT occurs due to VMEXIT_INVALID when a
- // processor attempts to enter the guest mode. See "Canonicalization and
- // Consistency Checks" on "VMRUN Instruction".
- //
+ VpData->GuestVmcb.StateSaveArea.Cr4 = __readcr4();
+
+ if ((VpData->GuestVmcb.StateSaveArea.Cr4 & UMIP) != 0)
+ {
+ UMIPSystem = 0x1;
+ VpData->GuestVmcb.StateSaveArea.Cr4 = (VpData->GuestVmcb.StateSaveArea.Cr4 & ~UMIP);
+ VpData->GuestVmcb.ControlArea.InterceptCrRead |= SVM_INTERCEPT_CR_READ_CR4;
+ VpData->GuestVmcb.ControlArea.InterceptCrWrite |= SVM_INTERCEPT_CR_WRITE_CR4;
+ }
+
VpData->GuestVmcb.ControlArea.InterceptMisc1 |= SVM_INTERCEPT_MISC1_CPUID;
+ VpData->GuestVmcb.ControlArea.InterceptMisc1 |= SVM_INTERCEPT_MISC1_GDTR_READ;
VpData->GuestVmcb.ControlArea.InterceptMisc2 |= SVM_INTERCEPT_MISC2_VMRUN;
+ VpData->GuestVmcb.ControlArea.InterceptException |= SVM_InterceptException_DB;
+
//
// Also, configure to trigger #VMEXIT on MSR access as configured by the
// MSRPM. In our case, write to IA32_MSR_EFER is intercepted.
@@ -1028,28 +1991,8 @@ SvPrepareForVirtualization (
VpData->GuestVmcb.ControlArea.InterceptMisc1 |= SVM_INTERCEPT_MISC1_MSR_PROT;
VpData->GuestVmcb.ControlArea.MsrpmBasePa = msrpmPa.QuadPart;
- //
- // Specify guest's address space ID (ASID). TLB is maintained by the ID for
- // guests. Use the same value for all processors since all of them run a
- // single guest in our case. Use 1 as the most likely supported ASID by the
- // processor. The actual the supported number of ASID can be obtained with
- // CPUID. See "CPUID Fn8000_000A_EBX SVM Revision and Feature
- // Identification". Zero of ASID is reserved and illegal.
- //
VpData->GuestVmcb.ControlArea.GuestAsid = 1;
- //
- // Enable Nested Page Tables. By enabling this, the processor performs the
- // nested page walk, that involves with an additional page walk to translate
- // a guest physical address to a system physical address. An address of
- // nested page tables is specified by the NCr3 field of VMCB.
- //
- // We have already build the nested page tables with SvBuildNestedPageTables.
- //
- // Note that our hypervisor does not trigger any additional #VMEXIT due to
- // the use of Nested Page Tables since all physical addresses from 0-512 GB
- // are configured to be accessible from the guest.
- //
VpData->GuestVmcb.ControlArea.NpEnable |= SVM_NP_ENABLE_NP_ENABLE;
VpData->GuestVmcb.ControlArea.NCr3 = pml4BasePa.QuadPart;
@@ -1077,10 +2020,10 @@ SvPrepareForVirtualization (
VpData->GuestVmcb.StateSaveArea.SsAttrib = SvGetSegmentAccessRight(ContextRecord->SegSs, gdtr.Base);
VpData->GuestVmcb.StateSaveArea.Efer = __readmsr(IA32_MSR_EFER);
+ OrigLSTAR = __readmsr(IA32_MSR_LSTAR);
VpData->GuestVmcb.StateSaveArea.Cr0 = __readcr0();
VpData->GuestVmcb.StateSaveArea.Cr2 = __readcr2();
VpData->GuestVmcb.StateSaveArea.Cr3 = __readcr3();
- VpData->GuestVmcb.StateSaveArea.Cr4 = __readcr4();
VpData->GuestVmcb.StateSaveArea.Rflags = ContextRecord->EFlags;
VpData->GuestVmcb.StateSaveArea.Rsp = ContextRecord->Rsp;
VpData->GuestVmcb.StateSaveArea.Rip = ContextRecord->Rip;
@@ -1101,6 +2044,8 @@ SvPrepareForVirtualization (
//
__svm_vmsave(guestVmcbPa.QuadPart);
+ VpData->GuestVmcb.StateSaveArea.LStar = UINT64(sysPtr);
+
//
// Store data to stack so that the host (hypervisor) can use those values.
//
@@ -1150,6 +2095,23 @@ SvVirtualizeProcessor (
PVIRTUAL_PROCESSOR_DATA vpData;
PCONTEXT contextRecord;
+ //SYSCALL hook - msvc x64 inline assembly xd
+
+ sysPtr = &SyscallHandler;
+ TempPTR = UINT64(sysPtr);
+ *(UINT64*)(TempPTR) = 0x18200F327536F883;
+ *(UINT64*)(TempPTR + 0x8) = 0x7500000039053B48;
+ *(UINT64*)(TempPTR + 0x10) = 0xFFFFFFFFFFB84821;
+ *(UINT64*)(TempPTR + 0x18) = 0x1277C1394800007F;
+ *(UINT64*)(TempPTR + 0x20) = 0xC889480D75D2854D;
+ *(UINT64*)(TempPTR + 0x28) = 0x48000000210D8B48;
+ *(UINT64*)(TempPTR + 0x30) = 0x4800000036B8070F;
+ *(UINT64*)(TempPTR + 0x38) = 0x00000000000225FF;
+
+ *(UINT64*)(TempPTR + 0x40) = OrigLSTAR;
+ *(UINT64*)(TempPTR + 0x48) = TargetCR3;
+ *(UINT64*)(TempPTR + 0x50) = TargetSysHandler;
+
SV_DEBUG_BREAK();
vpData = nullptr;
@@ -1157,8 +2119,8 @@ SvVirtualizeProcessor (
NT_ASSERT(ARGUMENT_PRESENT(Context));
_Analysis_assume_(ARGUMENT_PRESENT(Context));
- contextRecord = static_cast<PCONTEXT>(ExAllocatePool2(
- POOL_FLAG_NON_PAGED,
+ contextRecord = static_cast<PCONTEXT>(ExAllocatePoolWithTag(
+ NonPagedPool,
sizeof(*contextRecord),
'MVSS'));
if (contextRecord == nullptr)
@@ -1171,9 +2133,11 @@ SvVirtualizeProcessor (
//
// Allocate per processor data.
//
-#pragma prefast(suppress : __WARNING_MEMORY_LEAK, "Ownership is taken on success.")
+#pragma prefast(push)
+#pragma prefast(disable : __WARNING_MEMORY_LEAK, "Ownership is taken on success.")
vpData = static_cast<PVIRTUAL_PROCESSOR_DATA>(
SvAllocatePageAlingedPhysicalMemory(sizeof(VIRTUAL_PROCESSOR_DATA)));
+#pragma prefast(pop)
if (vpData == nullptr)
{
SvDebugPrint("Insufficient memory.\n");
@@ -1223,11 +2187,11 @@ SvVirtualizeProcessor (
//
SvLaunchVm(&vpData->HostStackLayout.GuestVmcbPa);
SV_DEBUG_BREAK();
-#pragma prefast(suppress : __WARNING_USE_OTHER_FUNCTION, "Unrecoverble path.")
KeBugCheck(MANUALLY_INITIATED_CRASH);
}
SvDebugPrint("The processor has been virtualized.\n");
+
status = STATUS_SUCCESS;
Exit:
@@ -1246,26 +2210,6 @@ Exit:
return status;
}
-/*!
- @brief Execute a callback on all processors one-by-one.
-
- @details This function execute Callback with Context as a parameter for
- each processor on the current IRQL. If the callback returned
- non-STATUS_SUCCESS value or any error occurred, this function
- stops execution of the callback and returns the error code.
-
- When NumOfProcessorCompleted is not NULL, this function always
- set a number of processors that successfully executed the
- callback.
-
- @param[in] Callback - A function to execute on all processors.
- @param[in] Context - A parameter to pass to the callback.
- @param[out] NumOfProcessorCompleted - A pointer to receive a number of
- processors executed the callback successfully.
-
- @result STATUS_SUCCESS when Callback executed and returned STATUS_SUCCESS
- on all processors; otherwise, an appropriate error code.
- */
_IRQL_requires_max_(APC_LEVEL)
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_same_
@@ -1446,23 +2390,6 @@ SvDevirtualizeAllProcessors (
/*!
@brief Build the MSR permissions map (MSRPM).
- @details This function sets up MSRPM to intercept to IA32_MSR_EFER,
- as suggested in "Extended Feature Enable Register (EFER)"
- ----
- Secure Virtual Machine Enable (SVME) Bit
- Bit 12, read/write. Enables the SVM extensions. (...) The
- effect of turning off EFER.SVME while a guest is running is
- undefined; therefore, the VMM should always prevent guests
- from writing EFER.
- ----
-
- Each MSR is controlled by two bits in the MSRPM. The LSB of
- the two bits controls read access to the MSR and the MSB
- controls write access. A value of 1 indicates that the
- operation is intercepted. This function locates an offset for
- IA32_MSR_EFER and sets the MSB bit. For details of logic, see
- "MSR Intercepts".
-
@param[in,out] MsrPermissionsMap - The MSRPM to set up.
*/
_IRQL_requires_same_
@@ -1472,9 +2399,9 @@ SvBuildMsrPermissionsMap (
_Inout_ PVOID MsrPermissionsMap
)
{
- constexpr UINT32 BITS_PER_MSR = 2;
- constexpr UINT32 SECOND_MSR_RANGE_BASE = 0xc0000000;
- constexpr UINT32 SECOND_MSRPM_OFFSET = 0x800 * CHAR_BIT;
+ static const UINT32 BITS_PER_MSR = 2;
+ static const UINT32 SECOND_MSR_RANGE_BASE = 0xc0000000;
+ static const UINT32 SECOND_MSRPM_OFFSET = 0x800 * CHAR_BIT;
RTL_BITMAP bitmapHeader;
ULONG offsetFrom2ndBase, offset;
@@ -1498,27 +2425,20 @@ SvBuildMsrPermissionsMap (
//
// Set the MSB bit indicating write accesses to the MSR should be intercepted.
//
+
RtlSetBits(&bitmapHeader, offset + 1, 1);
-}
-/*!
- @brief Build pass-through style page tables used in nested paging.
+ // Intercept LSTAR access.
- @details This function build page tables used in Nested Page Tables. The
- page tables are used to translate from a guest physical address
- to a system physical address and pointed by the NCr3 field of
- VMCB, like the traditional page tables are pointed by CR3.
+ offsetFrom2ndBase = (IA32_MSR_LSTAR - SECOND_MSR_RANGE_BASE) * BITS_PER_MSR;
+ offset = SECOND_MSRPM_OFFSET + offsetFrom2ndBase;
- The nested page tables built in this function are set to
- translate a guest physical address to the same system physical
- address. For example, guest physical address 0x1000 is
- translated into system physical address 0x1000.
+ RtlSetBits(&bitmapHeader, offset, 1);
+ RtlSetBits(&bitmapHeader, offset + 1, 1);
+}
- In order to save memory to build nested page tables, 2MB large
- pages are used (as opposed to the standard pages that describe
- translation only for 4K granularity. Also, only up to 1 TB of
- translation is built. 1GB huge pages are not used due to VMware
- not supporting this feature.
+/*!
+ @brief Build pass-through style page tables used in nested paging.
@param[out] SharedVpData - Out buffer to build nested page tables.
*/
@@ -1740,9 +2660,11 @@ SvVirtualizeAllProcessors (
// Allocate a data structure shared across all processors. This data is
// page tables used for Nested Page Tables.
//
-#pragma prefast(suppress : __WARNING_MEMORY_LEAK, "Ownership is taken on success.")
+#pragma prefast(push)
+#pragma prefast(disable : __WARNING_MEMORY_LEAK, "Ownership is taken on success.")
sharedVpData = static_cast<PSHARED_VIRTUAL_PROCESSOR_DATA>(
SvAllocatePageAlingedPhysicalMemory(sizeof(SHARED_VIRTUAL_PROCESSOR_DATA)));
+#pragma prefast(pop)
if (sharedVpData == nullptr)
{
SvDebugPrint("Insufficient memory.\n");
@@ -1768,18 +2690,6 @@ SvVirtualizeAllProcessors (
SvBuildNestedPageTables(sharedVpData);
SvBuildMsrPermissionsMap(sharedVpData->MsrPermissionsMap);
- //
- // Execute SvVirtualizeProcessor on and virtualize each processor one-by-one.
- // How many processors were successfully virtualized is stored in the third
- // parameter.
- //
- // STATUS_SUCCESS is returned if all processor are successfully virtualized.
- // When any error occurs while virtualizing processors, this function does
- // not attempt to virtualize the rest of processor. Therefore, only part of
- // processors on the system may have been virtualized on error. In this case,
- // it is a caller's responsibility to clean-up (de-virtualize) such
- // processors.
- //
status = SvExecuteOnEachProcessor(SvVirtualizeProcessor,
sharedVpData,
&numOfProcessorsCompleted);
@@ -1847,27 +2757,8 @@ DriverEntry (
callbackRegistration = nullptr;
DriverObject->DriverUnload = SvDriverUnload;
- //
- // Opts-in no-execute (NX) nonpaged pool when available for security. By
- // defining POOL_NX_OPTIN as 1 and calling this function, nonpaged pool
- // allocation by the ExAllocatePool family with the NonPagedPool flag
- // automatically allocates NX nonpaged pool on Windows 8 and later versions
- // of Windows, while on Windows 7 where NX nonpaged pool is unsupported,
- // executable nonpaged pool is returned as usual.
- //
ExInitializeDriverRuntime(DrvRtPoolNxOptIn);
- //
- // Registers a power state callback (SvPowerCallbackRoutine) to handle
- // system sleep and resume to manage virtualization state.
- //
- // First, opens the \Callback\PowerState callback object provides
- // notification regarding power state changes. This is a system defined
- // callback object that was already created by Windows. To open a system
- // defined callback object, the Create parameter of ExCreateCallback must be
- // FALSE (and AllowMultipleCallbacks is ignore when the Create parameter is
- // FALSE).
- //
objectName = RTL_CONSTANT_STRING(L"\\Callback\\PowerState");
objectAttributes = RTL_CONSTANT_OBJECT_ATTRIBUTES(&objectName,
OBJ_CASE_INSENSITIVE);
@@ -1938,6 +2829,49 @@ SvDriverUnload (
SV_DEBUG_BREAK();
+#ifdef KUSER_SPOOF
+
+ if (CounterThreadHandle)
+ {
+ PETHREAD CounterThread;
+ ObReferenceObjectByHandle(CounterThreadHandle, NULL, *PsThreadType, KernelMode, (PVOID*)&CounterThread, NULL);
+ StopCounterThread = TRUE;
+ KeWaitForSingleObject(CounterThread, Executive, KernelMode, FALSE, NULL);
+ ObDereferenceObject(CounterThread);
+ ZwClose(CounterThreadHandle);
+ StopCounterThread = FALSE;
+ CounterThreadHandle = NULL;
+ CounterInit = 0x0;
+
+ auto hostcr3 = __readcr3();
+ __writecr3(TargetCR3);
+
+ auto TargetProcessKuserPte = Utils::GetPte((void*)0x7FFE0000, TargetCR3);
+
+ if (TargetProcessKuserPte)
+ {
+ TargetProcessKuserPte->PageFrameNumber = OriginalKuserPFN;
+
+ __invlpg((void*)0x7FFE0000);
+ }
+
+ __writecr3(hostcr3);
+
+ PsReleaseProcessExitSynchronization(TargetProcess);
+ ObDereferenceObject(TargetProcess);
+
+ if (NewKuserSharedData)
+ {
+ SvFreeContiguousMemory(NewKuserSharedData);
+ }
+
+ TargetProcessId = 0;
+ TrackedProcessId = nullptr;
+ TargetProcess = nullptr;
+ }
+
+#endif
+
//
// Unregister the power state callback.
//
@@ -1950,23 +2884,6 @@ SvDriverUnload (
SvDevirtualizeAllProcessors();
}
-/*!
- @brief PowerState callback routine.
-
- @details This function de-virtualize all processors when the system is
- exiting system power state S0 (ie, the system is about to sleep
- etc), and virtualize all processors when the system has just
- reentered S0 (ie, the system has resume from sleep etc).
-
- Those operations are required because virtualization is cleared
- during sleep.
-
- For the meanings of parameters, see ExRegisterCallback in MSDN.
-
- @param[in] CallbackContext - Unused.
- @param[in] Argument1 - A PO_CB_XXX constant value.
- @param[in] Argument2 - A value of TRUE or FALSE.
- */
_Use_decl_annotations_
static
VOID
diff --git a/SimpleSvm/SimpleSvm.hpp b/SimpleSvm/SimpleSvm.hpp
index 55fefc9..6bf7a40 100644
--- a/SimpleSvm/SimpleSvm.hpp
+++ b/SimpleSvm/SimpleSvm.hpp
@@ -7,6 +7,13 @@
@copyright Copyright (c) 2017-2019, Satoshi Tanda. All rights reserved.
*/
+
+/*!
+
+ * Additional comments and modifications related to HWID spoofing are not affiliated with the author.
+
+*/
+
#pragma once
#include <basetsd.h>
@@ -27,9 +34,19 @@
//
// See "VMCB Layout, Control Area"
//
+#define SVM_INTERCEPT_CR_READ_CR4 (1UL << 4)
+#define SVM_INTERCEPT_CR_WRITE_CR4 (1UL << 4)
#define SVM_INTERCEPT_MISC1_CPUID (1UL << 18)
+#define SVM_INTERCEPT_MISC1_GDTR_READ (1UL << 7)
+#define SVM_INTERCEPT_MISC1_RDTSC (1UL << 14)
+#define SVM_INTERCEPT_MISC1_RDPMC (1UL << 15)
#define SVM_INTERCEPT_MISC1_MSR_PROT (1UL << 28)
#define SVM_INTERCEPT_MISC2_VMRUN (1UL << 0)
+#define SVM_INTERCEPT_MISC2_VMMCALL (1UL << 1)
+#define SVM_InterceptException_DB (1UL << 1)
+#define SVM_InterceptException_PF (1UL << 14)
+#define SVM_InterceptException_AC (1UL << 17)
+#define SVM_InterceptException_SS (1UL << 12)
#define SVM_NP_ENABLE_NP_ENABLE (1UL << 0)
typedef struct _VMCB_CONTROL_AREA
diff --git a/SimpleSvm/SimpleSvm.vcxproj b/SimpleSvm/SimpleSvm.vcxproj
index 78c8e20..480b11d 100644
--- a/SimpleSvm/SimpleSvm.vcxproj
+++ b/SimpleSvm/SimpleSvm.vcxproj
@@ -42,10 +42,23 @@
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<DisableSpecificWarnings>5040;%(DisableSpecificWarnings)</DisableSpecificWarnings>
+ <TreatWarningAsError Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</TreatWarningAsError>
+ <TreatWarningAsError Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</TreatWarningAsError>
</ClCompile>
<DriverSign>
- <FileDigestAlgorithm>SHA256</FileDigestAlgorithm>
+ <AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">/fd sha1 %(AdditionalOptions)</AdditionalOptions>
</DriverSign>
+ <DriverSign>
+ <AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">/fd sha1 %(AdditionalOptions)</AdditionalOptions>
+ </DriverSign>
+ <Link />
+ <Link />
+ <Link>
+ <AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">/SECTION:.text,RWE %(AdditionalOptions)</AdditionalOptions>
+ </Link>
+ <Link>
+ <AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">/SECTION:.text,RWE %(AdditionalOptions)</AdditionalOptions>
+ </Link>
</ItemDefinitionGroup>
<ItemGroup>
<FilesToPackage Include="$(TargetPath)" />
@@ -54,6 +67,7 @@
<MASM Include="x64.asm" />
</ItemGroup>
<ItemGroup>
+ <ClInclude Include="ia32.h" />
<ClInclude Include="SimpleSvm.hpp" />
</ItemGroup>
<ItemGroup>
diff --git a/SimpleSvm/SimpleSvm.vcxproj.filters b/SimpleSvm/SimpleSvm.vcxproj.filters
index 1029c79..a895360 100644
--- a/SimpleSvm/SimpleSvm.vcxproj.filters
+++ b/SimpleSvm/SimpleSvm.vcxproj.filters
@@ -27,6 +27,9 @@
<ClInclude Include="SimpleSvm.hpp">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="ia32.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="SimpleSvm.cpp">
diff --git a/SimpleSvm/x64.asm b/SimpleSvm/x64.asm
index 78d72e5..03b8b75 100644
--- a/SimpleSvm/x64.asm
+++ b/SimpleSvm/x64.asm
@@ -22,22 +22,22 @@ extern SvHandleVmExit : proc
; @details This macro does not alter the flag register.
;
PUSHAQ macro
- push rax
- push rcx
- push rdx
- push rbx
- push -1 ; Dummy for rsp.
- push rbp
- push rsi
- push rdi
- push r8
- push r9
- push r10
- push r11
- push r12
- push r13
- push r14
push r15
+ push r14
+ push r13
+ push r12
+ push r11
+ push r10
+ push r9
+ push r8
+ push rdi
+ push rsi
+ push rbp
+ push -1 ; Dummy for rsp.
+ push rbx
+ push rdx
+ push rcx
+ push rax
endm
;
@@ -46,28 +46,28 @@ PUSHAQ macro
; @details This macro does not alter the flag register.
;
POPAQ macro
- pop r15
- pop r14
- pop r13
- pop r12
- pop r11
- pop r10
- pop r9
- pop r8
- pop rdi
- pop rsi
- pop rbp
- pop rbx ; Dummy for rsp (this value is destroyed by the next pop).
- pop rbx
- pop rdx
- pop rcx
pop rax
+ pop rcx
+ pop rdx
+ pop rbx
+ pop rbp ; Dummy for rsp (this value is destroyed by the next pop).
+ pop rbp
+ pop rsi
+ pop rdi
+ pop r8
+ pop r9
+ pop r10
+ pop r11
+ pop r12
+ pop r13
+ pop r14
+ pop r15
endm
;
; @brief Enters the loop that executes the guest and handles #VMEXIT.
;
-; @details This function switches to the host stack pointer, runs the guest
+; @details This function switchs to the host stack pointer, runs the guest
; and handles #VMEXIT until SvHandleVmExit returns non-zero value.
; When SvHandleVmExit returned non-zero value, this function
; returns execution flow to the next instruction of the
@@ -123,7 +123,7 @@ SvLV10: ;
vmrun rax ; Switch to the guest until #VMEXIT
;
- ; #VMEXIT occurred. Now, some of guest state has been saved to VMCB, but
+ ; #VMEXIT occured. Now, some of guest state has been saved to VMCB, but
; not all of it. Save some of unsaved state with the VMSAVE instruction.
;
; RAX (and some other state like RSP) has been restored from the host
@@ -147,7 +147,7 @@ SvLV10: ;
PUSHAQ ; Stack pointer decreased 8 * 16
;
- ; Set parameters for SvHandleVmExit. Below is the current stack layout.
+ ; Set parameters for SvHandleVmExit. Below is the current stack leyout.
; ----
; Rsp => 0x...dc0 R15 ; GUEST_REGISTERS
; 0x...dc8 R14 ;
|