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
|
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2000-2009 Darklegion Development
This file is part of Tremulous.
Tremulous is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Tremulous is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tremulous; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// vm_x86.c -- load time compiler and execution environment for x86
#include "vm_local.h"
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __FreeBSD__
#include <sys/types.h>
#endif
#ifndef _WIN32
#include <sys/mman.h> // for PROT_ stuff
#endif
/* need this on NX enabled systems (i386 with PAE kernel or
* noexec32=on x86_64) */
#if defined(__linux__) || defined(__FreeBSD__)
#define VM_X86_MMAP
#endif
static void VM_Destroy_Compiled(vm_t* self);
/*
eax scratch
ebx/bl opStack offset
ecx scratch (required for shifts)
edx scratch (required for divisions)
esi program stack
edi opStack base
x86_64:
r8 vm->instructionPointers
r9 vm->dataBase
*/
#define VMFREE_BUFFERS() do {Z_Free(buf); Z_Free(jused);} while(0)
static byte *buf = NULL;
static byte *jused = NULL;
static int jusedSize = 0;
static int compiledOfs = 0;
static byte *code = NULL;
static int pc = 0;
#define FTOL_PTR
static int instruction, pass;
static int lastConst = 0;
static int oc0, oc1, pop0, pop1;
static int jlabel;
typedef enum
{
LAST_COMMAND_NONE = 0,
LAST_COMMAND_MOV_STACK_EAX,
LAST_COMMAND_SUB_BL_1,
LAST_COMMAND_SUB_BL_2,
} ELastCommand;
typedef enum
{
VM_JMP_VIOLATION = 0,
VM_BLOCK_COPY = 1
} ESysCallType;
static ELastCommand LastCommand;
static int iss8(int32_t v)
{
return (SCHAR_MIN <= v && v <= SCHAR_MAX);
}
#if 0
static int isu8(uint32_t v)
{
return (v <= UCHAR_MAX);
}
#endif
static int NextConstant4(void)
{
return (code[pc] | (code[pc+1]<<8) | (code[pc+2]<<16) | (code[pc+3]<<24));
}
static int Constant4( void ) {
int v;
v = NextConstant4();
pc += 4;
return v;
}
static int Constant1( void ) {
int v;
v = code[pc];
pc += 1;
return v;
}
static void Emit1( int v )
{
buf[ compiledOfs ] = v;
compiledOfs++;
LastCommand = LAST_COMMAND_NONE;
}
static void Emit2(int v)
{
Emit1(v & 255);
Emit1((v >> 8) & 255);
}
static void Emit4(int v)
{
Emit1(v & 0xFF);
Emit1((v >> 8) & 0xFF);
Emit1((v >> 16) & 0xFF);
Emit1((v >> 24) & 0xFF);
}
static void EmitPtr(void *ptr)
{
intptr_t v = (intptr_t) ptr;
Emit4(v);
#if idx64
Emit1((v >> 32) & 0xFF);
Emit1((v >> 40) & 0xFF);
Emit1((v >> 48) & 0xFF);
Emit1((v >> 56) & 0xFF);
#endif
}
static int Hex( int c ) {
if ( c >= 'a' && c <= 'f' ) {
return 10 + c - 'a';
}
if ( c >= 'A' && c <= 'F' ) {
return 10 + c - 'A';
}
if ( c >= '0' && c <= '9' ) {
return c - '0';
}
VMFREE_BUFFERS();
Com_Error( ERR_DROP, "Hex: bad char '%c'", c );
return 0;
}
static void EmitString( const char *string ) {
int c1, c2;
int v;
while ( 1 ) {
c1 = string[0];
c2 = string[1];
v = ( Hex( c1 ) << 4 ) | Hex( c2 );
Emit1( v );
if ( !string[2] ) {
break;
}
string += 3;
}
}
static void EmitRexString(byte rex, const char *string)
{
#if idx64
if(rex)
Emit1(rex);
#endif
EmitString(string);
}
#define MASK_REG(modrm, mask) \
EmitString("81"); \
EmitString((modrm)); \
Emit4((mask))
// add bl, bytes
#define STACK_PUSH(bytes) \
EmitString("80 C3"); \
Emit1(bytes)
// sub bl, bytes
#define STACK_POP(bytes) \
EmitString("80 EB"); \
Emit1(bytes)
static void EmitCommand(ELastCommand command)
{
switch(command)
{
case LAST_COMMAND_MOV_STACK_EAX:
EmitString("89 04 9F"); // mov dword ptr [edi + ebx * 4], eax
break;
case LAST_COMMAND_SUB_BL_1:
STACK_POP(1); // sub bl, 1
break;
case LAST_COMMAND_SUB_BL_2:
STACK_POP(2); // sub bl, 2
break;
default:
break;
}
LastCommand = command;
}
static void EmitPushStack(vm_t *vm)
{
if (!jlabel)
{
if(LastCommand == LAST_COMMAND_SUB_BL_1)
{ // sub bl, 1
compiledOfs -= 3;
vm->instructionPointers[instruction - 1] = compiledOfs;
return;
}
if(LastCommand == LAST_COMMAND_SUB_BL_2)
{ // sub bl, 2
compiledOfs -= 3;
vm->instructionPointers[instruction - 1] = compiledOfs;
STACK_POP(1); // sub bl, 1
return;
}
}
STACK_PUSH(1); // add bl, 1
}
static void EmitMovEAXStack(vm_t *vm, int andit)
{
if(!jlabel)
{
if(LastCommand == LAST_COMMAND_MOV_STACK_EAX)
{ // mov [edi + ebx * 4], eax
compiledOfs -= 3;
vm->instructionPointers[instruction - 1] = compiledOfs;
}
else if(pop1 == OP_CONST && buf[compiledOfs-7] == 0xC7 && buf[compiledOfs-6] == 0x04 && buf[compiledOfs - 5] == 0x9F)
{ // mov [edi + ebx * 4], 0x12345678
compiledOfs -= 7;
vm->instructionPointers[instruction - 1] = compiledOfs;
EmitString("B8"); // mov eax, 0x12345678
if(andit)
Emit4(lastConst & andit);
else
Emit4(lastConst);
return;
}
else if(pop1 != OP_DIVI && pop1 != OP_DIVU && pop1 != OP_MULI && pop1 != OP_MULU &&
pop1 != OP_STORE4 && pop1 != OP_STORE2 && pop1 != OP_STORE1)
{
EmitString("8B 04 9F"); // mov eax, dword ptr [edi + ebx * 4]
}
}
else
EmitString("8B 04 9F"); // mov eax, dword ptr [edi + ebx * 4]
if(andit)
{
EmitString("25"); // and eax, 0x12345678
Emit4(andit);
}
}
void EmitMovECXStack(vm_t *vm)
{
if(!jlabel)
{
if(LastCommand == LAST_COMMAND_MOV_STACK_EAX) // mov [edi + ebx * 4], eax
{
compiledOfs -= 3;
vm->instructionPointers[instruction - 1] = compiledOfs;
EmitString("89 C1"); // mov ecx, eax
return;
}
if(pop1 == OP_DIVI || pop1 == OP_DIVU || pop1 == OP_MULI || pop1 == OP_MULU ||
pop1 == OP_STORE4 || pop1 == OP_STORE2 || pop1 == OP_STORE1)
{
EmitString("89 C1"); // mov ecx, eax
return;
}
}
EmitString("8B 0C 9F"); // mov ecx, dword ptr [edi + ebx * 4]
}
void EmitMovEDXStack(vm_t *vm, int andit)
{
if(!jlabel)
{
if(LastCommand == LAST_COMMAND_MOV_STACK_EAX)
{ // mov dword ptr [edi + ebx * 4], eax
compiledOfs -= 3;
vm->instructionPointers[instruction - 1] = compiledOfs;
EmitString("8B D0"); // mov edx, eax
}
else if(pop1 == OP_DIVI || pop1 == OP_DIVU || pop1 == OP_MULI || pop1 == OP_MULU ||
pop1 == OP_STORE4 || pop1 == OP_STORE2 || pop1 == OP_STORE1)
{
EmitString("8B D0"); // mov edx, eax
}
else if(pop1 == OP_CONST && buf[compiledOfs-7] == 0xC7 && buf[compiledOfs-6] == 0x07 && buf[compiledOfs - 5] == 0x9F)
{ // mov dword ptr [edi + ebx * 4], 0x12345678
compiledOfs -= 7;
vm->instructionPointers[instruction - 1] = compiledOfs;
EmitString("BA"); // mov edx, 0x12345678
if(andit)
Emit4(lastConst & andit);
else
Emit4(lastConst);
return;
}
else
EmitString("8B 14 9F"); // mov edx, dword ptr [edi + ebx * 4]
}
else
EmitString("8B 14 9F"); // mov edx, dword ptr [edi + ebx * 4]
if(andit)
MASK_REG("E2", andit); // and edx, 0x12345678
}
#define JUSED(x) \
do { \
if (x < 0 || x >= vm->instructionCount) { \
VMFREE_BUFFERS(); \
Com_Error( ERR_DROP, \
"VM_CompileX86: jump target out of range at offset %d", pc ); \
} \
jused[x] = 1; \
} while(0)
#define SET_JMPOFS(x) do { buf[(x)] = compiledOfs - ((x) + 1); } while(0)
/*
=================
ErrJump
Error handler for jump/call to invalid instruction number
=================
*/
static void ErrJump(void)
{
Com_Error(ERR_DROP, "program tried to execute code outside VM");
exit(1);
}
/*
=================
DoSyscall
Uses asm to retrieve arguments from registers to work around different calling conventions
=================
*/
#if defined(_MSC_VER) && idx64
extern void qsyscall64(void);
extern uint8_t qvmcall64(int *programStack, int *opStack, intptr_t *instructionPointers, byte *dataBase);
// Microsoft does not support inline assembler on x64 platforms. Meh.
void DoSyscall(int syscallNum, int programStack, int *opStackBase, uint8_t opStackOfs, intptr_t arg)
{
#else
static void DoSyscall(void)
{
int syscallNum;
int programStack;
int *opStackBase;
uint8_t opStackOfs;
intptr_t arg;
#endif
vm_t *savedVM;
#if defined(_MSC_VER)
#if !idx64
__asm
{
mov dword ptr syscallNum, eax
mov dword ptr programStack, esi
mov byte ptr opStackOfs, bl
mov dword ptr opStackBase, edi
mov dword ptr arg, ecx
}
#endif
#else
__asm__ volatile(
""
: "=a" (syscallNum), "=S" (programStack), "=D" (opStackBase), "=b" (opStackOfs),
"=c" (arg)
);
#endif
// save currentVM so as to allow for recursive VM entry
savedVM = currentVM;
// modify VM stack pointer for recursive VM entry
currentVM->programStack = programStack - 4;
if(syscallNum < 0)
{
int *data;
#if idx64
int index;
intptr_t args[11];
#endif
data = (int *) (savedVM->dataBase + programStack + 4);
#if idx64
args[0] = ~syscallNum;
for(index = 1; index < ARRAY_LEN(args); index++)
args[index] = data[index];
opStackBase[opStackOfs + 1] = savedVM->systemCall(args);
#else
data[0] = ~syscallNum;
opStackBase[opStackOfs + 1] = savedVM->systemCall(data);
#endif
}
else
{
switch(syscallNum)
{
case VM_JMP_VIOLATION:
ErrJump();
break;
case VM_BLOCK_COPY:
if(opStackOfs < 1)
Com_Error(ERR_DROP, "VM_BLOCK_COPY failed due to corrupted opStack");
VM_BlockCopy(opStackBase[(opStackOfs - 1)], opStackBase[opStackOfs], arg);
break;
default:
Com_Error(ERR_DROP, "Unknown VM operation %d", syscallNum);
break;
}
}
currentVM = savedVM;
}
/*
=================
EmitCallRel
Relative call to vm->codeBase + callOfs
=================
*/
void EmitCallRel(vm_t *vm, int callOfs)
{
EmitString("E8"); // call 0x12345678
Emit4(callOfs - compiledOfs - 4);
}
/*
=================
EmitCallDoSyscall
Call to DoSyscall()
=================
*/
int EmitCallDoSyscall(vm_t *vm)
{
// use edx register to store DoSyscall address
#if defined(_MSC_VER) && idx64
EmitRexString(0x48, "BA"); // mov edx, qsyscall64
EmitPtr(qsyscall64);
#else
EmitRexString(0x48, "BA"); // mov edx, DoSyscall
EmitPtr(DoSyscall);
#endif
// Push important registers to stack as we can't really make
// any assumptions about calling conventions.
EmitString("51"); // push ebx
EmitString("56"); // push esi
EmitString("57"); // push edi
#if idx64
EmitRexString(0x41, "50"); // push r8
EmitRexString(0x41, "51"); // push r9
#endif
// align the stack pointer to a 16-byte-boundary
EmitString("55"); // push ebp
EmitRexString(0x48, "89 E5"); // mov ebp, esp
EmitRexString(0x48, "83 E4 F0"); // and esp, 0xFFFFFFF0
// call the syscall wrapper function DoSyscall()
EmitString("FF D2"); // call edx
// reset the stack pointer to its previous value
EmitRexString(0x48, "89 EC"); // mov esp, ebp
EmitString("5D"); // pop ebp
#if idx64
EmitRexString(0x41, "59"); // pop r9
EmitRexString(0x41, "58"); // pop r8
#endif
EmitString("5F"); // pop edi
EmitString("5E"); // pop esi
EmitString("59"); // pop ebx
EmitString("C3"); // ret
return compiledOfs;
}
/*
=================
EmitCallErrJump
Emit the code that triggers execution of the jump violation handler
=================
*/
static void EmitCallErrJump(vm_t *vm, int sysCallOfs)
{
EmitString("B8"); // mov eax, 0x12345678
Emit4(VM_JMP_VIOLATION);
EmitCallRel(vm, sysCallOfs);
}
/*
=================
EmitCallProcedure
VM OP_CALL procedure for call destinations obtained at runtime
=================
*/
int EmitCallProcedure(vm_t *vm, int sysCallOfs)
{
int jmpSystemCall, jmpBadAddr;
int retval;
EmitString("8B 04 9F"); // mov eax, dword ptr [edi + ebx * 4]
STACK_POP(1); // sub bl, 1
EmitString("85 C0"); // test eax, eax
// Jump to syscall code, 1 byte offset should suffice
EmitString("7C"); // jl systemCall
jmpSystemCall = compiledOfs++;
/************ Call inside VM ************/
EmitString("81 F8"); // cmp eax, vm->instructionCount
Emit4(vm->instructionCount);
// Error jump if invalid jump target
EmitString("73"); // jae badAddr
jmpBadAddr = compiledOfs++;
#if idx64
EmitRexString(0x49, "FF 14 C0"); // call qword ptr [r8 + eax * 8]
#else
EmitString("FF 14 85"); // call dword ptr [vm->instructionPointers + eax * 4]
Emit4((intptr_t) vm->instructionPointers);
#endif
EmitString("8B 04 9F"); // mov eax, dword ptr [edi + ebx * 4]
EmitString("C3"); // ret
// badAddr:
SET_JMPOFS(jmpBadAddr);
EmitCallErrJump(vm, sysCallOfs);
/************ System Call ************/
// systemCall:
SET_JMPOFS(jmpSystemCall);
retval = compiledOfs;
EmitCallRel(vm, sysCallOfs);
// have opStack reg point at return value
STACK_PUSH(1); // add bl, 1
EmitString("C3"); // ret
return retval;
}
/*
=================
EmitJumpIns
Jump to constant instruction number
=================
*/
void EmitJumpIns(vm_t *vm, const char *jmpop, int cdest)
{
JUSED(cdest);
EmitString(jmpop); // j??? 0x12345678
// we only know all the jump addresses in the third pass
if(pass == 2)
Emit4(vm->instructionPointers[cdest] - compiledOfs - 4);
else
compiledOfs += 4;
}
/*
=================
EmitCallIns
Call to constant instruction number
=================
*/
void EmitCallIns(vm_t *vm, int cdest)
{
JUSED(cdest);
EmitString("E8"); // call 0x12345678
// we only know all the jump addresses in the third pass
if(pass == 2)
Emit4(vm->instructionPointers[cdest] - compiledOfs - 4);
else
compiledOfs += 4;
}
/*
=================
EmitCallConst
Call to constant instruction number or syscall
=================
*/
void EmitCallConst(vm_t *vm, int cdest, int callProcOfsSyscall)
{
if(cdest < 0)
{
EmitString("B8"); // mov eax, cdest
Emit4(cdest);
EmitCallRel(vm, callProcOfsSyscall);
}
else
EmitCallIns(vm, cdest);
}
/*
=================
EmitBranchConditions
Emits x86 branch condition as given in op
=================
*/
void EmitBranchConditions(vm_t *vm, int op)
{
switch(op)
{
case OP_EQ:
EmitJumpIns(vm, "0F 84", Constant4()); // je 0x12345678
break;
case OP_NE:
EmitJumpIns(vm, "0F 85", Constant4()); // jne 0x12345678
break;
case OP_LTI:
EmitJumpIns(vm, "0F 8C", Constant4()); // jl 0x12345678
break;
case OP_LEI:
EmitJumpIns(vm, "0F 8E", Constant4()); // jle 0x12345678
break;
case OP_GTI:
EmitJumpIns(vm, "0F 8F", Constant4()); // jg 0x12345678
break;
case OP_GEI:
EmitJumpIns(vm, "0F 8D", Constant4()); // jge 0x12345678
break;
case OP_LTU:
EmitJumpIns(vm, "0F 82", Constant4()); // jb 0x12345678
break;
case OP_LEU:
EmitJumpIns(vm, "0F 86", Constant4()); // jbe 0x12345678
break;
case OP_GTU:
EmitJumpIns(vm, "0F 87", Constant4()); // ja 0x12345678
break;
case OP_GEU:
EmitJumpIns(vm, "0F 83", Constant4()); // jae 0x12345678
break;
}
}
/*
=================
ConstOptimize
Constant values for immediately following instructions may be translated to immediate values
instead of opStack operations, which will save expensive operations on memory
=================
*/
qboolean ConstOptimize(vm_t *vm, int callProcOfsSyscall)
{
int v;
int op1;
// we can safely perform optimizations only in case if
// we are 100% sure that next instruction is not a jump label
if (vm->jumpTableTargets && !jused[instruction])
op1 = code[pc+4];
else
return qfalse;
switch ( op1 ) {
case OP_LOAD4:
EmitPushStack(vm);
#if idx64
EmitRexString(0x41, "8B 81"); // mov eax, dword ptr [r9 + 0x12345678]
Emit4(Constant4() & vm->dataMask);
#else
EmitString("B8"); // mov eax, 0x12345678
EmitPtr(vm->dataBase + (Constant4() & vm->dataMask));
EmitString("8B 00"); // mov eax, dword ptr [eax]
#endif
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
pc++; // OP_LOAD4
instruction += 1;
return qtrue;
case OP_LOAD2:
EmitPushStack(vm);
#if idx64
EmitRexString(0x41, "0F B7 81"); // movzx eax, word ptr [r9 + 0x12345678]
Emit4(Constant4() & vm->dataMask);
#else
EmitString("B8"); // mov eax, 0x12345678
EmitPtr(vm->dataBase + (Constant4() & vm->dataMask));
EmitString("0F B7 00"); // movzx eax, word ptr [eax]
#endif
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
pc++; // OP_LOAD2
instruction += 1;
return qtrue;
case OP_LOAD1:
EmitPushStack(vm);
#if idx64
EmitRexString(0x41, "0F B6 81"); // movzx eax, byte ptr [r9 + 0x12345678]
Emit4(Constant4() & vm->dataMask);
#else
EmitString("B8"); // mov eax, 0x12345678
EmitPtr(vm->dataBase + (Constant4() & vm->dataMask));
EmitString("0F B6 00"); // movzx eax, byte ptr [eax]
#endif
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
pc++; // OP_LOAD1
instruction += 1;
return qtrue;
case OP_STORE4:
EmitMovEAXStack(vm, (vm->dataMask & ~3));
#if idx64
EmitRexString(0x41, "C7 04 01"); // mov dword ptr [r9 + eax], 0x12345678
Emit4(Constant4());
#else
EmitString("C7 80"); // mov dword ptr [eax + 0x12345678], 0x12345678
Emit4((intptr_t) vm->dataBase);
Emit4(Constant4());
#endif
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
pc++; // OP_STORE4
instruction += 1;
return qtrue;
case OP_STORE2:
EmitMovEAXStack(vm, (vm->dataMask & ~1));
#if idx64
Emit1(0x66); // mov word ptr [r9 + eax], 0x1234
EmitRexString(0x41, "C7 04 01");
Emit2(Constant4());
#else
EmitString("66 C7 80"); // mov word ptr [eax + 0x12345678], 0x1234
Emit4((intptr_t) vm->dataBase);
Emit2(Constant4());
#endif
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
pc++; // OP_STORE2
instruction += 1;
return qtrue;
case OP_STORE1:
EmitMovEAXStack(vm, vm->dataMask);
#if idx64
EmitRexString(0x41, "C6 04 01"); // mov byte [r9 + eax], 0x12
Emit1(Constant4());
#else
EmitString("C6 80"); // mov byte ptr [eax + 0x12345678], 0x12
Emit4((intptr_t) vm->dataBase);
Emit1(Constant4());
#endif
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
pc++; // OP_STORE1
instruction += 1;
return qtrue;
case OP_ADD:
v = Constant4();
EmitMovEAXStack(vm, 0);
if(iss8(v))
{
EmitString("83 C0"); // add eax, 0x7F
Emit1(v);
}
else
{
EmitString("05"); // add eax, 0x12345678
Emit4(v);
}
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
pc++; // OP_ADD
instruction += 1;
return qtrue;
case OP_SUB:
v = Constant4();
EmitMovEAXStack(vm, 0);
if(iss8(v))
{
EmitString("83 E8"); // sub eax, 0x7F
Emit1(v);
}
else
{
EmitString("2D"); // sub eax, 0x12345678
Emit4(v);
}
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
pc++; // OP_SUB
instruction += 1;
return qtrue;
case OP_MULI:
v = Constant4();
EmitMovEAXStack(vm, 0);
if(iss8(v))
{
EmitString("6B C0"); // imul eax, 0x7F
Emit1(v);
}
else
{
EmitString("69 C0"); // imul eax, 0x12345678
Emit4(v);
}
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
pc++; // OP_MULI
instruction += 1;
return qtrue;
case OP_LSH:
v = NextConstant4();
if(v < 0 || v > 31)
break;
EmitMovEAXStack(vm, 0);
EmitString("C1 E0"); // shl eax, 0x12
Emit1(v);
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
pc += 5; // CONST + OP_LSH
instruction += 1;
return qtrue;
case OP_RSHI:
v = NextConstant4();
if(v < 0 || v > 31)
break;
EmitMovEAXStack(vm, 0);
EmitString("C1 F8"); // sar eax, 0x12
Emit1(v);
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
pc += 5; // CONST + OP_RSHI
instruction += 1;
return qtrue;
case OP_RSHU:
v = NextConstant4();
if(v < 0 || v > 31)
break;
EmitMovEAXStack(vm, 0);
EmitString("C1 E8"); // shr eax, 0x12
Emit1(v);
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
pc += 5; // CONST + OP_RSHU
instruction += 1;
return qtrue;
case OP_BAND:
v = Constant4();
EmitMovEAXStack(vm, 0);
if(iss8(v))
{
EmitString("83 E0"); // and eax, 0x7F
Emit1(v);
}
else
{
EmitString("25"); // and eax, 0x12345678
Emit4(v);
}
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
pc += 1; // OP_BAND
instruction += 1;
return qtrue;
case OP_BOR:
v = Constant4();
EmitMovEAXStack(vm, 0);
if(iss8(v))
{
EmitString("83 C8"); // or eax, 0x7F
Emit1(v);
}
else
{
EmitString("0D"); // or eax, 0x12345678
Emit4(v);
}
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
pc += 1; // OP_BOR
instruction += 1;
return qtrue;
case OP_BXOR:
v = Constant4();
EmitMovEAXStack(vm, 0);
if(iss8(v))
{
EmitString("83 F0"); // xor eax, 0x7F
Emit1(v);
}
else
{
EmitString("35"); // xor eax, 0x12345678
Emit4(v);
}
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
pc += 1; // OP_BXOR
instruction += 1;
return qtrue;
case OP_EQ:
case OP_NE:
case OP_LTI:
case OP_LEI:
case OP_GTI:
case OP_GEI:
case OP_LTU:
case OP_LEU:
case OP_GTU:
case OP_GEU:
EmitMovEAXStack(vm, 0);
EmitCommand(LAST_COMMAND_SUB_BL_1);
EmitString("3D"); // cmp eax, 0x12345678
Emit4(Constant4());
pc++; // OP_*
EmitBranchConditions(vm, op1);
instruction++;
return qtrue;
case OP_EQF:
case OP_NEF:
if(NextConstant4())
break;
pc += 5; // CONST + OP_EQF|OP_NEF
EmitMovEAXStack(vm, 0);
EmitCommand(LAST_COMMAND_SUB_BL_1);
// floating point hack :)
EmitString("25"); // and eax, 0x7FFFFFFF
Emit4(0x7FFFFFFF);
if(op1 == OP_EQF)
EmitJumpIns(vm, "0F 84", Constant4()); // jz 0x12345678
else
EmitJumpIns(vm, "0F 85", Constant4()); // jnz 0x12345678
instruction += 1;
return qtrue;
case OP_JUMP:
EmitJumpIns(vm, "E9", Constant4()); // jmp 0x12345678
pc += 1; // OP_JUMP
instruction += 1;
return qtrue;
case OP_CALL:
v = Constant4();
EmitCallConst(vm, v, callProcOfsSyscall);
pc += 1; // OP_CALL
instruction += 1;
return qtrue;
default:
break;
}
return qfalse;
}
/*
=================
VM_Compile
=================
*/
void VM_Compile(vm_t *vm, vmHeader_t *header)
{
int op;
int maxLength;
int v;
int i;
int callProcOfsSyscall, callProcOfs, callDoSyscallOfs;
jusedSize = header->instructionCount + 2;
// allocate a very large temp buffer, we will shrink it later
maxLength = header->codeLength * 8 + 64;
buf = Z_Malloc(maxLength);
jused = Z_Malloc(jusedSize);
code = Z_Malloc(header->codeLength+32);
Com_Memset(jused, 0, jusedSize);
Com_Memset(buf, 0, maxLength);
// copy code in larger buffer and put some zeros at the end
// so we can safely look ahead for a few instructions in it
// without a chance to get false-positive because of some garbage bytes
Com_Memset(code, 0, header->codeLength+32);
Com_Memcpy(code, (byte *)header + header->codeOffset, header->codeLength );
// ensure that the optimisation pass knows about all the jump
// table targets
for( i = 0; i < vm->numJumpTableTargets; i++ ) {
jused[ *(int *)(vm->jumpTableTargets + ( i * sizeof( int ) ) ) ] = 1;
}
// Start buffer with x86-VM specific procedures
compiledOfs = 0;
callDoSyscallOfs = compiledOfs;
callProcOfs = EmitCallDoSyscall(vm);
callProcOfsSyscall = EmitCallProcedure(vm, callDoSyscallOfs);
vm->entryOfs = compiledOfs;
for(pass=0; pass < 3; pass++) {
oc0 = -23423;
oc1 = -234354;
pop0 = -43435;
pop1 = -545455;
// translate all instructions
pc = 0;
instruction = 0;
//code = (byte *)header + header->codeOffset;
compiledOfs = vm->entryOfs;
LastCommand = LAST_COMMAND_NONE;
while(instruction < header->instructionCount)
{
if(compiledOfs > maxLength - 16)
{
VMFREE_BUFFERS();
Com_Error(ERR_DROP, "VM_CompileX86: maxLength exceeded");
}
vm->instructionPointers[ instruction ] = compiledOfs;
if ( !vm->jumpTableTargets )
jlabel = 1;
else
jlabel = jused[ instruction ];
instruction++;
if(pc > header->codeLength)
{
VMFREE_BUFFERS();
Com_Error(ERR_DROP, "VM_CompileX86: pc > header->codeLength");
}
op = code[ pc ];
pc++;
switch ( op ) {
case 0:
break;
case OP_BREAK:
EmitString("CC"); // int 3
break;
case OP_ENTER:
EmitString("81 EE"); // sub esi, 0x12345678
Emit4(Constant4());
break;
case OP_CONST:
if(ConstOptimize(vm, callProcOfsSyscall))
break;
EmitPushStack(vm);
EmitString("C7 04 9F"); // mov dword ptr [edi + ebx * 4], 0x12345678
lastConst = Constant4();
Emit4(lastConst);
if(code[pc] == OP_JUMP)
JUSED(lastConst);
break;
case OP_LOCAL:
EmitPushStack(vm);
EmitString("8D 86"); // lea eax, [0x12345678 + esi]
oc0 = oc1;
oc1 = Constant4();
Emit4(oc1);
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
break;
case OP_ARG:
EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
EmitString("8B D6"); // mov edx, esi
EmitString("81 C2"); // add edx, 0x12345678
Emit4((Constant1() & 0xFF));
MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
#if idx64
EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
#else
EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
Emit4((intptr_t) vm->dataBase);
#endif
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_CALL:
EmitCallRel(vm, callProcOfs);
break;
case OP_PUSH:
EmitPushStack(vm);
break;
case OP_POP:
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_LEAVE:
v = Constant4();
EmitString("81 C6"); // add esi, 0x12345678
Emit4(v);
EmitString("C3"); // ret
break;
case OP_LOAD4:
if (code[pc] == OP_CONST && code[pc+5] == OP_ADD && code[pc+6] == OP_STORE4)
{
if(oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
{
compiledOfs -= 12;
vm->instructionPointers[instruction - 1] = compiledOfs;
}
pc++; // OP_CONST
v = Constant4();
EmitMovEDXStack(vm, vm->dataMask);
if(v == 1 && oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
{
#if idx64
EmitRexString(0x41, "FF 04 11"); // inc dword ptr [r9 + edx]
#else
EmitString("FF 82"); // inc dword ptr [edx + 0x12345678]
Emit4((intptr_t) vm->dataBase);
#endif
}
else
{
#if idx64
EmitRexString(0x41, "8B 04 11"); // mov eax, dword ptr [r9 + edx]
#else
EmitString("8B 82"); // mov eax, dword ptr [edx + 0x12345678]
Emit4((intptr_t) vm->dataBase);
#endif
EmitString("05"); // add eax, v
Emit4(v);
if (oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
{
#if idx64
EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
#else
EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
Emit4((intptr_t) vm->dataBase);
#endif
}
else
{
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
EmitString("8B 14 9F"); // mov edx, dword ptr [edi + ebx * 4]
MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
#if idx64
EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
#else
EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
Emit4((intptr_t) vm->dataBase);
#endif
}
}
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
pc++; // OP_ADD
pc++; // OP_STORE
instruction += 3;
break;
}
if(code[pc] == OP_CONST && code[pc+5] == OP_SUB && code[pc+6] == OP_STORE4)
{
if(oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
{
compiledOfs -= 12;
vm->instructionPointers[instruction - 1] = compiledOfs;
}
pc++; // OP_CONST
v = Constant4();
EmitMovEDXStack(vm, vm->dataMask);
if(v == 1 && oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
{
#if idx64
EmitRexString(0x41, "FF 0C 11"); // dec dword ptr [r9 + edx]
#else
EmitString("FF 8A"); // dec dword ptr [edx + 0x12345678]
Emit4((intptr_t) vm->dataBase);
#endif
}
else
{
#if idx64
EmitRexString(0x41, "8B 04 11"); // mov eax, dword ptr [r9 + edx]
#else
EmitString("8B 82"); // mov eax, dword ptr [edx + 0x12345678]
Emit4((intptr_t) vm->dataBase);
#endif
EmitString("2D"); // sub eax, v
Emit4(v);
if(oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
{
#if idx64
EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
#else
EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
Emit4((intptr_t) vm->dataBase);
#endif
}
else
{
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
EmitString("8B 14 9F"); // mov edx, dword ptr [edi + ebx * 4]
MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
#if idx64
EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
#else
EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
Emit4((intptr_t) vm->dataBase);
#endif
}
}
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
pc++; // OP_SUB
pc++; // OP_STORE
instruction += 3;
break;
}
if(buf[compiledOfs - 3] == 0x89 && buf[compiledOfs - 2] == 0x04 && buf[compiledOfs - 1] == 0x9F)
{
compiledOfs -= 3;
vm->instructionPointers[instruction - 1] = compiledOfs;
MASK_REG("E0", vm->dataMask); // and eax, 0x12345678
#if idx64
EmitRexString(0x41, "8B 04 01"); // mov eax, dword ptr [r9 + eax]
#else
EmitString("8B 80"); // mov eax, dword ptr [eax + 0x1234567]
Emit4((intptr_t) vm->dataBase);
#endif
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
break;
}
EmitMovEAXStack(vm, vm->dataMask);
#if idx64
EmitRexString(0x41, "8B 04 01"); // mov eax, dword ptr [r9 + eax]
#else
EmitString("8B 80"); // mov eax, dword ptr [eax + 0x12345678]
Emit4((intptr_t) vm->dataBase);
#endif
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
break;
case OP_LOAD2:
EmitMovEAXStack(vm, vm->dataMask);
#if idx64
EmitRexString(0x41, "0F B7 04 01"); // movzx eax, word ptr [r9 + eax]
#else
EmitString("0F B7 80"); // movzx eax, word ptr [eax + 0x12345678]
Emit4((intptr_t) vm->dataBase);
#endif
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
break;
case OP_LOAD1:
EmitMovEAXStack(vm, vm->dataMask);
#if idx64
EmitRexString(0x41, "0F B6 04 01"); // movzx eax, byte ptr [r9 + eax]
#else
EmitString("0F B6 80"); // movzx eax, byte ptr [eax + 0x12345678]
Emit4((intptr_t) vm->dataBase);
#endif
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
break;
case OP_STORE4:
EmitMovEAXStack(vm, 0);
EmitString("8B 54 9F FC"); // mov edx, dword ptr -4[edi + ebx * 4]
MASK_REG("E2", vm->dataMask & ~3); // and edx, 0x12345678
#if idx64
EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
#else
EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
Emit4((intptr_t) vm->dataBase);
#endif
EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
break;
case OP_STORE2:
EmitMovEAXStack(vm, 0);
EmitString("8B 54 9F FC"); // mov edx, dword ptr -4[edi + ebx * 4]
MASK_REG("E2", vm->dataMask & ~1); // and edx, 0x12345678
#if idx64
Emit1(0x66); // mov word ptr [r9 + edx], eax
EmitRexString(0x41, "89 04 11");
#else
EmitString("66 89 82"); // mov word ptr [edx + 0x12345678], eax
Emit4((intptr_t) vm->dataBase);
#endif
EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
break;
case OP_STORE1:
EmitMovEAXStack(vm, 0);
EmitString("8B 54 9F FC"); // mov edx, dword ptr -4[edi + ebx * 4]
MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
#if idx64
EmitRexString(0x41, "88 04 11"); // mov byte ptr [r9 + edx], eax
#else
EmitString("88 82"); // mov byte ptr [edx + 0x12345678], eax
Emit4((intptr_t) vm->dataBase);
#endif
EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
break;
case OP_EQ:
case OP_NE:
case OP_LTI:
case OP_LEI:
case OP_GTI:
case OP_GEI:
case OP_LTU:
case OP_LEU:
case OP_GTU:
case OP_GEU:
EmitMovEAXStack(vm, 0);
EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
EmitString("39 44 9F 04"); // cmp eax, dword ptr 4[edi + ebx * 4]
EmitBranchConditions(vm, op);
break;
case OP_EQF:
case OP_NEF:
case OP_LTF:
case OP_LEF:
case OP_GTF:
case OP_GEF:
EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
EmitString("D9 44 9F 04"); // fld dword ptr 4[edi + ebx * 4]
EmitString("D8 5C 9F 08"); // fcomp dword ptr 8[edi + ebx * 4]
EmitString("DF E0"); // fnstsw ax
switch(op)
{
case OP_EQF:
EmitString("F6 C4 40"); // test ah,0x40
EmitJumpIns(vm, "0F 85", Constant4()); // jne 0x12345678
break;
case OP_NEF:
EmitString("F6 C4 40"); // test ah,0x40
EmitJumpIns(vm, "0F 84", Constant4()); // je 0x12345678
break;
case OP_LTF:
EmitString("F6 C4 01"); // test ah,0x01
EmitJumpIns(vm, "0F 85", Constant4()); // jne 0x12345678
break;
case OP_LEF:
EmitString("F6 C4 41"); // test ah,0x41
EmitJumpIns(vm, "0F 85", Constant4()); // jne 0x12345678
break;
case OP_GTF:
EmitString("F6 C4 41"); // test ah,0x41
EmitJumpIns(vm, "0F 84", Constant4()); // je 0x12345678
break;
case OP_GEF:
EmitString("F6 C4 01"); // test ah,0x01
EmitJumpIns(vm, "0F 84", Constant4()); // je 0x12345678
break;
}
break;
case OP_NEGI:
EmitMovEAXStack(vm, 0);
EmitString("F7 D8"); // neg eax
EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
break;
case OP_ADD:
EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
EmitString("01 44 9F FC"); // add dword ptr -4[edi + ebx * 4], eax
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_SUB:
EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
EmitString("29 44 9F FC"); // sub dword ptr -4[edi + ebx * 4], eax
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_DIVI:
EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
EmitString("99"); // cdq
EmitString("F7 3C 9F"); // idiv dword ptr [edi + ebx * 4]
EmitString("89 44 9F FC"); // mov dword ptr -4[edi + ebx * 4],eax
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_DIVU:
EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
EmitString("33 D2"); // xor edx, edx
EmitString("F7 34 9F"); // div dword ptr [edi + ebx * 4]
EmitString("89 44 9F FC"); // mov dword ptr -4[edi + ebx * 4],eax
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_MODI:
EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
EmitString("99" ); // cdq
EmitString("F7 3C 9F"); // idiv dword ptr [edi + ebx * 4]
EmitString("89 54 9F FC"); // mov dword ptr -4[edi + ebx * 4],edx
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_MODU:
EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
EmitString("33 D2"); // xor edx, edx
EmitString("F7 34 9F"); // div dword ptr [edi + ebx * 4]
EmitString("89 54 9F FC"); // mov dword ptr -4[edi + ebx * 4],edx
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_MULI:
EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
EmitString("F7 2C 9F"); // imul dword ptr [edi + ebx * 4]
EmitString("89 44 9F FC"); // mov dword ptr -4[edi + ebx * 4],eax
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_MULU:
EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
EmitString("F7 24 9F"); // mul dword ptr [edi + ebx * 4]
EmitString("89 44 9F FC"); // mov dword ptr -4[edi + ebx * 4],eax
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_BAND:
EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
EmitString("21 44 9F FC"); // and dword ptr -4[edi + ebx * 4],eax
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_BOR:
EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
EmitString("09 44 9F FC"); // or dword ptr -4[edi + ebx * 4],eax
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_BXOR:
EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
EmitString("31 44 9F FC"); // xor dword ptr -4[edi + ebx * 4],eax
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_BCOM:
EmitString("F7 14 9F"); // not dword ptr [edi + ebx * 4]
break;
case OP_LSH:
EmitMovECXStack(vm);
EmitString("D3 64 9F FC"); // shl dword ptr -4[edi + ebx * 4], cl
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_RSHI:
EmitMovECXStack(vm);
EmitString("D3 7C 9F FC"); // sar dword ptr -4[edi + ebx * 4], cl
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_RSHU:
EmitMovECXStack(vm);
EmitString("D3 6C 9F FC"); // shr dword ptr -4[edi + ebx * 4], cl
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_NEGF:
EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
EmitString("D9 E0"); // fchs
EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
break;
case OP_ADDF:
EmitString("D9 44 9F FC"); // fld dword ptr -4[edi + ebx * 4]
EmitString("D8 04 9F"); // fadd dword ptr [edi + ebx * 4]
EmitString("D9 5C 9F FC"); // fstp dword ptr -4[edi + ebx * 4]
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
break;
case OP_SUBF:
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
EmitString("D8 64 9F 04"); // fsub dword ptr 4[edi + ebx * 4]
EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
break;
case OP_DIVF:
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
EmitString("D8 74 9F 04"); // fdiv dword ptr 4[edi + ebx * 4]
EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
break;
case OP_MULF:
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
EmitString("D8 4C 9F 04"); // fmul dword ptr 4[edi + ebx * 4]
EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
break;
case OP_CVIF:
EmitString("DB 04 9F"); // fild dword ptr [edi + ebx * 4]
EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
break;
case OP_CVFI:
#ifndef FTOL_PTR // WHENHELLISFROZENOVER
// not IEEE complient, but simple and fast
EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
EmitString("DB 1C 9F"); // fistp dword ptr [edi + ebx * 4]
#else // FTOL_PTR
// call the library conversion function
EmitRexString(0x48, "BA"); // mov edx, Q_VMftol
EmitPtr(Q_VMftol);
EmitRexString(0x48, "FF D2"); // call edx
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
#endif
break;
case OP_SEX8:
EmitString("0F BE 04 9F"); // movsx eax, byte ptr [edi + ebx * 4]
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
break;
case OP_SEX16:
EmitString("0F BF 04 9F"); // movsx eax, word ptr [edi + ebx * 4]
EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
break;
case OP_BLOCK_COPY:
EmitString("B8"); // mov eax, 0x12345678
Emit4(VM_BLOCK_COPY);
EmitString("B9"); // mov ecx, 0x12345678
Emit4(Constant4());
EmitCallRel(vm, callDoSyscallOfs);
EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
break;
case OP_JUMP:
EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
EmitString("8B 44 9F 04"); // mov eax, dword ptr 4[edi + ebx * 4]
EmitString("81 F8"); // cmp eax, vm->instructionCount
Emit4(vm->instructionCount);
#if idx64
EmitString("73 04"); // jae +4
EmitRexString(0x49, "FF 24 C0"); // jmp qword ptr [r8 + eax * 8]
#else
EmitString("73 07"); // jae +7
EmitString("FF 24 85"); // jmp dword ptr [instructionPointers + eax * 4]
Emit4((intptr_t) vm->instructionPointers);
#endif
EmitCallErrJump(vm, callDoSyscallOfs);
break;
default:
VMFREE_BUFFERS();
Com_Error(ERR_DROP, "VM_CompileX86: bad opcode %i at offset %i", op, pc);
}
pop0 = pop1;
pop1 = op;
}
}
// copy to an exact sized buffer with the appropriate permission bits
vm->codeLength = compiledOfs;
#ifdef VM_X86_MMAP
vm->codeBase = mmap(NULL, compiledOfs, PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if(vm->codeBase == MAP_FAILED)
Com_Error(ERR_FATAL, "VM_CompileX86: can't mmap memory");
#elif _WIN32
// allocate memory with EXECUTE permissions under windows.
vm->codeBase = VirtualAlloc(NULL, compiledOfs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if(!vm->codeBase)
Com_Error(ERR_FATAL, "VM_CompileX86: VirtualAlloc failed");
#else
vm->codeBase = malloc(compiledOfs);
if(!vm->codeBase)
Com_Error(ERR_FATAL, "VM_CompileX86: malloc failed");
#endif
Com_Memcpy( vm->codeBase, buf, compiledOfs );
#ifdef VM_X86_MMAP
if(mprotect(vm->codeBase, compiledOfs, PROT_READ|PROT_EXEC))
Com_Error(ERR_FATAL, "VM_CompileX86: mprotect failed");
#elif _WIN32
{
DWORD oldProtect = 0;
// remove write permissions.
if(!VirtualProtect(vm->codeBase, compiledOfs, PAGE_EXECUTE_READ, &oldProtect))
Com_Error(ERR_FATAL, "VM_CompileX86: VirtualProtect failed");
}
#endif
Z_Free( code );
Z_Free( buf );
Z_Free( jused );
Com_Printf( "VM file %s compiled to %i bytes of code\n", vm->name, compiledOfs );
vm->destroy = VM_Destroy_Compiled;
// offset all the instruction pointers for the new location
for ( i = 0 ; i < header->instructionCount ; i++ ) {
vm->instructionPointers[i] += (intptr_t) vm->codeBase;
}
}
void VM_Destroy_Compiled(vm_t* self)
{
#ifdef VM_X86_MMAP
munmap(self->codeBase, self->codeLength);
#elif _WIN32
VirtualFree(self->codeBase, 0, MEM_RELEASE);
#else
free(self->codeBase);
#endif
}
/*
==============
VM_CallCompiled
This function is called directly by the generated code
==============
*/
int VM_CallCompiled(vm_t *vm, int *args)
{
byte stack[OPSTACK_SIZE + 15];
void *entryPoint;
int programCounter;
int programStack, stackOnEntry;
byte *image;
int *opStack;
int opStackOfs;
currentVM = vm;
// interpret the code
vm->currentlyInterpreting = qtrue;
// we might be called recursively, so this might not be the very top
programStack = stackOnEntry = vm->programStack;
// set up the stack frame
image = vm->dataBase;
programCounter = 0;
programStack -= 48;
*(int *)&image[ programStack + 44] = args[9];
*(int *)&image[ programStack + 40] = args[8];
*(int *)&image[ programStack + 36] = args[7];
*(int *)&image[ programStack + 32] = args[6];
*(int *)&image[ programStack + 28] = args[5];
*(int *)&image[ programStack + 24] = args[4];
*(int *)&image[ programStack + 20] = args[3];
*(int *)&image[ programStack + 16] = args[2];
*(int *)&image[ programStack + 12] = args[1];
*(int *)&image[ programStack + 8 ] = args[0];
*(int *)&image[ programStack + 4 ] = 0; // return stack
*(int *)&image[ programStack ] = -1; // will terminate the loop on return
// off we go into generated code...
entryPoint = vm->codeBase + vm->entryOfs;
opStack = PADP(stack, 16);
*opStack = 0xDEADBEEF;
opStackOfs = 0;
#ifdef _MSC_VER
#if idx64
opStackOfs = qvmcall64(&programStack, opStack, vm->instructionPointers, vm->dataBase);
#else
__asm
{
pushad
mov esi, dword ptr programStack
mov edi, dword ptr opStack
mov ebx, dword ptr opStackOfs
call entryPoint
mov dword ptr opStackOfs, ebx
mov dword ptr opStack, edi
mov dword ptr programStack, esi
popad
}
#endif
#elif idx64
__asm__ volatile(
"movq %5, %%rax\r\n"
"movq %3, %%r8\r\n"
"movq %4, %%r9\r\n"
"push %%r15\r\n"
"push %%r14\r\n"
"push %%r13\r\n"
"push %%r12\r\n"
"callq *%%rax\r\n"
"pop %%r12\r\n"
"pop %%r13\r\n"
"pop %%r14\r\n"
"pop %%r15\r\n"
: "+S" (programStack), "+D" (opStack), "+b" (opStackOfs)
: "g" (vm->instructionPointers), "g" (vm->dataBase), "g" (entryPoint)
: "cc", "memory", "%rax", "%rcx", "%rdx", "%r8", "%r9", "%r10", "%r11"
);
#else
__asm__ volatile(
"calll *%3\r\n"
: "+S" (programStack), "+D" (opStack), "+b" (opStackOfs)
: "g" (entryPoint)
: "cc", "memory", "%eax", "%ecx", "%edx"
);
#endif
if(opStackOfs != 1 || *opStack != 0xDEADBEEF)
{
Com_Error(ERR_DROP, "opStack corrupted in compiled code");
}
if(programStack != stackOnEntry - 48)
Com_Error(ERR_DROP, "programStack corrupted in compiled code");
vm->programStack = stackOnEntry;
return opStack[opStackOfs];
}
|