Debian Codegraph @

code/qcommon/vm_x86.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // vm_x86.c -- load time compiler and execution environment for x86
23
24 #if defined (__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
25
26 #include "vm_local.h"
27
28 #ifdef _WIN32
29 #include <windows.h>
30 #else
31 #ifdef __FreeBSD__
32 #include <sys/types.h>
33 #endif
34
35 #include <sys/mman.h> // for PROT_ stuff
36
37 /* need this on NX enabled systems (i386 with PAE kernel or
38 * noexec32=on x86_64) */
39 #define VM_X86_MMAP
40
41 // workaround for systems that use the old MAP_ANON macro
42 #ifndef MAP_ANONYMOUS
43 #define MAP_ANONYMOUS MAP_ANON
44 #endif
45 #endif
46
47 static void VM_Destroy_Compiled(vm_t* self);
48
49 /*
50
51 eax scratch
52 ebx/bl opStack offset
53 ecx scratch (required for shifts)
54 edx scratch (required for divisions)
55 esi program stack
56 edi opStack base
57 x86_64:
58 r8 vm->instructionPointers
59 r9 vm->dataBase
60
61 */
62
63 #define VMFREE_BUFFERS() do {Z_Free(buf); Z_Free(jused);} while(0)
64 static byte *buf = NULL;
65 static byte *jused = NULL;
66 static int jusedSize = 0;
67 static int compiledOfs = 0;
68 static byte *code = NULL;
69 static int pc = 0;
70
71 #define FTOL_PTR
72
73 static int instruction, pass;
74 static int lastConst = 0;
75 static int oc0, oc1, pop0, pop1;
76 static int jlabel;
77
78 typedef enum
79 {
80 LAST_COMMAND_NONE = 0,
81 LAST_COMMAND_MOV_STACK_EAX,
82 LAST_COMMAND_SUB_BL_1,
83 LAST_COMMAND_SUB_BL_2,
84 } ELastCommand;
85
86 typedef enum
87 {
88 VM_JMP_VIOLATION = 0,
89 VM_BLOCK_COPY = 1
90 } ESysCallType;
91
92 static ELastCommand LastCommand;
93
94 static int iss8(int32_t v)
95 {
96 return (SCHAR_MIN <= v && v <= SCHAR_MAX);
97 }
98
99 #if 0
100 static int isu8(uint32_t v)
101 {
102 return (v <= UCHAR_MAX);
103 }
104 #endif
105
106 static int NextConstant4(void)
107 {
108 return ((unsigned int)code[pc] | ((unsigned int)code[pc+1]<<8) | ((unsigned int)code[pc+2]<<16) | ((unsigned int)code[pc+3]<<24));
109 }
110
111 static int Constant4( void ) {
112 int v;
113
114 v = NextConstant4();
115 pc += 4;
116 return v;
117 }
118
119 static int Constant1( void ) {
120 int v;
121
122 v = code[pc];
123 pc += 1;
124 return v;
125 }
126
127 static void Emit1( int v )
128 {
129 buf[ compiledOfs ] = v;
130 compiledOfs++;
131
132 LastCommand = LAST_COMMAND_NONE;
133 }
134
135 static void Emit2(int v)
136 {
137 Emit1(v & 255);
138 Emit1((v >> 8) & 255);
139 }
140
141
142 static void Emit4(int v)
143 {
144 Emit1(v & 0xFF);
145 Emit1((v >> 8) & 0xFF);
146 Emit1((v >> 16) & 0xFF);
147 Emit1((v >> 24) & 0xFF);
148 }
149
150 static void EmitPtr(void *ptr)
151 {
152 intptr_t v = (intptr_t) ptr;
153
154 Emit4(v);
155 #if idx64
156 Emit1((v >> 32) & 0xFF);
157 Emit1((v >> 40) & 0xFF);
158 Emit1((v >> 48) & 0xFF);
159 Emit1((v >> 56) & 0xFF);
160 #endif
161 }
162
163 static int Hex( int c ) {
164 if ( c >= 'a' && c <= 'f' ) {
165 return 10 + c - 'a';
166 }
167 if ( c >= 'A' && c <= 'F' ) {
168 return 10 + c - 'A';
169 }
170 if ( c >= '0' && c <= '9' ) {
171 return c - '0';
172 }
173
174 VMFREE_BUFFERS();
175 Com_Error( ERR_DROP, "Hex: bad char '%c'", c );
176
177 return 0;
178 }
179 static void EmitString( const char *string ) {
180 int c1, c2;
181 int v;
182
183 while ( 1 ) {
184 c1 = string[0];
185 c2 = string[1];
186
187 v = ( Hex( c1 ) << 4 ) | Hex( c2 );
188 Emit1( v );
189
190 if ( !string[2] ) {
191 break;
192 }
193 string += 3;
194 }
195 }
196 static void EmitRexString(byte rex, const char *string)
197 {
198 #if idx64
199 if(rex)
200 Emit1(rex);
201 #endif
202
203 EmitString(string);
204 }
205
206
207 #define MASK_REG(modrm, mask) \
208 do { \
209 EmitString("81"); \
210 EmitString((modrm)); \
211 Emit4((mask)); \
212 } while(0)
213
214 // add bl, bytes
215 #define STACK_PUSH(bytes) \
216 do { \
217 EmitString("80 C3"); \
218 Emit1(bytes); \
219 } while(0)
220
221 // sub bl, bytes
222 #define STACK_POP(bytes) \
223 do { \
224 EmitString("80 EB"); \
225 Emit1(bytes); \
226 } while(0)
227
228 static void EmitCommand(ELastCommand command)
229 {
230 switch(command)
231 {
232 case LAST_COMMAND_MOV_STACK_EAX:
233 EmitString("89 04 9F"); // mov dword ptr [edi + ebx * 4], eax
234 break;
235
236 case LAST_COMMAND_SUB_BL_1:
237 STACK_POP(1); // sub bl, 1
238 break;
239
240 case LAST_COMMAND_SUB_BL_2:
241 STACK_POP(2); // sub bl, 2
242 break;
243 default:
244 break;
245 }
246 LastCommand = command;
247 }
248
249 static void EmitPushStack(vm_t *vm)
250 {
251 if (!jlabel)
252 {
253 if(LastCommand == LAST_COMMAND_SUB_BL_1)
254 { // sub bl, 1
255 compiledOfs -= 3;
256 vm->instructionPointers[instruction - 1] = compiledOfs;
257 return;
258 }
259 if(LastCommand == LAST_COMMAND_SUB_BL_2)
260 { // sub bl, 2
261 compiledOfs -= 3;
262 vm->instructionPointers[instruction - 1] = compiledOfs;
263 STACK_POP(1); // sub bl, 1
264 return;
265 }
266 }
267
268 STACK_PUSH(1); // add bl, 1
269 }
270
271 static void EmitMovEAXStack(vm_t *vm, int andit)
272 {
273 if(!jlabel)
274 {
275 if(LastCommand == LAST_COMMAND_MOV_STACK_EAX)
276 { // mov [edi + ebx * 4], eax
277 compiledOfs -= 3;
278 vm->instructionPointers[instruction - 1] = compiledOfs;
279 }
280 else if(pop1 == OP_CONST && buf[compiledOfs-7] == 0xC7 && buf[compiledOfs-6] == 0x04 && buf[compiledOfs - 5] == 0x9F)
281 { // mov [edi + ebx * 4], 0x12345678
282 compiledOfs -= 7;
283 vm->instructionPointers[instruction - 1] = compiledOfs;
284 EmitString("B8"); // mov eax, 0x12345678
285
286 if(andit)
287 Emit4(lastConst & andit);
288 else
289 Emit4(lastConst);
290
291 return;
292 }
293 else if(pop1 != OP_DIVI && pop1 != OP_DIVU && pop1 != OP_MULI && pop1 != OP_MULU &&
294 pop1 != OP_STORE4 && pop1 != OP_STORE2 && pop1 != OP_STORE1)
295 {
296 EmitString("8B 04 9F"); // mov eax, dword ptr [edi + ebx * 4]
297 }
298 }
299 else
300 EmitString("8B 04 9F"); // mov eax, dword ptr [edi + ebx * 4]
301
302 if(andit)
303 {
304 EmitString("25"); // and eax, 0x12345678
305 Emit4(andit);
306 }
307 }
308
309 void EmitMovECXStack(vm_t *vm)
310 {
311 if(!jlabel)
312 {
313 if(LastCommand == LAST_COMMAND_MOV_STACK_EAX) // mov [edi + ebx * 4], eax
314 {
315 compiledOfs -= 3;
316 vm->instructionPointers[instruction - 1] = compiledOfs;
317 EmitString("89 C1"); // mov ecx, eax
318 return;
319 }
320 if(pop1 == OP_DIVI || pop1 == OP_DIVU || pop1 == OP_MULI || pop1 == OP_MULU ||
321 pop1 == OP_STORE4 || pop1 == OP_STORE2 || pop1 == OP_STORE1)
322 {
323 EmitString("89 C1"); // mov ecx, eax
324 return;
325 }
326 }
327
328 EmitString("8B 0C 9F"); // mov ecx, dword ptr [edi + ebx * 4]
329 }
330
331
332 void EmitMovEDXStack(vm_t *vm, int andit)
333 {
334 if(!jlabel)
335 {
336 if(LastCommand == LAST_COMMAND_MOV_STACK_EAX)
337 { // mov dword ptr [edi + ebx * 4], eax
338 compiledOfs -= 3;
339 vm->instructionPointers[instruction - 1] = compiledOfs;
340
341 EmitString("8B D0"); // mov edx, eax
342 }
343 else if(pop1 == OP_DIVI || pop1 == OP_DIVU || pop1 == OP_MULI || pop1 == OP_MULU ||
344 pop1 == OP_STORE4 || pop1 == OP_STORE2 || pop1 == OP_STORE1)
345 {
346 EmitString("8B D0"); // mov edx, eax
347 }
348 else if(pop1 == OP_CONST && buf[compiledOfs-7] == 0xC7 && buf[compiledOfs-6] == 0x07 && buf[compiledOfs - 5] == 0x9F)
349 { // mov dword ptr [edi + ebx * 4], 0x12345678
350 compiledOfs -= 7;
351 vm->instructionPointers[instruction - 1] = compiledOfs;
352 EmitString("BA"); // mov edx, 0x12345678
353
354 if(andit)
355 Emit4(lastConst & andit);
356 else
357 Emit4(lastConst);
358
359 return;
360 }
361 else
362 EmitString("8B 14 9F"); // mov edx, dword ptr [edi + ebx * 4]
363
364 }
365 else
366 EmitString("8B 14 9F"); // mov edx, dword ptr [edi + ebx * 4]
367
368 if(andit)
369 MASK_REG("E2", andit); // and edx, 0x12345678
370 }
371
372 #define JUSED(x) \
373 do { \
374 if (x < 0 || x >= vm->instructionCount) { \
375 VMFREE_BUFFERS(); \
376 Com_Error( ERR_DROP, \
377 "VM_CompileX86: jump target out of range at offset %d", pc ); \
378 } \
379 jused[x] = 1; \
380 } while(0)
381
382 #define SET_JMPOFS(x) do { buf[(x)] = compiledOfs - ((x) + 1); } while(0)
383
384
385 /*
386 =================
387 ErrJump
388 Error handler for jump/call to invalid instruction number
389 =================
390 */
391
392 static void Q_NO_RETURN ErrJump(void)
393 {
394 Com_Error(ERR_DROP, "program tried to execute code outside VM");
395 }
396
397 /*
398 =================
399 DoSyscall
400
401 Assembler helper routines will write its arguments directly to global variables so as to
402 work around different calling conventions
403 =================
404 */
405
406 int vm_syscallNum;
407 int vm_programStack;
408 int *vm_opStackBase;
409 uint8_t vm_opStackOfs;
410 intptr_t vm_arg;
411
412 static void DoSyscall(void)
413 {
414 vm_t *savedVM;
415
416 // save currentVM so as to allow for recursive VM entry
417 savedVM = currentVM;
418 // modify VM stack pointer for recursive VM entry
419 currentVM->programStack = vm_programStack - 4;
420
421 if(vm_syscallNum < 0)
422 {
423 int *data, *ret;
424 #if idx64
425 int index;
426 intptr_t args[MAX_VMSYSCALL_ARGS];
427 #endif
428
429 data = (int *) (savedVM->dataBase + vm_programStack + 4);
430 ret = &vm_opStackBase[vm_opStackOfs + 1];
431
432 #if idx64
433 args[0] = ~vm_syscallNum;
434 for(index = 1; index < ARRAY_LEN(args); index++)
435 args[index] = data[index];
436
437 *ret = savedVM->systemCall(args);
438 #else
439 data[0] = ~vm_syscallNum;
440 *ret = savedVM->systemCall((intptr_t *) data);
441 #endif
442 }
443 else
444 {
445 switch(vm_syscallNum)
446 {
447 case VM_JMP_VIOLATION:
448 ErrJump();
449 break;
450 case VM_BLOCK_COPY:
451 if(vm_opStackOfs < 1)
452 Com_Error(ERR_DROP, "VM_BLOCK_COPY failed due to corrupted opStack");
453
454 VM_BlockCopy(vm_opStackBase[(vm_opStackOfs - 1)], vm_opStackBase[vm_opStackOfs], vm_arg);
455 break;
456 default:
457 Com_Error(ERR_DROP, "Unknown VM operation %d", vm_syscallNum);
458 break;
459 }
460 }
461
462 currentVM = savedVM;
463 }
464
465 /*
466 =================
467 EmitCallRel
468 Relative call to vm->codeBase + callOfs
469 =================
470 */
471
472 void EmitCallRel(vm_t *vm, int callOfs)
473 {
474 EmitString("E8"); // call 0x12345678
475 Emit4(callOfs - compiledOfs - 4);
476 }
477
478 /*
479 =================
480 EmitCallDoSyscall
481 Call to DoSyscall()
482 =================
483 */
484
485 int EmitCallDoSyscall(vm_t *vm)
486 {
487 // use edx register to store DoSyscall address
488 EmitRexString(0x48, "BA"); // mov edx, DoSyscall
489 EmitPtr(DoSyscall);
490
491 // Push important registers to stack as we can't really make
492 // any assumptions about calling conventions.
493 EmitString("51"); // push ebx
494 EmitString("56"); // push esi
495 EmitString("57"); // push edi
496 #if idx64
497 EmitRexString(0x41, "50"); // push r8
498 EmitRexString(0x41, "51"); // push r9
499 #endif
500
501 // write arguments to global vars
502 // syscall number
503 EmitString("A3"); // mov [0x12345678], eax
504 EmitPtr(&vm_syscallNum);
505 // vm_programStack value
506 EmitString("89 F0"); // mov eax, esi
507 EmitString("A3"); // mov [0x12345678], eax
508 EmitPtr(&vm_programStack);
509 // vm_opStackOfs
510 EmitString("88 D8"); // mov al, bl
511 EmitString("A2"); // mov [0x12345678], al
512 EmitPtr(&vm_opStackOfs);
513 // vm_opStackBase
514 EmitRexString(0x48, "89 F8"); // mov eax, edi
515 EmitRexString(0x48, "A3"); // mov [0x12345678], eax
516 EmitPtr(&vm_opStackBase);
517 // vm_arg
518 EmitString("89 C8"); // mov eax, ecx
519 EmitString("A3"); // mov [0x12345678], eax
520 EmitPtr(&vm_arg);
521
522 // align the stack pointer to a 16-byte-boundary
523 EmitString("55"); // push ebp
524 EmitRexString(0x48, "89 E5"); // mov ebp, esp
525 EmitRexString(0x48, "83 E4 F0"); // and esp, 0xFFFFFFF0
526
527 // call the syscall wrapper function DoSyscall()
528
529 EmitString("FF D2"); // call edx
530
531 // reset the stack pointer to its previous value
532 EmitRexString(0x48, "89 EC"); // mov esp, ebp
533 EmitString("5D"); // pop ebp
534
535 #if idx64
536 EmitRexString(0x41, "59"); // pop r9
537 EmitRexString(0x41, "58"); // pop r8
538 #endif
539 EmitString("5F"); // pop edi
540 EmitString("5E"); // pop esi
541 EmitString("59"); // pop ebx
542
543 EmitString("C3"); // ret
544
545 return compiledOfs;
546 }
547
548 /*
549 =================
550 EmitCallErrJump
551 Emit the code that triggers execution of the jump violation handler
552 =================
553 */
554
555 static void EmitCallErrJump(vm_t *vm, int sysCallOfs)
556 {
557 EmitString("B8"); // mov eax, 0x12345678
558 Emit4(VM_JMP_VIOLATION);
559
560 EmitCallRel(vm, sysCallOfs);
561 }
562
563 /*
564 =================
565 EmitCallProcedure
566 VM OP_CALL procedure for call destinations obtained at runtime
567 =================
568 */
569
570 int EmitCallProcedure(vm_t *vm, int sysCallOfs)
571 {
572 int jmpSystemCall, jmpBadAddr;
573 int retval;
574
575 EmitString("8B 04 9F"); // mov eax, dword ptr [edi + ebx * 4]
576 STACK_POP(1); // sub bl, 1
577 EmitString("85 C0"); // test eax, eax
578
579 // Jump to syscall code, 1 byte offset should suffice
580 EmitString("7C"); // jl systemCall
581 jmpSystemCall = compiledOfs++;
582
583 /************ Call inside VM ************/
584
585 EmitString("81 F8"); // cmp eax, vm->instructionCount
586 Emit4(vm->instructionCount);
587
588 // Error jump if invalid jump target
589 EmitString("73"); // jae badAddr
590 jmpBadAddr = compiledOfs++;
591
592 #if idx64
593 EmitRexString(0x49, "FF 14 C0"); // call qword ptr [r8 + eax * 8]
594 #else
595 EmitString("FF 14 85"); // call dword ptr [vm->instructionPointers + eax * 4]
596 Emit4((intptr_t) vm->instructionPointers);
597 #endif
598 EmitString("8B 04 9F"); // mov eax, dword ptr [edi + ebx * 4]
599 EmitString("C3"); // ret
600
601 // badAddr:
602 SET_JMPOFS(jmpBadAddr);
603 EmitCallErrJump(vm, sysCallOfs);
604
605 /************ System Call ************/
606
607 // systemCall:
608 SET_JMPOFS(jmpSystemCall);
609 retval = compiledOfs;
610
611 EmitCallRel(vm, sysCallOfs);
612
613 // have opStack reg point at return value
614 STACK_PUSH(1); // add bl, 1
615 EmitString("C3"); // ret
616
617 return retval;
618 }
619
620 /*
621 =================
622 EmitJumpIns
623 Jump to constant instruction number
624 =================
625 */
626
627 void EmitJumpIns(vm_t *vm, const char *jmpop, int cdest)
628 {
629 JUSED(cdest);
630
631 EmitString(jmpop); // j??? 0x12345678
632
633 // we only know all the jump addresses in the third pass
634 if(pass == 2)
635 Emit4(vm->instructionPointers[cdest] - compiledOfs - 4);
636 else
637 compiledOfs += 4;
638 }
639
640 /*
641 =================
642 EmitCallIns
643 Call to constant instruction number
644 =================
645 */
646
647 void EmitCallIns(vm_t *vm, int cdest)
648 {
649 JUSED(cdest);
650
651 EmitString("E8"); // call 0x12345678
652
653 // we only know all the jump addresses in the third pass
654 if(pass == 2)
655 Emit4(vm->instructionPointers[cdest] - compiledOfs - 4);
656 else
657 compiledOfs += 4;
658 }
659
660 /*
661 =================
662 EmitCallConst
663 Call to constant instruction number or syscall
664 =================
665 */
666
667 void EmitCallConst(vm_t *vm, int cdest, int callProcOfsSyscall)
668 {
669 if(cdest < 0)
670 {
671 EmitString("B8"); // mov eax, cdest
672 Emit4(cdest);
673
674 EmitCallRel(vm, callProcOfsSyscall);
675 }
676 else
677 EmitCallIns(vm, cdest);
678 }
679
680 /*
681 =================
682 EmitBranchConditions
683 Emits x86 branch condition as given in op
684 =================
685 */
686 void EmitBranchConditions(vm_t *vm, int op)
687 {
688 switch(op)
689 {
690 case OP_EQ:
691 EmitJumpIns(vm, "0F 84", Constant4()); // je 0x12345678
692 break;
693 case OP_NE:
694 EmitJumpIns(vm, "0F 85", Constant4()); // jne 0x12345678
695 break;
696 case OP_LTI:
697 EmitJumpIns(vm, "0F 8C", Constant4()); // jl 0x12345678
698 break;
699 case OP_LEI:
700 EmitJumpIns(vm, "0F 8E", Constant4()); // jle 0x12345678
701 break;
702 case OP_GTI:
703 EmitJumpIns(vm, "0F 8F", Constant4()); // jg 0x12345678
704 break;
705 case OP_GEI:
706 EmitJumpIns(vm, "0F 8D", Constant4()); // jge 0x12345678
707 break;
708 case OP_LTU:
709 EmitJumpIns(vm, "0F 82", Constant4()); // jb 0x12345678
710 break;
711 case OP_LEU:
712 EmitJumpIns(vm, "0F 86", Constant4()); // jbe 0x12345678
713 break;
714 case OP_GTU:
715 EmitJumpIns(vm, "0F 87", Constant4()); // ja 0x12345678
716 break;
717 case OP_GEU:
718 EmitJumpIns(vm, "0F 83", Constant4()); // jae 0x12345678
719 break;
720 }
721 }
722
723
724 /*
725 =================
726 ConstOptimize
727 Constant values for immediately following instructions may be translated to immediate values
728 instead of opStack operations, which will save expensive operations on memory
729 =================
730 */
731
732 qboolean ConstOptimize(vm_t *vm, int callProcOfsSyscall)
733 {
734 int v;
735 int op1;
736
737 // we can safely perform optimizations only in case if
738 // we are 100% sure that next instruction is not a jump label
739 if (vm->jumpTableTargets && !jused[instruction])
740 op1 = code[pc+4];
741 else
742 return qfalse;
743
744 switch ( op1 ) {
745
746 case OP_LOAD4:
747 EmitPushStack(vm);
748 #if idx64
749 EmitRexString(0x41, "8B 81"); // mov eax, dword ptr [r9 + 0x12345678]
750 Emit4(Constant4() & vm->dataMask);
751 #else
752 EmitString("B8"); // mov eax, 0x12345678
753 EmitPtr(vm->dataBase + (Constant4() & vm->dataMask));
754 EmitString("8B 00"); // mov eax, dword ptr [eax]
755 #endif
756 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
757
758 pc++; // OP_LOAD4
759 instruction += 1;
760 return qtrue;
761
762 case OP_LOAD2:
763 EmitPushStack(vm);
764 #if idx64
765 EmitRexString(0x41, "0F B7 81"); // movzx eax, word ptr [r9 + 0x12345678]
766 Emit4(Constant4() & vm->dataMask);
767 #else
768 EmitString("B8"); // mov eax, 0x12345678
769 EmitPtr(vm->dataBase + (Constant4() & vm->dataMask));
770 EmitString("0F B7 00"); // movzx eax, word ptr [eax]
771 #endif
772 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
773
774 pc++; // OP_LOAD2
775 instruction += 1;
776 return qtrue;
777
778 case OP_LOAD1:
779 EmitPushStack(vm);
780 #if idx64
781 EmitRexString(0x41, "0F B6 81"); // movzx eax, byte ptr [r9 + 0x12345678]
782 Emit4(Constant4() & vm->dataMask);
783 #else
784 EmitString("B8"); // mov eax, 0x12345678
785 EmitPtr(vm->dataBase + (Constant4() & vm->dataMask));
786 EmitString("0F B6 00"); // movzx eax, byte ptr [eax]
787 #endif
788 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
789
790 pc++; // OP_LOAD1
791 instruction += 1;
792 return qtrue;
793
794 case OP_STORE4:
795 EmitMovEAXStack(vm, vm->dataMask);
796 #if idx64
797 EmitRexString(0x41, "C7 04 01"); // mov dword ptr [r9 + eax], 0x12345678
798 Emit4(Constant4());
799 #else
800 EmitString("C7 80"); // mov dword ptr [eax + 0x12345678], 0x12345678
801 Emit4((intptr_t) vm->dataBase);
802 Emit4(Constant4());
803 #endif
804 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
805 pc++; // OP_STORE4
806 instruction += 1;
807 return qtrue;
808
809 case OP_STORE2:
810 EmitMovEAXStack(vm, vm->dataMask);
811 #if idx64
812 Emit1(0x66); // mov word ptr [r9 + eax], 0x1234
813 EmitRexString(0x41, "C7 04 01");
814 Emit2(Constant4());
815 #else
816 EmitString("66 C7 80"); // mov word ptr [eax + 0x12345678], 0x1234
817 Emit4((intptr_t) vm->dataBase);
818 Emit2(Constant4());
819 #endif
820 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
821
822 pc++; // OP_STORE2
823 instruction += 1;
824 return qtrue;
825
826 case OP_STORE1:
827 EmitMovEAXStack(vm, vm->dataMask);
828 #if idx64
829 EmitRexString(0x41, "C6 04 01"); // mov byte [r9 + eax], 0x12
830 Emit1(Constant4());
831 #else
832 EmitString("C6 80"); // mov byte ptr [eax + 0x12345678], 0x12
833 Emit4((intptr_t) vm->dataBase);
834 Emit1(Constant4());
835 #endif
836 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
837
838 pc++; // OP_STORE1
839 instruction += 1;
840 return qtrue;
841
842 case OP_ADD:
843 v = Constant4();
844
845 EmitMovEAXStack(vm, 0);
846 if(iss8(v))
847 {
848 EmitString("83 C0"); // add eax, 0x7F
849 Emit1(v);
850 }
851 else
852 {
853 EmitString("05"); // add eax, 0x12345678
854 Emit4(v);
855 }
856 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
857
858 pc++; // OP_ADD
859 instruction += 1;
860 return qtrue;
861
862 case OP_SUB:
863 v = Constant4();
864
865 EmitMovEAXStack(vm, 0);
866 if(iss8(v))
867 {
868 EmitString("83 E8"); // sub eax, 0x7F
869 Emit1(v);
870 }
871 else
872 {
873 EmitString("2D"); // sub eax, 0x12345678
874 Emit4(v);
875 }
876 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
877
878 pc++; // OP_SUB
879 instruction += 1;
880 return qtrue;
881
882 case OP_MULI:
883 v = Constant4();
884
885 EmitMovEAXStack(vm, 0);
886 if(iss8(v))
887 {
888 EmitString("6B C0"); // imul eax, 0x7F
889 Emit1(v);
890 }
891 else
892 {
893 EmitString("69 C0"); // imul eax, 0x12345678
894 Emit4(v);
895 }
896 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
897 pc++; // OP_MULI
898 instruction += 1;
899
900 return qtrue;
901
902 case OP_LSH:
903 v = NextConstant4();
904 if(v < 0 || v > 31)
905 break;
906
907 EmitMovEAXStack(vm, 0);
908 EmitString("C1 E0"); // shl eax, 0x12
909 Emit1(v);
910 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
911
912 pc += 5; // CONST + OP_LSH
913 instruction += 1;
914 return qtrue;
915
916 case OP_RSHI:
917 v = NextConstant4();
918 if(v < 0 || v > 31)
919 break;
920
921 EmitMovEAXStack(vm, 0);
922 EmitString("C1 F8"); // sar eax, 0x12
923 Emit1(v);
924 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
925
926 pc += 5; // CONST + OP_RSHI
927 instruction += 1;
928 return qtrue;
929
930 case OP_RSHU:
931 v = NextConstant4();
932 if(v < 0 || v > 31)
933 break;
934
935 EmitMovEAXStack(vm, 0);
936 EmitString("C1 E8"); // shr eax, 0x12
937 Emit1(v);
938 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
939
940 pc += 5; // CONST + OP_RSHU
941 instruction += 1;
942 return qtrue;
943
944 case OP_BAND:
945 v = Constant4();
946
947 EmitMovEAXStack(vm, 0);
948 if(iss8(v))
949 {
950 EmitString("83 E0"); // and eax, 0x7F
951 Emit1(v);
952 }
953 else
954 {
955 EmitString("25"); // and eax, 0x12345678
956 Emit4(v);
957 }
958 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
959
960 pc += 1; // OP_BAND
961 instruction += 1;
962 return qtrue;
963
964 case OP_BOR:
965 v = Constant4();
966
967 EmitMovEAXStack(vm, 0);
968 if(iss8(v))
969 {
970 EmitString("83 C8"); // or eax, 0x7F
971 Emit1(v);
972 }
973 else
974 {
975 EmitString("0D"); // or eax, 0x12345678
976 Emit4(v);
977 }
978 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
979
980 pc += 1; // OP_BOR
981 instruction += 1;
982 return qtrue;
983
984 case OP_BXOR:
985 v = Constant4();
986
987 EmitMovEAXStack(vm, 0);
988 if(iss8(v))
989 {
990 EmitString("83 F0"); // xor eax, 0x7F
991 Emit1(v);
992 }
993 else
994 {
995 EmitString("35"); // xor eax, 0x12345678
996 Emit4(v);
997 }
998 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
999
1000 pc += 1; // OP_BXOR
1001 instruction += 1;
1002 return qtrue;
1003
1004 case OP_EQ:
1005 case OP_NE:
1006 case OP_LTI:
1007 case OP_LEI:
1008 case OP_GTI:
1009 case OP_GEI:
1010 case OP_LTU:
1011 case OP_LEU:
1012 case OP_GTU:
1013 case OP_GEU:
1014 EmitMovEAXStack(vm, 0);
1015 EmitCommand(LAST_COMMAND_SUB_BL_1);
1016 EmitString("3D"); // cmp eax, 0x12345678
1017 Emit4(Constant4());
1018
1019 pc++; // OP_*
1020 EmitBranchConditions(vm, op1);
1021 instruction++;
1022
1023 return qtrue;
1024
1025 case OP_EQF:
1026 case OP_NEF:
1027 if(NextConstant4())
1028 break;
1029 pc += 5; // CONST + OP_EQF|OP_NEF
1030
1031 EmitMovEAXStack(vm, 0);
1032 EmitCommand(LAST_COMMAND_SUB_BL_1);
1033 // floating point hack :)
1034 EmitString("25"); // and eax, 0x7FFFFFFF
1035 Emit4(0x7FFFFFFF);
1036 if(op1 == OP_EQF)
1037 EmitJumpIns(vm, "0F 84", Constant4()); // jz 0x12345678
1038 else
1039 EmitJumpIns(vm, "0F 85", Constant4()); // jnz 0x12345678
1040
1041 instruction += 1;
1042 return qtrue;
1043
1044
1045 case OP_JUMP:
1046 EmitJumpIns(vm, "E9", Constant4()); // jmp 0x12345678
1047
1048 pc += 1; // OP_JUMP
1049 instruction += 1;
1050 return qtrue;
1051
1052 case OP_CALL:
1053 v = Constant4();
1054 EmitCallConst(vm, v, callProcOfsSyscall);
1055
1056 pc += 1; // OP_CALL
1057 instruction += 1;
1058 return qtrue;
1059
1060 default:
1061 break;
1062 }
1063
1064 return qfalse;
1065 }
1066
1067 /*
1068 =================
1069 VM_Compile
1070 =================
1071 */
1072 void VM_Compile(vm_t *vm, vmHeader_t *header)
1073 {
1074 int op;
1075 int maxLength;
1076 int v;
1077 int i;
1078 int callProcOfsSyscall, callProcOfs, callDoSyscallOfs;
1079
1080 jusedSize = header->instructionCount + 2;
1081
1082 // allocate a very large temp buffer, we will shrink it later
1083 maxLength = header->codeLength * 8 + 64;
1084 buf = Z_Malloc(maxLength);
1085 jused = Z_Malloc(jusedSize);
1086 code = Z_Malloc(header->codeLength+32);
1087
1088 Com_Memset(jused, 0, jusedSize);
1089 Com_Memset(buf, 0, maxLength);
1090
1091 // copy code in larger buffer and put some zeros at the end
1092 // so we can safely look ahead for a few instructions in it
1093 // without a chance to get false-positive because of some garbage bytes
1094 Com_Memset(code, 0, header->codeLength+32);
1095 Com_Memcpy(code, (byte *)header + header->codeOffset, header->codeLength );
1096
1097 // ensure that the optimisation pass knows about all the jump
1098 // table targets
1099 pc = -1; // a bogus value to be printed in out-of-bounds error messages
1100 for( i = 0; i < vm->numJumpTableTargets; i++ ) {
1101 JUSED( *(int *)(vm->jumpTableTargets + ( i * sizeof( int ) ) ) );
1102 }
1103
1104 // Start buffer with x86-VM specific procedures
1105 compiledOfs = 0;
1106
1107 callDoSyscallOfs = compiledOfs;
1108 callProcOfs = EmitCallDoSyscall(vm);
1109 callProcOfsSyscall = EmitCallProcedure(vm, callDoSyscallOfs);
1110 vm->entryOfs = compiledOfs;
1111
1112 for(pass=0; pass < 3; pass++) {
1113 oc0 = -23423;
1114 oc1 = -234354;
1115 pop0 = -43435;
1116 pop1 = -545455;
1117
1118 // translate all instructions
1119 pc = 0;
1120 instruction = 0;
1121 //code = (byte *)header + header->codeOffset;
1122 compiledOfs = vm->entryOfs;
1123
1124 LastCommand = LAST_COMMAND_NONE;
1125
1126 while(instruction < header->instructionCount)
1127 {
1128 if(compiledOfs > maxLength - 16)
1129 {
1130 VMFREE_BUFFERS();
1131 Com_Error(ERR_DROP, "VM_CompileX86: maxLength exceeded");
1132 }
1133
1134 vm->instructionPointers[ instruction ] = compiledOfs;
1135
1136 if ( !vm->jumpTableTargets )
1137 jlabel = 1;
1138 else
1139 jlabel = jused[ instruction ];
1140
1141 instruction++;
1142
1143 if(pc > header->codeLength)
1144 {
1145 VMFREE_BUFFERS();
1146 Com_Error(ERR_DROP, "VM_CompileX86: pc > header->codeLength");
1147 }
1148
1149 op = code[ pc ];
1150 pc++;
1151 switch ( op ) {
1152 case 0:
1153 break;
1154 case OP_BREAK:
1155 EmitString("CC"); // int 3
1156 break;
1157 case OP_ENTER:
1158 EmitString("81 EE"); // sub esi, 0x12345678
1159 Emit4(Constant4());
1160 break;
1161 case OP_CONST:
1162 if(ConstOptimize(vm, callProcOfsSyscall))
1163 break;
1164
1165 EmitPushStack(vm);
1166 EmitString("C7 04 9F"); // mov dword ptr [edi + ebx * 4], 0x12345678
1167 lastConst = Constant4();
1168
1169 Emit4(lastConst);
1170 if(code[pc] == OP_JUMP)
1171 JUSED(lastConst);
1172
1173 break;
1174 case OP_LOCAL:
1175 EmitPushStack(vm);
1176 EmitString("8D 86"); // lea eax, [0x12345678 + esi]
1177 oc0 = oc1;
1178 oc1 = Constant4();
1179 Emit4(oc1);
1180 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
1181 break;
1182 case OP_ARG:
1183 EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
1184 EmitString("8B D6"); // mov edx, esi
1185 EmitString("81 C2"); // add edx, 0x12345678
1186 Emit4((Constant1() & 0xFF));
1187 MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
1188 #if idx64
1189 EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
1190 #else
1191 EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
1192 Emit4((intptr_t) vm->dataBase);
1193 #endif
1194 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1195 break;
1196 case OP_CALL:
1197 EmitCallRel(vm, callProcOfs);
1198 break;
1199 case OP_PUSH:
1200 EmitPushStack(vm);
1201 break;
1202 case OP_POP:
1203 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1204 break;
1205 case OP_LEAVE:
1206 v = Constant4();
1207 EmitString("81 C6"); // add esi, 0x12345678
1208 Emit4(v);
1209 EmitString("C3"); // ret
1210 break;
1211 case OP_LOAD4:
1212 if (code[pc] == OP_CONST && code[pc+5] == OP_ADD && code[pc+6] == OP_STORE4)
1213 {
1214 if(oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
1215 {
1216 compiledOfs -= 12;
1217 vm->instructionPointers[instruction - 1] = compiledOfs;
1218 }
1219
1220 pc++; // OP_CONST
1221 v = Constant4();
1222
1223 EmitMovEDXStack(vm, vm->dataMask);
1224 if(v == 1 && oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
1225 {
1226 #if idx64
1227 EmitRexString(0x41, "FF 04 11"); // inc dword ptr [r9 + edx]
1228 #else
1229 EmitString("FF 82"); // inc dword ptr [edx + 0x12345678]
1230 Emit4((intptr_t) vm->dataBase);
1231 #endif
1232 }
1233 else
1234 {
1235 #if idx64
1236 EmitRexString(0x41, "8B 04 11"); // mov eax, dword ptr [r9 + edx]
1237 #else
1238 EmitString("8B 82"); // mov eax, dword ptr [edx + 0x12345678]
1239 Emit4((intptr_t) vm->dataBase);
1240 #endif
1241 EmitString("05"); // add eax, v
1242 Emit4(v);
1243
1244 if (oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
1245 {
1246 #if idx64
1247 EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
1248 #else
1249 EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
1250 Emit4((intptr_t) vm->dataBase);
1251 #endif
1252 }
1253 else
1254 {
1255 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1256 EmitString("8B 14 9F"); // mov edx, dword ptr [edi + ebx * 4]
1257 MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
1258 #if idx64
1259 EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
1260 #else
1261 EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
1262 Emit4((intptr_t) vm->dataBase);
1263 #endif
1264 }
1265 }
1266
1267 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1268 pc++; // OP_ADD
1269 pc++; // OP_STORE
1270 instruction += 3;
1271 break;
1272 }
1273
1274 if(code[pc] == OP_CONST && code[pc+5] == OP_SUB && code[pc+6] == OP_STORE4)
1275 {
1276 if(oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
1277 {
1278 compiledOfs -= 12;
1279 vm->instructionPointers[instruction - 1] = compiledOfs;
1280 }
1281
1282 pc++; // OP_CONST
1283 v = Constant4();
1284
1285 EmitMovEDXStack(vm, vm->dataMask);
1286 if(v == 1 && oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
1287 {
1288 #if idx64
1289 EmitRexString(0x41, "FF 0C 11"); // dec dword ptr [r9 + edx]
1290 #else
1291 EmitString("FF 8A"); // dec dword ptr [edx + 0x12345678]
1292 Emit4((intptr_t) vm->dataBase);
1293 #endif
1294 }
1295 else
1296 {
1297 #if idx64
1298 EmitRexString(0x41, "8B 04 11"); // mov eax, dword ptr [r9 + edx]
1299 #else
1300 EmitString("8B 82"); // mov eax, dword ptr [edx + 0x12345678]
1301 Emit4((intptr_t) vm->dataBase);
1302 #endif
1303 EmitString("2D"); // sub eax, v
1304 Emit4(v);
1305
1306 if(oc0 == oc1 && pop0 == OP_LOCAL && pop1 == OP_LOCAL)
1307 {
1308 #if idx64
1309 EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
1310 #else
1311 EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
1312 Emit4((intptr_t) vm->dataBase);
1313 #endif
1314 }
1315 else
1316 {
1317 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1318 EmitString("8B 14 9F"); // mov edx, dword ptr [edi + ebx * 4]
1319 MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
1320 #if idx64
1321 EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
1322 #else
1323 EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
1324 Emit4((intptr_t) vm->dataBase);
1325 #endif
1326 }
1327 }
1328 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1329 pc++; // OP_SUB
1330 pc++; // OP_STORE
1331 instruction += 3;
1332 break;
1333 }
1334
1335 if(buf[compiledOfs - 3] == 0x89 && buf[compiledOfs - 2] == 0x04 && buf[compiledOfs - 1] == 0x9F)
1336 {
1337 compiledOfs -= 3;
1338 vm->instructionPointers[instruction - 1] = compiledOfs;
1339 MASK_REG("E0", vm->dataMask); // and eax, 0x12345678
1340 #if idx64
1341 EmitRexString(0x41, "8B 04 01"); // mov eax, dword ptr [r9 + eax]
1342 #else
1343 EmitString("8B 80"); // mov eax, dword ptr [eax + 0x1234567]
1344 Emit4((intptr_t) vm->dataBase);
1345 #endif
1346 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
1347 break;
1348 }
1349
1350 EmitMovEAXStack(vm, vm->dataMask);
1351 #if idx64
1352 EmitRexString(0x41, "8B 04 01"); // mov eax, dword ptr [r9 + eax]
1353 #else
1354 EmitString("8B 80"); // mov eax, dword ptr [eax + 0x12345678]
1355 Emit4((intptr_t) vm->dataBase);
1356 #endif
1357 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
1358 break;
1359 case OP_LOAD2:
1360 EmitMovEAXStack(vm, vm->dataMask);
1361 #if idx64
1362 EmitRexString(0x41, "0F B7 04 01"); // movzx eax, word ptr [r9 + eax]
1363 #else
1364 EmitString("0F B7 80"); // movzx eax, word ptr [eax + 0x12345678]
1365 Emit4((intptr_t) vm->dataBase);
1366 #endif
1367 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
1368 break;
1369 case OP_LOAD1:
1370 EmitMovEAXStack(vm, vm->dataMask);
1371 #if idx64
1372 EmitRexString(0x41, "0F B6 04 01"); // movzx eax, byte ptr [r9 + eax]
1373 #else
1374 EmitString("0F B6 80"); // movzx eax, byte ptr [eax + 0x12345678]
1375 Emit4((intptr_t) vm->dataBase);
1376 #endif
1377 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
1378 break;
1379 case OP_STORE4:
1380 EmitMovEAXStack(vm, 0);
1381 EmitString("8B 54 9F FC"); // mov edx, dword ptr -4[edi + ebx * 4]
1382 MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
1383 #if idx64
1384 EmitRexString(0x41, "89 04 11"); // mov dword ptr [r9 + edx], eax
1385 #else
1386 EmitString("89 82"); // mov dword ptr [edx + 0x12345678], eax
1387 Emit4((intptr_t) vm->dataBase);
1388 #endif
1389 EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
1390 break;
1391 case OP_STORE2:
1392 EmitMovEAXStack(vm, 0);
1393 EmitString("8B 54 9F FC"); // mov edx, dword ptr -4[edi + ebx * 4]
1394 MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
1395 #if idx64
1396 Emit1(0x66); // mov word ptr [r9 + edx], eax
1397 EmitRexString(0x41, "89 04 11");
1398 #else
1399 EmitString("66 89 82"); // mov word ptr [edx + 0x12345678], eax
1400 Emit4((intptr_t) vm->dataBase);
1401 #endif
1402 EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
1403 break;
1404 case OP_STORE1:
1405 EmitMovEAXStack(vm, 0);
1406 EmitString("8B 54 9F FC"); // mov edx, dword ptr -4[edi + ebx * 4]
1407 MASK_REG("E2", vm->dataMask); // and edx, 0x12345678
1408 #if idx64
1409 EmitRexString(0x41, "88 04 11"); // mov byte ptr [r9 + edx], eax
1410 #else
1411 EmitString("88 82"); // mov byte ptr [edx + 0x12345678], eax
1412 Emit4((intptr_t) vm->dataBase);
1413 #endif
1414 EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
1415 break;
1416
1417 case OP_EQ:
1418 case OP_NE:
1419 case OP_LTI:
1420 case OP_LEI:
1421 case OP_GTI:
1422 case OP_GEI:
1423 case OP_LTU:
1424 case OP_LEU:
1425 case OP_GTU:
1426 case OP_GEU:
1427 EmitMovEAXStack(vm, 0);
1428 EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
1429 EmitString("39 44 9F 04"); // cmp eax, dword ptr 4[edi + ebx * 4]
1430
1431 EmitBranchConditions(vm, op);
1432 break;
1433 case OP_EQF:
1434 case OP_NEF:
1435 case OP_LTF:
1436 case OP_LEF:
1437 case OP_GTF:
1438 case OP_GEF:
1439 EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
1440 EmitString("D9 44 9F 04"); // fld dword ptr 4[edi + ebx * 4]
1441 EmitString("D8 5C 9F 08"); // fcomp dword ptr 8[edi + ebx * 4]
1442 EmitString("DF E0"); // fnstsw ax
1443
1444 switch(op)
1445 {
1446 case OP_EQF:
1447 EmitString("F6 C4 40"); // test ah,0x40
1448 EmitJumpIns(vm, "0F 85", Constant4()); // jne 0x12345678
1449 break;
1450 case OP_NEF:
1451 EmitString("F6 C4 40"); // test ah,0x40
1452 EmitJumpIns(vm, "0F 84", Constant4()); // je 0x12345678
1453 break;
1454 case OP_LTF:
1455 EmitString("F6 C4 01"); // test ah,0x01
1456 EmitJumpIns(vm, "0F 85", Constant4()); // jne 0x12345678
1457 break;
1458 case OP_LEF:
1459 EmitString("F6 C4 41"); // test ah,0x41
1460 EmitJumpIns(vm, "0F 85", Constant4()); // jne 0x12345678
1461 break;
1462 case OP_GTF:
1463 EmitString("F6 C4 41"); // test ah,0x41
1464 EmitJumpIns(vm, "0F 84", Constant4()); // je 0x12345678
1465 break;
1466 case OP_GEF:
1467 EmitString("F6 C4 01"); // test ah,0x01
1468 EmitJumpIns(vm, "0F 84", Constant4()); // je 0x12345678
1469 break;
1470 }
1471 break;
1472 case OP_NEGI:
1473 EmitMovEAXStack(vm, 0);
1474 EmitString("F7 D8"); // neg eax
1475 EmitCommand(LAST_COMMAND_MOV_STACK_EAX);
1476 break;
1477 case OP_ADD:
1478 EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
1479 EmitString("01 44 9F FC"); // add dword ptr -4[edi + ebx * 4], eax
1480 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1481 break;
1482 case OP_SUB:
1483 EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
1484 EmitString("29 44 9F FC"); // sub dword ptr -4[edi + ebx * 4], eax
1485 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1486 break;
1487 case OP_DIVI:
1488 EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
1489 EmitString("99"); // cdq
1490 EmitString("F7 3C 9F"); // idiv dword ptr [edi + ebx * 4]
1491 EmitString("89 44 9F FC"); // mov dword ptr -4[edi + ebx * 4],eax
1492 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1493 break;
1494 case OP_DIVU:
1495 EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
1496 EmitString("33 D2"); // xor edx, edx
1497 EmitString("F7 34 9F"); // div dword ptr [edi + ebx * 4]
1498 EmitString("89 44 9F FC"); // mov dword ptr -4[edi + ebx * 4],eax
1499 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1500 break;
1501 case OP_MODI:
1502 EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
1503 EmitString("99" ); // cdq
1504 EmitString("F7 3C 9F"); // idiv dword ptr [edi + ebx * 4]
1505 EmitString("89 54 9F FC"); // mov dword ptr -4[edi + ebx * 4],edx
1506 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1507 break;
1508 case OP_MODU:
1509 EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
1510 EmitString("33 D2"); // xor edx, edx
1511 EmitString("F7 34 9F"); // div dword ptr [edi + ebx * 4]
1512 EmitString("89 54 9F FC"); // mov dword ptr -4[edi + ebx * 4],edx
1513 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1514 break;
1515 case OP_MULI:
1516 EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
1517 EmitString("F7 2C 9F"); // imul dword ptr [edi + ebx * 4]
1518 EmitString("89 44 9F FC"); // mov dword ptr -4[edi + ebx * 4],eax
1519 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1520 break;
1521 case OP_MULU:
1522 EmitString("8B 44 9F FC"); // mov eax,dword ptr -4[edi + ebx * 4]
1523 EmitString("F7 24 9F"); // mul dword ptr [edi + ebx * 4]
1524 EmitString("89 44 9F FC"); // mov dword ptr -4[edi + ebx * 4],eax
1525 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1526 break;
1527 case OP_BAND:
1528 EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
1529 EmitString("21 44 9F FC"); // and dword ptr -4[edi + ebx * 4],eax
1530 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1531 break;
1532 case OP_BOR:
1533 EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
1534 EmitString("09 44 9F FC"); // or dword ptr -4[edi + ebx * 4],eax
1535 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1536 break;
1537 case OP_BXOR:
1538 EmitMovEAXStack(vm, 0); // mov eax, dword ptr [edi + ebx * 4]
1539 EmitString("31 44 9F FC"); // xor dword ptr -4[edi + ebx * 4],eax
1540 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1541 break;
1542 case OP_BCOM:
1543 EmitString("F7 14 9F"); // not dword ptr [edi + ebx * 4]
1544 break;
1545 case OP_LSH:
1546 EmitMovECXStack(vm);
1547 EmitString("D3 64 9F FC"); // shl dword ptr -4[edi + ebx * 4], cl
1548 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1549 break;
1550 case OP_RSHI:
1551 EmitMovECXStack(vm);
1552 EmitString("D3 7C 9F FC"); // sar dword ptr -4[edi + ebx * 4], cl
1553 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1554 break;
1555 case OP_RSHU:
1556 EmitMovECXStack(vm);
1557 EmitString("D3 6C 9F FC"); // shr dword ptr -4[edi + ebx * 4], cl
1558 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1559 break;
1560 case OP_NEGF:
1561 EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
1562 EmitString("D9 E0"); // fchs
1563 EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
1564 break;
1565 case OP_ADDF:
1566 EmitString("D9 44 9F FC"); // fld dword ptr -4[edi + ebx * 4]
1567 EmitString("D8 04 9F"); // fadd dword ptr [edi + ebx * 4]
1568 EmitString("D9 5C 9F FC"); // fstp dword ptr -4[edi + ebx * 4]
1569 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1570 break;
1571 case OP_SUBF:
1572 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1573 EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
1574 EmitString("D8 64 9F 04"); // fsub dword ptr 4[edi + ebx * 4]
1575 EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
1576 break;
1577 case OP_DIVF:
1578 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1579 EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
1580 EmitString("D8 74 9F 04"); // fdiv dword ptr 4[edi + ebx * 4]
1581 EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
1582 break;
1583 case OP_MULF:
1584 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1585 EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
1586 EmitString("D8 4C 9F 04"); // fmul dword ptr 4[edi + ebx * 4]
1587 EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
1588 break;
1589 case OP_CVIF:
1590 EmitString("DB 04 9F"); // fild dword ptr [edi + ebx * 4]
1591 EmitString("D9 1C 9F"); // fstp dword ptr [edi + ebx * 4]
1592 break;
1593 case OP_CVFI:
1594 #ifndef FTOL_PTR // WHENHELLISFROZENOVER
1595 // not IEEE complient, but simple and fast
1596 EmitString("D9 04 9F"); // fld dword ptr [edi + ebx * 4]
1597 EmitString("DB 1C 9F"); // fistp dword ptr [edi + ebx * 4]
1598 #else // FTOL_PTR
1599 // call the library conversion function
1600 EmitRexString(0x48, "BA"); // mov edx, Q_VMftol
1601 EmitPtr(Q_VMftol);
1602 EmitRexString(0x48, "FF D2"); // call edx
1603 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
1604 #endif
1605 break;
1606 case OP_SEX8:
1607 EmitString("0F BE 04 9F"); // movsx eax, byte ptr [edi + ebx * 4]
1608 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
1609 break;
1610 case OP_SEX16:
1611 EmitString("0F BF 04 9F"); // movsx eax, word ptr [edi + ebx * 4]
1612 EmitCommand(LAST_COMMAND_MOV_STACK_EAX); // mov dword ptr [edi + ebx * 4], eax
1613 break;
1614
1615 case OP_BLOCK_COPY:
1616 EmitString("B8"); // mov eax, 0x12345678
1617 Emit4(VM_BLOCK_COPY);
1618 EmitString("B9"); // mov ecx, 0x12345678
1619 Emit4(Constant4());
1620
1621 EmitCallRel(vm, callDoSyscallOfs);
1622
1623 EmitCommand(LAST_COMMAND_SUB_BL_2); // sub bl, 2
1624 break;
1625
1626 case OP_JUMP:
1627 EmitCommand(LAST_COMMAND_SUB_BL_1); // sub bl, 1
1628 EmitString("8B 44 9F 04"); // mov eax, dword ptr 4[edi + ebx * 4]
1629 EmitString("81 F8"); // cmp eax, vm->instructionCount
1630 Emit4(vm->instructionCount);
1631 #if idx64
1632 EmitString("73 04"); // jae +4
1633 EmitRexString(0x49, "FF 24 C0"); // jmp qword ptr [r8 + eax * 8]
1634 #else
1635 EmitString("73 07"); // jae +7
1636 EmitString("FF 24 85"); // jmp dword ptr [instructionPointers + eax * 4]
1637 Emit4((intptr_t) vm->instructionPointers);
1638 #endif
1639 EmitCallErrJump(vm, callDoSyscallOfs);
1640 break;
1641 default:
1642 VMFREE_BUFFERS();
1643 Com_Error(ERR_DROP, "VM_CompileX86: bad opcode %i at offset %i", op, pc);
1644 }
1645 pop0 = pop1;
1646 pop1 = op;
1647 }
1648 }
1649
1650 // copy to an exact sized buffer with the appropriate permission bits
1651 vm->codeLength = compiledOfs;
1652 #ifdef VM_X86_MMAP
1653 vm->codeBase = mmap(NULL, compiledOfs, PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
1654 if(vm->codeBase == MAP_FAILED)
1655 Com_Error(ERR_FATAL, "VM_CompileX86: can't mmap memory");
1656 #elif _WIN32
1657 // allocate memory with EXECUTE permissions under windows.
1658 vm->codeBase = VirtualAlloc(NULL, compiledOfs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1659 if(!vm->codeBase)
1660 Com_Error(ERR_FATAL, "VM_CompileX86: VirtualAlloc failed");
1661 #else
1662 vm->codeBase = malloc(compiledOfs);
1663 if(!vm->codeBase)
1664 Com_Error(ERR_FATAL, "VM_CompileX86: malloc failed");
1665 #endif
1666
1667 Com_Memcpy( vm->codeBase, buf, compiledOfs );
1668
1669 #ifdef VM_X86_MMAP
1670 if(mprotect(vm->codeBase, compiledOfs, PROT_READ|PROT_EXEC))
1671 Com_Error(ERR_FATAL, "VM_CompileX86: mprotect failed");
1672 #elif _WIN32
1673 {
1674 DWORD oldProtect = 0;
1675
1676 // remove write permissions.
1677 if(!VirtualProtect(vm->codeBase, compiledOfs, PAGE_EXECUTE_READ, &oldProtect))
1678 Com_Error(ERR_FATAL, "VM_CompileX86: VirtualProtect failed");
1679 }
1680 #endif
1681
1682 Z_Free( code );
1683 Z_Free( buf );
1684 Z_Free( jused );
1685 Com_Printf( "VM file %s compiled to %i bytes of code\n", vm->name, compiledOfs );
1686
1687 vm->destroy = VM_Destroy_Compiled;
1688
1689 // offset all the instruction pointers for the new location
1690 for ( i = 0 ; i < header->instructionCount ; i++ ) {
1691 vm->instructionPointers[i] += (intptr_t) vm->codeBase;
1692 }
1693 }
1694
1695 void VM_Destroy_Compiled(vm_t* self)
1696 {
1697 #ifdef VM_X86_MMAP
1698 munmap(self->codeBase, self->codeLength);
1699 #elif _WIN32
1700 VirtualFree(self->codeBase, 0, MEM_RELEASE);
1701 #else
1702 free(self->codeBase);
1703 #endif
1704 }
1705
1706 /*
1707 ==============
1708 VM_CallCompiled
1709
1710 This function is called directly by the generated code
1711 ==============
1712 */
1713
1714 #if defined(_MSC_VER) && defined(idx64)
1715 extern uint8_t qvmcall64(int *programStack, int *opStack, intptr_t *instructionPointers, byte *dataBase);
1716 #endif
1717
1718 int VM_CallCompiled(vm_t *vm, int *args)
1719 {
1720 byte stack[OPSTACK_SIZE + 15];
1721 void *entryPoint;
1722 int programStack, stackOnEntry;
1723 byte *image;
1724 int *opStack;
1725 int opStackOfs;
1726 int arg;
1727
1728 currentVM = vm;
1729
1730 // interpret the code
1731 vm->currentlyInterpreting = qtrue;
1732
1733 // we might be called recursively, so this might not be the very top
1734 programStack = stackOnEntry = vm->programStack;
1735
1736 // set up the stack frame
1737 image = vm->dataBase;
1738
1739 programStack -= ( 8 + 4 * MAX_VMMAIN_ARGS );
1740
1741 for ( arg = 0; arg < MAX_VMMAIN_ARGS; arg++ )
1742 *(int *)&image[ programStack + 8 + arg * 4 ] = args[ arg ];
1743
1744 *(int *)&image[ programStack + 4 ] = 0; // return stack
1745 *(int *)&image[ programStack ] = -1; // will terminate the loop on return
1746
1747 // off we go into generated code...
1748 entryPoint = vm->codeBase + vm->entryOfs;
1749 opStack = PADP(stack, 16);
1750 *opStack = 0xDEADBEEF;
1751 opStackOfs = 0;
1752
1753 #ifdef _MSC_VER
1754 #if idx64
1755 opStackOfs = qvmcall64(&programStack, opStack, vm->instructionPointers, vm->dataBase);
1756 #else
1757 __asm
1758 {
1759 pushad
1760
1761 mov esi, dword ptr programStack
1762 mov edi, dword ptr opStack
1763 mov ebx, dword ptr opStackOfs
1764
1765 call entryPoint
1766
1767 mov dword ptr opStackOfs, ebx
1768 mov dword ptr opStack, edi
1769 mov dword ptr programStack, esi
1770
1771 popad
1772 }
1773 #endif
1774 #elif idx64
1775 __asm__ volatile(
1776 "movq %5, %%rax\n"
1777 "movq %3, %%r8\n"
1778 "movq %4, %%r9\n"
1779 "push %%r15\n"
1780 "push %%r14\n"
1781 "push %%r13\n"
1782 "push %%r12\n"
1783 "callq *%%rax\n"
1784 "pop %%r12\n"
1785 "pop %%r13\n"
1786 "pop %%r14\n"
1787 "pop %%r15\n"
1788 : "+S" (programStack), "+D" (opStack), "+b" (opStackOfs)
1789 : "g" (vm->instructionPointers), "g" (vm->dataBase), "g" (entryPoint)
1790 : "cc", "memory", "%rax", "%rcx", "%rdx", "%r8", "%r9", "%r10", "%r11"
1791 );
1792 #else
1793 __asm__ volatile(
1794 "calll *%3\n"
1795 : "+S" (programStack), "+D" (opStack), "+b" (opStackOfs)
1796 : "g" (entryPoint)
1797 : "cc", "memory", "%eax", "%ecx", "%edx"
1798 );
1799 #endif
1800
1801 if(opStackOfs != 1 || *opStack != 0xDEADBEEF)
1802 {
1803 Com_Error(ERR_DROP, "opStack corrupted in compiled code");
1804 }
1805 if(programStack != stackOnEntry - (8 + 4 * MAX_VMMAIN_ARGS))
1806 Com_Error(ERR_DROP, "programStack corrupted in compiled code");
1807
1808 vm->programStack = stackOnEntry;
1809
1810 return opStack[opStackOfs];
1811 }
1812 #endif