-
Notifications
You must be signed in to change notification settings - Fork 4
/
linux-v6.6.patch
11846 lines (11131 loc) · 542 KB
/
linux-v6.6.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 9a0efca89521c4fb8b572b8080e3df8aac3df49e Mon Sep 17 00:00:00 2001
From: Dongli Zhang <[email protected]>
Date: Tue, 9 Jan 2024 23:50:34 -0800
Subject: [PATCH 1/1] linux-v6.6
Signed-off-by: Dongli Zhang <[email protected]>
---
arch/arm64/include/asm/arch_timer.h | 3 +
arch/arm64/include/asm/kvm_emulate.h | 6 +
arch/arm64/include/asm/kvm_host.h | 20 ++
arch/arm64/include/asm/kvm_mmu.h | 33 ++
arch/arm64/kernel/process.c | 12 +
arch/arm64/kvm/arch_timer.c | 289 +++++++++++++++++
arch/arm64/kvm/arm.c | 136 ++++++++
arch/arm64/kvm/guest.c | 4 +
arch/arm64/kvm/handle_exit.c | 29 ++
arch/arm64/kvm/hyp/include/hyp/switch.h | 44 +++
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 10 +
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 7 +
arch/arm64/kvm/hyp/nvhe/timer-sr.c | 18 +
arch/arm64/kvm/hyp/vgic-v3-sr.c | 10 +
arch/arm64/kvm/hyp/vhe/switch.c | 61 ++++
arch/arm64/kvm/hyp/vhe/sysreg-sr.c | 32 ++
arch/arm64/kvm/hyp/vhe/tlb.c | 13 +
arch/arm64/kvm/mmio.c | 9 +
arch/arm64/kvm/mmu.c | 22 ++
arch/arm64/kvm/pmu-emul.c | 9 +
arch/arm64/kvm/sys_regs.c | 22 ++
arch/arm64/kvm/vgic/vgic-irqfd.c | 27 ++
arch/arm64/kvm/vgic/vgic-its.c | 125 +++++++
arch/arm64/kvm/vgic/vgic-mmio-v3.c | 4 +
arch/arm64/kvm/vgic/vgic-v3.c | 8 +
arch/arm64/kvm/vgic/vgic-v4.c | 4 +
arch/arm64/kvm/vgic/vgic.c | 132 ++++++++
arch/x86/events/amd/core.c | 29 ++
arch/x86/events/core.c | 79 +++++
arch/x86/events/perf_event.h | 11 +
arch/x86/include/asm/kvm_host.h | 230 +++++++++++++
arch/x86/include/asm/nmi.h | 22 ++
arch/x86/kernel/apic/hw_nmi.c | 27 ++
arch/x86/kernel/kvm.c | 8 +
arch/x86/kernel/nmi.c | 13 +
arch/x86/kvm/cpuid.c | 4 +
arch/x86/kvm/emulate.c | 3 +
arch/x86/kvm/irq.c | 4 +
arch/x86/kvm/irq_comm.c | 4 +
arch/x86/kvm/lapic.c | 56 ++++
arch/x86/kvm/lapic.h | 4 +
arch/x86/kvm/mmu/mmu.c | 7 +
arch/x86/kvm/pmu.c | 257 +++++++++++++++
arch/x86/kvm/pmu.h | 161 +++++++++
arch/x86/kvm/smm.c | 51 +++
arch/x86/kvm/svm/avic.c | 4 +
arch/x86/kvm/svm/nested.c | 5 +
arch/x86/kvm/svm/pmu.c | 123 +++++++
arch/x86/kvm/svm/svm.c | 12 +
arch/x86/kvm/vmx/vmx.c | 19 ++
arch/x86/kvm/vmx/vmx.h | 15 +
arch/x86/kvm/x86.c | 190 +++++++++++
arch/x86/kvm/x86.h | 41 +++
arch/x86/kvm/xen.c | 82 +++++
block/bdev.c | 34 ++
block/genhd.c | 13 +
block/partitions/core.c | 26 ++
drivers/acpi/bus.c | 4 +
drivers/acpi/osl.c | 151 +++++++++
drivers/acpi/scan.c | 4 +
drivers/block/virtio_blk.c | 35 ++
drivers/clocksource/arm_arch_timer.c | 45 +++
drivers/cpuidle/cpuidle.c | 7 +
drivers/cpuidle/poll_state.c | 10 +
drivers/irqchip/irq-gic-v3.c | 16 +
drivers/irqchip/irq-gic-v4.c | 5 +
drivers/net/virtio_net.c | 53 +++
drivers/nvme/host/core.c | 188 +++++++++++
drivers/nvme/host/nvme.h | 28 ++
drivers/nvme/host/pci.c | 234 +++++++++++++
drivers/pci/hotplug/acpiphp_glue.c | 114 +++++++
drivers/pci/pci.c | 18 +
drivers/pci/probe.c | 14 +
drivers/pci/setup-bus.c | 300 +++++++++++++++++
drivers/pci/setup-res.c | 125 +++++++
drivers/scsi/scsi_common.c | 13 +
drivers/scsi/scsi_scan.c | 4 +
drivers/scsi/virtio_scsi.c | 91 ++++++
drivers/vhost/net.c | 4 +
drivers/vhost/scsi.c | 39 +++
drivers/vhost/vhost.c | 100 ++++++
drivers/vhost/vhost.h | 56 ++++
drivers/virtio/virtio_ring.c | 90 +++++
include/kvm/arm_vgic.h | 59 ++++
include/linux/blk-mq.h | 31 ++
include/linux/kvm_host.h | 19 ++
include/linux/kvm_irqfd.h | 7 +
include/linux/perf_event.h | 7 +
include/uapi/linux/pci_regs.h | 15 +
kernel/async.c | 58 ++++
kernel/entry/kvm.c | 6 +
kernel/events/core.c | 84 +++++
kernel/panic.c | 13 +
kernel/sched/idle.c | 24 ++
kernel/stop_machine.c | 12 +
kernel/watchdog.c | 176 +++++++++-
kernel/watchdog_perf.c | 149 +++++++++
mm/filemap.c | 11 +
.../selftests/kvm/include/guest_modes.h | 18 +
.../selftests/kvm/include/kvm_util_base.h | 210 ++++++++++++
.../selftests/kvm/kvm_page_table_test.c | 166 ++++++++++
tools/testing/selftests/kvm/lib/elf.c | 39 +++
tools/testing/selftests/kvm/lib/guest_modes.c | 90 +++++
tools/testing/selftests/kvm/lib/io.c | 7 +
tools/testing/selftests/kvm/lib/kvm_util.c | 307 ++++++++++++++++++
tools/testing/selftests/kvm/lib/sparsebit.c | 9 +
.../testing/selftests/kvm/lib/ucall_common.c | 73 +++++
.../selftests/kvm/lib/x86_64/processor.c | 58 ++++
.../testing/selftests/kvm/lib/x86_64/ucall.c | 8 +
.../kvm/memslot_modification_stress_test.c | 4 +
.../selftests/kvm/set_memory_region_test.c | 16 +
tools/testing/selftests/kvm/steal_time.c | 119 +++++++
.../selftests/kvm/x86_64/kvm_clock_test.c | 10 +
.../selftests/kvm/x86_64/nx_huge_pages_test.c | 64 ++++
virt/kvm/eventfd.c | 70 ++++
virt/kvm/irqchip.c | 3 +
virt/kvm/kvm_main.c | 62 ++++
117 files changed, 6374 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/include/asm/arch_timer.h b/arch/arm64/include/asm/arch_timer.h
index 934c658ee..090dcc5ec 100644
--- a/arch/arm64/include/asm/arch_timer.h
+++ b/arch/arm64/include/asm/arch_timer.h
@@ -201,6 +201,9 @@ static __always_inline u64 __arch_counter_get_cntvct(void)
{
u64 cnt;
+ /*
+ * 虚拟化应该是PhysicalCountInt() - CNTVOFF_EL2
+ */
asm volatile(ALTERNATIVE("isb\n mrs %0, cntvct_el0",
"nop\n" __mrs_s("%0", SYS_CNTVCTSS_EL0),
ARM64_HAS_ECV)
diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h
index 3d6725ff0..5a2f7d4e9 100644
--- a/arch/arm64/include/asm/kvm_emulate.h
+++ b/arch/arm64/include/asm/kvm_emulate.h
@@ -294,6 +294,12 @@ static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
static __always_inline u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
{
+ /*
+ * ESR_EL2, Exception Syndrome Register (EL2)
+ * EC, bits [31:26]
+ * Exception Class. Indicates the reason for the exception
+ * that this register holds information about.
+ */
return vcpu->arch.fault.esr_el2;
}
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index af06ccb7e..868271186 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -43,6 +43,17 @@
#define KVM_REQ_SLEEP \
KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
+/*
+ * 在以下使用KVM_REQ_IRQ_PENDING:
+ * - arch/arm64/kvm/arm.c|806| <<check_vcpu_requests>> kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/arm.c|1169| <<vcpu_interrupt_line>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/pmu-emul.c|509| <<kvm_pmu_perf_overflow>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/vgic/vgic-v4.c|102| <<vgic_v4_doorbell_handler>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/vgic/vgic.c|388| <<vgic_queue_irq_unlock>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/vgic/vgic.c|437| <<vgic_queue_irq_unlock>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/vgic/vgic.c|755| <<vgic_prune_ap_list>> kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
+ * - arch/arm64/kvm/vgic/vgic.c|1112| <<vgic_kick_vcpus>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ */
#define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)
#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2)
#define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3)
@@ -155,6 +166,15 @@ struct kvm_s2_mmu {
* for vEL1/EL0 with vHCR_EL2.VM == 0. In that case, we use the
* canonical stage-2 page tables.
*/
+ /*
+ * 在以下使用kvm_s2_mmu->pgd_phys:
+ * - arch/arm64/include/asm/kvm_mmu.h|289| <<kvm_get_vttbr>> baddr = mmu->pgd_phys;
+ * - arch/arm64/kvm/hyp/nvhe/mem_protect.c|157| <<kvm_host_prepare_stage2>> mmu->pgd_phys = __hyp_pa(host_mmu.pgt.pgd);
+ * - arch/arm64/kvm/hyp/nvhe/mem_protect.c|264| <<kvm_guest_prepare_stage2>> vm->kvm.arch.mmu.pgd_phys = __hyp_pa(vm->pgt.pgd);
+ * - arch/arm64/kvm/hyp/nvhe/mem_protect.c|276| <<reclaim_guest_pages>> vm->kvm.arch.mmu.pgd_phys = 0ULL;
+ * - arch/arm64/kvm/mmu.c|925| <<kvm_init_stage2_mmu>> mmu->pgd_phys = __pa(pgt->pgd);
+ * - arch/arm64/kvm/mmu.c|1017| <<kvm_free_stage2_pgd>> mmu->pgd_phys = 0;
+ */
phys_addr_t pgd_phys;
struct kvm_pgtable *pgt;
diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
index 96a80e8f6..ea3a65d15 100644
--- a/arch/arm64/include/asm/kvm_mmu.h
+++ b/arch/arm64/include/asm/kvm_mmu.h
@@ -280,12 +280,21 @@ static inline int kvm_write_guest_lock(struct kvm *kvm, gpa_t gpa,
* path, we rely on a previously issued DSB so that page table updates
* and VMID reads are correctly ordered.
*/
+/*
+ * called by:
+ * - arch/arm64/include/asm/kvm_mmu.h|303| <<__load_stage2>> write_sysreg(kvm_get_vttbr(mmu), vttbr_el2);
+ * - arch/arm64/kvm/hyp/nvhe/mem_protect.c|297| <<__pkvm_prot_finalize>> params->vttbr = kvm_get_vttbr(mmu);
+ */
static __always_inline u64 kvm_get_vttbr(struct kvm_s2_mmu *mmu)
{
struct kvm_vmid *vmid = &mmu->vmid;
u64 vmid_field, baddr;
u64 cnp = system_supports_cnp() ? VTTBR_CNP_BIT : 0;
+ /*
+ * struct kvm_s2_mmu *mmu:
+ * -> phys_addr_t pgd_phys;
+ */
baddr = mmu->pgd_phys;
vmid_field = atomic64_read(&vmid->id) << VTTBR_VMID_SHIFT;
vmid_field &= VTTBR_VMID_MASK(kvm_arm_vmid_bits);
@@ -296,10 +305,34 @@ static __always_inline u64 kvm_get_vttbr(struct kvm_s2_mmu *mmu)
* Must be called from hyp code running at EL2 with an updated VTTBR
* and interrupts disabled.
*/
+/*
+ * called by:
+ * - arch/arm64/kvm/hyp/include/nvhe/mem_protect.h|89| <<__load_host_stage2>> __load_stage2(&host_mmu.arch.mmu, &host_mmu.arch);
+ * - arch/arm64/kvm/hyp/nvhe/mem_protect.c|310| <<__pkvm_prot_finalize>> __load_stage2(&host_mmu.arch.mmu, &host_mmu.arch);
+ * - arch/arm64/kvm/hyp/nvhe/switch.c|306| <<__kvm_vcpu_run>> __load_stage2(mmu, kern_hyp_va(mmu->arch));
+ * - arch/arm64/kvm/hyp/nvhe/tlb.c|65| <<__tlb_switch_to_guest>> __load_stage2(mmu, kern_hyp_va(mmu->arch));
+ * - arch/arm64/kvm/hyp/vhe/switch.c|227| <<__kvm_vcpu_run_vhe>> __load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+ * - arch/arm64/kvm/hyp/vhe/tlb.c|56| <<__tlb_switch_to_guest>> __load_stage2(mmu, mmu->arch);
+ */
static __always_inline void __load_stage2(struct kvm_s2_mmu *mmu,
struct kvm_arch *arch)
{
+ /*
+ * struct kvm_arch *arch:
+ * -> u64 vtcr
+ */
write_sysreg(arch->vtcr, vtcr_el2);
+ /*
+ * called by:
+ * - arch/arm64/include/asm/kvm_mmu.h|303| <<__load_stage2>> write_sysreg(kvm_get_vttbr(mmu), vttbr_el2);
+ * - arch/arm64/kvm/hyp/nvhe/mem_protect.c|297| <<__pkvm_prot_finalize>> params->vttbr = kvm_get_vttbr(mmu);
+ *
+ * 在以下使用vttbr_el2:
+ * - arch/arm64/include/asm/el2_setup.h|113| <<global>> .macro __init_el2_stage2 msr vttbr_el2, xzr
+ * - arch/arm64/include/asm/kvm_mmu.h|303| <<__load_stage2>> write_sysreg(kvm_get_vttbr(mmu), vttbr_el2);
+ * - arch/arm64/kvm/hyp/include/nvhe/mem_protect.h|91| <<__load_host_stage2>> write_sysreg(0, vttbr_el2);
+ * - arch/arm64/kvm/hyp/vhe/tlb.c|69| <<__tlb_switch_to_host>> write_sysreg(0, vttbr_el2);
+ */
write_sysreg(kvm_get_vttbr(mmu), vttbr_el2);
/*
diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c
index 0fcc4eb1a..cef5caffc 100644
--- a/arch/arm64/kernel/process.c
+++ b/arch/arm64/kernel/process.c
@@ -155,6 +155,10 @@ static const char *const btypes[] = {
};
#undef bstr
+/*
+ * called by:
+ * - arch/arm64/kernel/process.c|216| <<__show_regs>> print_pstate(regs);
+ */
static void print_pstate(struct pt_regs *regs)
{
u64 pstate = regs->pstate;
@@ -197,6 +201,14 @@ static void print_pstate(struct pt_regs *regs)
}
}
+/*
+ * called by:
+ * - arch/arm64/kernel/entry-common.c|297| <<__panic_unhandled>> __show_regs(regs);
+ * - arch/arm64/kernel/process.c|245| <<show_regs>> __show_regs(regs);
+ * - arch/arm64/kernel/traps.c|263| <<arm64_show_signal>> __show_regs(regs);
+ * - arch/arm64/kernel/traps.c|943| <<panic_bad_stack>> __show_regs(regs);
+ * - arch/arm64/kernel/traps.c|961| <<arm64_serror_panic>> __show_regs(regs);
+ */
void __show_regs(struct pt_regs *regs)
{
int i, top_reg;
diff --git a/arch/arm64/kvm/arch_timer.c b/arch/arm64/kvm/arch_timer.c
index a1e24228a..ee68e4c31 100644
--- a/arch/arm64/kvm/arch_timer.c
+++ b/arch/arm64/kvm/arch_timer.c
@@ -24,13 +24,25 @@
#include "trace.h"
static struct timecounter *timecounter;
+/*
+ * 在以下设置host_vtimer_irq:
+ * - arch/arm64/kvm/arch_timer.c|1332| <<kvm_irq_init>> host_vtimer_irq = info->virtual_irq;
+ */
static unsigned int host_vtimer_irq;
+/*
+ * 在以下设置host_ptimer_irq:
+ * - arch/arm64/kvm/arch_timer.c|1359| <<kvm_irq_init>> host_ptimer_irq = info->physical_irq;
+ */
static unsigned int host_ptimer_irq;
static u32 host_vtimer_irq_flags;
static u32 host_ptimer_irq_flags;
static DEFINE_STATIC_KEY_FALSE(has_gic_active_state);
+/*
+ * 在以下使用default_ppi[]:
+ * - arch/arm64/kvm/arch_timer.c|1034| <<kvm_timer_init_vm>> kvm->arch.timer_data.ppi[i] = default_ppi[i];
+ */
static const u8 default_ppi[] = {
[TIMER_PTIMER] = 30,
[TIMER_VTIMER] = 27,
@@ -63,6 +75,13 @@ static int nr_timers(struct kvm_vcpu *vcpu)
return NR_KVM_TIMERS;
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|283| <<kvm_timer_irq_can_fire>> ((timer_get_ctl(timer_ctx) &
+ * - arch/arm64/kvm/arch_timer.c|626| <<timer_restore_state>> write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTV_CTL);
+ * - arch/arm64/kvm/arch_timer.c|636| <<timer_restore_state>> write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTP_CTL);
+ * - arch/arm64/kvm/arch_timer.c|1102| <<read_timer_ctl>> u32 ctl = timer_get_ctl(timer);
+ */
u32 timer_get_ctl(struct arch_timer_context *ctxt)
{
struct kvm_vcpu *vcpu = ctxt->vcpu;
@@ -82,6 +101,15 @@ u32 timer_get_ctl(struct arch_timer_context *ctxt)
}
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|276| <<kvm_timer_compute_delta>> return kvm_counter_compute_delta(timer_ctx, timer_get_cval(timer_ctx));
+ * - arch/arm64/kvm/arch_timer.c|417| <<kvm_timer_should_fire>> cval = timer_get_cval(timer_ctx);
+ * - arch/arm64/kvm/arch_timer.c|624| <<timer_restore_state>> write_sysreg_el0(timer_get_cval(ctx), SYS_CNTV_CVAL);
+ * - arch/arm64/kvm/arch_timer.c|630| <<timer_restore_state>> cval = timer_get_cval(ctx);
+ * - arch/arm64/kvm/arch_timer.c|1143| <<kvm_arm_timer_read>> val = timer_get_cval(timer) - kvm_phys_timer_read() + timer_get_offset(timer);
+ * - arch/arm64/kvm/arch_timer.c|1152| <<kvm_arm_timer_read>> val = timer_get_cval(timer);
+ */
u64 timer_get_cval(struct arch_timer_context *ctxt)
{
struct kvm_vcpu *vcpu = ctxt->vcpu;
@@ -101,6 +129,18 @@ u64 timer_get_cval(struct arch_timer_context *ctxt)
}
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|259| <<kvm_counter_compute_delta>> u64 now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
+ * - arch/arm64/kvm/arch_timer.c|418| <<kvm_timer_should_fire>> now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
+ * - arch/arm64/kvm/arch_timer.c|546| <<timer_save_state>> cval -= timer_get_offset(ctx);
+ * - arch/arm64/kvm/arch_timer.c|623| <<timer_restore_state>> set_cntvoff(timer_get_offset(ctx));
+ * - arch/arm64/kvm/arch_timer.c|631| <<timer_restore_state>> offset = timer_get_offset(ctx);
+ * - arch/arm64/kvm/arch_timer.c|794| <<timer_set_traps>> if (!has_cntpoff() && timer_get_offset(map->direct_ptimer))
+ * - arch/arm64/kvm/arch_timer.c|1143| <<kvm_arm_timer_read>> val = timer_get_cval(timer) - kvm_phys_timer_read() + timer_get_offset(timer);
+ * - arch/arm64/kvm/arch_timer.c|1156| <<kvm_arm_timer_read>> val = kvm_phys_timer_read() - timer_get_offset(timer);
+ * - arch/arm64/kvm/arch_timer.c|1202| <<kvm_arm_timer_write>> timer_set_cval(timer, kvm_phys_timer_read() - timer_get_offset(timer) + (s32)val);
+ */
static u64 timer_get_offset(struct arch_timer_context *ctxt)
{
u64 offset = 0;
@@ -116,6 +156,13 @@ static u64 timer_get_offset(struct arch_timer_context *ctxt)
return offset;
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|651| <<timer_save_state>> timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTV_CTL));
+ * - arch/arm64/kvm/arch_timer.c|677| <<timer_save_state>> timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTP_CTL));
+ * - arch/arm64/kvm/arch_timer.c|1114| <<kvm_timer_vcpu_reset>> timer_set_ctl(vcpu_get_timer(vcpu, i), 0);
+ * - arch/arm64/kvm/arch_timer.c|1399| <<kvm_arm_timer_write>> timer_set_ctl(timer, val & ~ARCH_TIMER_CTRL_IT_STAT);
+ */
static void timer_set_ctl(struct arch_timer_context *ctxt, u32 ctl)
{
struct kvm_vcpu *vcpu = ctxt->vcpu;
@@ -160,6 +207,13 @@ static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)
}
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|1023| <<kvm_timer_vcpu_init>> timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
+ * - arch/arm64/kvm/arch_timer.c|1024| <<kvm_timer_vcpu_init>> timer_set_offset(vcpu_ptimer(vcpu), 0);
+ * - arch/arm64/kvm/arch_timer.c|1064| <<kvm_arm_timer_set_reg>> timer_set_offset(timer, kvm_phys_timer_read() - value);
+ * - arch/arm64/kvm/arch_timer.c|1079| <<kvm_arm_timer_set_reg>> timer_set_offset(timer, kvm_phys_timer_read() - value);
+ */
static void timer_set_offset(struct arch_timer_context *ctxt, u64 offset)
{
if (!ctxt->offset.vm_offset) {
@@ -167,14 +221,48 @@ static void timer_set_offset(struct arch_timer_context *ctxt, u64 offset)
return;
}
+ /*
+ * struct arch_timer_context *ctxt:
+ * -> struct arch_timer_offset offset;
+ * -> u64 *vm_offset;
+ * -> u64 *vcpu_offset;
+ */
WRITE_ONCE(*ctxt->offset.vm_offset, offset);
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|259| <<kvm_counter_compute_delta>> u64 now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
+ * - arch/arm64/kvm/arch_timer.c|418| <<kvm_timer_should_fire>> now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
+ * - arch/arm64/kvm/arch_timer.c|1023| <<kvm_timer_vcpu_init>> timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
+ * - arch/arm64/kvm/arch_timer.c|1064| <<kvm_arm_timer_set_reg>> timer_set_offset(timer, kvm_phys_timer_read() - value);
+ * - arch/arm64/kvm/arch_timer.c|1079| <<kvm_arm_timer_set_reg>> timer_set_offset(timer, kvm_phys_timer_read() - value);
+ * - arch/arm64/kvm/arch_timer.c|1143| <<kvm_arm_timer_read>> val = timer_get_cval(timer) - kvm_phys_timer_read() + timer_get_offset(timer);
+ * - arch/arm64/kvm/arch_timer.c|1156| <<kvm_arm_timer_read>> val = kvm_phys_timer_read() - timer_get_offset(timer);
+ * - arch/arm64/kvm/arch_timer.c|1202| <<kvm_arm_timer_write>> timer_set_cval(timer, kvm_phys_timer_read() - timer_get_offset(timer) + (s32)val);
+ */
u64 kvm_phys_timer_read(void)
{
+ /*
+ * static struct timecounter *timecounter;
+ * -> const struct cyclecounter *cc;
+ */
return timecounter->cc->read(timecounter->cc);
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|302| <<kvm_arch_timer_handler>> get_timer_map(vcpu, &map);
+ * - arch/arm64/kvm/arch_timer.c|667| <<kvm_timer_blocking>> get_timer_map(vcpu, &map);
+ * - arch/arm64/kvm/arch_timer.c|936| <<kvm_timer_vcpu_load>> get_timer_map(vcpu, &map);
+ * - arch/arm64/kvm/arch_timer.c|987| <<kvm_timer_vcpu_put>> get_timer_map(vcpu, &map);
+ * - arch/arm64/kvm/arch_timer.c|1045| <<kvm_timer_vcpu_reset>> get_timer_map(vcpu, &map);
+ * - arch/arm64/kvm/arch_timer.c|1298| <<kvm_arm_timer_read_sysreg>> get_timer_map(vcpu, &map);
+ * - arch/arm64/kvm/arch_timer.c|1350| <<kvm_arm_timer_write_sysreg>> get_timer_map(vcpu, &map);
+ * - arch/arm64/kvm/arch_timer.c|1670| <<kvm_timer_enable>> get_timer_map(vcpu, &map);
+ * - arch/arm64/kvm/hyp/vhe/switch.c|54| <<__activate_traps>> get_timer_map(vcpu, &map);
+ * - arch/arm64/kvm/hyp/vhe/switch.c|124| <<__deactivate_traps>> get_timer_map(vcpu, &map);
+ */
void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
{
if (vcpu_has_nv(vcpu)) {
@@ -190,7 +278,13 @@ void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
map->emul_ptimer = vcpu_hptimer(vcpu);
}
} else if (has_vhe()) {
+ /*
+ * (&(v)->arch.timer_cpu.timers[TIMER_VTIMER])
+ */
map->direct_vtimer = vcpu_vtimer(vcpu);
+ /*
+ * (&(v)->arch.timer_cpu.timers[TIMER_PTIMER])
+ */
map->direct_ptimer = vcpu_ptimer(vcpu);
map->emul_vtimer = NULL;
map->emul_ptimer = NULL;
@@ -210,6 +304,11 @@ static inline bool userspace_irqchip(struct kvm *kvm)
unlikely(!irqchip_in_kernel(kvm));
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|574| <<timer_emulate>> soft_timer_start(&ctx->hrtimer, kvm_timer_compute_delta(ctx));
+ * - arch/arm64/kvm/arch_timer.c|684| <<kvm_timer_blocking>> soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
+ */
static void soft_timer_start(struct hrtimer *hrt, u64 ns)
{
hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
@@ -221,6 +320,11 @@ static void soft_timer_cancel(struct hrtimer *hrt)
hrtimer_cancel(hrt);
}
+/*
+ * 在以下使用kvm_arch_timer_handler():
+ * - arch/arm64/kvm/arch_timer.c|1389| <<kvm_timer_hyp_init>> err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler, "kvm guest vtimer", kvm_get_running_vcpus());
+ * - arch/arm64/kvm/arch_timer.c|1413| <<kvm_timer_hyp_init>> err = request_percpu_irq(host_ptimer_irq, kvm_arch_timer_handler, "kvm guest ptimer", kvm_get_running_vcpus());
+ */
static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
{
struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
@@ -328,6 +432,10 @@ static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
return min_delta;
}
+/*
+ * bg_timer的到期执行函数,当需要调用kvm_vcpu_block让vcpu睡眠时,
+ * 需要先启动bg_timer,bg_timer到期时再将vcpu唤醒;
+ */
static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
{
struct arch_timer_cpu *timer;
@@ -352,6 +460,15 @@ static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
return HRTIMER_NORESTART;
}
+/*
+ * 虚拟counter的中断被路由到el2层. 当VM执行时,时钟到期,虚拟counter的中断产生.
+ * 这时VM exit,由KVM处理该中断,执行中断的inject(把中断路由给vGIC).
+ * 然后再次进入guest时,就有中断了.
+ *
+ * 还有一种情况: guest side退出前设置一个timer,这时KVM需要维护这个timer,以在
+ * timer到期的时候(还在host mode)唤醒guest并inject irq.
+ * KVM注册了一个hrtimer来处理这种情况.
+ */
static enum hrtimer_restart kvm_hrtimer_expire(struct hrtimer *hrt)
{
struct arch_timer_context *ctx;
@@ -443,6 +560,16 @@ void kvm_timer_update_run(struct kvm_vcpu *vcpu)
regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|247| <<kvm_arch_timer_handler>> kvm_timer_update_irq(vcpu, true, ctx);
+ * - arch/arm64/kvm/arch_timer.c|377| <<kvm_hrtimer_expire>> kvm_timer_update_irq(vcpu, true, ctx);
+ * - arch/arm64/kvm/arch_timer.c|472| <<timer_emulate>> kvm_timer_update_irq(ctx->vcpu, should_fire, ctx);
+ * - arch/arm64/kvm/arch_timer.c|667| <<kvm_timer_vcpu_load_gic>> kvm_timer_update_irq(ctx->vcpu, kvm_timer_should_fire(ctx), ctx);
+ * - arch/arm64/kvm/arch_timer.c|687| <<kvm_timer_vcpu_load_nogic>> kvm_timer_update_irq(vcpu, kvm_timer_should_fire(vtimer), vtimer);
+ * - arch/arm64/kvm/arch_timer.c|920| <<unmask_vtimer_irq_user>> kvm_timer_update_irq(vcpu, false, vtimer);
+ * - arch/arm64/kvm/arch_timer.c|969| <<kvm_timer_vcpu_reset>> kvm_timer_update_irq(vcpu, false,
+ */
static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
struct arch_timer_context *timer_ctx)
{
@@ -453,6 +580,14 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
timer_ctx->irq.level);
if (!userspace_irqchip(vcpu->kvm)) {
+ /*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|456| <<kvm_timer_update_irq>> ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
+ * - arch/arm64/kvm/arm.c|1195| <<kvm_vm_ioctl_irq_line>> return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
+ * - arch/arm64/kvm/arm.c|1203| <<kvm_vm_ioctl_irq_line>> return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
+ * - arch/arm64/kvm/pmu-emul.c|351| <<kvm_pmu_update_state>> int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
+ * - arch/arm64/kvm/vgic/vgic-irqfd.c|26| <<vgic_irqfd_set_irq>> return kvm_vgic_inject_irq(kvm, 0, spi_id, level, NULL);
+ */
ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
timer_irq(timer_ctx),
timer_ctx->irq.level,
@@ -461,6 +596,12 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
}
}
+/*
+ * 似乎一般的硬件虚拟化是不用的:
+ * - arch/arm64/kvm/arch_timer.c|955| <<kvm_timer_vcpu_load>> timer_emulate(map.emul_vtimer);
+ * - arch/arm64/kvm/arch_timer.c|957| <<kvm_timer_vcpu_load>> timer_emulate(map.emul_ptimer);
+ * - arch/arm64/kvm/arch_timer.c|1355| <<kvm_arm_timer_write_sysreg>> timer_emulate(timer);
+ */
/* Only called for a fully emulated timer */
static void timer_emulate(struct arch_timer_context *ctx)
{
@@ -601,6 +742,13 @@ static void kvm_timer_unblocking(struct kvm_vcpu *vcpu)
soft_timer_cancel(&timer->bg_timer);
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|850| <<kvm_timer_vcpu_load>> timer_restore_state(map.direct_vtimer);
+ * - arch/arm64/kvm/arch_timer.c|852| <<kvm_timer_vcpu_load>> timer_restore_state(map.direct_ptimer);
+ * - arch/arm64/kvm/arch_timer.c|1189| <<kvm_arm_timer_read_sysreg>> timer_restore_state(timer);
+ * - arch/arm64/kvm/arch_timer.c|1240| <<kvm_arm_timer_write_sysreg>> timer_restore_state(timer);
+ */
static void timer_restore_state(struct arch_timer_context *ctx)
{
struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu);
@@ -762,6 +910,10 @@ static void kvm_timer_vcpu_load_nested_switch(struct kvm_vcpu *vcpu,
}
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|1003| <<kvm_timer_vcpu_load>> timer_set_traps(vcpu, &map);
+ */
static void timer_set_traps(struct kvm_vcpu *vcpu, struct timer_map *map)
{
bool tpt, tpc;
@@ -817,13 +969,62 @@ static void timer_set_traps(struct kvm_vcpu *vcpu, struct timer_map *map)
set = 0;
clr = 0;
+ /*
+ * - EL0PCTEN, bit [0]
+ *
+ * When HCR_EL2.TGE is 0, this control does not cause any instructions to be
+ * trapped.
+ * When HCR_EL2.TGE is 1, traps EL0 accesses to the frequency register and
+ * physical counter register to EL2, e.g., CNTPCT_EL0.
+ *
+ * - EL0VCTEN, bit [1]
+ *
+ * When HCR_EL2.TGE is 0, this control does not cause any instructions to be
+ * trapped.
+ * When HCR_EL2.TGE is 1, traps EL0 accesses to the frequency register and virtual
+ * counter register to EL2, e.g., CNTVCT_EL0.
+ */
assign_clear_set_bit(tpt, CNTHCTL_EL1PCEN << 10, set, clr);
assign_clear_set_bit(tpc, CNTHCTL_EL1PCTEN << 10, set, clr);
+ /*
+ * 在以下使用cnthctl_el2:
+ * - arch/arm64/include/asm/el2_setup.h|56| <<global>> msr cnthctl_el2, x0
+ * - arch/arm64/kvm/arch_timer.c|1007| <<timer_set_traps>> sysreg_clear_set(cnthctl_el2, clr, set);
+ * - arch/arm64/kvm/arch_timer.c|1817| <<kvm_timer_init_vhe>> sysreg_clear_set(cnthctl_el2, 0, CNTHCTL_ECV);
+ * - arch/arm64/kvm/hyp/nvhe/timer-sr.c|31| <<__timer_disable_traps>> val = read_sysreg(cnthctl_el2);
+ * - arch/arm64/kvm/hyp/nvhe/timer-sr.c|33| <<__timer_disable_traps>> write_sysreg(val, cnthctl_el2);
+ * - arch/arm64/kvm/hyp/nvhe/timer-sr.c|61| <<__timer_enable_traps>> sysreg_clear_set(cnthctl_el2, clr, set);
+ *
+ * - EL1NVVCT, bit [16]
+ * Traps EL1 accesses to the specified EL1 virtual timer registers
+ * using the EL02 descriptors to EL2, when EL2 is enabled for the
+ * current Security state.
+ *
+ * - EL1NVPCT, bit [15]
+ * Traps EL1 accesses to the specified EL1 physical timer registers
+ * using the EL02 descriptors to EL2, when EL2 is enabled for the
+ * current Security state.
+ *
+ *
+ * - EL1TVT: 如果是0就不会trap任何
+ *
+ * - EL1PTEN: 如果是1或者HCR_EL2.TGE=1, 就不会trap任何
+ * 如果是0(没配置), 就会trap一些寄存器, 比如CNTP_CTL_EL0
+ */
+
+ /*
+ * Modify bits in a sysreg. Bits in the clear mask are zeroed, then bits in the
+ * set mask are set. Other bits are left as-is.
+ */
/* This only happens on VHE, so use the CNTHCTL_EL2 accessor. */
sysreg_clear_set(cnthctl_el2, clr, set);
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arm.c|465| <<kvm_arch_vcpu_load>> kvm_timer_vcpu_load(vcpu);
+ */
void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
{
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
@@ -1048,6 +1249,10 @@ void kvm_timer_cpu_down(void)
disable_percpu_irq(host_ptimer_irq);
}
+/*
+ * called by:
+ * - arch/arm64/kvm/guest.c|639| <<set_timer_reg>> return kvm_arm_timer_set_reg(vcpu, reg->id, val);
+ */
int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
{
struct arch_timer_context *timer;
@@ -1091,6 +1296,10 @@ int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
return 0;
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|1308| <<kvm_arm_timer_read>> val = read_timer_ctl(timer);
+ */
static u64 read_timer_ctl(struct arch_timer_context *timer)
{
/*
@@ -1132,6 +1341,25 @@ u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
return (u64)-1;
}
+/*
+ * enum kvm_arch_timer_regs {
+ * TIMER_REG_CNT,
+ * TIMER_REG_CVAL,
+ * TIMER_REG_TVAL,
+ * TIMER_REG_CTL,
+ * TIMER_REG_VOFF,
+ * };
+ *
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|1114| <<kvm_arm_timer_get_reg(KVM_REG_ARM_TIMER_CTL)>> return kvm_arm_timer_read(vcpu, vcpu_vtimer(vcpu), TIMER_REG_CTL);
+ * - arch/arm64/kvm/arch_timer.c|1117| <<kvm_arm_timer_get_reg(KVM_REG_ARM_TIMER_CNT)>> return kvm_arm_timer_read(vcpu, vcpu_vtimer(vcpu), TIMER_REG_CNT);
+ * - arch/arm64/kvm/arch_timer.c|1120| <<kvm_arm_timer_get_reg(KVM_REG_ARM_TIMER_CVAL)>> return kvm_arm_timer_read(vcpu, vcpu_vtimer(vcpu), TIMER_REG_CVAL);
+ * - arch/arm64/kvm/arch_timer.c|1123| <<kvm_arm_timer_get_reg(KVM_REG_ARM_PTIMER_CTL)>> return kvm_arm_timer_read(vcpu, vcpu_ptimer(vcpu), TIMER_REG_CTL);
+ * - arch/arm64/kvm/arch_timer.c|1126| <<kvm_arm_timer_get_reg(KVM_REG_ARM_PTIMER_CNT)>> return kvm_arm_timer_read(vcpu, vcpu_ptimer(vcpu), TIMER_REG_CNT);
+ * - arch/arm64/kvm/arch_timer.c|1129| <<kvm_arm_timer_get_reg(KVM_REG_ARM_PTIMER_CVAL)>> return kvm_arm_timer_read(vcpu, vcpu_ptimer(vcpu), TIMER_REG_CVAL);
+ * - arch/arm64/kvm/arch_timer.c|1182| <<kvm_arm_timer_read_sysreg>> return kvm_arm_timer_read(vcpu, timer, treg);
+ * - arch/arm64/kvm/arch_timer.c|1187| <<kvm_arm_timer_read_sysreg>> val = kvm_arm_timer_read(vcpu, timer, treg);
+ */
static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
struct arch_timer_context *timer,
enum kvm_arch_timer_regs treg)
@@ -1192,6 +1420,15 @@ u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu,
return val;
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|1199| <<kvm_arm_timer_set_reg>> kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
+ * - arch/arm64/kvm/arch_timer.c|1210| <<kvm_arm_timer_set_reg>> kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
+ * - arch/arm64/kvm/arch_timer.c|1214| <<kvm_arm_timer_set_reg>> kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
+ * - arch/arm64/kvm/arch_timer.c|1225| <<kvm_arm_timer_set_reg>> kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
+ * - arch/arm64/kvm/arch_timer.c|1394| <<kvm_arm_timer_write_sysreg>> kvm_arm_timer_write(vcpu, timer, treg, val);
+ * - arch/arm64/kvm/arch_timer.c|1399| <<kvm_arm_timer_write_sysreg>> kvm_arm_timer_write(vcpu, timer, treg, val);
+ */
static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
struct arch_timer_context *timer,
enum kvm_arch_timer_regs treg,
@@ -1219,6 +1456,10 @@ static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
}
}
+/*
+ * called by:
+ * - arch/arm64/kvm/sys_regs.c|1204| <<access_arch_timer>> kvm_arm_timer_write_sysreg(vcpu, tmr, treg, p->regval);
+ */
void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,
enum kvm_arch_timers tmr,
enum kvm_arch_timer_regs treg,
@@ -1319,6 +1560,10 @@ static void kvm_irq_fixup_flags(unsigned int virq, u32 *flags)
}
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arch_timer.c|1383| <<kvm_timer_hyp_init>> err = kvm_irq_init(info);
+ */
static int kvm_irq_init(struct arch_timer_kvm_info *info)
{
struct irq_domain *domain = NULL;
@@ -1367,12 +1612,27 @@ static int kvm_irq_init(struct arch_timer_kvm_info *info)
return 0;
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arm.c|2059| <<init_subsystems>> err = kvm_timer_hyp_init(vgic_present);
+ */
int __init kvm_timer_hyp_init(bool has_gic)
{
struct arch_timer_kvm_info *info;
int err;
+ /*
+ * struct arch_timer_kvm_info {
+ * struct timecounter timecounter;
+ * int virtual_irq;
+ * int physical_irq;
+ * };
+ */
info = arch_timer_get_kvm_info();
+ /*
+ * 这个文件的静态变量
+ * static struct timecounter *timecounter;
+ */
timecounter = &info->timecounter;
if (!timecounter->cc) {
@@ -1445,6 +1705,10 @@ int __init kvm_timer_hyp_init(bool has_gic)
return err;
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arm.c|416| <<kvm_arch_vcpu_destroy>> kvm_timer_vcpu_terminate(vcpu);
+ */
void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
{
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
@@ -1506,6 +1770,10 @@ static bool kvm_arch_timer_get_input_level(int vintid)
return false;
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arm.c|632| <<kvm_arch_vcpu_run_pid_change>> ret = kvm_timer_enable(vcpu);
+ */
int kvm_timer_enable(struct kvm_vcpu *vcpu)
{
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
@@ -1552,9 +1820,26 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu)
return 0;
}
+/*
+ * called by:
+ * - arch/arm64/kvm/arm.c|1869| <<cpu_hyp_init_features>> kvm_timer_init_vhe();
+ */
/* If we have CNTPOFF, permanently set ECV to enable it */
void kvm_timer_init_vhe(void)
{
+ /*
+ * 在以下使用cnthctl_el2:
+ * - arch/arm64/include/asm/el2_setup.h|56| <<global>> msr cnthctl_el2, x0
+ * - arch/arm64/kvm/arch_timer.c|1007| <<timer_set_traps>> sysreg_clear_set(cnthctl_el2, clr, set);
+ * - arch/arm64/kvm/arch_timer.c|1817| <<kvm_timer_init_vhe>> sysreg_clear_set(cnthctl_el2, 0, CNTHCTL_ECV);
+ * - arch/arm64/kvm/hyp/nvhe/timer-sr.c|31| <<__timer_disable_traps>> val = read_sysreg(cnthctl_el2);
+ * - arch/arm64/kvm/hyp/nvhe/timer-sr.c|33| <<__timer_disable_traps>> write_sysreg(val, cnthctl_el2);
+ * - arch/arm64/kvm/hyp/nvhe/timer-sr.c|61| <<__timer_enable_traps>> sysreg_clear_set(cnthctl_el2, clr, set);
+ */
+ /*
+ * Modify bits in a sysreg. Bits in the clear mask are zeroed, then bits in the
+ * set mask are set. Other bits are left as-is.
+ */
if (cpus_have_final_cap(ARM64_HAS_ECV_CNTPOFF))
sysreg_clear_set(cnthctl_el2, 0, CNTHCTL_ECV);
}
@@ -1611,6 +1896,10 @@ int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
return ret;
}
+/*
+ * 处理KVM_ARM_SET_COUNTER_OFFSET:
+ * - arch/arm64/kvm/arm.c|1634| <<kvm_arch_vm_ioctl>> return kvm_vm_ioctl_set_counter_offset(kvm, &offset);
+ */
int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
{
int __user *uaddr = (int __user *)(long)attr->addr;
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 4866b3f7b..8383bd564 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -133,6 +133,10 @@ static int kvm_arm_default_max_vcpus(void)
* kvm_arch_init_vm - initializes a VM data structure
* @kvm: pointer to the KVM struct
*/
+/*
+ * called by:
+ * - virt/kvm/kvm_main.c|1222| <<kvm_create_vm>> r = kvm_arch_init_vm(kvm, type);
+ */
int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
{
int ret;
@@ -161,6 +165,10 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
}
cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
+ /*
+ * called by:
+ * - arch/arm64/kvm/arm.c|164| <<kvm_arch_init_vm>> ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
+ */
ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
if (ret)
goto err_free_cpumask;
@@ -421,6 +429,14 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
}
+/*
+ * called by:
+ * - arch/arm64/kvm/emulate-nested.c|1947| <<kvm_emulate_nested_eret>> kvm_arch_vcpu_load(vcpu, smp_processor_id());
+ * - arch/arm64/kvm/emulate-nested.c|2028| <<kvm_inject_nested>> kvm_arch_vcpu_load(vcpu, smp_processor_id());
+ * - arch/arm64/kvm/reset.c|300| <<kvm_reset_vcpu>> kvm_arch_vcpu_load(vcpu, smp_processor_id());
+ * - virt/kvm/kvm_main.c|216| <<vcpu_load>> kvm_arch_vcpu_load(vcpu, cpu);
+ * - virt/kvm/kvm_main.c|5963| <<kvm_sched_in>> kvm_arch_vcpu_load(vcpu, cpu);
+ */
void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
{
struct kvm_s2_mmu *mmu;
@@ -705,6 +721,12 @@ static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
* the vCPU is runnable. The vCPU may or may not be scheduled out, depending
* on when a wake event arrives, e.g. there may already be a pending wake event.
*/
+/*
+ * called by:
+ * - arch/arm64/kvm/arm.c|756| <<kvm_vcpu_suspend>> kvm_vcpu_wfi(vcpu);
+ * - arch/arm64/kvm/handle_exit.c|147| <<kvm_handle_wfx>> kvm_vcpu_wfi(vcpu);
+ * - arch/arm64/kvm/psci.c|49| <<kvm_psci_vcpu_suspend>> kvm_vcpu_wfi(vcpu);
+ */
void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
{
/*
@@ -774,6 +796,10 @@ static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
* < 0 if we should exit to userspace, where the return value indicates
* an error
*/
+/*
+ * called by:
+ * - arch/arm64/kvm/arm.c|975| <<kvm_arch_vcpu_ioctl_run>> ret = check_vcpu_requests(vcpu);
+ */
static int check_vcpu_requests(struct kvm_vcpu *vcpu)
{
if (kvm_request_pending(vcpu)) {
@@ -783,6 +809,17 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
kvm_reset_vcpu(vcpu);
+ /*
+ * 在以下使用KVM_REQ_IRQ_PENDING:
+ * - arch/arm64/kvm/arm.c|806| <<check_vcpu_requests>> kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/arm.c|1169| <<vcpu_interrupt_line>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/pmu-emul.c|509| <<kvm_pmu_perf_overflow>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/vgic/vgic-v4.c|102| <<vgic_v4_doorbell_handler>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/vgic/vgic.c|388| <<vgic_queue_irq_unlock>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/vgic/vgic.c|437| <<vgic_queue_irq_unlock>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ * - arch/arm64/kvm/vgic/vgic.c|755| <<vgic_prune_ap_list>> kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
+ * - arch/arm64/kvm/vgic/vgic.c|1112| <<vgic_kick_vcpus>> kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+ */
/*
* Clear IRQ_PENDING requests that were made to guarantee
* that a VCPU sees new virtual interrupts.
@@ -842,6 +879,10 @@ static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
* true. For an exit to preemptible + interruptible kernel context (i.e. check
* for pending work and re-enter), return true without writing to ret.
*/
+/*
+ * called by:
+ * - arch/arm64/kvm/arm.c|1000| <<kvm_arch_vcpu_ioctl_run>> if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
+ */
static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
{
struct kvm_run *run = vcpu->run;
@@ -870,6 +911,15 @@ static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
return true;
}
+ /*
+ * xfer_to_guest_mode_work_pending - Check if work is pending which needs to be
+ * handled before returning to guest mode
+ *
+ * Returns: True if work pending, False otherwise.
+ *
+ * Has to be invoked with interrupts disabled before the transition to
+ * guest mode.
+ */
return kvm_request_pending(vcpu) ||
xfer_to_guest_mode_work_pending();
}
@@ -881,6 +931,10 @@ static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
* This must be noinstr as instrumentation may make use of RCU, and this is not
* safe during the EQS.
*/
+/*
+ * called by:
+ * - arch/arm64/kvm/arm.c|1008| <<kvm_arch_vcpu_ioctl_run>> ret = kvm_arm_vcpu_enter_exit(vcpu);
+ */
static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
{
int ret;
@@ -902,17 +956,54 @@ static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
* return with return value 0 and with the kvm_run structure filled in with the
* required data for the requested emulation.
*/
+/*
+ * kvm_arch_vcpu_ioctl_run()
+ * -> handle_exit()
+ * -> handle_trap_exceptions(vcpu)
+ * -> kvm_get_exit_handler()
+ * -> arm_exit_handlers[esr_ec]
+ *
+ * called by:
+ * - virt/kvm/kvm_main.c|4175| <<kvm_vcpu_ioctl(KVM_RUN)>> r = kvm_arch_vcpu_ioctl_run(vcpu);
+ */
int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
{
struct kvm_run *run = vcpu->run;
int ret;
+ /*
+ * 设置run->exit_reason为KVM_EXIT_MMIO
+ * - arch/arm64/kvm/mmio.c|199| <<io_mem_abort>> run->exit_reason = KVM_EXIT_MMIO;
+ */
if (run->exit_reason == KVM_EXIT_MMIO) {
+ /*
+ * called by:
+ * - arch/arm64/kvm/arm.c|935| <<kvm_arch_vcpu_ioctl_run>> ret = kvm_handle_mmio_return(vcpu);
+ * - arch/arm64/kvm/mmio.c|187| <<io_mem_abort>> kvm_handle_mmio_return(vcpu);
+ *
+ * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation
+ * or in-kernel IO emulatio
+ */
ret = kvm_handle_mmio_return(vcpu);
if (ret)
return ret;
}
+ /*
+ * 关于arm64的调用:
+ * - 只在这里!!!
+ *
+ * 关于kvm_arch_vcpu_load()的调用:
+ * - arch/arm64/kvm/emulate-nested.c|1947| <<kvm_emulate_nested_eret>> kvm_arch_vcpu_load(vcpu, smp_processor_id());
+ * - arch/arm64/kvm/emulate-nested.c|2028| <<kvm_inject_nested>> kvm_arch_vcpu_load(vcpu, smp_processor_id());
+ * - arch/arm64/kvm/reset.c|300| <<kvm_reset_vcpu>> kvm_arch_vcpu_load(vcpu, smp_processor_id());
+ * - virt/kvm/kvm_main.c|216| <<vcpu_load>> kvm_arch_vcpu_load(vcpu, cpu);
+ * - virt/kvm/kvm_main.c|5963| <<kvm_sched_in>> kvm_arch_vcpu_load(vcpu, cpu);
+ *
+ * vcpu_load()
+ * -> kvm_arch_vcpu_load()
+ * -> kvm_vcpu_load_sysregs_vhe()
+ */
vcpu_load(vcpu);
if (run->immediate_exit) {
@@ -920,6 +1011,15 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
goto out;
}
+ /*
+ * called by:
+ * - arch/arm64/kvm/arm.c|939| <<kvm_arch_vcpu_ioctl_run>> kvm_sigset_activate(vcpu);
+ * - arch/mips/kvm/mips.c|431| <<kvm_arch_vcpu_ioctl_run>> kvm_sigset_activate(vcpu);
+ * - arch/powerpc/kvm/powerpc.c|1859| <<kvm_arch_vcpu_ioctl_run>> kvm_sigset_activate(vcpu);
+ * - arch/riscv/kvm/vcpu.c|664| <<kvm_arch_vcpu_ioctl_run>> kvm_sigset_activate(vcpu);