-
Notifications
You must be signed in to change notification settings - Fork 4
/
linux-uek5-v4.14.35-1902.303.5.3-kvm.patch
8591 lines (8119 loc) · 354 KB
/
linux-uek5-v4.14.35-1902.303.5.3-kvm.patch
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
From c606e6bade9d2695b473fe8b687dce2d44ececb4 Mon Sep 17 00:00:00 2001
From: Dongli Zhang <[email protected]>
Date: Mon, 8 Feb 2021 08:50:33 -0800
Subject: [PATCH 1/1] linux-uek5-v4.14.35-1902.303.5.3-kvm
Signed-off-by: Dongli Zhang <[email protected]>
---
arch/x86/include/asm/kvm_host.h | 17 +
arch/x86/include/asm/spec-ctrl.h | 5 +
arch/x86/include/asm/vmx.h | 22 +
arch/x86/include/uapi/asm/kvm.h | 11 +
arch/x86/kernel/cpu/bugs.c | 29 +
arch/x86/kernel/reboot.c | 5 +
arch/x86/kvm/kvm_cache_regs.h | 47 +
arch/x86/kvm/lapic.c | 8 +
arch/x86/kvm/mmu.c | 79 ++
arch/x86/kvm/vmx.c | 1335 +++++++++++++++++++
arch/x86/kvm/vmx_evmcs.h | 9 +
arch/x86/kvm/x86.c | 34 +
arch/x86/kvm/x86.h | 12 +
drivers/acpi/osl.c | 56 +
drivers/net/tap.c | 3 +
drivers/net/tun.c | 31 +
drivers/net/virtio_net.c | 4 +
drivers/pci/hotplug/acpiphp_glue.c | 7 +
drivers/pci/probe.c | 16 +
drivers/pci/setup-bus.c | 4 +
drivers/scsi/scsi_lib.c | 10 +
drivers/scsi/virtio_scsi.c | 47 +
drivers/vhost/net.c | 309 +++++
drivers/vhost/scsi.c | 143 ++
drivers/vhost/vhost.c | 1367 ++++++++++++++++++++
drivers/vhost/vhost.h | 160 +++
drivers/virtio/virtio_pci_modern.c | 21 +
drivers/virtio/virtio_ring.c | 210 +++
drivers/xen/xenbus/xenbus_probe_frontend.c | 18 +
fs/eventfd.c | 81 ++
include/kvm/iodev.h | 32 +
include/linux/irq.h | 13 +
include/linux/kvm_host.h | 26 +
include/linux/ptr_ring.h | 286 ++++
include/linux/skb_array.h | 153 ++-
include/scsi/scsi_cmnd.h | 13 +
include/uapi/linux/virtio_ring.h | 10 +
kernel/irq/chip.c | 13 +
kernel/irq/spurious.c | 3 +
kernel/rcu/tree.c | 5 +
lib/nmi_backtrace.c | 8 +
net/core/dev.c | 5 +
virt/kvm/eventfd.c | 157 +++
virt/kvm/kvm_main.c | 94 ++
44 files changed, 4917 insertions(+), 1 deletion(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index f1ad9c115d79..a06c3f2b0420 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -66,6 +66,16 @@
#define KVM_REQ_MASTERCLOCK_UPDATE KVM_ARCH_REQ(13)
#define KVM_REQ_MCLOCK_INPROGRESS \
KVM_ARCH_REQ_FLAGS(14, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
+/*
+ * 在以下使用KVM_REQ_SCAN_IOAPIC:
+ * - arch/x86/kvm/hyperv.c|130| <<synic_set_sint>> kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
+ * - arch/x86/kvm/x86.c|7379| <<kvm_make_scan_ioapic_request>> kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
+ * - arch/x86/kvm/x86.c|7536| <<vcpu_enter_guest>> if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
+ *
+ * KVM_REQ_SCAN_IOAPIC = 0x0000000000000317
+ * KVM_REQ_MMU_RELOAD = 0x0000000000000301
+ * KVM_REQUEST_MASK = 0x00000000000000ff
+ */
#define KVM_REQ_SCAN_IOAPIC \
KVM_ARCH_REQ_FLAGS(15, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
#define KVM_REQ_GLOBAL_CLOCK_UPDATE KVM_ARCH_REQ(16)
@@ -1423,6 +1433,13 @@ enum {
#define HF_VINTR_MASK (1 << 2)
#define HF_NMI_MASK (1 << 3)
#define HF_IRET_MASK (1 << 4)
+/*
+ * 在以下使用HF_GUEST_MASK:
+ * - arch/x86/kvm/kvm_cache_regs.h|95| <<enter_guest_mode>> vcpu->arch.hflags |= HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|100| <<leave_guest_mode>> vcpu->arch.hflags &= ~HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|110| <<is_guest_mode>> return vcpu->arch.hflags & HF_GUEST_MASK;
+ * - arch/x86/kvm/x86.c|5911| <<init_emulate_ctxt>> BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
+ */
#define HF_GUEST_MASK (1 << 5) /* VCPU is in guest-mode */
#define HF_SMM_MASK (1 << 6)
#define HF_SMM_INSIDE_NMI_MASK (1 << 7)
diff --git a/arch/x86/include/asm/spec-ctrl.h b/arch/x86/include/asm/spec-ctrl.h
index 5393babc0598..7cd01b5b0e65 100644
--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -23,6 +23,11 @@ extern void x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bo
*
* Avoids writing to the MSR if the content/bits are the same
*/
+/*
+ * called by:
+ * - arch/x86/kvm/svm.c|5611| <<svm_vcpu_run>> x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
+ * - arch/x86/kvm/vmx.c|10997| <<vmx_vcpu_run>> x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
+ */
static inline
void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
{
diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h
index 9527ba5d62da..7d676417b21c 100644
--- a/arch/x86/include/asm/vmx.h
+++ b/arch/x86/include/asm/vmx.h
@@ -73,6 +73,10 @@
#define SECONDARY_EXEC_RDRAND_EXITING 0x00000800
#define SECONDARY_EXEC_ENABLE_INVPCID 0x00001000
#define SECONDARY_EXEC_ENABLE_VMFUNC 0x00002000
+/*
+ * If this control is 1, executions of VMREAD and VMWRITE in VMX non-root operation
+ * may access a shadow VMCS (instead of causing VM exits).
+ */
#define SECONDARY_EXEC_SHADOW_VMCS 0x00004000
#define SECONDARY_EXEC_ENCLS_EXITING 0x00008000
#define SECONDARY_EXEC_RDSEED_EXITING 0x00010000
@@ -220,6 +224,18 @@ enum vmcs_field {
TSC_MULTIPLIER_HIGH = 0x00002033,
GUEST_PHYSICAL_ADDRESS = 0x00002400,
GUEST_PHYSICAL_ADDRESS_HIGH = 0x00002401,
+ /*
+ * 在以下使用VMCS_LINK_POINTER (L0设置vmcs的地址直接vmptrld指令吧?只有L1的shadow才用这里):
+ * - arch/x86/kvm/vmx.c|1396| <<global>> FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
+ * - arch/x86/kvm/vmx_evmcs.h|71| <<global>> EVMCS1_FIELD(VMCS_LINK_POINTER, vmcs_link_pointer,
+ * - tools/testing/selftests/kvm/include/vmx.h|217| <<global>> VMCS_LINK_POINTER = 0x00002800,
+ * - arch/x86/kvm/vmx.c|7098| <<vmx_vcpu_setup>> vmcs_write64(VMCS_LINK_POINTER, -1ull);
+ * - arch/x86/kvm/vmx.c|9080| <<vmx_disable_shadow_vmcs>> vmcs_write64(VMCS_LINK_POINTER, -1ull);
+ * - arch/x86/kvm/vmx.c|9694| <<set_current_vmptr>> vmcs_write64(VMCS_LINK_POINTER,
+ * - arch/x86/kvm/vmx.c|13120| <<prepare_vmcs02_full>> vmcs_write64(VMCS_LINK_POINTER, -1ull);
+ * - tools/testing/selftests/kvm/lib/vmx.c|222| <<init_vmcs_guest_state>> vmwrite(VMCS_LINK_POINTER, -1ll);
+ * - tools/testing/selftests/kvm/state_test.c|84| <<l1_guest_code>> vmwrite(VMCS_LINK_POINTER, vmx_pages->shadow_vmcs_gpa);
+ */
VMCS_LINK_POINTER = 0x00002800,
VMCS_LINK_POINTER_HIGH = 0x00002801,
GUEST_IA32_DEBUGCTL = 0x00002802,
@@ -507,6 +523,12 @@ enum vmcs_field {
#define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
#define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
#define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
+/*
+ * VMPTRLD指令从内存中加载一个64位物理地址作为current-VMCS pointer,
+ * 这个current-VMCS pointer由处理器内部记录和维护,除了VMXON,VMPTRLD
+ * 和VMCLEAR指令需要提供VMXON或VMCS指针作为操作数外,其他的指令指定
+ * 都是在current-VMS上操作.
+ */
#define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
#define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
#define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h
index fd23d5778ea1..faaec4cc01cc 100644
--- a/arch/x86/include/uapi/asm/kvm.h
+++ b/arch/x86/include/uapi/asm/kvm.h
@@ -386,6 +386,17 @@ struct kvm_sync_regs {
#define KVM_STATE_NESTED_SMM_VMXON 0x00000002
struct kvm_vmx_nested_state {
+ /*
+ * 在以下使用kvm_vmx_nested_state->vmxon_pa (__u64):
+ * - arch/x86/kvm/vmx.c|15375| <<vmx_get_nested_state>> .vmx.vmxon_pa = -1ull,
+ * - arch/x86/kvm/vmx.c|15386| <<vmx_get_nested_state>> kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
+ * - arch/x86/kvm/vmx.c|15470| <<vmx_set_nested_state>> return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
+ * - arch/x86/kvm/vmx.c|15472| <<vmx_set_nested_state>> if (kvm_state->vmx.vmxon_pa == -1ull) {
+ * - arch/x86/kvm/vmx.c|15483| <<vmx_set_nested_state>> if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
+ * - arch/x86/kvm/vmx.c|15507| <<vmx_set_nested_state>> if (kvm_state->vmx.vmxon_pa == -1ull)
+ * - arch/x86/kvm/vmx.c|15510| <<vmx_set_nested_state>> vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
+ * - arch/x86/kvm/vmx.c|15519| <<vmx_set_nested_state>> if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
+ */
__u64 vmxon_pa;
__u64 vmcs_pa;
diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index ed472e0bd6f8..b79b7e73bcd4 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -383,6 +383,35 @@ void x86_spec_ctrl_set(enum spec_ctrl_set_context context)
}
EXPORT_SYMBOL_GPL(x86_spec_ctrl_set);
+/*
+ * 多了两个MSR,IA32_SPEC_CTRL和IA32_PRED_CMD,IBRS和STIBP通过前一个MSR控制,IBPB通过后一个MSR控制.
+ *
+ * IBRS和STIBP是一种control,IBPB是一种command,具体来说,就是IBRS和STIBP会有状态信息,而IBPB是一种瞬时值.
+ *
+ * IBRS: Indirect Branch Restricted Speculation,一般情况下,在高权限
+ * 代码里面向IBRS的控制位写1,就能够保证indirect branch不被低权限时
+ * 候train出来的predictor影响,也能够防止逻辑处理器的影响(超线程的时
+ * 候).这里权限转换就是host user->host kernel, guest->host等等.可以
+ * 把IBRS理解成不同特权级之间的predictor隔离.IBRS不能防止同一个级别
+ * 的predictor共享,需要配合IBPB.IBRS也不能防止RSB的污染,需要在进入特
+ * 权级的时候情况RSB.
+ *
+ * STIBP: Single thread indirect branch predictors, 超线程中,一个core
+ * 的逻辑处理器会共享一个indirect branch predictor,STIBP就是禁止这种
+ * 共享,防止一个逻辑处理器的predictor被另一个污染.STIBP是IBRS的一个子
+ * 集,所以一般开启了IBRS就不用开STIBP了.
+ *
+ * IBPB: Indirect Branch Predictor Barrier (IBPB): IBPB类似于一个barrier,
+ * 在这之前的indirect branch predictor不会影响这之后的.
+ *
+ * IBRS和IBPB可以结合起来一起作为spectre变种2的mitigation: IBRS用于防
+ * 止权限之间的predictor污染,IBPB用来阻止同一个权限下不同的实体之间的
+ * predictor污染(比如应用程序之间或者虚拟机之间).
+ *
+ * called by:
+ * - arch/x86/include/asm/spec-ctrl.h|29| <<x86_spec_ctrl_set_guest>> x86_virt_spec_ctrl(guest_spec_ctrl, guest_virt_spec_ctrl, true);
+ * - arch/x86/include/asm/spec-ctrl.h|43| <<x86_spec_ctrl_restore_host>> x86_virt_spec_ctrl(guest_spec_ctrl, guest_virt_spec_ctrl, false);
+ */
void
x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
{
diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c
index d6b58653cf1f..a978202f985f 100644
--- a/arch/x86/kernel/reboot.c
+++ b/arch/x86/kernel/reboot.c
@@ -780,6 +780,11 @@ static nmi_shootdown_cb shootdown_callback;
static atomic_t waiting_for_crash_ipi;
static int crash_ipi_issued;
+/*
+ * called by:
+ * - arch/x86/kernel/reboot.c|833| <<nmi_shootdown_cpus>> if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
+ * - arch/x86/kernel/reboot.c|864| <<run_crash_ipi_callback>> crash_nmi_callback(0, regs);
+ */
static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
{
int cpu;
diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h
index 9619dcc2b325..23ec9905f603 100644
--- a/arch/x86/kvm/kvm_cache_regs.h
+++ b/arch/x86/kvm/kvm_cache_regs.h
@@ -64,6 +64,11 @@ static inline ulong kvm_read_cr4_bits(struct kvm_vcpu *vcpu, ulong mask)
ulong tmask = mask & KVM_POSSIBLE_CR4_GUEST_BITS;
if (tmask & vcpu->arch.cr4_guest_owned_bits)
kvm_x86_ops->decache_cr4_guest_bits(vcpu);
+ /*
+ * struct kvm_vcpu *vcpu:
+ * -> struct kvm_vcpu_arch arch;
+ * -> unsigned long cr4;
+ */
return vcpu->arch.cr4 & mask;
}
@@ -85,13 +90,41 @@ static inline u64 kvm_read_edx_eax(struct kvm_vcpu *vcpu)
| ((u64)(kvm_register_read(vcpu, VCPU_REGS_RDX) & -1u) << 32);
}
+/*
+ * 为vcpu->arch.hflags设置HF_GUEST_MASK, 在以下使用HF_GUEST_MASK:
+ * - arch/x86/kvm/kvm_cache_regs.h|95| <<enter_guest_mode>> vcpu->arch.hflags |= HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|100| <<leave_guest_mode>> vcpu->arch.hflags &= ~HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|110| <<is_guest_mode>> return vcpu->arch.hflags & HF_GUEST_MASK;
+ * - arch/x86/kvm/x86.c|5911| <<init_emulate_ctxt>> BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
+ */
static inline void enter_guest_mode(struct kvm_vcpu *vcpu)
{
+ /*
+ * 在以下使用HF_GUEST_MASK:
+ * - arch/x86/kvm/kvm_cache_regs.h|95| <<enter_guest_mode>> vcpu->arch.hflags |= HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|100| <<leave_guest_mode>> vcpu->arch.hflags &= ~HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|110| <<is_guest_mode>> return vcpu->arch.hflags & HF_GUEST_MASK;
+ * - arch/x86/kvm/x86.c|5911| <<init_emulate_ctxt>> BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
+ */
vcpu->arch.hflags |= HF_GUEST_MASK;
}
+/*
+ * 为vcpu->arch.hflags清除HF_GUEST_MASK, 在以下使用HF_GUEST_MASK:
+ * - arch/x86/kvm/kvm_cache_regs.h|95| <<enter_guest_mode>> vcpu->arch.hflags |= HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|100| <<leave_guest_mode>> vcpu->arch.hflags &= ~HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|110| <<is_guest_mode>> return vcpu->arch.hflags & HF_GUEST_MASK;
+ * - arch/x86/kvm/x86.c|5911| <<init_emulate_ctxt>> BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
+ */
static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
{
+ /*
+ * 在以下使用HF_GUEST_MASK:
+ * - arch/x86/kvm/kvm_cache_regs.h|95| <<enter_guest_mode>> vcpu->arch.hflags |= HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|100| <<leave_guest_mode>> vcpu->arch.hflags &= ~HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|110| <<is_guest_mode>> return vcpu->arch.hflags & HF_GUEST_MASK;
+ * - arch/x86/kvm/x86.c|5911| <<init_emulate_ctxt>> BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
+ */
vcpu->arch.hflags &= ~HF_GUEST_MASK;
if (vcpu->arch.load_eoi_exitmap_pending) {
@@ -100,8 +133,22 @@ static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
}
}
+/*
+ * 判断为vcpu->arch.hflags是否设置了HF_GUEST_MASK, 在以下使用HF_GUEST_MASK:
+ * - arch/x86/kvm/kvm_cache_regs.h|95| <<enter_guest_mode>> vcpu->arch.hflags |= HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|100| <<leave_guest_mode>> vcpu->arch.hflags &= ~HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|110| <<is_guest_mode>> return vcpu->arch.hflags & HF_GUEST_MASK;
+ * - arch/x86/kvm/x86.c|5911| <<init_emulate_ctxt>> BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
+ */
static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
{
+ /*
+ * 在以下使用HF_GUEST_MASK:
+ * - arch/x86/kvm/kvm_cache_regs.h|95| <<enter_guest_mode>> vcpu->arch.hflags |= HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|100| <<leave_guest_mode>> vcpu->arch.hflags &= ~HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|110| <<is_guest_mode>> return vcpu->arch.hflags & HF_GUEST_MASK;
+ * - arch/x86/kvm/x86.c|5911| <<init_emulate_ctxt>> BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
+ */
return vcpu->arch.hflags & HF_GUEST_MASK;
}
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index e2e4807f4836..1bb1a3556f02 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -44,6 +44,14 @@
#include "cpuid.h"
#include "hyperv.h"
+/*
+ * smp_kvm_posted_intr_ipi() Posted-interrupt notification event
+ *
+ * smp_kvm_posted_intr_wakeup_ipi() Nested posted-interrupt event
+ *
+ * smp_kvm_posted_intr_nested_ipi() Posted-interrupt wakeup event
+ */
+
#ifndef CONFIG_X86_64
#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
#else
diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index d3626520bb7b..15bf0c980819 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -5014,6 +5014,79 @@ static void init_kvm_softmmu(struct kvm_vcpu *vcpu)
context->inject_page_fault = kvm_inject_page_fault;
}
+/*
+ * 在L1中创建L2的时候会调用几次kvm_init_mmu()
+ *
+ * kvm_init_mmu
+ * prepare_vmcs02
+ * enter_vmx_non_root_mode
+ * nested_vmx_run
+ * handle_vmlaunch
+ * __dta_vmx_handle_exit_444
+ * __dta_vcpu_enter_guest_1349
+ * kvm_arch_vcpu_ioctl_run
+ * kvm_vcpu_ioctl
+ * do_vfs_ioctl
+ * sys_ioctl
+ * do_syscall_64
+ * entry_SYSCALL_64_after_hwframe
+ *
+ * kvm_init_mmu
+ * load_vmcs12_mmu_host_state
+ * load_vmcs12_host_state
+ * __dta_nested_vmx_vmexit_416
+ * vmx_check_nested_events
+ * kvm_arch_vcpu_ioctl_run
+ * kvm_vcpu_ioctl
+ * do_vfs_ioctl
+ * sys_ioctl
+ * do_syscall_64
+ * entry_SYSCALL_64_after_hwframe
+ *
+ * kvm_init_mmu
+ * load_vmcs12_mmu_host_state
+ * load_vmcs12_host_state
+ * __dta_nested_vmx_vmexit_416
+ * nested_ept_inject_page_fault
+ * ept_page_fault
+ * kvm_mmu_page_fault
+ * handle_ept_violation
+ * __dta_vmx_handle_exit_444
+ * __dta_vcpu_enter_guest_1349
+ * kvm_arch_vcpu_ioctl_run
+ * kvm_vcpu_ioctl
+ * do_vfs_ioctl
+ * sys_ioctl
+ * do_syscall_64
+ * entry_SYSCALL_64_after_hwframe
+ *
+ * kvm_init_mmu
+ * load_vmcs12_mmu_host_state
+ * load_vmcs12_host_state
+ * __dta_nested_vmx_vmexit_416
+ * __dta_vmx_handle_exit_444
+ * __dta_vcpu_enter_guest_1349
+ * kvm_arch_vcpu_ioctl_run
+ * kvm_vcpu_ioctl
+ * do_vfs_ioctl
+ * sys_ioctl
+ * do_syscall_64
+ * entry_SYSCALL_64_after_hwframe
+ *
+ * kvm_init_mmu
+ * prepare_vmcs02
+ * enter_vmx_non_root_mode
+ * nested_vmx_run
+ * handle_vmresume
+ * __dta_vmx_handle_exit_444
+ * __dta_vcpu_enter_guest_1349
+ * kvm_arch_vcpu_ioctl_run
+ * kvm_vcpu_ioctl
+ * do_vfs_ioctl
+ * sys_ioctl
+ * do_syscall_64
+ * entry_SYSCALL_64_after_hwframe
+ */
static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
{
struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
@@ -5057,6 +5130,12 @@ static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
update_last_nonleaf_level(vcpu, g_context);
}
+/*
+ * called by:
+ * - arch/x86/kvm/mmu.c|5169| <<kvm_mmu_reset_context>> kvm_init_mmu(vcpu, true);
+ * - arch/x86/kvm/mmu.c|5659| <<kvm_mmu_setup>> kvm_init_mmu(vcpu, false);
+ * - arch/x86/kvm/vmx.c|12143| <<nested_vmx_load_cr3>> kvm_init_mmu(vcpu, false);
+ */
void kvm_init_mmu(struct kvm_vcpu *vcpu, bool reset_roots)
{
if (reset_roots) {
diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 18331ebf892f..6c4b43146c91 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -62,6 +62,94 @@
#include "pmu.h"
#include "vmx_evmcs.h"
+/*
+ * 有一个结构是struct vmcs.
+ *
+ * struct vmcs {
+ * struct vmcs_hdr hdr;
+ * u32 abort;
+ * char data[0];
+ * };
+ *
+ * 在alloc_vmcs_cpu()分配的时候是:
+ *
+ * pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
+ *
+ *
+ * loaded_vmcs points to the VMCS currently used in this vcpu. For a
+ * non-nested (L1) guest, it always points to vmcs01. For a nested
+ * guest (L2), it points to a different VMCS. loaded_cpu_state points
+ * to the VMCS whose state is loaded into the CPU registers that only
+ * need to be switched when transitioning to/from the kernel; a NULL
+ * value indicates that host state is loaded.
+ *
+ * struct vcpu_vmx:
+ * -> struct kvm_vcpu vcpu;
+ * -> u8 fail;
+ * -> struct loaded_vmcs vmcs01;
+ * -> struct vmcs *vmcs;
+ * -> struct vmcs *shadow_vmcs;
+ * -> int cpu;
+ * -> struct loaded_vmcs *loaded_vmcs;
+ * -> struct loaded_vmcs *loaded_cpu_state;
+ * -> u32 exit_reason;
+ * -> struct nested_vmx nested;
+ * -> struct vmcs12 *cached_vmcs12;
+ * -> struct vmcs12 *cached_shadow_vmcs12;
+ * -> struct loaded_vmcs vmcs02;
+ * -> struct vmcs *vmcs;
+ * -> struct vmcs *shadow_vmcs;
+ * -> int cpu;
+ *
+ * ----------------------------------------
+ *
+ * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
+ * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
+ * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
+ * guest in a way that will both be appropriate to L1's requests, and our
+ * needs. In addition to modifying the active vmcs (which is vmcs02), this
+ * function also has additional necessary side-effects, like setting various
+ * vcpu->arch fields.
+ *
+ * 假设是L1中要vmresume.
+ *
+ * 1. 为从L0进入L2做准备.
+ *
+ * handle_vmresume()
+ * -> nested_vmx_run(vcpu, false);
+ *
+ * copy_shadow_to_vmcs12(vmx)会把vmx->vmcs01.shadow_vmcs(类型vmcs)的数据同
+ * 步到vmx->nested.cached_vmcs12(类型vmcs12) 这里的shadow vmcs是L1中用来支
+ * 持L2的vmcs.
+ *
+ * Copy the writable VMCS shadow fields back to the VMCS12, in case
+ * they have been modified by the L1 guest. Note that the "read-only"
+ * VM-exit information fields are actually writable if the vCPU is
+ * configured to support "VMWRITE to any supported field in the VMCS."
+ *
+ *
+ * enter_vmx_non_root_mode()
+ * -> enter_guest_mode(vcpu) --> 为vcpu->arch.hflags设置HF_GUEST_MASK
+ * -> vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); --> 把vmx->loaded_vmcs设置成参数的vmx->nested.vmcs02
+ * -> prepare_vmcs02()
+ *
+ * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
+ * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
+ * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
+ * guest in a way that will both be appropriate to L1's requests, and our
+ * needs. In addition to modifying the active vmcs (which is vmcs02), this
+ * function also has additional necessary side-effects, like setting various
+ * vcpu->arch fields.
+ *
+ * 2. 从L0进入L2.
+ *
+ * vmx_vcpu_run()
+ * -> copy_vmcs12_to_shadow() --> 把vmx->nested.cached_vmcs12(类型vmcs12)给同步到vmx->vmcs01.shadow_vmcs(类型vmcs)
+ * 这里为了在L1修改针对L2的vmcs时不用trap到L0了
+ * -> vmx->__launched = vmx->loaded_vmcs->launched;
+ * -> 进入guest mode (直接vmcs02的L2)
+ */
+
#define __ex(x) __kvm_handle_fault_on_reboot(x)
#define __ex_clear(x, reg) \
____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
@@ -362,6 +450,14 @@ struct kvm_vmx {
struct vmcs_hdr {
u32 revision_id:31;
+ /*
+ * 在以下使用vmcs_hdr->shadow_vmcs:
+ * - arch/x86/kvm/vmx.c|4999| <<alloc_vmcs_cpu>> vmcs->hdr.shadow_vmcs = 1;
+ * - arch/x86/kvm/vmx.c|13341| <<nested_vmx_check_vmcs_link_ptr>> shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
+ * - arch/x86/kvm/vmx.c|9481| <<handle_vmptrld>> (new_vmcs12->hdr.shadow_vmcs &&
+ * - arch/x86/kvm/vmx.c|13540| <<nested_vmx_run>> if (vmcs12->hdr.shadow_vmcs) {
+ * - arch/x86/kvm/vmx.c|15014| <<vmx_set_nested_state>> !shadow_vmcs12->hdr.shadow_vmcs)
+ */
u32 shadow_vmcs:1;
};
@@ -395,16 +491,57 @@ struct vmcs_host_state {
*/
struct loaded_vmcs {
struct vmcs *vmcs;
+ /*
+ * 在以下修改loaded_vmcs->shadow_vmcs:
+ * - arch/x86/kvm/vmx.c|4878| <<alloc_loaded_vmcs>> loaded_vmcs->shadow_vmcs = NULL;
+ * - arch/x86/kvm/vmx.c|8410| <<alloc_shadow_vmcs>> loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
+ * - arch/x86/kvm/vmx.c|8627| <<free_nested>> vmx->vmcs01.shadow_vmcs = NULL;
+ * 在以下使用loaded_vmcs->shadow_vmcs:
+ * - arch/x86/kvm/vmx.c|2203| <<loaded_vmcs_init>> if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
+ * - arch/x86/kvm/vmx.c|2204| <<loaded_vmcs_init>> vmcs_clear(loaded_vmcs->shadow_vmcs);
+ * - arch/x86/kvm/vmx.c|4864| <<free_loaded_vmcs>> WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
+ * - arch/x86/kvm/vmx.c|8407| <<alloc_shadow_vmcs>> WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
+ * - arch/x86/kvm/vmx.c|8409| <<alloc_shadow_vmcs>> if (!loaded_vmcs->shadow_vmcs) {
+ * - arch/x86/kvm/vmx.c|8411| <<alloc_shadow_vmcs>> if (loaded_vmcs->shadow_vmcs)
+ * - arch/x86/kvm/vmx.c|8412| <<alloc_shadow_vmcs>> vmcs_clear(loaded_vmcs->shadow_vmcs);
+ * - arch/x86/kvm/vmx.c|8414| <<alloc_shadow_vmcs>> return loaded_vmcs->shadow_vmcs;
+ * - arch/x86/kvm/vmx.c|8625| <<free_nested>> vmcs_clear(vmx->vmcs01.shadow_vmcs);
+ * - arch/x86/kvm/vmx.c|8626| <<free_nested>> free_vmcs(vmx->vmcs01.shadow_vmcs);
+ * - arch/x86/kvm/vmx.c|8807| <<copy_shadow_to_vmcs12>> struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
+ * - arch/x86/kvm/vmx.c|8864| <<copy_vmcs12_to_shadow>> struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
+ * - arch/x86/kvm/vmx.c|9062| <<set_current_vmptr>> __pa(vmx->vmcs01.shadow_vmcs));
+ *
+ * 用set_current_vmptr()把shadow_vmcs给硬件
+ */
struct vmcs *shadow_vmcs;
int cpu;
+ /*
+ * 在以下使用loaded_vmcs->launched:
+ * - arch/x86/kvm/vmx.c|2236| <<loaded_vmcs_init>> loaded_vmcs->launched = 0;
+ * - arch/x86/kvm/vmx.c|11243| <<vmx_vcpu_run>> vmx->loaded_vmcs->launched = 1;
+ * - arch/x86/kvm/vmx.c|2233| <<loaded_vmcs_init>> if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
+ * - arch/x86/kvm/vmx.c|11027| <<vmx_vcpu_run>> vmx->__launched = vmx->loaded_vmcs->launched;
+ */
bool launched;
bool nmi_known_unmasked;
bool hv_timer_armed;
/* Support for vnmi-less CPUs */
int soft_vnmi_blocked;
+ /*
+ * 在以下使用loaded_vmcs->entry_time:
+ * - arch/x86/kvm/vmx.c|10858| <<vmx_recover_nmi_blocking>> vmx->loaded_vmcs->entry_time));
+ * - arch/x86/kvm/vmx.c|11003| <<vmx_vcpu_run>> vmx->loaded_vmcs->entry_time = ktime_get();
+ */
ktime_t entry_time;
s64 vnmi_blocked_time;
unsigned long *msr_bitmap;
+ /*
+ * 在以下使用loaded_vmcs->loaded_vmcss_on_cpu_link:
+ * - arch/x86/kvm/vmx.c|2455| <<crash_vmclear_local_loaded_vmcss>> loaded_vmcss_on_cpu_link)
+ * - arch/x86/kvm/vmx.c|2473| <<__loaded_vmcs_clear>> list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
+ * - arch/x86/kvm/vmx.c|3370| <<vmx_vcpu_load>> list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
+ * - arch/x86/kvm/vmx.c|4771| <<vmclear_local_loaded_vmcss>> loaded_vmcss_on_cpu_link)
+ */
struct list_head loaded_vmcss_on_cpu_link;
struct vmcs_host_state host_state;
};
@@ -811,34 +948,110 @@ struct nested_vmx_msrs {
*/
struct nested_vmx {
/* Has the level1 guest done vmxon? */
+ /*
+ * 在以下修改nested_vmx->vmxon:
+ * - arch/x86/kvm/vmx.c|8566| <<enter_vmx_operation>> vmx->nested.vmxon = true;
+ * - arch/x86/kvm/vmx.c|8751| <<free_nested>> vmx->nested.vmxon = false;
+ * - arch/x86/kvm/vmx.c|14524| <<vmx_pre_enter_smm>> vmx->nested.vmxon = false;
+ * - arch/x86/kvm/vmx.c|14535| <<vmx_pre_leave_smm>> vmx->nested.vmxon = true;
+ * - arch/x86/kvm/vmx.c|14716| <<vmx_set_nested_state>> vmx->nested.vmxon = false;
+ */
bool vmxon;
+ /*
+ * 在以下使用nested_vmx->vmxon_ptr:
+ * - arch/x86/kvm/vmx.c|8660| <<handle_vmon>> vmx->nested.vmxon_ptr = vmptr;
+ * - arch/x86/kvm/vmx.c|8828| <<handle_vmclear>> if (vmptr == vmx->nested.vmxon_ptr) {
+ * - arch/x86/kvm/vmx.c|9285| <<handle_vmptrld>> if (vmptr == vmx->nested.vmxon_ptr) {
+ * - arch/x86/kvm/vmx.c|14575| <<vmx_get_nested_state>> kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
+ * - arch/x86/kvm/vmx.c|14699| <<vmx_set_nested_state>> vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
+ */
gpa_t vmxon_ptr;
bool pml_full;
/* The guest-physical address of the current VMCS L1 keeps for L2 */
+ /*
+ * 在以下修改nested_vmx->current_vmptr:
+ * - arch/x86/kvm/vmx.c|8657| <<nested_release_vmcs12>> vmx->nested.current_vmptr = -1ull;
+ * - arch/x86/kvm/vmx.c|8682| <<free_nested>> vmx->nested.current_vmptr = -1ull;
+ * - arch/x86/kvm/vmx.c|9141| <<set_current_vmptr>> vmx->nested.current_vmptr = vmptr;
+ * - arch/x86/kvm/vmx.c|11494| <<vmx_create_vcpu>> vmx->nested.current_vmptr = -1ull;
+ */
gpa_t current_vmptr;
/*
* Cache of the guest's VMCS, existing outside of guest memory.
* Loaded from guest memory during VMPTRLD. Flushed to guest
* memory during VMCLEAR and VMPTRLD.
*/
+ /*
+ * 在以下使用nested_vmx->cached_vmcs12:
+ * - arch/x86/kvm/vmx.c|1372| <<get_vmcs12>> return to_vmx(vcpu)->nested.cached_vmcs12;
+ * - arch/x86/kvm/vmx.c|8586| <<enter_vmx_operation>> vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
+ * - arch/x86/kvm/vmx.c|8587| <<enter_vmx_operation>> if (!vmx->nested.cached_vmcs12)
+ * - arch/x86/kvm/vmx.c|8618| <<enter_vmx_operation>> kfree(vmx->nested.cached_vmcs12);
+ * - arch/x86/kvm/vmx.c|8809| <<nested_release_vmcs12>> vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
+ * - arch/x86/kvm/vmx.c|8860| <<free_nested>> kfree(vmx->nested.cached_vmcs12);
+ * - arch/x86/kvm/vmx.c|9404| <<handle_vmptrld>> memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
+ */
struct vmcs12 *cached_vmcs12;
/*
* Cache of the guest's shadow VMCS, existing outside of guest
* memory. Loaded from guest memory during VM entry. Flushed
* to guest memory during VM exit.
*/
+ /*
+ * 在以下使用nested_vmx->cached_shadow_vmcs12:
+ * - arch/x86/kvm/vmx.c|1391| <<get_shadow_vmcs12>> return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
+ * - arch/x86/kvm/vmx.c|8590| <<enter_vmx_operation>> vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
+ * - arch/x86/kvm/vmx.c|8591| <<enter_vmx_operation>> if (!vmx->nested.cached_shadow_vmcs12)
+ * - arch/x86/kvm/vmx.c|8615| <<enter_vmx_operation>> kfree(vmx->nested.cached_shadow_vmcs12);
+ * - arch/x86/kvm/vmx.c|8861| <<free_nested>> kfree(vmx->nested.cached_shadow_vmcs12);
+ */
struct vmcs12 *cached_shadow_vmcs12;
/*
* Indicates if the shadow vmcs must be updated with the
* data hold by vmcs12
*/
+ /*
+ * 在以下使用和修改nested_vmx->sync_shadow_vmcs:
+ * - arch/x86/kvm/vmx.c|8610| <<nested_release_vmcs12>> vmx->nested.sync_shadow_vmcs = false;
+ * - arch/x86/kvm/vmx.c|9107| <<set_current_vmptr>> vmx->nested.sync_shadow_vmcs = true;
+ * - arch/x86/kvm/vmx.c|10954| <<vmx_vcpu_run>> vmx->nested.sync_shadow_vmcs = false;
+ * - arch/x86/kvm/vmx.c|13720| <<nested_vmx_vmexit>> vmx->nested.sync_shadow_vmcs = true;
+ * - arch/x86/kvm/vmx.c|13810| <<nested_vmx_entry_failure>> to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
+ * - arch/x86/kvm/vmx.c|10952| <<vmx_vcpu_run>> if (vmx->nested.sync_shadow_vmcs) {
+ * - arch/x86/kvm/vmx.c|14359| <<vmx_get_nested_state>> else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
+ */
bool sync_shadow_vmcs;
bool dirty_vmcs12;
bool change_vmcs01_virtual_apic_mode;
/* L2 must run next, and mustn't decide to exit to L1. */
+ /*
+ * 在以下修改nested_vmx->nested_run_pending:
+ * - arch/x86/kvm/vmx.c|11616| <<vmx_vcpu_run>> vmx->nested.nested_run_pending = 0;
+ * - arch/x86/kvm/vmx.c|13526| <<nested_vmx_run>> vmx->nested.nested_run_pending = 1;
+ * - arch/x86/kvm/vmx.c|13530| <<nested_vmx_run>> vmx->nested.nested_run_pending = 0;
+ * - arch/x86/kvm/vmx.c|13555| <<nested_vmx_run>> vmx->nested.nested_run_pending = 0;
+ * - arch/x86/kvm/vmx.c|14218| <<vmx_leave_nested>> to_vmx(vcpu)->nested.nested_run_pending = 0;
+ * - arch/x86/kvm/vmx.c|14902| <<vmx_set_nested_state>> vmx->nested.nested_run_pending = !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
+ * 在以下使用nested_vmx->nested_run_pending:
+ * - arch/x86/kvm/vmx.c|7171| <<vmx_nmi_allowed>> if (to_vmx(vcpu)->nested.nested_run_pending)
+ * - arch/x86/kvm/vmx.c|7185| <<vmx_interrupt_allowed>> return (!to_vmx(vcpu)->nested.nested_run_pending &&
+ * - arch/x86/kvm/vmx.c|8024| <<handle_invalid_guest_state>> WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
+ * - arch/x86/kvm/vmx.c|10205| <<nested_vmx_exit_reflected>> if (vmx->nested.nested_run_pending)
+ * - arch/x86/kvm/vmx.c|12123| <<vmx_inject_page_fault_nested>> !to_vmx(vcpu)->nested.nested_run_pending) {
+ * - arch/x86/kvm/vmx.c|12793| <<prepare_vmcs02_full>> if (vmx->nested.nested_run_pending &&
+ * - arch/x86/kvm/vmx.c|12860| <<prepare_vmcs02>> if (vmx->nested.nested_run_pending &&
+ * - arch/x86/kvm/vmx.c|12868| <<prepare_vmcs02>> if (vmx->nested.nested_run_pending) {
+ * - arch/x86/kvm/vmx.c|13005| <<prepare_vmcs02>> if (vmx->nested.nested_run_pending &&
+ * - arch/x86/kvm/vmx.c|13076| <<prepare_vmcs02>> if (vmx->nested.nested_run_pending &&
+ * - arch/x86/kvm/vmx.c|13309| <<check_vmentry_postreqs>> if (to_vmx(vcpu)->nested.nested_run_pending &&
+ * - arch/x86/kvm/vmx.c|13648| <<vmx_check_nested_events>> vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
+ * - arch/x86/kvm/vmx.c|14054| <<nested_vmx_vmexit>> WARN_ON_ONCE(vmx->nested.nested_run_pending);
+ * - arch/x86/kvm/vmx.c|14680| <<vmx_smi_allowed>> if (to_vmx(vcpu)->nested.nested_run_pending)
+ * - arch/x86/kvm/vmx.c|14766| <<vmx_get_nested_state>> if (vmx->nested.nested_run_pending)
+ */
bool nested_run_pending;
struct loaded_vmcs vmcs02;
@@ -992,8 +1205,33 @@ struct vcpu_vmx {
* value indicates that host state is loaded.
*/
struct loaded_vmcs vmcs01;
+ /*
+ * 大概只在以下修改vcpu_vmx->loaded_vmcs:
+ * - arch/x86/kvm/vmx.c|11675| <<vmx_switch_vmcs>> vmx->loaded_vmcs = vmcs;
+ * - arch/x86/kvm/vmx.c|11770| <<vmx_create_vcpu>> vmx->loaded_vmcs = &vmx->vmcs01;
+ */
struct loaded_vmcs *loaded_vmcs;
+ /*
+ * 在以下使用vcpu_vmx->loaded_cpu_state:
+ * - arch/x86/kvm/vmx.c|3148| <<vmx_prepare_switch_to_guest>> if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
+ * - arch/x86/kvm/vmx.c|3156| <<vmx_prepare_switch_to_guest>> if (vmx->loaded_cpu_state)
+ * - arch/x86/kvm/vmx.c|3159| <<vmx_prepare_switch_to_guest>> vmx->loaded_cpu_state = vmx->loaded_vmcs;
+ * - arch/x86/kvm/vmx.c|3160| <<vmx_prepare_switch_to_guest>> host_state = &vmx->loaded_cpu_state->host_state;
+ * - arch/x86/kvm/vmx.c|3222| <<vmx_prepare_switch_to_host>> if (!vmx->loaded_cpu_state)
+ * - arch/x86/kvm/vmx.c|3225| <<vmx_prepare_switch_to_host>> WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
+ * - arch/x86/kvm/vmx.c|3226| <<vmx_prepare_switch_to_host>> host_state = &vmx->loaded_cpu_state->host_state;
+ * - arch/x86/kvm/vmx.c|3229| <<vmx_prepare_switch_to_host>> vmx->loaded_cpu_state = NULL;
+ * - arch/x86/kvm/vmx.c|3261| <<vmx_read_guest_kernel_gs_base>> if (vmx->loaded_cpu_state)
+ * - arch/x86/kvm/vmx.c|3270| <<vmx_write_guest_kernel_gs_base>> if (vmx->loaded_cpu_state)
+ */
struct loaded_vmcs *loaded_cpu_state;
+ /*
+ * 在以下使用vcpu_vmx->__launched:
+ * - arch/x86/kvm/vmx.c|11027| <<vmx_vcpu_run>> vmx->__launched = vmx->loaded_vmcs->launched;
+ * - arch/x86/kvm/vmx.c|11130| <<vmx_vcpu_run>> [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
+ *
+ * Check if vmlaunch of vmresume is needed
+ */
bool __launched; /* temporary, used in vmx_vcpu_run */
struct msr_autoload {
struct vmx_msrs guest;
@@ -1015,6 +1253,19 @@ struct vcpu_vmx {
} seg[8];
} segment_cache;
int vpid;
+ /*
+ * 在以下使用vcpu_vmx->emulation_required:
+ * - arch/x86/kvm/vmx.c|3354| <<vmx_set_rflags>> to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
+ * - arch/x86/kvm/vmx.c|3498| <<vmx_queue_exception>> WARN_ON_ONCE(vmx->emulation_required);
+ * - arch/x86/kvm/vmx.c|5462| <<vmx_set_cr0>> vmx->emulation_required = emulation_required(vcpu);
+ * - arch/x86/kvm/vmx.c|5709| <<vmx_set_segment>> vmx->emulation_required = emulation_required(vcpu);
+ * - arch/x86/kvm/vmx.c|7948| <<handle_invalid_guest_state>> WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
+ * - arch/x86/kvm/vmx.c|7953| <<handle_invalid_guest_state>> while (vmx->emulation_required && count-- != 0) {
+ * - arch/x86/kvm/vmx.c|7971| <<handle_invalid_guest_state>> if (vmx->emulation_required && !vmx->rmode.vm86_active &&
+ * - arch/x86/kvm/vmx.c|10387| <<vmx_handle_exit>> if (vmx->emulation_required)
+ * - arch/x86/kvm/vmx.c|11007| <<vmx_vcpu_run>> if (vmx->emulation_required)
+ * - arch/x86/kvm/vmx.c|12789| <<prepare_vmcs02>> if (vmx->emulation_required) {
+ */
bool emulation_required;
u32 exit_reason;
@@ -1268,13 +1519,41 @@ static inline short vmcs_field_to_offset(unsigned long field)
return offset;
}
+/*
+ * 返回to_vmx(vcpu)->nested.cached_vmcs12;
+ *
+ * struct vcpu_vmx:
+ * -> struct nested_vmx nested;
+ * -> struct vmcs12 *cached_vmcs12;
+ * -> struct vmcs12 *cached_shadow_vmcs12;
+ */
static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
{
+ /*
+ * struct vcpu_vmx:
+ * -> struct nested_vmx nested;
+ * -> struct vmcs12 *cached_vmcs12;
+ * -> struct vmcs12 *cached_shadow_vmcs12;
+ */
return to_vmx(vcpu)->nested.cached_vmcs12;
}
+/*
+ * 返回to_vmx(vcpu)->nested.cached_shadow_vmcs12;
+ *
+ * struct vcpu_vmx:
+ * -> struct nested_vmx nested;
+ * -> struct vmcs12 *cached_vmcs12;
+ * -> struct vmcs12 *cached_shadow_vmcs12;
+ */
static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
{
+ /*
+ * struct vcpu_vmx:
+ * -> struct nested_vmx nested;
+ * -> struct vmcs12 *cached_vmcs12;
+ * -> struct vmcs12 *cached_shadow_vmcs12;
+ */
return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
}
@@ -1341,6 +1620,43 @@ static bool cpu_has_load_perf_global_ctrl;
static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
static DEFINE_SPINLOCK(vmx_vpid_lock);
+/*
+ * crash> vmcs_config
+ * vmcs_config = $1 = {
+ * size = 1024,
+ * order = 0,
+ * basic_cap = 14286848,
+ * revision_id = 4,
+ * pin_based_exec_ctrl = 255,
+ * cpu_based_exec_ctrl = 3047190010,
+ * cpu_based_2nd_exec_ctrl = 34832383,
+ * vmexit_ctrl = 9433087,
+ * vmentry_ctrl = 86527,
+ * nested = {
+ * procbased_ctls_low = 67133810,
+ * procbased_ctls_high = 4294574078,
+ * secondary_ctls_low = 0,
+ * secondary_ctls_high = 156663,
+ * pinbased_ctls_low = 22,
+ * pinbased_ctls_high = 255,
+ * exit_ctls_low = 224763,
+ * exit_ctls_high = 8384511,
+ * entry_ctls_low = 4603,
+ * entry_ctls_high = 54271,
+ * misc_low = 536871013,
+ * misc_high = 0,
+ * ept_caps = 104022081,
+ * vpid_caps = 3841,
+ * basic = 60816187455798992,
+ * cr0_fixed0 = 2147483681,
+ * cr0_fixed1 = 4294967295,
+ * cr4_fixed0 = 8192,
+ * cr4_fixed1 = 7825407,
+ * vmcs_enum = 46,
+ * vmfunc_controls = 1
+ * }
+ * }
+ */
static struct vmcs_config {
int size;
int order;
@@ -2178,6 +2494,14 @@ static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
loaded_vmcs->launched = 0;
}
+/*
+ * called by:
+ * - arch/x86/kvm/vmx.c|3142| <<vmx_vcpu_load>> vmcs_load(vmx->loaded_vmcs->vmcs);
+ * - arch/x86/kvm/vmx.c|8747| <<copy_shadow_to_vmcs12>> vmcs_load(shadow_vmcs);
+ * - arch/x86/kvm/vmx.c|8763| <<copy_shadow_to_vmcs12>> vmcs_load(vmx->loaded_vmcs->vmcs);
+ * - arch/x86/kvm/vmx.c|8783| <<copy_vmcs12_to_shadow>> vmcs_load(shadow_vmcs);
+ * - arch/x86/kvm/vmx.c|8794| <<copy_vmcs12_to_shadow>> vmcs_load(vmx->loaded_vmcs->vmcs);
+ */
static void vmcs_load(struct vmcs *vmcs)
{
u64 phys_addr = __pa(vmcs);
@@ -2186,6 +2510,18 @@ static void vmcs_load(struct vmcs *vmcs)
if (static_branch_unlikely(&enable_evmcs))
return evmcs_load(phys_addr);
+ /*
+ * The behavior of the VMCS-maintenance instructions is summarized below:
+ * VMPTRLD — This instruction takes a single 64-bit source operand that is in memory. It makes the referenced
+ * VMCS active and current, loading the current-VMCS pointer with this operand and establishes the current VMCS
+ * based on the contents of VMCS-data area in the referenced VMCS region. Because this makes the referenced
+ * VMCS active, a logical processor may start maintaining on the processor some of the VMCS data for the VMCS.
+ *
+ * VMPTRLD指令从内存中加载一个64位物理地址作为current-VMCS pointer,
+ * 这个current-VMCS pointer由处理器内部记录和维护,除了VMXON,VMPTRLD
+ * 和VMCLEAR指令需要提供VMXON或VMCS指针作为操作数外,其他的指令指定
+ * 都是在current-VMS上操作.
+ */
asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
: CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
: "memory");
@@ -3048,6 +3384,10 @@ static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
}
#endif
+/*
+ * called by:
+ * - arch/x86/kvm/vmx.c|3455| <<vmx_vcpu_load>> vmx_vcpu_pi_load(vcpu, cpu);
+ */
static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
{
struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
@@ -3114,8 +3454,18 @@ static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
* Switches to specified vcpu, until a matching vcpu_put(), but assumes
* vcpu mutex is already taken.
*/
+/*
+ * 在以下使用vmx_vcpu_load():
+ * - struct kvm_x86_ops vmx_x86_ops.vcpu_load = vmx_vcpu_load()
+ * - arch/x86/kvm/vmx.c|11100| <<vmx_switch_vmcs>> vmx_vcpu_load(vcpu, cpu);
+ * - arch/x86/kvm/vmx.c|11183| <<vmx_create_vcpu>> vmx_vcpu_load(&vmx->vcpu, cpu);
+ */
static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
{
+ /*
+ * struct vcpu_vmx:
+ * -> struct kvm_vcpu vcpu;
+ */
struct vcpu_vmx *vmx = to_vmx(vcpu);
bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
@@ -4220,6 +4570,17 @@ static void vmx_leave_nested(struct kvm_vcpu *vcpu);
* Returns 0 on success, non-0 otherwise.
* Assumes vcpu_load() was already called.
*/
+/*
+ * called by:
+ * - arch/powerpc/kvm/book3s.c|736| <<kvmppc_set_msr>> vcpu->kvm->arch.kvm_ops->set_msr(vcpu, msr);
+ * - arch/x86/kvm/emulate.c|2521| <<rsm_load_state_64>> ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA);
+ * - arch/x86/kvm/emulate.c|2613| <<em_rsm>> ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
+ * - arch/x86/kvm/emulate.c|3661| <<em_wrmsr>> if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
+ * - arch/x86/kvm/pmu.c|320| <<kvm_pmu_set_msr>> return kvm_x86_ops->pmu_ops->set_msr(vcpu, msr_info);
+ * - arch/x86/kvm/x86.c|1261| <<kvm_set_msr>> return kvm_x86_ops->set_msr(vcpu, msr);
+ *
+ * struct kvm_x86_ops vmx_x86_ops.set_msr = vmx_set_msr()
+ */
static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
{
struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -4775,24 +5136,96 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
return 0;
}
+/*
+ * called by:
+ * - arch/x86/kvm/vmx.c|5025| <<alloc_vmcs>> return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
+ * - arch/x86/kvm/vmx.c|5169| <<alloc_kvm_area>> vmcs = alloc_vmcs_cpu(false, cpu);
+ */
static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
{
int node = cpu_to_node(cpu);
struct page *pages;
struct vmcs *vmcs;
+ /*
+ * struct vmcs {
+ * struct vmcs_hdr hdr;
+ * u32 abort;
+ * char data[0];
+ * };
+ */
pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
if (!pages)
return NULL;
vmcs = page_address(pages);
+ /*
+ * crash> vmcs_config
+ * vmcs_config = $1 = {
+ * size = 1024,
+ * order = 0,
+ * basic_cap = 14286848,
+ * revision_id = 4,
+ * pin_based_exec_ctrl = 255,
+ * cpu_based_exec_ctrl = 3047190010,
+ * cpu_based_2nd_exec_ctrl = 34832383,
+ * vmexit_ctrl = 9433087,
+ * vmentry_ctrl = 86527,
+ * nested = {
+ * procbased_ctls_low = 67133810,
+ * procbased_ctls_high = 4294574078,
+ * secondary_ctls_low = 0,
+ * secondary_ctls_high = 156663,
+ * pinbased_ctls_low = 22,
+ * pinbased_ctls_high = 255,
+ * exit_ctls_low = 224763,
+ * exit_ctls_high = 8384511,
+ * entry_ctls_low = 4603,
+ * entry_ctls_high = 54271,
+ * misc_low = 536871013,
+ * misc_high = 0,
+ * ept_caps = 104022081,
+ * vpid_caps = 3841,
+ * basic = 60816187455798992,
+ * cr0_fixed0 = 2147483681,
+ * cr0_fixed1 = 4294967295,
+ * cr4_fixed0 = 8192,
+ * cr4_fixed1 = 7825407,
+ * vmcs_enum = 46,
+ * vmfunc_controls = 1
+ * }
+ * }
+ */
memset(vmcs, 0, vmcs_config.size);
/* KVM supports Enlightened VMCS v1 only */
+ /*
+ * crash> enable_evmcs
+ * enable_evmcs = $2 = {