-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinux-v5.11.patch
6581 lines (6187 loc) · 290 KB
/
linux-v5.11.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 da5dc3c3f4a86e4c7abcf7675ed8329946a5e456 Mon Sep 17 00:00:00 2001
From: Dongli Zhang <dongli.zhang0129@gmail.com>
Date: Mon, 3 May 2021 08:30:58 -0700
Subject: [PATCH 1/1] linux v5.11
Signed-off-by: Dongli Zhang <dongli.zhang0129@gmail.com>
---
arch/x86/include/asm/irq_remapping.h | 9 +
arch/x86/include/asm/irq_vectors.h | 17 +
arch/x86/include/asm/kvm_host.h | 243 ++++++++++
arch/x86/include/asm/pvclock-abi.h | 20 +
arch/x86/include/uapi/asm/kvm_para.h | 7 +
arch/x86/kernel/cpu/hypervisor.c | 4 +
arch/x86/kernel/cpu/mshyperv.c | 5 +
arch/x86/kernel/hpet.c | 4 +
arch/x86/kernel/idt.c | 8 +
arch/x86/kernel/kvm.c | 7 +
arch/x86/kernel/kvmclock.c | 202 ++++++++
arch/x86/kernel/pvclock.c | 14 +
arch/x86/kernel/smpboot.c | 7 +
arch/x86/kernel/tsc.c | 35 ++
arch/x86/kvm/cpuid.c | 13 +
arch/x86/kvm/irq_comm.c | 8 +
arch/x86/kvm/kvm_cache_regs.h | 33 ++
arch/x86/kvm/mmu/mmu.c | 151 ++++++
arch/x86/kvm/mmu/mmu_internal.h | 29 ++
arch/x86/kvm/mmu/tdp_mmu.c | 8 +
arch/x86/kvm/svm/nested.c | 11 +
arch/x86/kvm/svm/svm.c | 18 +
arch/x86/kvm/vmx/capabilities.h | 5 +
arch/x86/kvm/vmx/nested.c | 206 ++++++++-
arch/x86/kvm/vmx/nested.h | 14 +
arch/x86/kvm/vmx/posted_intr.c | 363 +++++++++++++++
arch/x86/kvm/vmx/posted_intr.h | 62 +++
arch/x86/kvm/vmx/vmcs12.c | 7 +
arch/x86/kvm/vmx/vmcs12.h | 53 +++
arch/x86/kvm/vmx/vmenter.S | 3 +
arch/x86/kvm/vmx/vmx.c | 155 +++++++
arch/x86/kvm/vmx/vmx.h | 53 +++
arch/x86/kvm/x86.c | 664 +++++++++++++++++++++++++++
drivers/clocksource/hyperv_timer.c | 8 +
drivers/iommu/intel/iommu.c | 20 +
drivers/iommu/intel/irq_remapping.c | 5 +
drivers/iommu/intel/svm.c | 7 +
drivers/iommu/iommu.c | 39 ++
drivers/iommu/irq_remapping.c | 10 +
drivers/net/xen-netback/common.h | 18 +
drivers/net/xen-netback/rx.c | 154 +++++++
drivers/nvme/host/core.c | 14 +
drivers/vfio/pci/vfio_pci.c | 9 +
drivers/vfio/pci/vfio_pci_config.c | 4 +
drivers/vfio/pci/vfio_pci_intrs.c | 39 ++
drivers/vfio/pci/vfio_pci_private.h | 9 +
drivers/vfio/pci/vfio_pci_rdwr.c | 4 +
drivers/vfio/vfio.c | 3 +
drivers/vfio/vfio_iommu_type1.c | 100 ++++
drivers/vfio/virqfd.c | 28 ++
drivers/xen/events/events_base.c | 62 +++
drivers/xen/events/events_internal.h | 4 +
include/linux/clocksource.h | 12 +
include/linux/kvm_host.h | 76 +++
include/uapi/linux/kvm.h | 12 +
kernel/cpu.c | 72 +++
kernel/sched/core.c | 8 +
virt/kvm/eventfd.c | 4 +
virt/kvm/irqchip.c | 25 +
virt/kvm/kvm_main.c | 310 +++++++++++++
60 files changed, 3493 insertions(+), 1 deletion(-)
diff --git a/arch/x86/include/asm/irq_remapping.h b/arch/x86/include/asm/irq_remapping.h
index 7cc49432187f..76d94387fd53 100644
--- a/arch/x86/include/asm/irq_remapping.h
+++ b/arch/x86/include/asm/irq_remapping.h
@@ -27,6 +27,15 @@ enum {
};
struct vcpu_data {
+ /*
+ * 在以下使用vcpu_data->pi_desc_addr:
+ * - arch/x86/kvm/svm/avic.c|794| <<get_pi_vcpu_info>> vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
+ * - arch/x86/kvm/svm/avic.c|895| <<svm_update_pi_irte>> vcpu_info.pi_desc_addr, set);
+ * - arch/x86/kvm/vmx/posted_intr.c|633| <<pi_update_irte>> vcpu_info.pi_desc_addr = __pa(&to_vmx(vcpu)->pi_desc);
+ * - arch/x86/kvm/vmx/posted_intr.c|637| <<pi_update_irte>> vcpu_info.vector, vcpu_info.pi_desc_addr, set);
+ * - drivers/iommu/intel/irq_remapping.c|1224| <<intel_ir_set_vcpu_affinity>> irte_pi.pda_l = (vcpu_pi_info->pi_desc_addr >>
+ * - drivers/iommu/intel/irq_remapping.c|1226| <<intel_ir_set_vcpu_affinity>> irte_pi.pda_h = (vcpu_pi_info->pi_desc_addr >> 32) &
+ */
u64 pi_desc_addr; /* Physical address of PI Descriptor */
u32 vector; /* Guest vector of the interrupt */
};
diff --git a/arch/x86/include/asm/irq_vectors.h b/arch/x86/include/asm/irq_vectors.h
index 889f8b1b5b7f..29b36c111467 100644
--- a/arch/x86/include/asm/irq_vectors.h
+++ b/arch/x86/include/asm/irq_vectors.h
@@ -92,7 +92,24 @@
/* Vector for KVM to deliver posted interrupt IPI */
#ifdef CONFIG_HAVE_KVM
+/*
+ * 在以下使用POSTED_INTR_VECTOR:
+ * - arch/x86/kernel/idt.c|138| <<global>> INTG(POSTED_INTR_VECTOR, asm_sysvec_kvm_posted_intr_ipi),
+ * - arch/x86/include/asm/idtentry.h|660| <<SYM_CODE_END>> DECLARE_IDTENTRY_SYSVEC(POSTED_INTR_VECTOR, sysvec_kvm_posted_intr_ipi);
+ * - arch/x86/kvm/vmx/posted_intr.c|390| <<__pi_post_block>> new.nv = POSTED_INTR_VECTOR;
+ * - arch/x86/kvm/vmx/vmx.c|3958| <<kvm_vcpu_trigger_posted_interrupt>> int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
+ * - arch/x86/kvm/vmx/vmx.c|4377| <<init_vmcs>> vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
+ * - arch/x86/kvm/vmx/vmx.c|7091| <<vmx_create_vcpu>> vmx->pi_desc.nv = POSTED_INTR_VECTOR;
+ */
#define POSTED_INTR_VECTOR 0xf2
+/*
+ * 在以下使用POSTED_INTR_WAKEUP_VECTOR:
+ * - arch/x86/kernel/idt.c|139| <<global>> INTG(POSTED_INTR_WAKEUP_VECTOR, asm_sysvec_kvm_posted_intr_wakeup_ipi),
+ * - arch/x86/include/asm/idtentry.h|661| <<SYM_CODE_END>> DECLARE_IDTENTRY_SYSVEC(POSTED_INTR_WAKEUP_VECTOR, sysvec_kvm_posted_intr_wakeup_ipi);
+ * - arch/x86/kvm/vmx/posted_intr.c|268| <<vmx_vcpu_pi_load>> if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || vcpu->cpu == cpu) {
+ * - arch/x86/kvm/vmx/posted_intr.c|371| <<__pi_post_block>> WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
+ * - arch/x86/kvm/vmx/posted_intr.c|467| <<pi_pre_block>> new.nv = POSTED_INTR_WAKEUP_VECTOR;
+ */
#define POSTED_INTR_WAKEUP_VECTOR 0xf1
#define POSTED_INTR_NESTED_VECTOR 0xf0
#endif
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 3d6616f6f6ef..dc69cf8f6571 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -57,20 +57,50 @@
#define KVM_REQ_REPORT_TPR_ACCESS KVM_ARCH_REQ(1)
#define KVM_REQ_TRIPLE_FAULT KVM_ARCH_REQ(2)
#define KVM_REQ_MMU_SYNC KVM_ARCH_REQ(3)
+/*
+ * 在以下使用KVM_REQ_CLOCK_UPDATE:
+ * - arch/x86/kvm/x86.c|2552| <<kvm_gen_update_masterclock>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|2681| <<kvm_guest_time_update>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
+ * - arch/x86/kvm/x86.c|2767| <<kvmclock_update_fn>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|2776| <<kvm_gen_kvmclock_update>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
+ * - arch/x86/kvm/x86.c|4013| <<kvm_arch_vcpu_load>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|4656| <<kvm_set_guest_paused>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|5700| <<kvm_arch_vm_ioctl>> kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
+ * - arch/x86/kvm/x86.c|7682| <<kvm_hyperv_tsc_notifier>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|7745| <<__kvmclock_cpufreq_notifier>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|8867| <<vcpu_enter_guest>> if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) { --> kvm_guest_time_update()
+ * - arch/x86/kvm/x86.c|9121| <<vcpu_enter_guest>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|10242| <<kvm_arch_hardware_enable>> kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+ */
#define KVM_REQ_CLOCK_UPDATE KVM_ARCH_REQ(4)
#define KVM_REQ_LOAD_MMU_PGD KVM_ARCH_REQ(5)
#define KVM_REQ_EVENT KVM_ARCH_REQ(6)
#define KVM_REQ_APF_HALT KVM_ARCH_REQ(7)
+/*
+ * 在以下使用KVM_REQ_STEAL_UPDATE:
+ * - arch/x86/kvm/x86.c|3212| <<kvm_set_msr_common>> kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|4010| <<kvm_arch_vcpu_load>> kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|8867| <<vcpu_enter_guest>> if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
+ */
#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(8)
#define KVM_REQ_NMI KVM_ARCH_REQ(9)
#define KVM_REQ_PMU KVM_ARCH_REQ(10)
#define KVM_REQ_PMI KVM_ARCH_REQ(11)
#define KVM_REQ_SMI KVM_ARCH_REQ(12)
+/*
+ * 调用kvm_gen_update_masterclock()
+ */
#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)
#define KVM_REQ_SCAN_IOAPIC \
KVM_ARCH_REQ_FLAGS(15, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
+/*
+ * 在以下使用KVM_REQ_GLOBAL_CLOCK_UPDATE:
+ * - arch/x86/kvm/x86.c|1992| <<kvm_write_system_time>> kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|4037| <<kvm_arch_vcpu_load>> kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
+ * - arch/x86/kvm/x86.c|8865| <<vcpu_enter_guest>> if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu)) --> kvm_gen_kvmclock_update()
+ */
#define KVM_REQ_GLOBAL_CLOCK_UPDATE KVM_ARCH_REQ(16)
#define KVM_REQ_APIC_PAGE_RELOAD \
KVM_ARCH_REQ_FLAGS(17, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
@@ -666,18 +696,74 @@ struct kvm_vcpu_arch {
struct gfn_to_pfn_cache cache;
} st;
+ /*
+ * 在以下使用kvm_vcpu_arch->l1_tsc_offset:
+ * - arch/x86/kvm/svm/nested.c|407| <<nested_prepare_vmcb_control>> svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
+ * - arch/x86/kvm/svm/nested.c|658| <<nested_svm_vmexit>> svm->vcpu.arch.l1_tsc_offset;
+ * - arch/x86/kvm/x86.c|2222| <<kvm_read_l1_tsc>> return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
+ * - arch/x86/kvm/x86.c|2228| <<kvm_vcpu_write_tsc_offset>> vcpu->arch.l1_tsc_offset = offset;
+ * - arch/x86/kvm/x86.c|2352| <<adjust_tsc_offset_guest>> u64 tsc_offset = vcpu->arch.l1_tsc_offset;
+ * - arch/x86/kvm/x86.c|3257| <<kvm_set_msr_common>> u64 adj = kvm_compute_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset;
+ * - arch/x86/kvm/x86.c|3561| <<kvm_get_msr_common>> u64 tsc_offset = msr_info->host_initiated ? vcpu->arch.l1_tsc_offset :
+ */
u64 l1_tsc_offset;
u64 tsc_offset;
u64 last_guest_tsc;
+ /*
+ * 在以下设置kvm_vcpu_arch->last_host_tsc:
+ * - arch/x86/kvm/x86.c|4426| <<kvm_arch_vcpu_put>> vcpu->arch.last_host_tsc = rdtsc();
+ * - arch/x86/kvm/x86.c|10627| <<kvm_arch_hardware_enable>> vcpu->arch.last_host_tsc = local_tsc;
+ * 在以下使用kvm_vcpu_arch->last_host_tsc:
+ * - arch/x86/kvm/x86.c|4349| <<kvm_arch_vcpu_load>> s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
+ * - arch/x86/kvm/x86.c|4350| <<kvm_arch_vcpu_load>> rdtsc() - vcpu->arch.last_host_tsc;
+ * - arch/x86/kvm/x86.c|10575| <<kvm_arch_hardware_enable>> if (stable && vcpu->arch.last_host_tsc > local_tsc) {
+ * - arch/x86/kvm/x86.c|10577| <<kvm_arch_hardware_enable>> if (vcpu->arch.last_host_tsc > max_tsc)
+ * - arch/x86/kvm/x86.c|10578| <<kvm_arch_hardware_enable>> max_tsc = vcpu->arch.last_host_tsc;
+ */
u64 last_host_tsc;
u64 tsc_offset_adjustment;
u64 this_tsc_nsec;
u64 this_tsc_write;
u64 this_tsc_generation;
+ /*
+ * 在以下使用kvm_vcpu_arch->tsc_catchup:
+ * - arch/x86/kvm/x86.c|2216| <<set_tsc_khz>> vcpu->arch.tsc_catchup = 1;
+ * - arch/x86/kvm/x86.c|3035| <<kvm_guest_time_update>> if (vcpu->tsc_catchup) {
+ * - arch/x86/kvm/x86.c|4428| <<kvm_arch_vcpu_load>> vcpu->arch.tsc_catchup = 1;
+ */
bool tsc_catchup;
+ /*
+ * 在以下使用kvm_vcpu_arch->tsc_always_catchup:
+ * - arch/x86/kvm/x86.c|2217| <<set_tsc_khz>> vcpu->arch.tsc_always_catchup = 1;
+ * - arch/x86/kvm/x86.c|9534| <<vcpu_enter_guest>> if (unlikely(vcpu->arch.tsc_always_catchup))
+ */
bool tsc_always_catchup;
s8 virtual_tsc_shift;
+ /*
+ * 在以下使用kvm_vcpu_arch->virtual_tsc_mult:
+ * - arch/x86/kvm/x86.c|2166| <<kvm_set_tsc_khz>> &vcpu->arch.virtual_tsc_mult);
+ * - arch/x86/kvm/x86.c|2187| <<compute_guest_tsc>> vcpu->arch.virtual_tsc_mult,
+ * -arch/x86/kvm/x86.h|301| <<nsec_to_cycles>> return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
+ */
u32 virtual_tsc_mult;
+ /*
+ * 在以下设置kvm_vcpu_arch->virtual_tsc_khz:
+ * - arch/x86/kvm/x86.c|2266| <<kvm_set_tsc_khz>> vcpu->arch.virtual_tsc_khz = user_tsc_khz;
+ * 在以下使用kvm_vcpu_arch->virtual_tsc_khz:
+ * - arch/x86/kvm/hyperv.c|1426| <<kvm_hv_get_msr>> data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
+ * - arch/x86/kvm/lapic.c|1552| <<__wait_lapic_expire>> do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
+ * - arch/x86/kvm/lapic.c|1572| <<adjust_lapic_timer_advance>> do_div(ns, vcpu->arch.virtual_tsc_khz);
+ * - arch/x86/kvm/lapic.c|1577| <<adjust_lapic_timer_advance>> do_div(ns, vcpu->arch.virtual_tsc_khz);
+ * - arch/x86/kvm/lapic.c|1662| <<start_sw_tscdeadline>> unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
+ * - arch/x86/kvm/vmx/nested.c|2173| <<vmx_start_preemption_timer>> if (vcpu->arch.virtual_tsc_khz == 0)
+ * - arch/x86/kvm/vmx/nested.c|2178| <<vmx_start_preemption_timer>> do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
+ * - arch/x86/kvm/vmx/nested.c|4022| <<vmx_get_preemption_timer_value>> value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
+ * - arch/x86/kvm/x86.c|2491| <<kvm_synchronize_tsc>> if (vcpu->arch.virtual_tsc_khz) {
+ * - arch/x86/kvm/x86.c|2502| <<kvm_synchronize_tsc>> u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
+ * - arch/x86/kvm/x86.c|2520| <<kvm_synchronize_tsc>> vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
+ * - arch/x86/kvm/x86.c|2565| <<kvm_synchronize_tsc>> kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
+ * - arch/x86/kvm/x86.c|5440| <<kvm_arch_vcpu_ioctl>> r = vcpu->arch.virtual_tsc_khz;
+ */
u32 virtual_tsc_khz;
s64 ia32_tsc_adjust_msr;
u64 msr_ia32_power_ctl;
@@ -906,13 +992,53 @@ struct kvm_arch {
unsigned long n_requested_mmu_pages;
unsigned long n_max_mmu_pages;
unsigned int indirect_shadow_pages;
+ /*
+ * 在以下使用kvm_mmu_page->mmu_valid_gen:
+ * - arch/x86/kvm/mmu/mmu.c|1698| <<kvm_mmu_alloc_page>> sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;
+ * - arch/x86/kvm/mmu/mmu.c|1909| <<is_obsolete_sp>> unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
+ *
+ * 在以下使用kvm_arch->mmu_valid_gen:
+ * - arch/x86/kvm/mmu/mmu.c|1698| <<kvm_mmu_alloc_page>> sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;
+ * - arch/x86/kvm/mmu/mmu.c|1909| <<is_obsolete_sp>> unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
+ * - arch/x86/kvm/mmu/mmu.c|5510| <<kvm_mmu_zap_all_fast>> kvm->arch.mmu_valid_gen = kvm->arch.mmu_valid_gen ? 0 : 1;
+ */
u8 mmu_valid_gen;
+ /*
+ * 在以下使用kvm_arch->mmu_page_hash:
+ * - arch/x86/kvm/mmu/mmu.c|1838| <<for_each_gfn_indirect_valid_sp>> for_each_valid_sp(_kvm, _sp, &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)]) \
+ * - arch/x86/kvm/mmu/mmu.c|2068| <<kvm_mmu_get_page>> sp_list = &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)];
+ */
struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
/*
* Hash table of struct kvm_mmu_page.
*/
+ /*
+ * 在以下使用kvm_arch->active_mmu_pages:
+ * - arch/x86/kvm/mmu/mmu.c|1690| <<kvm_mmu_alloc_page>> list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
+ * - arch/x86/kvm/mmu/mmu.c|2416| <<kvm_mmu_zap_oldest_mmu_pages>> if (list_empty(&kvm->arch.active_mmu_pages))
+ * - arch/x86/kvm/mmu/mmu.c|2420| <<kvm_mmu_zap_oldest_mmu_pages>> list_for_each_entry_safe(sp, tmp, &kvm->arch.active_mmu_pages, link) {
+ * - arch/x86/kvm/mmu/mmu.c|5370| <<kvm_zap_obsolete_pages>> list_for_each_entry_safe_reverse(sp, node, &kvm->arch.active_mmu_pages, link) {
+ * - arch/x86/kvm/mmu/mmu.c|5686| <<kvm_mmu_zap_all>> list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) {
+ * - arch/x86/kvm/mmu/mmu_audit.c|89| <<walk_all_active_sps>> list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link)
+ * - arch/x86/kvm/x86.c|10381| <<kvm_arch_init_vm>> INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
+ */
struct list_head active_mmu_pages;
+ /*
+ * 在以下使用kvm_arch->zapped_obsolete_pages:
+ * - arch/x86/kvm/mmu/mmu.c|5399| <<kvm_zap_obsolete_pages>> __kvm_mmu_prepare_zap_page(kvm, sp, &kvm->arch.zapped_obsolete_pages, &nr_zapped)) {
+ * - arch/x86/kvm/mmu/mmu.c|5410| <<kvm_zap_obsolete_pages>> kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages);
+ * - arch/x86/kvm/mmu/mmu.c|5458| <<kvm_has_zapped_obsolete_pages>> return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages));
+ * - arch/x86/kvm/mmu/mmu.c|5764| <<mmu_shrink_scan>> kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages);
+ * - arch/x86/kvm/x86.c|10382| <<kvm_arch_init_vm>> INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
+ */
struct list_head zapped_obsolete_pages;
+ /*
+ * 在以下使用kvm_arch->lpage_disallowed_mmu_pages:
+ * - arch/x86/kvm/mmu/mmu.c|786| <<account_huge_nx_page>> list_add_tail(&sp->lpage_disallowed_link, &kvm->arch.lpage_disallowed_mmu_pages);
+ * - arch/x86/kvm/mmu/mmu.c|5996| <<kvm_recover_nx_lpages>> if (list_empty(&kvm->arch.lpage_disallowed_mmu_pages))
+ * - arch/x86/kvm/mmu/mmu.c|6004| <<kvm_recover_nx_lpages>> sp = list_first_entry(&kvm->arch.lpage_disallowed_mmu_pages,
+ * - arch/x86/kvm/x86.c|10383| <<kvm_arch_init_vm>> INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
+ */
struct list_head lpage_disallowed_mmu_pages;
struct kvm_page_track_notifier_node mmu_sp_tracker;
struct kvm_page_track_notifier_head track_notifier_head;
@@ -939,26 +1065,99 @@ struct kvm_arch {
bool mwait_in_guest;
bool hlt_in_guest;
+ /*
+ * 在以下使用kvm_arch->pause_in_guest:
+ * - arch/x86/kvm/svm/svm.c|4427| <<svm_vm_init>> kvm->arch.pause_in_guest = true;
+ * - arch/x86/kvm/vmx/vmx.c|7115| <<vmx_vm_init>> kvm->arch.pause_in_guest = true;
+ * - arch/x86/kvm/x86.c|5809| <<kvm_vm_ioctl_enable_cap>> kvm->arch.pause_in_guest = true;
+ * - arch/x86/kvm/x86.h|331| <<kvm_pause_in_guest>> return kvm->arch.pause_in_guest;
+ */
bool pause_in_guest;
bool cstate_in_guest;
unsigned long irq_sources_bitmap;
+ /*
+ * system_time = kernel_ns + kvm->arch.kvmclock_offset
+ * 其中kvmclock_offset的初始值为VM创建时的Host Boot Time的相反数,
+ * 即令VM创建时的system_time为0,system_time表示Guest的Boot Time.
+ * 同时,用户态可以通过KVM_SET_CLOCK ioctl来设置kvmclock的system_time值,
+ * 从而修改kvmclock_offset,这就允许QEMU采用不同的初始化行为.
+ *
+ * 在以下设置kvm_arch->kvmclock_offset:
+ * - arch/x86/kvm/x86.c|5760| <<kvm_arch_vm_ioctl>> kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
+ * - arch/x86/kvm/x86.c|10495| <<kvm_arch_init_vm>> kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
+ */
s64 kvmclock_offset;
raw_spinlock_t tsc_write_lock;
+ /*
+ * 在以下使用kvm_arch->last_tsc_nsec:
+ * - arch/x86/kvm/x86.c|2271| <<kvm_synchronize_tsc>> elapsed = ns - kvm->arch.last_tsc_nsec;
+ * - arch/x86/kvm/x86.c|2333| <<kvm_synchronize_tsc>> kvm->arch.last_tsc_nsec = ns;
+ * - arch/x86/kvm/x86.c|10422| <<kvm_arch_hardware_enable>> kvm->arch.last_tsc_nsec = 0;
+ */
u64 last_tsc_nsec;
u64 last_tsc_write;
u32 last_tsc_khz;
u64 cur_tsc_nsec;
u64 cur_tsc_write;
u64 cur_tsc_offset;
+ /*
+ * 在以下使用kvm_arch->cur_tsc_generation:
+ * - arch/x86/kvm/x86.c|2311| <<kvm_synchronize_tsc>> already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
+ * - arch/x86/kvm/x86.c|2322| <<kvm_synchronize_tsc>> kvm->arch.cur_tsc_generation++;
+ * - arch/x86/kvm/x86.c|2340| <<kvm_synchronize_tsc>> vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
+ */
u64 cur_tsc_generation;
+ /*
+ * 在以下使用kvm_arch->nr_vcpus_matched_tsc:
+ * - arch/x86/kvm/x86.c|2145| <<kvm_track_tsc_matching>> vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
+ * - arch/x86/kvm/x86.c|2160| <<kvm_track_tsc_matching>> trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
+ * - arch/x86/kvm/x86.c|2317| <<kvm_synchronize_tsc>> kvm->arch.nr_vcpus_matched_tsc = 0;
+ * - arch/x86/kvm/x86.c|2319| <<kvm_synchronize_tsc>> kvm->arch.nr_vcpus_matched_tsc++;
+ * - arch/x86/kvm/x86.c|2516| <<pvclock_update_vm_gtod_copy>> vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
+ */
int nr_vcpus_matched_tsc;
spinlock_t pvclock_gtod_sync_lock;
+ /*
+ * 在以下设置kvm_arch->use_master_clock:
+ * - arch/x86/kvm/x86.c|2521| <<pvclock_update_vm_gtod_copy>> ka->use_master_clock = host_tsc_clocksource && vcpus_matched
+ * 在以下使用kvm_arch->use_master_clock:
+ * - arch/x86/kvm/x86.c|2156| <<kvm_track_tsc_matching>> if (ka->use_master_clock ||
+ * - arch/x86/kvm/x86.c|2162| <<kvm_track_tsc_matching>> ka->use_master_clock, gtod->clock.vclock_mode);
+ * - arch/x86/kvm/x86.c|2525| <<pvclock_update_vm_gtod_copy>> if (ka->use_master_clock)
+ * - arch/x86/kvm/x86.c|2529| <<pvclock_update_vm_gtod_copy>> trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
+ * - arch/x86/kvm/x86.c|2569| <<get_kvmclock_ns>> if (!ka->use_master_clock) {
+ * - arch/x86/kvm/x86.c|2669| <<kvm_guest_time_update>> use_master_clock = ka->use_master_clock;
+ * - arch/x86/kvm/x86.c|4036| <<kvm_arch_vcpu_load>> if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
+ * - arch/x86/kvm/x86.c|5709| <<kvm_arch_vm_ioctl>> user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
+ */
bool use_master_clock;
+ /*
+ * 在以下使用kvm_arch->master_kernel_ns:
+ * - arch/x86/kvm/x86.c|2543| <<pvclock_update_vm_gtod_copy>> host_tsc_clocksource = kvm_get_time_and_clockread(&ka->master_kernel_ns, &ka->master_cycle_now);
+ * - arch/x86/kvm/x86.c|2605| <<get_kvmclock_ns>> hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
+ * - arch/x86/kvm/x86.c|2733| <<kvm_guest_time_update>> kernel_ns = ka->master_kernel_ns;
+ */
u64 master_kernel_ns;
u64 master_cycle_now;
+ /*
+ * 在以下使用kvm_arch->kvmclock_update_work:
+ * - arch/x86/kvm/x86.c|2823| <<kvmclock_update_fn>> kvmclock_update_work);
+ * - arch/x86/kvm/x86.c|2838| <<kvm_gen_kvmclock_update>> schedule_delayed_work(&kvm->arch.kvmclock_update_work,
+ * - arch/x86/kvm/x86.c|2854| <<kvmclock_sync_fn>> schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
+ * - arch/x86/kvm/x86.c|10500| <<kvm_arch_init_vm>> INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
+ * - arch/x86/kvm/x86.c|10548| <<kvm_arch_sync_events>> cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
+ */
struct delayed_work kvmclock_update_work;
+ /*
+ * 在以下使用kvm_arch->kvmclock_sync_work:
+ * - arch/x86/kvm/x86.c|2848| <<kvmclock_sync_fn>> kvmclock_sync_work);
+ * - arch/x86/kvm/x86.c|2855| <<kvmclock_sync_fn>> schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
+ * - arch/x86/kvm/x86.c|10166| <<kvm_arch_vcpu_postcreate>> schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
+ * - arch/x86/kvm/x86.c|10501| <<kvm_arch_init_vm>> INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
+ * - arch/x86/kvm/x86.c|10547| <<kvm_arch_sync_events>> cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
+ */
struct delayed_work kvmclock_sync_work;
struct kvm_xen_hvm_config xen_hvm_config;
@@ -972,7 +1171,18 @@ struct kvm_arch {
int audit_point;
#endif
+ /*
+ * 在以下使用kvm_arch->backwards_tsc_observed:
+ * - arch/x86/kvm/x86.c|2724| <<pvclock_update_vm_gtod_copy>> && !ka->backwards_tsc_observed
+ * - arch/x86/kvm/x86.c|10608| <<kvm_arch_hardware_enable>> kvm->arch.backwards_tsc_observed = true;
+ */
bool backwards_tsc_observed;
+ /*
+ * 在以下使用kvm_arch->boot_vcpu_runs_old_kvmclock:
+ * - arch/x86/kvm/x86.c|2053| <<kvm_write_system_time>> if (ka->boot_vcpu_runs_old_kvmclock != old_msr)
+ * - arch/x86/kvm/x86.c|2056| <<kvm_write_system_time>> ka->boot_vcpu_runs_old_kvmclock = old_msr;
+ * - arch/x86/kvm/x86.c|2725| <<pvclock_update_vm_gtod_copy>> && !ka->boot_vcpu_runs_old_kvmclock;
+ */
bool boot_vcpu_runs_old_kvmclock;
u32 bsp_vcpu_id;
@@ -1029,11 +1239,21 @@ struct kvm_arch {
};
struct kvm_vm_stat {
+ /*
+ * 在以下使用kvm_vm_stat->mmu_shadow_zapped:
+ * - arch/x86/kvm/x86.c|235| <<global>> VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped),
+ * - arch/x86/kvm/mmu/mmu.c|2330| <<__kvm_mmu_prepare_zap_page>> ++kvm->stat.mmu_shadow_zapped;
+ */
ulong mmu_shadow_zapped;
ulong mmu_pte_write;
ulong mmu_pte_updated;
ulong mmu_pde_zapped;
ulong mmu_flooded;
+ /*
+ * 在以下使用kvm_vm_stat->mmu_recycled:
+ * - arch/x86/kvm/x86.c|240| <<global>> VM_STAT("mmu_recycled", mmu_recycled),
+ * - arch/x86/kvm/mmu/mmu.c|2465| <<kvm_mmu_zap_oldest_mmu_pages>> kvm->stat.mmu_recycled += total_zapped;
+ */
ulong mmu_recycled;
ulong mmu_cache_miss;
ulong mmu_unsync;
@@ -1050,6 +1270,22 @@ struct kvm_vcpu_stat {
u64 invlpg;
u64 exits;
+ /*
+ * 在以下修改kvm_vcpu_stat->io_exits:
+ * - arch/x86/kvm/svm/svm.c|2077| <<io_interception>> ++svm->vcpu.stat.io_exits
+ * - arch/x86/kvm/vmx/vmx.c|4904| <<handle_io>> ++vcpu->stat.io_exits;
+ *
+ * vcpu_enter_guest()
+ * -> kvm_x86_ops.handle_exit = vmx_handle_exit()
+ * -> kvm_vmx_exit_handlers[EXIT_REASON_IO_INSTRUCTION](vcpu) = handle_io()
+ * -> ++vcpu->stat.io_exits;
+ *
+ * vcpu_enter_guest()
+ * -> kvm_x86_ops.handle_exit = handle_exit()
+ * -> svm_invoke_exit_handler()
+ * -> svm_exit_handlers[SVM_EXIT_IOIO](svm) = io_interception()
+ * -> ++svm->vcpu.stat.io_exits
+ */
u64 io_exits;
u64 mmio_exits;
u64 signal_exits;
@@ -1639,6 +1875,13 @@ enum {
#define HF_GIF_MASK (1 << 0)
#define HF_NMI_MASK (1 << 3)
#define HF_IRET_MASK (1 << 4)
+/*
+ * 在以下使用HF_GUEST_MASK:
+ * - arch/x86/kvm/kvm_cache_regs.h|159| <<enter_guest_mode>> vcpu->arch.hflags |= HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|164| <<leave_guest_mode>> vcpu->arch.hflags &= ~HF_GUEST_MASK;
+ * - arch/x86/kvm/kvm_cache_regs.h|174| <<is_guest_mode>> return vcpu->arch.hflags & HF_GUEST_MASK;
+ * - arch/x86/kvm/x86.c|6994| <<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/pvclock-abi.h b/arch/x86/include/asm/pvclock-abi.h
index 1436226efe3e..fb9222374bac 100644
--- a/arch/x86/include/asm/pvclock-abi.h
+++ b/arch/x86/include/asm/pvclock-abi.h
@@ -26,7 +26,13 @@
struct pvclock_vcpu_time_info {
u32 version;
u32 pad0;
+ /*
+ * host最后一次tsc的值(可能用软件的方式involve了offset和scaling)
+ */
u64 tsc_timestamp;
+ /*
+ * VM已经启动了的时间 (clocksource 1GHZ)
+ */
u64 system_time;
u32 tsc_to_system_mul;
s8 tsc_shift;
@@ -40,6 +46,20 @@ struct pvclock_wall_clock {
u32 nsec;
} __attribute__((__packed__));
+/*
+ * 在以下使用PVCLOCK_TSC_STABLE_BIT:
+ * - arch/x86/include/asm/vdso/gettimeofday.h|227| <<vread_pvclock>> if (unlikely(!(pvti->flags & PVCLOCK_TSC_STABLE_BIT)))
+ * - arch/x86/kernel/kvmclock.c|410| <<kvm_setup_vsyscall_timeinfo>> if (!(flags & PVCLOCK_TSC_STABLE_BIT))
+ * - arch/x86/kernel/kvmclock.c|521| <<kvmclock_init>> pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
+ * - arch/x86/kernel/kvmclock.c|532| <<kvmclock_init>> kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
+ * - arch/x86/kernel/pvclock.c|85| <<pvclock_clocksource_read>> if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
+ * - arch/x86/kernel/pvclock.c|86| <<pvclock_clocksource_read>> (flags & PVCLOCK_TSC_STABLE_BIT))
+ * - arch/x86/kvm/hyperv.c|1039| <<compute_tsc_page_parameters>> if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
+ * - arch/x86/kvm/x86.c|2736| <<kvm_guest_time_update>> pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
+ * - arch/x86/xen/time.c|465| <<xen_setup_vsyscall_time_info>> if (!(ti->pvti.flags & PVCLOCK_TSC_STABLE_BIT)) {
+ * - arch/x86/xen/time.c|513| <<xen_time_init>> if (pvti->flags & PVCLOCK_TSC_STABLE_BIT) {
+ * - arch/x86/xen/time.c|514| <<xen_time_init>> pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
+ */
#define PVCLOCK_TSC_STABLE_BIT (1 << 0)
#define PVCLOCK_GUEST_STOPPED (1 << 1)
/* PVCLOCK_COUNTS_FROM_ZERO broke ABI and can't be used anymore. */
diff --git a/arch/x86/include/uapi/asm/kvm_para.h b/arch/x86/include/uapi/asm/kvm_para.h
index 950afebfba88..894bc545d00d 100644
--- a/arch/x86/include/uapi/asm/kvm_para.h
+++ b/arch/x86/include/uapi/asm/kvm_para.h
@@ -49,6 +49,13 @@
#define MSR_KVM_WALL_CLOCK_NEW 0x4b564d00
#define MSR_KVM_SYSTEM_TIME_NEW 0x4b564d01
#define MSR_KVM_ASYNC_PF_EN 0x4b564d02
+/*
+ * 主要在以下使用MSR_KVM_STEAL_TIME:
+ * - arch/x86/kernel/kvm.c|310| <<kvm_register_steal_time>> wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
+ * - arch/x86/kernel/kvm.c|424| <<kvm_disable_steal_time>> wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
+ * - arch/x86/kvm/x86.c|3197| <<kvm_set_msr_common>> case MSR_KVM_STEAL_TIME:
+ * - arch/x86/kvm/x86.c|3524| <<kvm_get_msr_common>> case MSR_KVM_STEAL_TIME:
+ */
#define MSR_KVM_STEAL_TIME 0x4b564d03
#define MSR_KVM_PV_EOI_EN 0x4b564d04
#define MSR_KVM_POLL_CONTROL 0x4b564d05
diff --git a/arch/x86/kernel/cpu/hypervisor.c b/arch/x86/kernel/cpu/hypervisor.c
index 553bfbfc3a1b..3f02ef8b251a 100644
--- a/arch/x86/kernel/cpu/hypervisor.c
+++ b/arch/x86/kernel/cpu/hypervisor.c
@@ -68,6 +68,10 @@ detect_hypervisor_vendor(void)
if (unlikely(nopv) && !(*p)->ignore_nopv)
continue;
+ /*
+ * ms_hyperv_platform() 返回 HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS
+ * kvm_detect()
+ */
pri = (*p)->detect();
if (pri > max_pri) {
max_pri = pri;
diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
index 43b54bef5448..ed53dfb4aa98 100644
--- a/arch/x86/kernel/cpu/mshyperv.c
+++ b/arch/x86/kernel/cpu/mshyperv.c
@@ -32,6 +32,11 @@
#include <asm/nmi.h>
#include <clocksource/hyperv_timer.h>
+/*
+ * "ENLIGHTENING" KVM HYPER-V EMULATION, FOSDEM 2019
+ * https://archive.fosdem.org/2019/schedule/event/vai_enlightening_kvm/attachments/slides/2860/export/events/attachments/vai_enlightening_kvm/slides/2860/vkuznets_fosdem2019_enlightening_kvm.pdf
+ */
+
struct ms_hyperv_info ms_hyperv;
EXPORT_SYMBOL_GPL(ms_hyperv);
diff --git a/arch/x86/kernel/hpet.c b/arch/x86/kernel/hpet.c
index 08651a4e6aa0..dca36e861950 100644
--- a/arch/x86/kernel/hpet.c
+++ b/arch/x86/kernel/hpet.c
@@ -854,6 +854,10 @@ static u64 read_hpet(struct clocksource *cs)
}
#endif
+/*
+ * 在以下使用clocksource_hpet:
+ * - arch/x86/kernel/hpet.c|1013| <<hpet_enable>> clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq);
+ */
static struct clocksource clocksource_hpet = {
.name = "hpet",
.rating = 250,
diff --git a/arch/x86/kernel/idt.c b/arch/x86/kernel/idt.c
index ee1a283f8e96..d613ad931b8f 100644
--- a/arch/x86/kernel/idt.c
+++ b/arch/x86/kernel/idt.c
@@ -136,6 +136,14 @@ static const __initconst struct idt_data apic_idts[] = {
INTG(X86_PLATFORM_IPI_VECTOR, asm_sysvec_x86_platform_ipi),
# ifdef CONFIG_HAVE_KVM
INTG(POSTED_INTR_VECTOR, asm_sysvec_kvm_posted_intr_ipi),
+ /*
+ * 在以下使用POSTED_INTR_WAKEUP_VECTOR:
+ * - arch/x86/kernel/idt.c|139| <<global>> INTG(POSTED_INTR_WAKEUP_VECTOR, asm_sysvec_kvm_posted_intr_wakeup_ipi),
+ * - arch/x86/include/asm/idtentry.h|661| <<SYM_CODE_END>> DECLARE_IDTENTRY_SYSVEC(POSTED_INTR_WAKEUP_VECTOR, sysvec_kvm_posted_intr_wakeup_ipi);
+ * - arch/x86/kvm/vmx/posted_intr.c|268| <<vmx_vcpu_pi_load>> if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || vcpu->cpu == cpu) {
+ * - arch/x86/kvm/vmx/posted_intr.c|371| <<__pi_post_block>> WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
+ * - arch/x86/kvm/vmx/posted_intr.c|467| <<pi_pre_block>> new.nv = POSTED_INTR_WAKEUP_VECTOR;
+ */
INTG(POSTED_INTR_WAKEUP_VECTOR, asm_sysvec_kvm_posted_intr_wakeup_ipi),
INTG(POSTED_INTR_NESTED_VECTOR, asm_sysvec_kvm_posted_intr_nested_ipi),
# endif
diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
index 5e78e01ca3b4..abcdbabc1d09 100644
--- a/arch/x86/kernel/kvm.c
+++ b/arch/x86/kernel/kvm.c
@@ -307,6 +307,13 @@ static void kvm_register_steal_time(void)
if (!has_steal_clock)
return;
+ /*
+ * 主要在以下使用MSR_KVM_STEAL_TIME:
+ * - arch/x86/kernel/kvm.c|310| <<kvm_register_steal_time>> wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
+ * - arch/x86/kernel/kvm.c|424| <<kvm_disable_steal_time>> wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
+ * - arch/x86/kvm/x86.c|3197| <<kvm_set_msr_common>> case MSR_KVM_STEAL_TIME:
+ * - arch/x86/kvm/x86.c|3524| <<kvm_get_msr_common>> case MSR_KVM_STEAL_TIME:
+ */
wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
pr_info("stealtime: cpu %d, msr %llx\n", cpu,
(unsigned long long) slow_virt_to_phys(st));
diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c
index aa593743acf6..99c9c6120b6f 100644
--- a/arch/x86/kernel/kvmclock.c
+++ b/arch/x86/kernel/kvmclock.c
@@ -24,7 +24,20 @@
#include <asm/kvmclock.h>
static int kvmclock __initdata = 1;
+/*
+ * 在以下使用kvmclock_vsyscall:
+ * - arch/x86/kernel/kvmclock.c|49| <<parse_no_kvmclock_vsyscall>> kvmclock_vsyscall = 0;
+ * - arch/x86/kernel/kvmclock.c|364| <<kvm_setup_vsyscall_timeinfo>> if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
+ */
static int kvmclock_vsyscall __initdata = 1;
+/*
+ * 在以下使用msr_kvm_system_time:
+ * - arch/x86/kernel/kvmclock.c|231| <<kvm_register_clock>> wrmsrl(msr_kvm_system_time, pa);
+ * - arch/x86/kernel/kvmclock.c|273| <<kvm_crash_shutdown>> native_write_msr(msr_kvm_system_time, 0, 0);
+ * - arch/x86/kernel/kvmclock.c|281| <<kvm_shutdown>> native_write_msr(msr_kvm_system_time, 0, 0);
+ * - arch/x86/kernel/kvmclock.c|432| <<kvmclock_init>> msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
+ * - arch/x86/kernel/kvmclock.c|444| <<kvmclock_init>> msr_kvm_system_time, msr_kvm_wall_clock);
+ */
static int msr_kvm_system_time __ro_after_init = MSR_KVM_SYSTEM_TIME;
static int msr_kvm_wall_clock __ro_after_init = MSR_KVM_WALL_CLOCK;
static u64 kvm_sched_clock_offset __ro_after_init;
@@ -47,17 +60,56 @@ early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
#define HVC_BOOT_ARRAY_SIZE \
(PAGE_SIZE / sizeof(struct pvclock_vsyscall_time_info))
+/*
+ * 在以下使用hv_clock_boot:
+ * - arch/x86/kernel/kvmclock.c|277| <<kvm_setup_vsyscall_timeinfo>> flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+ * - arch/x86/kernel/kvmclock.c|304| <<kvmclock_setup_percpu>> p = &hv_clock_boot[cpu];
+ * - arch/x86/kernel/kvmclock.c|336| <<kvmclock_init>> this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
+ * - arch/x86/kernel/kvmclock.c|338| <<kvmclock_init>> pvclock_set_pvti_cpu0_va(hv_clock_boot);
+ * - arch/x86/kernel/kvmclock.c|343| <<kvmclock_init>> flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+ */
static struct pvclock_vsyscall_time_info
hv_clock_boot[HVC_BOOT_ARRAY_SIZE] __bss_decrypted __aligned(PAGE_SIZE);
static struct pvclock_wall_clock wall_clock __bss_decrypted;
+/*
+ * 在以下使用hv_clock_per_cpu:
+ * - arch/x86/kernel/kvmclock.c|58| <<this_cpu_pvti>> return &this_cpu_read(hv_clock_per_cpu)->pvti;
+ * - arch/x86/kernel/kvmclock.c|63| <<this_cpu_hvclock>> return this_cpu_read(hv_clock_per_cpu);
+ * - arch/x86/kernel/kvmclock.c|274| <<kvm_setup_vsyscall_timeinfo>> if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
+ * - arch/x86/kernel/kvmclock.c|292| <<kvmclock_setup_percpu>> struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
+ * - arch/x86/kernel/kvmclock.c|299| <<kvmclock_setup_percpu>> if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
+ * - arch/x86/kernel/kvmclock.c|310| <<kvmclock_setup_percpu>> per_cpu(hv_clock_per_cpu, cpu) = p;
+ * - arch/x86/kernel/kvmclock.c|336| <<kvmclock_init>> this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
+ */
static DEFINE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu);
+/*
+ * 在以下使用hvclock_mem:
+ * - arch/x86/kernel/kvmclock.c|241| <<kvmclock_init_mem>> order = get_order(ncpus * sizeof(*hvclock_mem));
+ * - arch/x86/kernel/kvmclock.c|249| <<kvmclock_init_mem>> hvclock_mem = page_address(p);
+ * - arch/x86/kernel/kvmclock.c|256| <<kvmclock_init_mem>> r = set_memory_decrypted((unsigned long ) hvclock_mem,
+ * - arch/x86/kernel/kvmclock.c|260| <<kvmclock_init_mem>> hvclock_mem = NULL;
+ * - arch/x86/kernel/kvmclock.c|266| <<kvmclock_init_mem>> memset(hvclock_mem, 0, PAGE_SIZE << order);
+ * - arch/x86/kernel/kvmclock.c|305| <<kvmclock_setup_percpu>> else if (hvclock_mem)
+ * - arch/x86/kernel/kvmclock.c|306| <<kvmclock_setup_percpu>> p = hvclock_mem + cpu - HVC_BOOT_ARRAY_SIZE;
+ */
static struct pvclock_vsyscall_time_info *hvclock_mem;
+/*
+ * called by:
+ * - arch/x86/kernel/kvmclock.c|103| <<kvm_get_wallclock>> pvclock_read_wallclock(&wall_clock, this_cpu_pvti(), now);
+ * - arch/x86/kernel/kvmclock.c|117| <<kvm_clock_read>> ret = pvclock_clocksource_read(this_cpu_pvti());
+ * - arch/x86/kernel/kvmclock.c|158| <<kvm_get_tsc_khz>> return pvclock_tsc_khz(this_cpu_pvti());
+ */
static inline struct pvclock_vcpu_time_info *this_cpu_pvti(void)
{
return &this_cpu_read(hv_clock_per_cpu)->pvti;
}
+/*
+ * called by:
+ * - arch/x86/kernel/kvmclock.c|175| <<kvm_check_and_clear_guest_paused>> struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
+ * - arch/x86/kernel/kvmclock.c|207| <<kvm_register_clock>> struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
+ */
static inline struct pvclock_vsyscall_time_info *this_cpu_hvclock(void)
{
return this_cpu_read(hv_clock_per_cpu);
@@ -101,6 +153,10 @@ static u64 kvm_sched_clock_read(void)
return kvm_clock_read() - kvm_sched_clock_offset;
}
+/*
+ * called by:
+ * - arch/x86/kernel/kvmclock.c|528| <<kvmclock_init>> kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
+ */
static inline void kvm_sched_clock_init(bool stable)
{
if (!stable)
@@ -127,6 +183,9 @@ static inline void kvm_sched_clock_init(bool stable)
static unsigned long kvm_get_tsc_khz(void)
{
setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
+ /*
+ * 将pv_tsc_khz根据local cpu的pvti的tsc_shift和tsc_to_system_mul做校准
+ */
return pvclock_tsc_khz(this_cpu_pvti());
}
@@ -174,6 +233,12 @@ struct clocksource kvm_clock = {
};
EXPORT_SYMBOL_GPL(kvm_clock);
+/*
+ * called by:
+ * - arch/x86/kernel/kvmclock.c|224| <<kvm_restore_sched_clock_state>> kvm_register_clock("primary cpu clock, resume");
+ * - arch/x86/kernel/kvmclock.c|230| <<kvm_setup_secondary_clock>> kvm_register_clock("secondary cpu clock");
+ * - arch/x86/kernel/kvmclock.c|419| <<kvmclock_init>> kvm_register_clock("primary cpu clock");
+ */
static void kvm_register_clock(char *txt)
{
struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
@@ -191,12 +256,23 @@ static void kvm_save_sched_clock_state(void)
{
}
+/*
+ * 在以下使用kvm_restore_sched_clock_state():
+ * - arch/x86/kernel/kvmclock.c|444| <<kvmclock_init>> x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
+ */
static void kvm_restore_sched_clock_state(void)
{
kvm_register_clock("primary cpu clock, resume");
}
#ifdef CONFIG_X86_LOCAL_APIC
+/*
+ * 在以下使用kvm_setup_secondary_clock():
+ * - arch/x86/kernel/kvmclock.c|441| <<kvmclock_init>> x86_cpuinit.early_percpu_clock_init = kvm_setup_secondary_clock;
+ *
+ * called by:
+ * - arch/x86/kernel/smpboot.c|238| <<start_secondary>> x86_cpuinit.early_percpu_clock_init();
+ */
static void kvm_setup_secondary_clock(void)
{
kvm_register_clock("secondary cpu clock");
@@ -227,6 +303,19 @@ static void kvm_shutdown(void)
native_machine_shutdown();
}
+/*
+ * [0] kvmclock_init_mem
+ * [0] kvm_setup_vsyscall_timeinfo
+ * [0] do_one_initcall
+ * [0] kernel_init_freeable
+ * [0] kernel_init
+ * [0] ret_from_fork
+ *
+ * called by:
+ * - arch/x86/kernel/kvmclock.c|358| <<kvm_setup_vsyscall_timeinfo>> kvmclock_init_mem();
+ *
+ * 在prefill_possible_map()之后执行
+ */
static void __init kvmclock_init_mem(void)
{
unsigned long ncpus;
@@ -234,6 +323,9 @@ static void __init kvmclock_init_mem(void)
struct page *p;
int r;
+ /*
+ * 在prefill_possible_map()之后执行
+ */
if (HVC_BOOT_ARRAY_SIZE >= num_possible_cpus())
return;
@@ -246,6 +338,16 @@ static void __init kvmclock_init_mem(void)
return;
}
+ /*
+ * 在以下使用hvclock_mem:
+ * - arch/x86/kernel/kvmclock.c|241| <<kvmclock_init_mem>> order = get_order(ncpus * sizeof(*hvclock_mem));
+ * - arch/x86/kernel/kvmclock.c|249| <<kvmclock_init_mem>> hvclock_mem = page_address(p);
+ * - arch/x86/kernel/kvmclock.c|256| <<kvmclock_init_mem>> r = set_memory_decrypted((unsigned long ) hvclock_mem,
+ * - arch/x86/kernel/kvmclock.c|260| <<kvmclock_init_mem>> hvclock_mem = NULL;
+ * - arch/x86/kernel/kvmclock.c|266| <<kvmclock_init_mem>> memset(hvclock_mem, 0, PAGE_SIZE << order);
+ * - arch/x86/kernel/kvmclock.c|305| <<kvmclock_setup_percpu>> else if (hvclock_mem)
+ * - arch/x86/kernel/kvmclock.c|306| <<kvmclock_setup_percpu>> p = hvclock_mem + cpu - HVC_BOOT_ARRAY_SIZE;
+ */
hvclock_mem = page_address(p);
/*
@@ -266,14 +368,50 @@ static void __init kvmclock_init_mem(void)
memset(hvclock_mem, 0, PAGE_SIZE << order);
}
+/*
+ * 先调用kvmclock_init(), 再调用kvm_setup_vsyscall_timeinfo()
+ */
static int __init kvm_setup_vsyscall_timeinfo(void)
{
#ifdef CONFIG_X86_64
u8 flags;
+ /*
+ * 在以下使用hv_clock_per_cpu:
+ * - arch/x86/kernel/kvmclock.c|58| <<this_cpu_pvti>> return &this_cpu_read(hv_clock_per_cpu)->pvti;
+ * - arch/x86/kernel/kvmclock.c|63| <<this_cpu_hvclock>> return this_cpu_read(hv_clock_per_cpu);
+ * - arch/x86/kernel/kvmclock.c|274| <<kvm_setup_vsyscall_timeinfo>> if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
+ * - arch/x86/kernel/kvmclock.c|292| <<kvmclock_setup_percpu>> struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
+ * - arch/x86/kernel/kvmclock.c|299| <<kvmclock_setup_percpu>> if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
+ * - arch/x86/kernel/kvmclock.c|310| <<kvmclock_setup_percpu>> per_cpu(hv_clock_per_cpu, cpu) = p;
+ * - arch/x86/kernel/kvmclock.c|336| <<kvmclock_init>> this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
+ *
+ * 在以下使用kvmclock_vsyscall:
+ * - arch/x86/kernel/kvmclock.c|49| <<parse_no_kvmclock_vsyscall>> kvmclock_vsyscall = 0;
+ * - arch/x86/kernel/kvmclock.c|364| <<kvm_setup_vsyscall_timeinfo>> if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
+ */
if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
return 0;
+ /*
+ * 在以下使用hv_clock_boot:
+ * - arch/x86/kernel/kvmclock.c|277| <<kvm_setup_vsyscall_timeinfo>> flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+ * - arch/x86/kernel/kvmclock.c|304| <<kvmclock_setup_percpu>> p = &hv_clock_boot[cpu];
+ * - arch/x86/kernel/kvmclock.c|336| <<kvmclock_init>> this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
+ * - arch/x86/kernel/kvmclock.c|338| <<kvmclock_init>> pvclock_set_pvti_cpu0_va(hv_clock_boot);
+ * - arch/x86/kernel/kvmclock.c|343| <<kvmclock_init>> flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+ *
+ * struct pvclock_vcpu_time_info {
+ * u32 version;
+ * u32 pad0;
+ * u64 tsc_timestamp;
+ * u64 system_time;
+ * u32 tsc_to_system_mul;
+ * s8 tsc_shift;
+ * u8 flags;
+ * u8 pad[2];
+ * } __attribute__((__packed__));
+ */
flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
if (!(flags & PVCLOCK_TSC_STABLE_BIT))
return 0;
@@ -289,6 +427,16 @@ early_initcall(kvm_setup_vsyscall_timeinfo);
static int kvmclock_setup_percpu(unsigned int cpu)
{
+ /*
+ * 在以下使用hv_clock_per_cpu:
+ * - arch/x86/kernel/kvmclock.c|58| <<this_cpu_pvti>> return &this_cpu_read(hv_clock_per_cpu)->pvti;
+ * - arch/x86/kernel/kvmclock.c|63| <<this_cpu_hvclock>> return this_cpu_read(hv_clock_per_cpu);
+ * - arch/x86/kernel/kvmclock.c|274| <<kvm_setup_vsyscall_timeinfo>> if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
+ * - arch/x86/kernel/kvmclock.c|292| <<kvmclock_setup_percpu>> struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
+ * - arch/x86/kernel/kvmclock.c|299| <<kvmclock_setup_percpu>> if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
+ * - arch/x86/kernel/kvmclock.c|310| <<kvmclock_setup_percpu>> per_cpu(hv_clock_per_cpu, cpu) = p;
+ * - arch/x86/kernel/kvmclock.c|336| <<kvmclock_init>> this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
+ */
struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
/*
@@ -300,6 +448,26 @@ static int kvmclock_setup_percpu(unsigned int cpu)
return 0;
/* Use the static page for the first CPUs, allocate otherwise */
+ /*
+ * 在以下使用hv_clock_boot:
+ * - arch/x86/kernel/kvmclock.c|277| <<kvm_setup_vsyscall_timeinfo>> flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+ * - arch/x86/kernel/kvmclock.c|304| <<kvmclock_setup_percpu>> p = &hv_clock_boot[cpu];
+ * - arch/x86/kernel/kvmclock.c|336| <<kvmclock_init>> this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
+ * - arch/x86/kernel/kvmclock.c|338| <<kvmclock_init>> pvclock_set_pvti_cpu0_va(hv_clock_boot);
+ * - arch/x86/kernel/kvmclock.c|343| <<kvmclock_init>> flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+ *
+ * 在HVC_BOOT_ARRAY_SIZE=64
+ *
+ * static struct pvclock_vsyscall_time_info *hvclock_mem;
+ * 在以下使用hvclock_mem:
+ * - arch/x86/kernel/kvmclock.c|241| <<kvmclock_init_mem>> order = get_order(ncpus * sizeof(*hvclock_mem));
+ * - arch/x86/kernel/kvmclock.c|249| <<kvmclock_init_mem>> hvclock_mem = page_address(p);
+ * - arch/x86/kernel/kvmclock.c|256| <<kvmclock_init_mem>> r = set_memory_decrypted((unsigned long ) hvclock_mem,
+ * - arch/x86/kernel/kvmclock.c|260| <<kvmclock_init_mem>> hvclock_mem = NULL;
+ * - arch/x86/kernel/kvmclock.c|266| <<kvmclock_init_mem>> memset(hvclock_mem, 0, PAGE_SIZE << order);
+ * - arch/x86/kernel/kvmclock.c|305| <<kvmclock_setup_percpu>> else if (hvclock_mem)
+ * - arch/x86/kernel/kvmclock.c|306| <<kvmclock_setup_percpu>> p = hvclock_mem + cpu - HVC_BOOT_ARRAY_SIZE;
+ */
if (cpu < HVC_BOOT_ARRAY_SIZE)
p = &hv_clock_boot[cpu];
else if (hvclock_mem)
@@ -307,10 +475,26 @@ static int kvmclock_setup_percpu(unsigned int cpu)
else
return -ENOMEM;
+ /*
+ * 在以下使用hv_clock_per_cpu:
+ * - arch/x86/kernel/kvmclock.c|58| <<this_cpu_pvti>> return &this_cpu_read(hv_clock_per_cpu)->pvti;
+ * - arch/x86/kernel/kvmclock.c|63| <<this_cpu_hvclock>> return this_cpu_read(hv_clock_per_cpu);
+ * - arch/x86/kernel/kvmclock.c|274| <<kvm_setup_vsyscall_timeinfo>> if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
+ * - arch/x86/kernel/kvmclock.c|292| <<kvmclock_setup_percpu>> struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
+ * - arch/x86/kernel/kvmclock.c|299| <<kvmclock_setup_percpu>> if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
+ * - arch/x86/kernel/kvmclock.c|310| <<kvmclock_setup_percpu>> per_cpu(hv_clock_per_cpu, cpu) = p;
+ * - arch/x86/kernel/kvmclock.c|336| <<kvmclock_init>> this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
+ */
per_cpu(hv_clock_per_cpu, cpu) = p;
return p ? 0 : -ENOMEM;
}
+/*
+ * called by:
+ * - arch/x86/kernel/kvm.c|757| <<kvm_init_platform>> kvmclock_init();
+ *
+ * 先调用kvmclock_init(), 再调用kvm_setup_vsyscall_timeinfo()
+ */
void __init kvmclock_init(void)
{
u8 flags;
@@ -333,6 +517,10 @@ void __init kvmclock_init(void)
pr_info("kvm-clock: Using msrs %x and %x",
msr_kvm_system_time, msr_kvm_wall_clock);
+ /*
+ * 定义是 ... 每个都是一个指针 ...
+ * static DEFINE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu);
+ */
this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
kvm_register_clock("primary cpu clock");
pvclock_set_pvti_cpu0_va(hv_clock_boot);
@@ -340,11 +528,22 @@ void __init kvmclock_init(void)
if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
+ /*
+ * 在以下使用hv_clock_boot:
+ * - arch/x86/kernel/kvmclock.c|277| <<kvm_setup_vsyscall_timeinfo>> flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+ * - arch/x86/kernel/kvmclock.c|304| <<kvmclock_setup_percpu>> p = &hv_clock_boot[cpu];
+ * - arch/x86/kernel/kvmclock.c|336| <<kvmclock_init>> this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
+ * - arch/x86/kernel/kvmclock.c|338| <<kvmclock_init>> pvclock_set_pvti_cpu0_va(hv_clock_boot);
+ * - arch/x86/kernel/kvmclock.c|343| <<kvmclock_init>> flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+ */
flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
x86_platform.calibrate_tsc = kvm_get_tsc_khz;
x86_platform.calibrate_cpu = kvm_get_tsc_khz;
+ /*
+ * 获得系统boot时的秒数和纳秒数
+ */
x86_platform.get_wallclock = kvm_get_wallclock;
x86_platform.set_wallclock = kvm_set_wallclock;
#ifdef CONFIG_X86_LOCAL_APIC
@@ -371,6 +570,9 @@ void __init kvmclock_init(void)
!check_tsc_unstable())
kvm_clock.rating = 299;
+ /*
+ * #define NSEC_PER_SEC 1000000000L
+ */
clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
pv_info.name = "KVM";
}
diff --git a/arch/x86/kernel/pvclock.c b/arch/x86/kernel/pvclock.c
index 11065dc03f5b..158835781c64 100644
--- a/arch/x86/kernel/pvclock.c
+++ b/arch/x86/kernel/pvclock.c
@@ -16,6 +16,12 @@
#include <asm/pvclock.h>
#include <asm/vgtod.h>
+/*
+ * 在以下使用valid_flags:
+ * - arch/x86/kernel/pvclock.c|24| <<pvclock_set_flags>> valid_flags = flags;
+ * - arch/x86/kernel/pvclock.c|64| <<pvclock_read_flags>> return flags & valid_flags;
+ * - arch/x86/kernel/pvclock.c|85| <<pvclock_clocksource_read>> if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
+ */
static u8 valid_flags __read_mostly = 0;
static struct pvclock_vsyscall_time_info *pvti_cpu0_va __read_mostly;
@@ -24,6 +30,9 @@ void pvclock_set_flags(u8 flags)
valid_flags = flags;
}
+/*
+ * 将pv_tsc_khz根据local cpu的pvti的tsc_shift和tsc_to_system_mul做校准
+ */
unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
{
u64 pv_tsc_khz = 1000000ULL << 32;