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
|
; $Id: PAEand32Bit.mac $
;; @file
; VMM - World Switchers, template for PAE and 32-Bit.
;
;
; Copyright (C) 2006-2019 Oracle Corporation
;
; This file is part of VirtualBox Open Source Edition (OSE), as
; available from http://www.virtualbox.org. This file is free software;
; you can redistribute it and/or modify it under the terms of the GNU
; General Public License (GPL) as published by the Free Software
; Foundation, in version 2 as it comes in the "COPYING" file of the
; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
;
;%define DEBUG_STUFF 1
;*******************************************************************************
;* Header Files *
;*******************************************************************************
%include "VBox/asmdefs.mac"
%include "VBox/apic.mac"
%include "iprt/x86.mac"
%include "VBox/vmm/cpum.mac"
%include "VBox/vmm/stam.mac"
%include "VBox/vmm/vm.mac"
%include "VBox/err.mac"
%include "CPUMInternal.mac"
%include "VMMSwitcher.mac"
%undef NEED_ID
%ifdef NEED_PAE_ON_32BIT_HOST
%define NEED_ID
%endif
%ifdef NEED_32BIT_ON_PAE_HOST
%define NEED_ID
%endif
;
; Start the fixup records
; We collect the fixups in the .data section as we go along
; It is therefore VITAL that no-one is using the .data section
; for anything else between 'Start' and 'End'.
;
BEGINDATA
GLOBALNAME Fixups
BEGINCODE
GLOBALNAME Start
;;
; The C interface.
;
BEGINPROC vmmR0ToRawMode
%ifdef DEBUG_STUFF
COM_S_NEWLINE
COM_S_CHAR '^'
%endif
%ifdef VBOX_WITH_STATISTICS
;
; Switcher stats.
;
FIXUP FIX_HC_VM_OFF, 1, VM.StatSwitcherToGC
mov edx, 0ffffffffh
STAM_PROFILE_ADV_START edx
%endif
;
; Call worker.
;
FIXUP FIX_HC_CPUM_OFF, 1, 0
mov edx, 0ffffffffh
push cs ; allow for far return and restore cs correctly.
call NAME(vmmR0ToRawModeAsm)
%ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
; Restore blocked Local APIC NMI vectors
; Do this here to ensure the host CS is already restored
mov ecx, [edx + CPUMCPU.fApicDisVectors]
test ecx, ecx
jz gth_apic_done
cmp byte [edx + CPUMCPU.fX2Apic], 1
je gth_x2apic
; Legacy xAPIC mode:
mov edx, [edx + CPUMCPU.pvApicBase]
shr ecx, 1
jnc gth_nolint0
and dword [edx + APIC_REG_LVT_LINT0], ~APIC_REG_LVT_MASKED
gth_nolint0:
shr ecx, 1
jnc gth_nolint1
and dword [edx + APIC_REG_LVT_LINT1], ~APIC_REG_LVT_MASKED
gth_nolint1:
shr ecx, 1
jnc gth_nopc
and dword [edx + APIC_REG_LVT_PC], ~APIC_REG_LVT_MASKED
gth_nopc:
shr ecx, 1
jnc gth_notherm
and dword [edx + APIC_REG_LVT_THMR], ~APIC_REG_LVT_MASKED
gth_notherm:
shr ecx, 1
jnc gth_nocmci
and dword [edx + APIC_REG_LVT_CMCI], ~APIC_REG_LVT_MASKED
gth_nocmci:
jmp gth_apic_done
; x2APIC mode:
gth_x2apic:
push eax ; save eax
push ebx ; save it for fApicDisVectors
push edx ; save edx just in case.
mov ebx, ecx ; ebx = fApicDisVectors, ecx free for MSR use
shr ebx, 1
jnc gth_x2_nolint0
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_LINT0 >> 4)
rdmsr
and eax, ~APIC_REG_LVT_MASKED
wrmsr
gth_x2_nolint0:
shr ebx, 1
jnc gth_x2_nolint1
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_LINT1 >> 4)
rdmsr
and eax, ~APIC_REG_LVT_MASKED
wrmsr
gth_x2_nolint1:
shr ebx, 1
jnc gth_x2_nopc
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_PC >> 4)
rdmsr
and eax, ~APIC_REG_LVT_MASKED
wrmsr
gth_x2_nopc:
shr ebx, 1
jnc gth_x2_notherm
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_THMR >> 4)
rdmsr
and eax, ~APIC_REG_LVT_MASKED
wrmsr
gth_x2_notherm:
shr ebx, 1
jnc gth_x2_nocmci
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_CMCI >> 4)
rdmsr
and eax, ~APIC_REG_LVT_MASKED
wrmsr
gth_x2_nocmci:
pop edx
pop ebx
pop eax
gth_apic_done:
%endif ; VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
%ifdef VBOX_WITH_STATISTICS
;
; Switcher stats.
;
FIXUP FIX_HC_VM_OFF, 1, VM.StatSwitcherToHC
mov edx, 0ffffffffh
STAM_PROFILE_ADV_STOP edx
%endif
ret
ENDPROC vmmR0ToRawMode
; *****************************************************************************
; vmmR0ToRawModeAsm
;
; Phase one of the switch from host to guest context (host MMU context)
;
; INPUT:
; - edx virtual address of CPUM structure (valid in host context)
;
; USES/DESTROYS:
; - eax, ecx, edx
;
; ASSUMPTION:
; - current CS and DS selectors are wide open
;
; *****************************************************************************
ALIGNCODE(16)
BEGINPROC vmmR0ToRawModeAsm
;;
;; Save CPU host context
;; Skip eax, edx and ecx as these are not preserved over calls.
;;
CPUMCPU_FROM_CPUM(edx)
; general registers.
mov [edx + CPUMCPU.Host.ebx], ebx
mov [edx + CPUMCPU.Host.edi], edi
mov [edx + CPUMCPU.Host.esi], esi
mov [edx + CPUMCPU.Host.esp], esp
mov [edx + CPUMCPU.Host.ebp], ebp
; selectors.
mov [edx + CPUMCPU.Host.ds], ds
mov [edx + CPUMCPU.Host.es], es
mov [edx + CPUMCPU.Host.fs], fs
mov [edx + CPUMCPU.Host.gs], gs
mov [edx + CPUMCPU.Host.ss], ss
; special registers.
sldt [edx + CPUMCPU.Host.ldtr]
sidt [edx + CPUMCPU.Host.idtr]
sgdt [edx + CPUMCPU.Host.gdtr]
str [edx + CPUMCPU.Host.tr]
; flags
pushfd
pop dword [edx + CPUMCPU.Host.eflags]
%ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
; Block Local APIC NMI vectors
cmp byte [edx + CPUMCPU.fX2Apic], 1
je htg_x2apic
; Legacy xAPIC mode. No write completion required when writing to the
; LVT registers as we have mapped the APIC page non-cacheable and the
; MMIO is CPU-local.
mov ebx, [edx + CPUMCPU.pvApicBase]
or ebx, ebx
jz htg_apic_done
xor edi, edi ; fApicDisVectors
mov eax, [ebx + APIC_REG_LVT_LINT0]
mov ecx, eax
and ecx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ecx, APIC_REG_LVT_MODE_NMI
jne htg_nolint0
or edi, 0x01
or eax, APIC_REG_LVT_MASKED
mov [ebx + APIC_REG_LVT_LINT0], eax
htg_nolint0:
mov eax, [ebx + APIC_REG_LVT_LINT1]
mov ecx, eax
and ecx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ecx, APIC_REG_LVT_MODE_NMI
jne htg_nolint1
or edi, 0x02
or eax, APIC_REG_LVT_MASKED
mov [ebx + APIC_REG_LVT_LINT1], eax
htg_nolint1:
mov eax, [ebx + APIC_REG_LVT_PC]
mov ecx, eax
and ecx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ecx, APIC_REG_LVT_MODE_NMI
jne htg_nopc
or edi, 0x04
or eax, APIC_REG_LVT_MASKED
mov [ebx + APIC_REG_LVT_PC], eax
htg_nopc:
mov eax, [ebx + APIC_REG_VERSION]
shr eax, 16
cmp al, 5
jb htg_notherm
je htg_nocmci
mov eax, [ebx + APIC_REG_LVT_CMCI]
mov ecx, eax
and ecx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ecx, APIC_REG_LVT_MODE_NMI
jne htg_nocmci
or edi, 0x10
or eax, APIC_REG_LVT_MASKED
mov [ebx + APIC_REG_LVT_CMCI], eax
htg_nocmci:
mov eax, [ebx + APIC_REG_LVT_THMR]
mov ecx, eax
and ecx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ecx, APIC_REG_LVT_MODE_NMI
jne htg_notherm
or edi, 0x08
or eax, APIC_REG_LVT_MASKED
mov [ebx + APIC_REG_LVT_THMR], eax
htg_notherm:
mov [edx + CPUMCPU.fApicDisVectors], edi
jmp htg_apic_done
; x2APIC mode:
htg_x2apic:
mov esi, edx ; Save edx.
xor edi, edi ; fApicDisVectors
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_LINT0 >> 4)
rdmsr
mov ebx, eax
and ebx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ebx, APIC_REG_LVT_MODE_NMI
jne htg_x2_nolint0
or edi, 0x01
or eax, APIC_REG_LVT_MASKED
wrmsr
htg_x2_nolint0:
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_LINT1 >> 4)
rdmsr
mov ebx, eax
and ebx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ebx, APIC_REG_LVT_MODE_NMI
jne htg_x2_nolint1
or edi, 0x02
or eax, APIC_REG_LVT_MASKED
wrmsr
htg_x2_nolint1:
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_PC >> 4)
rdmsr
mov ebx, eax
and ebx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ebx, APIC_REG_LVT_MODE_NMI
jne htg_x2_nopc
or edi, 0x04
or eax, APIC_REG_LVT_MASKED
wrmsr
htg_x2_nopc:
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_VERSION >> 4)
rdmsr
shr eax, 16
cmp al, 5
jb htg_x2_notherm
je htg_x2_nocmci
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_CMCI >> 4)
rdmsr
mov ebx, eax
and ebx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ebx, APIC_REG_LVT_MODE_NMI
jne htg_x2_nocmci
or edi, 0x10
or eax, APIC_REG_LVT_MASKED
wrmsr
htg_x2_nocmci:
mov ecx, MSR_IA32_X2APIC_START + (APIC_REG_LVT_THMR >> 4)
rdmsr
mov ebx, eax
and ebx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
cmp ebx, APIC_REG_LVT_MODE_NMI
jne htg_x2_notherm
or edi, 0x08
or eax, APIC_REG_LVT_MASKED
wrmsr
htg_x2_notherm:
mov edx, esi ; Restore edx.
mov [edx + CPUMCPU.fApicDisVectors], edi
htg_apic_done:
%endif ; VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
FIXUP FIX_NO_SYSENTER_JMP, 0, htg_no_sysenter - NAME(Start) ; this will insert a jmp htg_no_sysenter if host doesn't use sysenter.
; save MSR_IA32_SYSENTER_CS register.
mov ecx, MSR_IA32_SYSENTER_CS
mov ebx, edx ; save edx
rdmsr ; edx:eax <- MSR[ecx]
mov [ebx + CPUMCPU.Host.SysEnter.cs], eax
mov [ebx + CPUMCPU.Host.SysEnter.cs + 4], edx
xor eax, eax ; load 0:0 to cause #GP upon sysenter
xor edx, edx
wrmsr
xchg ebx, edx ; restore edx
jmp short htg_no_sysenter
ALIGNCODE(16)
htg_no_sysenter:
FIXUP FIX_NO_SYSCALL_JMP, 0, htg_no_syscall - NAME(Start) ; this will insert a jmp htg_no_syscall if host doesn't use syscall.
; clear MSR_K6_EFER_SCE.
mov ebx, edx ; save edx
mov ecx, MSR_K6_EFER
rdmsr ; edx:eax <- MSR[ecx]
and eax, ~MSR_K6_EFER_SCE
wrmsr
mov edx, ebx ; restore edx
jmp short htg_no_syscall
ALIGNCODE(16)
htg_no_syscall:
;; handle use flags.
mov esi, [edx + CPUMCPU.fUseFlags] ; esi == use flags.
and esi, ~(CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST) ; Clear CPUM_USED_* flags.
mov [edx + CPUMCPU.fUseFlags], esi
; debug registers.
test esi, CPUM_USE_DEBUG_REGS_HYPER | CPUM_USE_DEBUG_REGS_HOST
jnz htg_debug_regs_save_dr7and6
htg_debug_regs_no:
; control registers.
mov eax, cr0
mov [edx + CPUMCPU.Host.cr0], eax
;mov eax, cr2 ; assume host os don't suff things in cr2. (safe)
;mov [edx + CPUMCPU.Host.cr2], eax
mov eax, cr3
mov [edx + CPUMCPU.Host.cr3], eax
mov eax, cr4
mov [edx + CPUMCPU.Host.cr4], eax
;;
;; Start switching to VMM context.
;;
;
; Change CR0 and CR4 so we can correctly emulate FPU/MMX/SSE[23] exceptions
; Also disable WP. (eax==cr4 now)
; Note! X86_CR4_PSE and X86_CR4_PAE are important if the host thinks so :-)
; Note! X86_CR4_VMXE must not be touched in case the CPU is in vmx root mode
;
and eax, X86_CR4_MCE | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_VMXE
mov ecx, [edx + CPUMCPU.Guest.cr4]
;; @todo Switcher cleanup: Determine base CR4 during CPUMR0Init / VMMR3SelectSwitcher putting it
; in CPUMCPU.Hyper.cr4 (which isn't currently being used). That should
; simplify this operation a bit (and improve locality of the data).
;
; CR4.AndMask and CR4.OrMask are set in CPUMR3Init based on the presence of
; FXSAVE and XSAVE support on the host CPU
;
CPUM_FROM_CPUMCPU(edx)
and ecx, [edx + CPUM.CR4.AndMask]
or eax, ecx
or eax, [edx + CPUM.CR4.OrMask]
mov cr4, eax
CPUMCPU_FROM_CPUM(edx)
mov eax, [edx + CPUMCPU.Guest.cr0]
and eax, X86_CR0_EM
or eax, X86_CR0_PE | X86_CR0_PG | X86_CR0_TS | X86_CR0_ET | X86_CR0_NE | X86_CR0_MP
mov cr0, eax
; Load new gdt so we can do far jump to guest code after cr3 reload.
lgdt [edx + CPUMCPU.Hyper.gdtr]
DEBUG_CHAR('1') ; trashes esi
; Store the hypervisor cr3 for later loading
mov ebp, [edx + CPUMCPU.Hyper.cr3]
;;
;; Load Intermediate memory context.
;;
FIXUP SWITCHER_FIX_INTER_CR3_HC, 1
mov eax, 0ffffffffh
mov cr3, eax
DEBUG_CHAR('2') ; trashes esi
%ifdef NEED_ID
;;
;; Jump to identity mapped location
;;
FIXUP FIX_HC_2_ID_NEAR_REL, 1, NAME(IDEnterTarget) - NAME(Start)
jmp near NAME(IDEnterTarget)
; We're now on identity mapped pages!
ALIGNCODE(16)
GLOBALNAME IDEnterTarget
DEBUG_CHAR('3')
mov edx, cr4
%ifdef NEED_PAE_ON_32BIT_HOST
or edx, X86_CR4_PAE
%else
and edx, ~X86_CR4_PAE
%endif
mov eax, cr0
and eax, (~X86_CR0_PG) & 0xffffffff ; prevent yasm warning
mov cr0, eax
DEBUG_CHAR('4')
mov cr4, edx
FIXUP SWITCHER_FIX_INTER_CR3_GC, 1
mov edx, 0ffffffffh
mov cr3, edx
or eax, X86_CR0_PG
DEBUG_CHAR('5')
mov cr0, eax
DEBUG_CHAR('6')
%endif
;;
;; Jump to guest code mapping of the code and load the Hypervisor CS.
;;
FIXUP FIX_GC_FAR32, 1, NAME(FarJmpGCTarget) - NAME(Start)
jmp 0fff8h:0deadfaceh
;;
;; When we arrive at this label we're at the
;; guest code mapping of the switching code.
;;
ALIGNCODE(16)
GLOBALNAME FarJmpGCTarget
DEBUG_CHAR('-')
; load final cr3 and do far jump to load cs.
mov cr3, ebp ; ebp set above
DEBUG_CHAR('0')
;;
;; We're in VMM MMU context and VMM CS is loaded.
;; Setup the rest of the VMM state.
;;
FIXUP FIX_GC_CPUMCPU_OFF, 1, 0
mov edx, 0ffffffffh
; Activate guest IDT
DEBUG_CHAR('1')
lidt [edx + CPUMCPU.Hyper.idtr]
; Load selectors
DEBUG_CHAR('2')
FIXUP FIX_HYPER_DS, 1
mov eax, 0ffffh
mov ds, eax
mov es, eax
xor eax, eax
mov gs, eax
mov fs, eax
; Setup stack.
DEBUG_CHAR('3')
mov eax, [edx + CPUMCPU.Hyper.ss.Sel]
mov ss, ax
mov esp, [edx + CPUMCPU.Hyper.esp]
; Restore TSS selector; must mark it as not busy before using ltr (!)
DEBUG_CHAR('4')
FIXUP FIX_GC_TSS_GDTE_DW2, 2
and dword [0ffffffffh], ~0200h ; clear busy flag (2nd type2 bit)
DEBUG_CHAR('5')
ltr word [edx + CPUMCPU.Hyper.tr.Sel]
DEBUG_CHAR('6')
; Activate the ldt (now we can safely crash).
lldt [edx + CPUMCPU.Hyper.ldtr.Sel]
DEBUG_CHAR('7')
;; use flags.
mov esi, [edx + CPUMCPU.fUseFlags]
; debug registers
test esi, CPUM_USE_DEBUG_REGS_HYPER
jnz htg_debug_regs_guest
htg_debug_regs_guest_done:
DEBUG_CHAR('9')
%ifdef VBOX_WITH_NMI
;
; Setup K7 NMI.
;
mov esi, edx
; clear all PerfEvtSeln registers
xor eax, eax
xor edx, edx
mov ecx, MSR_K7_PERFCTR0
wrmsr
mov ecx, MSR_K7_PERFCTR1
wrmsr
mov ecx, MSR_K7_PERFCTR2
wrmsr
mov ecx, MSR_K7_PERFCTR3
wrmsr
mov eax, RT_BIT(20) | RT_BIT(17) | RT_BIT(16) | 076h
mov ecx, MSR_K7_EVNTSEL0
wrmsr
mov eax, 02329B000h
mov edx, 0fffffffeh ; -1.6GHz * 5
mov ecx, MSR_K7_PERFCTR0
wrmsr
FIXUP FIX_GC_APIC_BASE_32BIT, 1
mov eax, 0f0f0f0f0h
add eax, 0340h ; APIC_LVTPC
mov dword [eax], 0400h ; APIC_DM_NMI
xor edx, edx
mov eax, RT_BIT(20) | RT_BIT(17) | RT_BIT(16) | 076h | RT_BIT(22) ;+EN
mov ecx, MSR_K7_EVNTSEL0
wrmsr
mov edx, esi
%endif
; General registers (sans edx).
mov eax, [edx + CPUMCPU.Hyper.eax]
mov ebx, [edx + CPUMCPU.Hyper.ebx]
mov ecx, [edx + CPUMCPU.Hyper.ecx]
mov ebp, [edx + CPUMCPU.Hyper.ebp]
mov esi, [edx + CPUMCPU.Hyper.esi]
mov edi, [edx + CPUMCPU.Hyper.edi]
DEBUG_S_CHAR('!')
;;
;; Return to the VMM code which either called the switcher or
;; the code set up to run by HC.
;;
push dword [edx + CPUMCPU.Hyper.eflags]
push cs
push dword [edx + CPUMCPU.Hyper.eip]
mov edx, [edx + CPUMCPU.Hyper.edx] ; !! edx is no longer pointing to CPUMCPU here !!
%ifdef DEBUG_STUFF
COM_S_PRINT ';eip='
push eax
mov eax, [esp + 8]
COM_S_DWORD_REG eax
pop eax
COM_S_CHAR ';'
%endif
%ifdef VBOX_WITH_STATISTICS
push edx
FIXUP FIX_GC_VM_OFF, 1, VM.StatSwitcherToGC
mov edx, 0ffffffffh
STAM_PROFILE_ADV_STOP edx
pop edx
%endif
iret ; Use iret to make debugging and TF/RF work.
;;
; Detour for saving the host DR7 and DR6.
; esi and edx must be preserved.
htg_debug_regs_save_dr7and6:
DEBUG_S_CHAR('s');
mov eax, dr7 ; not sure, but if I read the docs right this will trap if GD is set. FIXME!!!
mov [edx + CPUMCPU.Host.dr7], eax
xor eax, eax ; clear everything. (bit 12? is read as 1...)
mov dr7, eax
mov eax, dr6 ; just in case we save the state register too.
mov [edx + CPUMCPU.Host.dr6], eax
jmp htg_debug_regs_no
;;
; Detour for saving host DR0-3 and loading hypervisor debug registers.
; esi and edx must be preserved.
htg_debug_regs_guest:
DEBUG_S_CHAR('D')
DEBUG_S_CHAR('R')
DEBUG_S_CHAR('x')
; save host DR0-3.
mov eax, dr0
mov [edx + CPUMCPU.Host.dr0], eax
mov ebx, dr1
mov [edx + CPUMCPU.Host.dr1], ebx
mov ecx, dr2
mov [edx + CPUMCPU.Host.dr2], ecx
mov eax, dr3
mov [edx + CPUMCPU.Host.dr3], eax
or dword [edx + CPUMCPU.fUseFlags], CPUM_USED_DEBUG_REGS_HOST
; load hyper DR0-7
mov ebx, [edx + CPUMCPU.Hyper.dr]
mov dr0, ebx
mov ecx, [edx + CPUMCPU.Hyper.dr + 8*1]
mov dr1, ecx
mov eax, [edx + CPUMCPU.Hyper.dr + 8*2]
mov dr2, eax
mov ebx, [edx + CPUMCPU.Hyper.dr + 8*3]
mov dr3, ebx
mov ecx, X86_DR6_INIT_VAL
mov dr6, ecx
mov eax, [edx + CPUMCPU.Hyper.dr + 8*7]
mov dr7, eax
or dword [edx + CPUMCPU.fUseFlags], CPUM_USED_DEBUG_REGS_HYPER
jmp htg_debug_regs_guest_done
ENDPROC vmmR0ToRawModeAsm
;;
; Trampoline for doing a call when starting the hyper visor execution.
;
; Push any arguments to the routine.
; Push the argument frame size (cArg * 4).
; Push the call target (_cdecl convention).
; Push the address of this routine.
;
;
ALIGNCODE(16)
BEGINPROC vmmRCCallTrampoline
%ifdef DEBUG_STUFF
COM_S_CHAR 'c'
COM_S_CHAR 't'
COM_S_CHAR '!'
%endif
; call routine
pop eax ; call address
pop edi ; argument count.
%ifdef DEBUG_STUFF
COM_S_PRINT ';eax='
COM_S_DWORD_REG eax
COM_S_CHAR ';'
%endif
call eax ; do call
add esp, edi ; cleanup stack
; return to the host context.
%ifdef DEBUG_STUFF
COM_S_CHAR '`'
%endif
.to_host_again:
call NAME(vmmRCToHostAsm)
mov eax, VERR_VMM_SWITCHER_IPE_1
jmp .to_host_again
ENDPROC vmmRCCallTrampoline
;;
; The C interface.
;
ALIGNCODE(16)
BEGINPROC vmmRCToHost
%ifdef DEBUG_STUFF
push esi
COM_NEWLINE
DEBUG_CHAR('b')
DEBUG_CHAR('a')
DEBUG_CHAR('c')
DEBUG_CHAR('k')
DEBUG_CHAR('!')
COM_NEWLINE
pop esi
%endif
mov eax, [esp + 4]
jmp NAME(vmmRCToHostAsm)
ENDPROC vmmRCToHost
;;
; vmmRCToHostAsmNoReturn
;
; This is an entry point used by TRPM when dealing with raw-mode traps,
; i.e. traps in the hypervisor code. This will not return and saves no
; state, because the caller has already saved the state.
;
; @param eax Return code.
;
ALIGNCODE(16)
BEGINPROC vmmRCToHostAsmNoReturn
DEBUG_S_CHAR('%')
%ifdef VBOX_WITH_STATISTICS
FIXUP FIX_GC_VM_OFF, 1, VM.StatTotalInGC
mov edx, 0ffffffffh
STAM32_PROFILE_ADV_STOP edx
FIXUP FIX_GC_VM_OFF, 1, VM.StatTotalGCToQemu
mov edx, 0ffffffffh
STAM32_PROFILE_ADV_START edx
FIXUP FIX_GC_VM_OFF, 1, VM.StatSwitcherToHC
mov edx, 0ffffffffh
STAM32_PROFILE_ADV_START edx
%endif
FIXUP FIX_GC_CPUMCPU_OFF, 1, 0
mov edx, 0ffffffffh
jmp vmmRCToHostAsm_SaveNoGeneralRegs
ENDPROC vmmRCToHostAsmNoReturn
;;
; vmmRCToHostAsm
;
; This is an entry point used by TRPM to return to host context when an
; interrupt occured or an guest trap needs handling in host context. It
; is also used by the C interface above.
;
; The hypervisor context is saved and it will return to the caller if
; host context so desires.
;
; @param eax Return code.
; @uses eax, edx, ecx (or it may use them in the future)
;
ALIGNCODE(16)
BEGINPROC vmmRCToHostAsm
DEBUG_S_CHAR('%')
push edx
%ifdef VBOX_WITH_STATISTICS
FIXUP FIX_GC_VM_OFF, 1, VM.StatTotalInGC
mov edx, 0ffffffffh
STAM_PROFILE_ADV_STOP edx
FIXUP FIX_GC_VM_OFF, 1, VM.StatTotalGCToQemu
mov edx, 0ffffffffh
STAM_PROFILE_ADV_START edx
FIXUP FIX_GC_VM_OFF, 1, VM.StatSwitcherToHC
mov edx, 0ffffffffh
STAM_PROFILE_ADV_START edx
%endif
;
; Load the CPUMCPU pointer.
;
FIXUP FIX_GC_CPUMCPU_OFF, 1, 0
mov edx, 0ffffffffh
; Save register context.
pop dword [edx + CPUMCPU.Hyper.edx]
pop dword [edx + CPUMCPU.Hyper.eip] ; call return from stack
mov dword [edx + CPUMCPU.Hyper.esp], esp
mov dword [edx + CPUMCPU.Hyper.eax], eax
mov dword [edx + CPUMCPU.Hyper.ebx], ebx
mov dword [edx + CPUMCPU.Hyper.ecx], ecx
mov dword [edx + CPUMCPU.Hyper.esi], esi
mov dword [edx + CPUMCPU.Hyper.edi], edi
mov dword [edx + CPUMCPU.Hyper.ebp], ebp
; special registers which may change.
vmmRCToHostAsm_SaveNoGeneralRegs:
mov edi, eax ; save return code in EDI (careful with COM_DWORD_REG from here on!)
; str [edx + CPUMCPU.Hyper.tr] - double fault only, and it won't be right then either.
sldt [edx + CPUMCPU.Hyper.ldtr.Sel]
; No need to save CRx here. They are set dynamically according to Guest/Host requirements.
; FPU context is saved before restore of host saving (another) branch.
; Disable debug regsiters if active so they cannot trigger while switching.
test dword [edx + CPUMCPU.fUseFlags], CPUM_USED_DEBUG_REGS_HYPER
jz .gth_disabled_dr7
mov eax, X86_DR7_INIT_VAL
mov dr7, eax
.gth_disabled_dr7:
%ifdef VBOX_WITH_NMI
;
; Disarm K7 NMI.
;
mov esi, edx
xor edx, edx
xor eax, eax
mov ecx, MSR_K7_EVNTSEL0
wrmsr
mov edx, esi
%endif
;;
;; Load Intermediate memory context.
;;
mov ecx, [edx + CPUMCPU.Host.cr3]
FIXUP SWITCHER_FIX_INTER_CR3_GC, 1
mov eax, 0ffffffffh
mov cr3, eax
DEBUG_CHAR('?')
;; We're now in intermediate memory context!
%ifdef NEED_ID
;;
;; Jump to identity mapped location
;;
FIXUP FIX_GC_2_ID_NEAR_REL, 1, NAME(IDExitTarget) - NAME(Start)
jmp near NAME(IDExitTarget)
; We're now on identity mapped pages!
ALIGNCODE(16)
GLOBALNAME IDExitTarget
DEBUG_CHAR('1')
mov edx, cr4
%ifdef NEED_PAE_ON_32BIT_HOST
and edx, ~X86_CR4_PAE
%else
or edx, X86_CR4_PAE
%endif
mov eax, cr0
and eax, (~X86_CR0_PG) & 0xffffffff ; prevent yasm warning
mov cr0, eax
DEBUG_CHAR('2')
mov cr4, edx
FIXUP SWITCHER_FIX_INTER_CR3_HC, 1
mov edx, 0ffffffffh
mov cr3, edx
or eax, X86_CR0_PG
DEBUG_CHAR('3')
mov cr0, eax
DEBUG_CHAR('4')
;;
;; Jump to HC mapping.
;;
FIXUP FIX_ID_2_HC_NEAR_REL, 1, NAME(HCExitTarget) - NAME(Start)
jmp near NAME(HCExitTarget)
%else
;;
;; Jump to HC mapping.
;;
FIXUP FIX_GC_2_HC_NEAR_REL, 1, NAME(HCExitTarget) - NAME(Start)
jmp near NAME(HCExitTarget)
%endif
;
; When we arrive here we're at the host context
; mapping of the switcher code.
;
ALIGNCODE(16)
GLOBALNAME HCExitTarget
DEBUG_CHAR('9')
; load final cr3
mov cr3, ecx
DEBUG_CHAR('@')
;;
;; Restore Host context.
;;
; Load CPUM pointer into edx
FIXUP FIX_HC_CPUM_OFF, 1, 0
mov edx, 0ffffffffh
CPUMCPU_FROM_CPUM(edx)
; activate host gdt and idt
lgdt [edx + CPUMCPU.Host.gdtr]
DEBUG_CHAR('0')
lidt [edx + CPUMCPU.Host.idtr]
DEBUG_CHAR('1')
; Restore TSS selector; must mark it as not busy before using ltr (!)
%if 1 ; ASSUME that this is supposed to be 'BUSY'. (saves 20-30 ticks on the T42p)
movzx eax, word [edx + CPUMCPU.Host.tr] ; eax <- TR
and al, 0F8h ; mask away TI and RPL bits, get descriptor offset.
add eax, [edx + CPUMCPU.Host.gdtr + 2] ; eax <- GDTR.address + descriptor offset.
and dword [eax + 4], ~0200h ; clear busy flag (2nd type2 bit)
ltr word [edx + CPUMCPU.Host.tr]
%else
movzx eax, word [edx + CPUMCPU.Host.tr] ; eax <- TR
and al, 0F8h ; mask away TI and RPL bits, get descriptor offset.
add eax, [edx + CPUMCPU.Host.gdtr + 2] ; eax <- GDTR.address + descriptor offset.
mov ecx, [eax + 4] ; ecx <- 2nd descriptor dword
mov ebx, ecx ; save original value
and ecx, ~0200h ; clear busy flag (2nd type2 bit)
mov [eax + 4], ecx ; not using xchg here is paranoia..
ltr word [edx + CPUMCPU.Host.tr]
xchg [eax + 4], ebx ; using xchg is paranoia too...
%endif
; activate ldt
DEBUG_CHAR('2')
lldt [edx + CPUMCPU.Host.ldtr]
; Restore segment registers
mov eax, [edx + CPUMCPU.Host.ds]
mov ds, eax
mov eax, [edx + CPUMCPU.Host.es]
mov es, eax
mov eax, [edx + CPUMCPU.Host.fs]
mov fs, eax
mov eax, [edx + CPUMCPU.Host.gs]
mov gs, eax
; restore stack
lss esp, [edx + CPUMCPU.Host.esp]
FIXUP FIX_NO_SYSENTER_JMP, 0, gth_sysenter_no - NAME(Start) ; this will insert a jmp gth_sysenter_no if host doesn't use sysenter.
; restore MSR_IA32_SYSENTER_CS register.
mov ecx, MSR_IA32_SYSENTER_CS
mov eax, [edx + CPUMCPU.Host.SysEnter.cs]
mov ebx, [edx + CPUMCPU.Host.SysEnter.cs + 4]
xchg edx, ebx ; save/load edx
wrmsr ; MSR[ecx] <- edx:eax
xchg edx, ebx ; restore edx
jmp short gth_sysenter_no
ALIGNCODE(16)
gth_sysenter_no:
FIXUP FIX_NO_SYSCALL_JMP, 0, gth_syscall_no - NAME(Start) ; this will insert a jmp gth_syscall_no if host doesn't use syscall.
; set MSR_K6_EFER_SCE.
mov ebx, edx ; save edx
mov ecx, MSR_K6_EFER
rdmsr
or eax, MSR_K6_EFER_SCE
wrmsr
mov edx, ebx ; restore edx
jmp short gth_syscall_no
ALIGNCODE(16)
gth_syscall_no:
; Restore FPU if guest has used it.
; Using fxrstor should ensure that we're not causing unwanted exception on the host.
mov esi, [edx + CPUMCPU.fUseFlags] ; esi == use flags.
test esi, (CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST)
jz near gth_fpu_no
mov ecx, cr0
and ecx, ~(X86_CR0_TS | X86_CR0_EM)
mov cr0, ecx
mov ebx, edx ; save edx
test esi, CPUM_USED_FPU_GUEST
jz gth_fpu_host
mov eax, [ebx + CPUMCPU.Guest.fXStateMask]
mov ecx, [ebx + CPUMCPU.Guest.pXStateR0]
test eax, eax
jz gth_fpu_guest_fxsave
mov edx, [ebx + CPUMCPU.Guest.fXStateMask + 4]
xsave [ecx]
jmp gth_fpu_host
gth_fpu_guest_fxsave:
fxsave [ecx]
gth_fpu_host:
mov eax, [ebx + CPUMCPU.Host.fXStateMask]
mov ecx, [ebx + CPUMCPU.Host.pXStateR0]
test eax, eax
jz gth_fpu_host_fxrstor
mov edx, [ebx + CPUMCPU.Host.fXStateMask + 4]
xrstor [ecx]
jmp gth_fpu_done
gth_fpu_host_fxrstor:
fxrstor [ecx]
gth_fpu_done:
mov edx, ebx ; restore edx
gth_fpu_no:
; Control registers.
; Would've liked to have these higher up in case of crashes, but
; the fpu stuff must be done before we restore cr0.
mov ecx, [edx + CPUMCPU.Host.cr4]
mov cr4, ecx
mov ecx, [edx + CPUMCPU.Host.cr0]
mov cr0, ecx
;mov ecx, [edx + CPUMCPU.Host.cr2] ; assumes this is a waste of time.
;mov cr2, ecx
; restore debug registers (if modified) (esi must still be fUseFlags!)
; (must be done after cr4 reload because of the debug extension.)
test esi, CPUM_USE_DEBUG_REGS_HYPER | CPUM_USE_DEBUG_REGS_HOST | CPUM_USED_DEBUG_REGS_HOST
jnz gth_debug_regs_restore
gth_debug_regs_done:
; restore general registers.
mov eax, edi ; restore return code. eax = return code !!
mov edi, [edx + CPUMCPU.Host.edi]
mov esi, [edx + CPUMCPU.Host.esi]
mov ebx, [edx + CPUMCPU.Host.ebx]
mov ebp, [edx + CPUMCPU.Host.ebp]
push dword [edx + CPUMCPU.Host.eflags]
popfd
%ifdef DEBUG_STUFF
; COM_S_CHAR '4'
%endif
retf
;;
; Detour for restoring the host debug registers.
; edx and edi must be preserved.
gth_debug_regs_restore:
DEBUG_S_CHAR('d')
mov eax, dr7 ; Some DR7 paranoia first...
mov ecx, X86_DR7_INIT_VAL
cmp eax, ecx
je .gth_debug_skip_dr7_disabling
mov dr7, ecx
.gth_debug_skip_dr7_disabling:
test esi, CPUM_USED_DEBUG_REGS_HOST
jz .gth_debug_regs_dr7
DEBUG_S_CHAR('r')
mov eax, [edx + CPUMCPU.Host.dr0]
mov dr0, eax
mov ebx, [edx + CPUMCPU.Host.dr1]
mov dr1, ebx
mov ecx, [edx + CPUMCPU.Host.dr2]
mov dr2, ecx
mov eax, [edx + CPUMCPU.Host.dr3]
mov dr3, eax
.gth_debug_regs_dr7:
mov ebx, [edx + CPUMCPU.Host.dr6]
mov dr6, ebx
mov ecx, [edx + CPUMCPU.Host.dr7]
mov dr7, ecx
and dword [edx + CPUMCPU.fUseFlags], ~(CPUM_USED_DEBUG_REGS_HOST | CPUM_USED_DEBUG_REGS_HYPER)
jmp gth_debug_regs_done
ENDPROC vmmRCToHostAsm
GLOBALNAME End
;
; The description string (in the text section).
;
NAME(Description):
db SWITCHER_DESCRIPTION
db 0
extern NAME(Relocate)
;
; End the fixup records.
;
BEGINDATA
db FIX_THE_END ; final entry.
GLOBALNAME FixupsEnd
;;
; The switcher definition structure.
ALIGNDATA(16)
GLOBALNAME Def
istruc VMMSWITCHERDEF
at VMMSWITCHERDEF.pvCode, RTCCPTR_DEF NAME(Start)
at VMMSWITCHERDEF.pvFixups, RTCCPTR_DEF NAME(Fixups)
at VMMSWITCHERDEF.pszDesc, RTCCPTR_DEF NAME(Description)
at VMMSWITCHERDEF.pfnRelocate, RTCCPTR_DEF NAME(Relocate)
at VMMSWITCHERDEF.enmType, dd SWITCHER_TYPE
at VMMSWITCHERDEF.cbCode, dd NAME(End) - NAME(Start)
at VMMSWITCHERDEF.offR0ToRawMode, dd NAME(vmmR0ToRawMode) - NAME(Start)
at VMMSWITCHERDEF.offRCToHost, dd NAME(vmmRCToHost) - NAME(Start)
at VMMSWITCHERDEF.offRCCallTrampoline, dd NAME(vmmRCCallTrampoline) - NAME(Start)
at VMMSWITCHERDEF.offRCToHostAsm, dd NAME(vmmRCToHostAsm) - NAME(Start)
at VMMSWITCHERDEF.offRCToHostAsmNoReturn, dd NAME(vmmRCToHostAsmNoReturn) - NAME(Start)
; disasm help
at VMMSWITCHERDEF.offHCCode0, dd 0
%ifdef NEED_ID
at VMMSWITCHERDEF.cbHCCode0, dd NAME(IDEnterTarget) - NAME(Start)
%else
at VMMSWITCHERDEF.cbHCCode0, dd NAME(FarJmpGCTarget) - NAME(Start)
%endif
at VMMSWITCHERDEF.offHCCode1, dd NAME(HCExitTarget) - NAME(Start)
at VMMSWITCHERDEF.cbHCCode1, dd NAME(End) - NAME(HCExitTarget)
%ifdef NEED_ID
at VMMSWITCHERDEF.offIDCode0, dd NAME(IDEnterTarget) - NAME(Start)
at VMMSWITCHERDEF.cbIDCode0, dd NAME(FarJmpGCTarget) - NAME(IDEnterTarget)
at VMMSWITCHERDEF.offIDCode1, dd NAME(IDExitTarget) - NAME(Start)
at VMMSWITCHERDEF.cbIDCode1, dd NAME(HCExitTarget) - NAME(IDExitTarget)
%else
at VMMSWITCHERDEF.offIDCode0, dd 0
at VMMSWITCHERDEF.cbIDCode0, dd 0
at VMMSWITCHERDEF.offIDCode1, dd 0
at VMMSWITCHERDEF.cbIDCode1, dd 0
%endif
at VMMSWITCHERDEF.offGCCode, dd NAME(FarJmpGCTarget) - NAME(Start)
%ifdef NEED_ID
at VMMSWITCHERDEF.cbGCCode, dd NAME(IDExitTarget) - NAME(FarJmpGCTarget)
%else
at VMMSWITCHERDEF.cbGCCode, dd NAME(HCExitTarget) - NAME(FarJmpGCTarget)
%endif
iend
|