forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmmhelper.c
executable file
·2384 lines (1762 loc) · 63 KB
/
vmmhelper.c
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
#include "common.h"
#include "vmmhelper.h"
#include "main.h"
#include "neward.h"
#include "mm.h"
/*
#include "vmmemu.h"
*/
#include "vmcall.h"
#include "vmpaging.h"
#include "vmeventhandler.h"
#include "vmreadwrite.h"
#include "msrnames.h"
#include "vmxcontrolstructures.h"
#include "distorm.h"
#include "realmodeemu.h"
#include "multicore.h"
#include "offloados.h"
#include "vmeventhandler_amd.h"
#include "vmxsetup.h"
#include "epthandler.h"
#ifndef DEBUG
#define sendstringf(s,x...)
#define sendstring(s)
#endif
//cpu specific stuff, put inside structure and give each cpu one
int ISPAGING(pcpuinfo currentcpuinfo)
{
if (isAMD)
{
regCR0 cr0;
cr0.CR0=currentcpuinfo->vmcb->CR0;
return cr0.PG==1;
}
else
{
if (hasUnrestrictedSupport)
return (vmread(vm_guest_cr0) & CR0_PG)==0;
else
return (vmread(vm_cr0_read_shadow) & CR0_PG)==1;
}
}
int ISREALMODE(pcpuinfo currentcpuinfo)
{
if (isAMD)
{
regCR0 cr0;
cr0.CR0=currentcpuinfo->vmcb->CR0;
return cr0.PE==0;
}
else
{
if (hasUnrestrictedSupport)
return (vmread(vm_guest_cr0) & 1)==0;
else
return (vmread(vm_cr0_read_shadow) & 1)==0;
}
}
int IS64BITPAGING(pcpuinfo currentcpuinfo)
{
if (isAMD)
{
return (currentcpuinfo->vmcb->EFER & (1<<10))==(1<<10);
}
else
return ((vmread(vm_entry_controls) & VMENTRYC_IA32E_MODE_GUEST) != 0);
}
int IS64BITCODE(pcpuinfo currentcpuinfo)
{
if (isAMD)
{
Segment_Attribs cs;
cs.SegmentAttrib=currentcpuinfo->vmcb->cs_attrib;
return cs.L;
}
else
return IS64BITPAGING(currentcpuinfo) && ((vmread(vm_guest_cs_access_rights) >> 13) & 1);
}
int isDebugFault(QWORD dr6, QWORD dr7)
//returns 1 if this results in a Fault
{
regDR6 d6;
regDR7 d7;
d6.DR6=dr6;
d7.DR7=dr7;
if ((d6.BD) && (d7.GD))
return 1; //general detect is a fault
else
return 0; //everything else is a trap
}
char * getVMExitReassonString(void)
{
int i=vmread(vm_exit_reason) & 0x7fffffff;
switch (i)
{
case 0: return "Exception or NMI";
case 1: return "External interrupt";
case 2: return "Tripple fault";
case 3: return "INIT Signal";
case 4: return "Start-up IPI (SIPI)";
case 5: return "SMI interrupt";
case 6: return "Other SMI";
case 7: return "Interrupt window";
case 8: return "NMI window";
case 9: return "Task switch";
case 10: return "CPUID";
case 14: return "INVLPG";
case 17: return "VMREAD";
case 18: return "VMCALL";
case 19: return "VMCLEAR";
case 21: return "VMPTRLD";
case 23: return "VMREAD";
case 25: return "VMWRITE";
case 27: return "VMXON"; //or: omg it's one bee
case 28: return "Controlregister access";
case vm_exit_io_access: return "IO Access";
case 31: return "RDMSR";
case 32: return "WRMSR";
case 33: return "Invalid guest state";
case 37: return "Monitor trap flag";
case vm_exit_ept_violation: return "EPT Violation";
case vm_exit_ept_misconfiguration: return "EPT Misconfiguration";
case 52: return "Preemption timer";
case 55: return "XSETBV";
default :return "NYI";
}
}
char * getVMInstructionErrorString(void)
/*
* get the last vm error and return a pointer to the string describing that error
*/
{
int i=vmread(0x4400) & 0x1f;
char *result;
switch (i)
{
case 0: result="No error"; break;
case 1: result="VMCALL executed in VMX root operation"; break;
case 2: result="VMCLEAR with invalid physical address"; break;
case 3: result="VMCLEAR with VMXON pointer"; break;
case 4: result="VMLAUNCH with non-clear VMCS"; break;
case 5: result="VMRESUME with non-launched VMCS"; break;
case 6: result="VMRESUME with a corrupted VMCS"; break;
case 7: result="VM entry with invalid fields"; break;
case 8: result="VM entry with invalid host-state fields"; break;
case 9: result="VMPTRLD with invalid physical address"; break;
case 10: result="VMPTRLD with VMXON pointer"; break;
case 11: result="VMPTRLD with incorrect VMCS revision identifier"; break;
case 12: result="VMREAD/VMWRITE from/to unsupported VMCS component"; break;
case 13: result="VMWRITE to read-only VMCS component"; break;
case 15: result="VMXON executed in VMX root operation"; break;
case 16: result="VM entry with invalid executive-VMCS pointer"; break;
case 17: result="VM entry with non-launched executive-VMCS pointer"; break;
case 18: result="VM entry with executive-VMCS pointer but not VMXON pointer"; break;
case 19: result="VMCALL with non-clear VMCS"; break;
case 20: result="VMCALL with invalid VM-exit control fields"; break;
case 22: result="VMCALL with incorrect MSEG revision number"; break;
case 23: result="VMXOFF under dual-monitor treatment of SMIs and SMM"; break;
case 24: result="VMCALL with invalid SMM-monitor features"; break;
case 25: result="VM entry with invalid VM-execution control fields in executive VMCS"; break;
case 26: result="VM entry with events blocked by MOV SS"; break;
default: result="Undefined"; break;
}
return result;
}
void setTrap(void)
{
UINT64 guestrflags=vmread(vm_guest_rflags);
PRFLAGS pguestrflags=(PRFLAGS)&guestrflags;
pguestrflags->TF=1;
vmwrite(vm_guest_rflags,(UINT64)guestrflags);
}
void setResumeFlag(void)
{
UINT64 guestrflags=vmread(vm_guest_rflags);
PRFLAGS pguestrflags=(PRFLAGS)&guestrflags;
pguestrflags->RF=1;
vmwrite(vm_guest_rflags,(UINT64)guestrflags);
}
void setupTSS8086(void)
// sets up the TSS used for realmode emulation
{
unsigned char *c;
sendstring("Seting up TSS (for VM8086)\n\r");
// setup the TSS for virtual 8086 mode
VirtualMachineTSS_V8086->Previous_Task_Link=0;
VirtualMachineTSS_V8086->Reserved1=0;
VirtualMachineTSS_V8086->ESP0=(ULONG)VirtualToPhysical((void *)((UINT64)RealmodeRing0Stack+4096-16));
VirtualMachineTSS_V8086->SS0=8; //32-bit code segment
VirtualMachineTSS_V8086->Reserved2=0;
VirtualMachineTSS_V8086->ESP1=0;
VirtualMachineTSS_V8086->SS1=0;
VirtualMachineTSS_V8086->Reserved3=0;
VirtualMachineTSS_V8086->ESP2=0;
VirtualMachineTSS_V8086->SS2=0;
VirtualMachineTSS_V8086->Reserved4=0;
VirtualMachineTSS_V8086->CR3=(ULONG)VirtualToPhysical(nonpagedEmulationPagedir);
VirtualMachineTSS_V8086->EIP=0;
VirtualMachineTSS_V8086->EFLAGS=0x33000; //x86 vm
VirtualMachineTSS_V8086->EAX=0;
VirtualMachineTSS_V8086->ECX=0;
VirtualMachineTSS_V8086->EDX=0;
VirtualMachineTSS_V8086->EBX=0;
VirtualMachineTSS_V8086->ESP=0xfff0;
VirtualMachineTSS_V8086->EBP=0;
VirtualMachineTSS_V8086->ESI=0;
VirtualMachineTSS_V8086->EDI=0;
VirtualMachineTSS_V8086->ES=0;
VirtualMachineTSS_V8086->Reserved5=0;
VirtualMachineTSS_V8086->CS=0;
VirtualMachineTSS_V8086->Reserved6=0;
VirtualMachineTSS_V8086->SS=0;
VirtualMachineTSS_V8086->Reserved7=0;
VirtualMachineTSS_V8086->DS=0;
VirtualMachineTSS_V8086->Reserved8=0;
VirtualMachineTSS_V8086->FS=0;
VirtualMachineTSS_V8086->Reserved9=0;
VirtualMachineTSS_V8086->GS=0;
VirtualMachineTSS_V8086->Reserved10=0;
VirtualMachineTSS_V8086->LDTss=0;
VirtualMachineTSS_V8086->Reserved11=0;
VirtualMachineTSS_V8086->Trap=0;
VirtualMachineTSS_V8086->Reserved12=0;
VirtualMachineTSS_V8086->IOBASE=sizeof(TSS)+32;
zeromemory((void *)((UINT64)VirtualMachineTSS_V8086+ sizeof(TSS)),32);
*(ULONG *)(VirtualMachineTSS_V8086+ sizeof(TSS))=0x200000; //int 0x15 break
zeromemory((void *)((UINT64)VirtualMachineTSS_V8086+ sizeof(TSS)+31),8193);
c=(unsigned char *)(VirtualMachineTSS_V8086);
c[sizeof(TSS)+32+8192]=0xff;
//c[sizeof(TSS)+0]=0xff; //ff; //break on 0 to 7
//c[sizeof(TSS)+1]=0xff;
//c[sizeof(TSS)+2]=0xff;
//c[sizeof(TSS)+3]=0xff;
//c[sizeof(TSS)+4]=0xff;
c[sizeof(TSS)+2]=0x20; //0x20=break on int15*/ // 0x28; //break on int15 and int13
}
/*
void exportwholevmstate(void)
{
//this routine will show every vmx register
int orig=nosendchar[getAPICID()];
unsigned int i;
nosendchar[getAPICID()]=0;
sendstring("\n\r\n\r");
sendstring("---------------------------------------------\n\r");
for (i=0x800; i<=0x80e; i+=2)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=0xc00; i<=0xc0c; i+=2)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=0x2000; i<=0x200d; i++)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=0x2010; i<=0x2013; i++)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=0x2800; i<=0x2803; i++)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=0x4000; i<=0x401c; i+=2)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=0x4400; i<=0x440e; i+=2)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=0x4800; i<=0x482a; i+=2)
sendstringf("%x : %8\n\r", i, vmread(i));
sendstringf("%x : %8\n\r", 0x4c00, vmread(0x4c00));
for (i=0x6000; i<=0x600e; i+=2)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=0x6400; i<=0x640a; i+=2)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=vm_guest_cr0; i<=0x6826; i+=2)
sendstringf("%x : %8\n\r", i, vmread(i));
for (i=0x6c00; i<=0x6c16; i+=2)
sendstringf("%x : %8\n\r", i, vmread(i));
sendstring("---------------------------------------------\n\r");
nosendchar[getAPICID()]=orig;
}
*/
void StoreVirtualMachineState(pcpuinfo currentcpuinfo UNUSED, VMRegisters *registers UNUSED)
{
#ifdef DEBUG
vmstates[vmstates_pos].registers=*registers;
vmstates[vmstates_pos].rsp=vmread(vm_guest_rsp);
vmstates[vmstates_pos].rip=vmread(vm_guest_rip);
vmstates[vmstates_pos].rflags=vmread(vm_guest_rflags);
vmstates[vmstates_pos].efer=currentcpuinfo->efer;
vmstates[vmstates_pos].es=vmread(vm_guest_es);
vmstates[vmstates_pos].cs=vmread(vm_guest_cs);
vmstates[vmstates_pos].ss=vmread(vm_guest_ss);
vmstates[vmstates_pos].ds=vmread(vm_guest_ds);
vmstates[vmstates_pos].fs=vmread(vm_guest_fs);
vmstates[vmstates_pos].gs=vmread(vm_guest_gs);
vmstates[vmstates_pos].ldtr=vmread(vm_guest_ldtr);
vmstates[vmstates_pos].tr=vmread(vm_guest_tr);
vmstates[vmstates_pos].es_base=vmread(vm_guest_es_base);
vmstates[vmstates_pos].cs_base=vmread(vm_guest_cs_base);
vmstates[vmstates_pos].ss_base=vmread(vm_guest_ss_base);
vmstates[vmstates_pos].ds_base=vmread(vm_guest_ds_base);
vmstates[vmstates_pos].fs_base=vmread(vm_guest_fs_base);
vmstates[vmstates_pos].gs_base=vmread(vm_guest_gs_base);
vmstates[vmstates_pos].ldtr_base=vmread(vm_guest_ldtr_base);
vmstates[vmstates_pos].tr_base=vmread(vm_guest_tr_base);
vmstates[vmstates_pos].exit_reason=vmread(vm_exit_reason);
vmstates[vmstates_pos].exit_interruptioninfo=vmread(vm_exit_interruptioninfo);
vmstates[vmstates_pos].exit_interruptionerror=vmread(vm_exit_interruptionerror);
vmstates[vmstates_pos].idtvector_information=vmread(vm_idtvector_information);
vmstates[vmstates_pos].idtvector_error=vmread(vm_idtvector_error);
// vmstates[vmstates_pos].cr3=vmread(vm_guest_cr3);
vmstates_pos++;
vmstates_pos=vmstates_pos % 4;
#endif
}
void displayPreviousStates(void)
{
#ifdef DEBUG
//find previous data
int previous_state=(vmstates_pos+2) % 4; //I need the one 2 steps back
int current_state=(vmstates_pos+3) % 4;
int itterator;
sendstringf("vmstates_pos=%d\n",vmstates_pos);
sendstringf("previous_state=%d\n",previous_state);
for (itterator=0; itterator<4; itterator++)
{
sendstringf("%s%d: CS:RIP=%x:%6 (base=%6)\n", ((itterator==previous_state)?"*":""), itterator, vmstates[itterator].cs, vmstates[itterator].rip, vmstates[itterator].cs_base);
sendstringf("%s%d: SS:RSP=%x:%6 (base=%6)\n", ((itterator==previous_state)?"*":""), itterator, vmstates[itterator].ss, vmstates[itterator].rsp, vmstates[itterator].ss_base);
sendstringf("%s%d: RFLAGS=%x\n", ((itterator==previous_state)?"*":""), itterator, vmstates[vmstates_pos].rflags);
sendstringf("%s%d: RAX=%x\n", ((itterator==previous_state)?"*":""), itterator, vmstates[itterator].registers.rax);
sendstringf("%s%d: Exit reason=%8 (%d) \n\r", ((itterator==previous_state)?"*":""),itterator, vmstates[itterator].exit_reason,vmstates[itterator].exit_reason & 0x0fffffff);
sendstringf("%s%d: VM-exit interruption information=%x\n\r", ((itterator==previous_state)?"*":""), itterator, vmstates[itterator].exit_interruptioninfo);
sendstringf("%s%d: VM-exit interruption error code=%x\n\r", ((itterator==previous_state)?"*":""), itterator, vmstates[itterator].exit_interruptionerror);
sendstringf("%s%d: IDT-vectoring information field=%x\n\r", ((itterator==previous_state)?"*":""), itterator,vmstates[itterator].idtvector_information);
sendstringf("%s%d: IDT-vectoring error code=%x\n\r", ((itterator==previous_state)?"*":""), itterator,vmstates[itterator].idtvector_error);
if (itterator!=current_state)
{
sendstringf("%s%d:---exit---\n", ((itterator==previous_state)?"*":""), itterator);
sendstringf("%s%d: CS:RIP=%x:%6 (base=%6)\n", ((itterator==previous_state)?"*":""), itterator, vmstates[itterator].exit_cs, vmstates[itterator].exit_rip, vmstates[itterator].exit_cs_base);
sendstringf("%s%d: SS:RSP=%x:%6 (base=%6)\n", ((itterator==previous_state)?"*":""), itterator, vmstates[itterator].exit_ss, vmstates[itterator].exit_rsp, vmstates[itterator].exit_ss_base);
sendstringf("%s%d: RFLAGS=%x\n", ((itterator==previous_state)?"*":""), itterator, vmstates[vmstates_pos].exit_rflags);
}
}
#endif
}
void sendvmstate(pcpuinfo currentcpuinfo UNUSED, VMRegisters *registers UNUSED)
{
#ifdef DEBUG
if (isAMD)
{
UINT64 rflags=currentcpuinfo->vmcb->RFLAGS;
PRFLAGS prflags=(PRFLAGS)&rflags;
sendstringf("CPL=%d\n", currentcpuinfo->vmcb->CPL);
if (registers) // print registers
{
sendstringf("RAX=%6 RBX=%6 R8=%6\n\r", currentcpuinfo->vmcb->RAX, registers->rbx, registers->r8);
sendstringf("RCX=%6 RDX=%6 R9=%6\n\r", registers->rcx, registers->rdx, registers->r9);
sendstringf("RSI=%6 RDI=%6 R10=%6\n\r",registers->rsi, registers->rdi, registers->r10);
sendstringf("RBP=%6 R11=%6\n\r",registers->rbp, registers->r11);
}
else
sendstring("\n...no registers...\n\n");
sendstringf("RSP=%6 R12=%6\n\r",currentcpuinfo->vmcb->RSP, registers?registers->r12:0);
sendstringf("RIP=%6 R13=%6\n\r",currentcpuinfo->vmcb->RIP, registers?registers->r13:0);
sendstringf(" R14=%6\n\r", registers?registers->r14:0);
sendstringf(" R15=%6\n\r", registers?registers->r15:0);
sendstringf("rflags=%6 (VM=%d RF=%d IOPL=%d NT=%d)\n\r",rflags,prflags->VM, prflags->RF, prflags->IOPL, prflags->NT);
sendstringf("(CF=%d PF=%d AF=%d ZF=%d SF=%d TF=%d IF=%d DF=%d OF=%d)\n\r\n\r", prflags->CF, prflags->PF, prflags->AF, prflags->ZF, prflags->SF, prflags->TF, prflags->IF, prflags->DF, prflags->OF);
sendstringf("cs=%8 (base=%6 , limit=%8, AT=%8)\n\r",currentcpuinfo->vmcb->cs_selector, currentcpuinfo->vmcb->cs_base, currentcpuinfo->vmcb->cs_limit, currentcpuinfo->vmcb->cs_attrib);
sendstringf("ss=%8 (base=%6 , limit=%8, AT=%8)\n\r",currentcpuinfo->vmcb->ss_selector, currentcpuinfo->vmcb->ss_base, currentcpuinfo->vmcb->ss_limit, currentcpuinfo->vmcb->ss_attrib);
sendstringf("ds=%8 (base=%6 , limit=%8, AT=%8)\n\r",currentcpuinfo->vmcb->ds_selector, currentcpuinfo->vmcb->ds_base, currentcpuinfo->vmcb->ds_limit, currentcpuinfo->vmcb->ds_attrib);
sendstringf("es=%8 (base=%6 , limit=%8, AT=%8)\n\r",currentcpuinfo->vmcb->es_selector, currentcpuinfo->vmcb->es_base, currentcpuinfo->vmcb->es_limit, currentcpuinfo->vmcb->es_attrib);
sendstringf("fs=%8 (base=%6 , limit=%8, AT=%8)\n\r",currentcpuinfo->vmcb->fs_selector, currentcpuinfo->vmcb->fs_base, currentcpuinfo->vmcb->fs_limit, currentcpuinfo->vmcb->fs_attrib);
sendstringf("gs=%8 (base=%6 , limit=%8, AT=%8)\n\r",currentcpuinfo->vmcb->gs_selector, currentcpuinfo->vmcb->gs_base, currentcpuinfo->vmcb->gs_limit, currentcpuinfo->vmcb->gs_attrib);
sendstringf("ldt=%8 (base=%6 , limit=%8, AT=%8)\n\r",currentcpuinfo->vmcb->ldtr_selector, currentcpuinfo->vmcb->ldtr_base, currentcpuinfo->vmcb->ldtr_limit, currentcpuinfo->vmcb->ldtr_attrib);
sendstringf("tr=%8 (base=%6 , limit=%8, AT=%8)\n\r",currentcpuinfo->vmcb->tr_selector, currentcpuinfo->vmcb->tr_base, currentcpuinfo->vmcb->tr_limit, currentcpuinfo->vmcb->tr_attrib);
sendstringf("\n\r");
sendstringf("gdt: base=%6 limit=%x\n\r",currentcpuinfo->vmcb->gdtr_base, currentcpuinfo->vmcb->gdtr_limit);
sendstringf("idt: base=%6 limit=%x\n\r",currentcpuinfo->vmcb->idtr_base, currentcpuinfo->vmcb->idtr_limit);
sendstringf("cr0=%6 cr3=%6 cr4=%6\n\r",currentcpuinfo->vmcb->CR0, currentcpuinfo->vmcb->CR3, currentcpuinfo->vmcb->CR4);
}
else
{
UINT64 rflags=vmread(vm_guest_rflags);
PRFLAGS prflags=(PRFLAGS)&rflags;
sendstringf("cpunr=%d\n\r", currentcpuinfo->cpunr);
sendstringf("getTaskRegister()=%x\n",getTaskRegister());
sendstringf("Activity state : %d interruptibility state : %d \n\r",vmread(vm_guest_activity_state), vmread(vm_guest_interruptability_state));
sendstringf("IS64BITPAGING=%d IS64BITCODE=%d ISREALMODE=%d\n\r", IS64BITPAGING(currentcpuinfo), IS64BITCODE(currentcpuinfo), ISREALMODE(currentcpuinfo));
if (hasUnrestrictedSupport)
sendstringf("efer=%x\n\r",vmread(vm_guest_IA32_EFER));
else
sendstringf("efer=%x (%x)\n\r",currentcpuinfo->efer, vmread(vm_guest_IA32_EFER));
sendstringf("ia32e mode guest=%d\n",((vmread(vm_entry_controls) & VMENTRYC_IA32E_MODE_GUEST) != 0) );
sendstringf("IA32_SYSENTER_CS=%x IA32_SYSENTER_EIP=%x IA32_SYSENTER_ESP=%x\n",vmread(vm_guest_IA32_SYSENTER_CS), vmread(vm_guest_IA32_SYSENTER_EIP), vmread(vm_guest_IA32_SYSENTER_ESP) );
if (registers) // print registers
{
sendstringf("RAX=%6 RBX=%6 R8=%6\n\r", registers->rax, registers->rbx, registers->r8);
sendstringf("RCX=%6 RDX=%6 R9=%6\n\r", registers->rcx, registers->rdx, registers->r9);
sendstringf("RSI=%6 RDI=%6 R10=%6\n\r",registers->rsi, registers->rdi, registers->r10);
sendstringf("RBP=%6 R11=%6\n\r",registers->rbp, registers->r11);
}
else
sendstring("\n...no registers...\n\n");
sendstringf("RSP=%6 R12=%6\n\r",vmread(vm_guest_rsp), registers?registers->r12:0);
sendstringf("RIP=%6 R13=%6\n\r",vmread(vm_guest_rip), registers?registers->r13:0);
sendstringf(" R14=%6\n\r", registers?registers->r14:0);
sendstringf(" R15=%6\n\r", registers?registers->r15:0);
sendstringf("rflags=%6 (VM=%d RF=%d IOPL=%d NT=%d)\n\r",rflags,prflags->VM, prflags->RF, prflags->IOPL, prflags->NT);
sendstringf("(CF=%d PF=%d AF=%d ZF=%d SF=%d TF=%d IF=%d DF=%d OF=%d)\n\r\n\r", prflags->CF, prflags->PF, prflags->AF, prflags->ZF, prflags->SF, prflags->TF, prflags->IF, prflags->DF, prflags->OF);
if (currentcpuinfo->invalidcs)
{
sendstring("Invalid cs...\n\r");
}
sendstringf("cs=%8 (base=%6 , limit=%8, AR=%8)\n\r",vmread(0x802),vmread(vm_guest_cs_base),vmread(vm_guest_cs_limit), vmread(0x4816));
sendstringf("ss=%8 (base=%6 , limit=%8, AR=%8)\n\r",vmread(0x804),vmread(vm_guest_ss_base),vmread(vm_guest_ss_limit), vmread(0x4818));
sendstringf("ds=%8 (base=%6 , limit=%8, AR=%8)\n\r",vmread(0x806),vmread(0x680c),vmread(0x4806), vmread(0x481a));
sendstringf("es=%8 (base=%6 , limit=%8, AR=%8)\n\r",vmread(0x800),vmread(0x6806),vmread(0x4800), vmread(0x4814));
sendstringf("fs=%8 (base=%6 , limit=%8, AR=%8)\n\r",vmread(0x808),vmread(0x680e),vmread(0x4808), vmread(0x481c));
sendstringf("gs=%8 (base=%6 , limit=%8, AR=%8)\n\r",vmread(0x80a),vmread(0x6810),vmread(0x480a), vmread(0x481e));
sendstringf("ldt=%8 (base=%6 , limit=%8, AR=%8)\n\r",vmread(0x80c),vmread(0x6812),vmread(0x480c), vmread(0x4820));
sendstringf("tr=%8 (base=%6 , limit=%8, AR=%8)\n\r",vmread(0x80e),vmread(0x6814),vmread(0x480e), vmread(0x4822));
sendstringf("\n\r");
sendstringf("gdt: base=%6 limit=%x\n\r",vmread(vm_guest_gdtr_base), vmread(vm_guest_gdt_limit));
sendstringf("idt: base=%6 limit=%x\n\r",vmread(vm_guest_idtr_base), vmread(vm_guest_idt_limit));
if (ISREALMODE(currentcpuinfo))
{
sendstringf("RM gdt: base=%6 limit=%x\n\r",currentcpuinfo->RealMode.GDTBase, currentcpuinfo->RealMode.GDTLimit);
sendstringf("RM idt: base=%6 limit=%x\n\r",currentcpuinfo->RealMode.IDTBase, currentcpuinfo->RealMode.IDTLimit);
}
regDR7 dr7;
dr7.DR7=vmread(vm_guest_dr7);
sendstringf("guest: dr0=%6 dr1=%6 dr2=%6 \n\r dr3=%6 dr6=%6 dr7=%6\n\r",getDR0(), getDR1(), getDR2(), getDR3(), getDR6(), dr7.DR7);
if (dr7.DR7 != 0x400)
{
sendstringf("dr7:");
if (dr7.G0)
sendstringf("G0 ");
if (dr7.L0)
sendstringf("L0 ");
if (dr7.G1)
sendstringf("G1 ");
if (dr7.L1)
sendstringf("L1 ");
if (dr7.G2)
sendstringf("G2 ");
if (dr7.L2)
sendstringf("L2 ");
if (dr7.G3)
sendstringf("G3 ");
if (dr7.L3)
sendstringf("L3 ");
if (dr7.LE)
sendstringf("LE ");
if (dr7.GE)
sendstringf("GE ");
if (dr7.RW0)
sendstringf("RW0 ");
if (dr7.LEN0)
sendstringf("LEN0 ");
if (dr7.RW1)
sendstringf("RW1 ");
if (dr7.LEN1)
sendstringf("LEN1 ");
if (dr7.RW2)
sendstringf("RW2 ");
if (dr7.LEN2)
sendstringf("LEN2 ");
if (dr7.RW3)
sendstringf("RW3 ");
if (dr7.LEN3)
sendstringf("LEN3 ");
}
sendstringf("host dr7=%6\n\r", getDR7());
sendstringf("cr2=%6\n\r",getCR2());
sendstringf("real:\n\r");
sendstringf("cr0=%6 cr3=%6 cr4=%6\n\r",vmread(vm_guest_cr0), vmread(vm_guest_cr3), vmread(vm_guest_cr4));
sendstringf("fake (what vm sees):\n\r");
QWORD fakeCR0=(vmread(vm_guest_cr0) & (~vmread(vm_cr0_guest_host_mask))) | (vmread(vm_cr0_read_shadow) & vmread(vm_cr0_guest_host_mask));
QWORD fakeCR4=(vmread(vm_guest_cr4) & (~vmread(vm_cr4_guest_host_mask))) | (vmread(vm_cr4_read_shadow) & vmread(vm_cr4_guest_host_mask));
if (hasUnrestrictedSupport)
{
sendstringf("vm_cr0_guest_host_mask=%6 vm_cr0_read_shadow=%6\n", vmread(vm_cr0_guest_host_mask), vmread(vm_cr0_read_shadow));
sendstringf("vm_cr4_guest_host_mask=%6 vm_cr4_read_shadow=%6\n", vmread(vm_cr4_guest_host_mask), vmread(vm_cr4_read_shadow));
sendstringf("cr0=%6 cr3=%6 cr4=%6\n\r",fakeCR0, currentcpuinfo->guestCR3, fakeCR4);
}
else
sendstringf("cr0=%6 cr3=%6 cr4=%6\n\r",vmread(vm_cr0_read_shadow), currentcpuinfo->guestCR3, vmread(vm_cr4_read_shadow));
}
if (currentcpuinfo->vmxdata.insideVMXRootMode)
{
sendstringf("VMXON=%6\n\r", currentcpuinfo->vmxdata.guest_vmxonaddress);
sendstringf("VMCS=%6\n\r", currentcpuinfo->vmxdata.guest_activeVMCS);
sendstringf("running=%d\n\r", currentcpuinfo->vmxdata.runningvmx);
}
#endif
sendstringf("vm_execution_controls_cpu=%6\n", vmread(vm_execution_controls_cpu));
if (vmread(vm_execution_controls_cpu) & SECONDARY_EXECUTION_CONTROLS)
{
sendstringf("vm_execution_controls_cpu_secondary=%6 (unrestricted=%d)\n", vmread(vm_execution_controls_cpu_secondary), (vmread(vm_execution_controls_cpu_secondary) & SPBEF_ENABLE_UNRESTRICTED)!=0);
}
}
//int autocont=8;
int twister=0;
//int guestwantstoknow=0;
#if DISPLAYDEBUG==1
int verbosity=10;
#else
int verbosity=1;
#endif
int rotations=0;
int cpu2=0; //debug to stop cpu1 when cpu2 is spawned
int vmeventcount=0;
criticalSection vmexitlock;
int vmexit_amd(pcpuinfo currentcpuinfo, UINT64 *registers, void *fxsave UNUSED)
{
// displayline("vmexit_amd called. currentcpuinfo=%p\n", currentcpuinfo);
// displayline("cpunr=%d\n", currentcpuinfo->cpunr);
int result=0;
nosendchar[getAPICID()]=1;
#ifdef DEBUG
csEnter(&vmexitlock);
sendstringf("vmexit_amd for cpu %d\n", currentcpuinfo->cpunr);
#endif
result=handleVMEvent_amd(currentcpuinfo, (VMRegisters*)registers);
#ifdef DEBUG
csLeave(&vmexitlock);
#endif
return result;
}
#ifdef DEBUG
QWORD lastbeat=0;
int vmexit2(pcpuinfo currentcpuinfo, UINT64 *registers, void *fxsave);
int vmexit(pcpuinfo currentcpuinfo, UINT64 *registers, void *fxsave)
{
int result;
if (_rdtsc()>(lastbeat+100000000ULL))
{
nosendchar[getAPICID()]=0;
enableserial();
//sendstringf("*Alive*\n");
lastbeat=_rdtsc();
}
//debug code
csEnter(&vmexitlock);
int used_vmstates_pos=vmstates_pos;
StoreVirtualMachineState(currentcpuinfo, (VMRegisters*)registers); //store the event and all other information
result=vmexit2(currentcpuinfo, registers, fxsave);
vmstates[used_vmstates_pos].exit_cs=vmread(vm_guest_cs);
vmstates[used_vmstates_pos].exit_cs_base=vmread(vm_guest_cs_base);
vmstates[used_vmstates_pos].exit_ss=vmread(vm_guest_ss);
vmstates[used_vmstates_pos].exit_ss_base=vmread(vm_guest_ss_base);
vmstates[used_vmstates_pos].exit_rip=vmread(vm_guest_rip);
vmstates[used_vmstates_pos].exit_rsp=vmread(vm_guest_rsp);
vmstates[used_vmstates_pos].exit_rflags=vmread(vm_guest_rflags);
//vmstates[used_vmstates_pos].exit_cr3=vmread(vm_guest_cr3);
if ((result) && ((result >> 8)!=0xce))
{
nosendchar[getAPICID()]=0;
sendvmstate(currentcpuinfo, (VMRegisters*)registers);
}
csLeave(&vmexitlock);
return result;
}
int vmexit2(pcpuinfo currentcpuinfo, UINT64 *registers, void *fxsave)
#else
int vmexit(pcpuinfo currentcpuinfo, UINT64 *registers, void *fxsave)
#endif
{
if (currentcpuinfo==NULL)
{
nosendchar[getAPICID()]=0;
sendstringf("currentcpuinfo==NULL");
while (1);
}
if (currentcpuinfo->vmxdata.runningvmx)
{
nosendchar[getAPICID()]=1;
return handleVMEvent(currentcpuinfo, (VMRegisters*)registers, fxsave);
}
if (hasUnrestrictedSupport) //do this till I have added support for all efer read spots
{
//currentcpuinfo->efer=vmread(vm_guest_IA32_EFER);
currentcpuinfo->guestCR0=vmread(vm_guest_cr0);
//currentcpuinfo->guestCR4=vmread(vm_guest_cr4);
}
//if (ISPAGING(currentcpuinfo)==1)
// currentcpuinfo->guestCR3=vmread(vm_guest_cr3);
// nosendchar[getAPICID()]=1;
//return handleVMEvent(currentcpuinfo, (VMRegisters*)registers, fxsave);
int skip=0;
vmeventcount++;
/*
//check if it's a (dos)timer event
if ((vmread(vm_exit_reason)==0) && (vmread(vm_exit_interruptioninfo)==0x80000b0d) && (vmread(vm_idtvector_information)==0x80000008))
{
return handleVMEvent(currentcpuinfo, (VMRegisters*)registers, fxsave);
}
//check if it's a pre-emptiontimer event
if (vmread(vm_exit_reason)==vm_exit_vmx_preemptiontimer_reachedzero)
{
return handleVMEvent(currentcpuinfo, (VMRegisters*)registers, fxsave);
}
*/
int result;
#ifndef DEBUG
volatile int wait=1; //debug var
// currentdisplayline=currentcpuinfo->cpunr+1;
// currentdisplayrow=0;
// displayline("%d: %d:%x (%x,%x) \n",currentcpuinfo->cpunr,vmeventcount,vmread(vm_exit_reason),vmread(vm_guest_cs),vmread(vm_guest_rip));
nosendchar[getAPICID()]=1;
result=handleVMEvent(currentcpuinfo, (VMRegisters*)registers, fxsave);
if (currentcpuinfo->NMIOccured==2) //nmi occured but no NMI window support
{
currentcpuinfo->NMIOccured=0;
return raiseNMI();
}
if ((result!=0) && ((result >> 8) != 0xce) )//on release, if an unexpected event happens, just fail the instruction and hope the OS won't make a too big mess out of it
{
while (wait) ;
return raiseInvalidOpcodeException(currentcpuinfo);
}
else
return result;
#else
//nosendchar[getAPICID()]=0;
//sendstringf("%x:%x\n",vmread(vm_guest_cs),vmread(vm_guest_rip));
//nosendchar[getAPICID()]=1;
#endif
UINT64 initialcount UNUSED;
//char lastevent[15];
int userbreak=0;
//DWORD before=*tocheck;
nosendchar[getAPICID()]=0;
//sendstring("vmexit\n\r");
initialcount=1;
//_cpuid(&a,&b,&c,&d);
/*
if (a)
{
userbreak=1;
nosendchar[getAPICID()]=1;
}
*/
if (vmread(vm_pending_debug_exceptions))
userbreak=1;
/*
if (vmread(0x4402)==30)
{
nosendchar[getAPICID()]=1;
result=handleVMEvent(currentcpuinfo, (VMRegisters*)registers);
return result;
}*/
if (IntHandlerDebug==0)
{
enableserial();
char b=getchar();
if (b=='b')
userbreak=1;
}
char command;
//stop all timers
/*
nosendchar[getAPICID()]=0;
if (i==0)
printstring("-",0,2,15,0);
else
if (i==1)
printstring("\\",0,2,15,0);
else
if (i==2)
printstring("|",0,2,15,0);
else
if (i==3)
printstring("/",0,2,15,0);
else
if (i==4)
printstring("-",0,2,15,0);
else
if (i==5)
printstring("\\",0,2,15,0);
else
if (i==6)
printstring("|",0,2,15,0);
else
if (i==7)
printstring("/",0,2,15,0);
i++;
if (i==8)
{
i=0;
rotations++;
if (rotations==500)
{
sendstring("+");
rotations=0;
}
}
twister=i;
*/
/*
nosendchar[getAPICID()]=1;
result=handleVMEvent(currentcpuinfo, (VMRegisters*)registers);
nosendchar[getAPICID()]=0;
//sendstring("Handled event\n\r");
return result;
*/
//if (vmread(0x6808)==0x2b0000)
// userbreak=1;
//if (DidHLT==0)
//debugbreak();
// showall=1;
//if (currentcpuinfo->cpunr)
// showall=1;
if (userbreak)
{
sendstring("user wants to break\n\r");
verbosity=10;
}
else
if ((vmread(0x4402)==0) && ((vmread(vm_exit_interruptioninfo) & 0x8000000f)==0x80000001) )
{
//int1 bp
//sendstringf("Int 1 bp");
}
else
//if (!showall)
{
switch (vmread(vm_exit_reason))
{
case vm_exit_vmresume:
//skip=1;
break;
case vm_exit_vmcall:
//skip=currentcpuinfo->cpunr==0;
//sendstring("VMCALL\n");
skip=1;
break;
case vm_exit_vmread:
//skip=1;
break;
case vm_exit_vmptrld:
//
break;