KVM源码目录树分析

  • linux 4.* 内核源码囊括KVM模块源码,拆分一下看看

1532753517013.png

1532753528076.png

KVM相关文件目录

首先在顶层目录搜索一下

  1. root@ubuntu16x64:~/kernel/linux-4.4.124# find ./ -name "*kvm*"
  2. ./arch/s390/include/uapi/asm/kvm_perf.h
  3. ./arch/s390/include/uapi/asm/kvm_virtio.h
  4. ./arch/s390/include/uapi/asm/kvm.h
  5. ./arch/s390/include/uapi/asm/kvm_para.h
  6. ./arch/s390/include/asm/kvm_host.h
  7. ./arch/s390/include/asm/kvm_para.h
  8. ./arch/s390/kvm
  9. ./arch/s390/kvm/kvm-s390.c
  10. ./arch/s390/kvm/kvm-s390.h
  11. ./arch/arm64/include/uapi/asm/kvm.h
  12. ./arch/arm64/include/asm/kvm_coproc.h
  13. ./arch/arm64/include/asm/kvm_emulate.h
  14. ./arch/arm64/include/asm/kvm_mmio.h
  15. ./arch/arm64/include/asm/kvm_psci.h
  16. ./arch/arm64/include/asm/kvm_arm.h
  17. ./arch/arm64/include/asm/kvm_mmu.h
  18. ./arch/arm64/include/asm/kvm_asm.h
  19. ./arch/arm64/include/asm/kvm_host.h
  20. ./arch/arm64/kvm
  21. ./arch/powerpc/include/uapi/asm/kvm.h
  22. ./arch/powerpc/include/uapi/asm/kvm_para.h
  23. ./arch/powerpc/include/asm/kvm_booke_hv_asm.h
  24. ./arch/powerpc/include/asm/kvm_fpu.h
  25. ./arch/powerpc/include/asm/kvm_book3s_asm.h
  26. ./arch/powerpc/include/asm/kvm_book3s_32.h
  27. ./arch/powerpc/include/asm/kvm_asm.h
  28. ./arch/powerpc/include/asm/kvm_book3s.h
  29. ./arch/powerpc/include/asm/kvm_book3s_64.h
  30. ./arch/powerpc/include/asm/kvm_host.h
  31. ./arch/powerpc/include/asm/kvm_booke.h
  32. ./arch/powerpc/include/asm/kvm_ppc.h
  33. ./arch/powerpc/include/asm/kvm_para.h
  34. ./arch/powerpc/kvm
  35. ./arch/powerpc/kernel/kvm.c
  36. ./arch/powerpc/kernel/kvm_emul.S
  37. ./arch/arm/include/uapi/asm/kvm.h
  38. ./arch/arm/include/uapi/asm/kvm_para.h
  39. ./arch/arm/include/asm/kvm_coproc.h
  40. ./arch/arm/include/asm/kvm_emulate.h
  41. ./arch/arm/include/asm/kvm_mmio.h
  42. ./arch/arm/include/asm/kvm_psci.h
  43. ./arch/arm/include/asm/kvm_arm.h
  44. ./arch/arm/include/asm/kvm_mmu.h
  45. ./arch/arm/include/asm/kvm_asm.h
  46. ./arch/arm/include/asm/kvm_host.h
  47. ./arch/arm/kvm
  48. ./arch/frv/include/uapi/asm/kvm_para.h
  49. ./arch/sparc/include/uapi/asm/kvm_para.h
  50. ./arch/x86/configs/kvm_guest.config
  51. ./arch/x86/include/uapi/asm/kvm_perf.h
  52. ./arch/x86/include/uapi/asm/kvm.h
  53. ./arch/x86/include/uapi/asm/kvm_para.h
  54. ./arch/x86/include/asm/kvm_emulate.h
  55. ./arch/x86/include/asm/kvm_guest.h
  56. ./arch/x86/include/asm/kvm_host.h
  57. ./arch/x86/include/asm/kvm_para.h
  58. ./arch/x86/kvm
  59. ./arch/x86/kvm/kvm_cache_regs.h
  60. ./arch/x86/kernel/kvmclock.o
  61. ./arch/x86/kernel/kvm.o
  62. ./arch/x86/kernel/kvm.c
  63. ./arch/x86/kernel/.kvmclock.o.cmd
  64. ./arch/x86/kernel/kvmclock.c
  65. ./arch/x86/kernel/.kvm.o.cmd
  66. ./arch/tile/include/uapi/asm/kvm_para.h
  67. ./arch/tile/kvm
  68. ./arch/mn10300/include/uapi/asm/kvm_para.h
  69. ./arch/mips/configs/malta_kvm_guest_defconfig
  70. ./arch/mips/configs/malta_kvm_defconfig
  71. ./arch/mips/include/uapi/asm/kvm.h
  72. ./arch/mips/include/uapi/asm/kvm_para.h
  73. ./arch/mips/include/asm/kvm_host.h
  74. ./arch/mips/include/asm/kvm_para.h
  75. ./arch/mips/kvm
  76. ./arch/um/include/asm/kvm_para.h
  77. ./arch/microblaze/include/uapi/asm/kvm_para.h
  78. ./arch/hexagon/include/uapi/asm/kvm_para.h
  79. ./arch/alpha/include/uapi/asm/kvm_para.h
  80. ./arch/score/include/uapi/asm/kvm_para.h
  81. ./virt/kvm
  82. ./virt/kvm/kvm_main.c
  83. ./drivers/s390/virtio/kvm_virtio.c
  84. ./include/linux/kvm_irqfd.h
  85. ./include/linux/kvm_types.h
  86. ./include/linux/kvm_host.h
  87. ./include/linux/kvm_para.h
  88. ./include/kvm
  89. ./include/asm-generic/kvm_para.h
  90. ./include/uapi/linux/kvm.h
  91. ./include/uapi/linux/kvm_para.h
  92. ./include/uapi/asm-generic/kvm_para.h
  93. ./include/config/kvm
  94. ./include/config/have/kvm
  95. ./include/config/have/kvm.h
  96. ./include/config/kvm.h
  97. ./include/trace/events/kvm.h
  98. ./tools/testing/selftests/rcutorture/bin/kvm-recheck-lock.sh
  99. ./tools/testing/selftests/rcutorture/bin/kvm-recheck-rcu.sh
  100. ./tools/testing/selftests/rcutorture/bin/kvm-build.sh
  101. ./tools/testing/selftests/rcutorture/bin/kvm-test-1-run.sh
  102. ./tools/testing/selftests/rcutorture/bin/kvm.sh
  103. ./tools/testing/selftests/rcutorture/bin/kvm-recheck.sh
  104. ./tools/testing/ktest/examples/kvm.conf
  105. ./tools/perf/arch/s390/util/kvm-stat.c
  106. ./tools/perf/arch/x86/util/kvm-stat.c
  107. ./tools/perf/util/kvm-stat.h
  108. ./tools/perf/builtin-kvm.c
  109. ./tools/perf/Documentation/perf-kvm.txt
  110. ./tools/lib/traceevent/plugin_kvm.c
  111. ./Documentation/virtual/kvm

移去非x86架构的相关文件

  1. # x86体系结构相关
  2. ./arch/x86/configs/kvm_guest.config
  3. ./arch/x86/include/uapi/asm/kvm_perf.h
  4. ./arch/x86/include/uapi/asm/kvm.h
  5. ./arch/x86/include/uapi/asm/kvm_para.h
  6. ./arch/x86/include/asm/kvm_emulate.h
  7. ./arch/x86/include/asm/kvm_guest.h
  8. ./arch/x86/include/asm/kvm_host.h
  9. ./arch/x86/include/asm/kvm_para.h
  10. ./arch/x86/kvm
  11. ./arch/x86/kvm/kvm_cache_regs.h
  12. ./arch/x86/kernel/kvmclock.o
  13. ./arch/x86/kernel/kvm.o
  14. ./arch/x86/kernel/kvm.c
  15. ./arch/x86/kernel/.kvmclock.o.cmd
  16. ./arch/x86/kernel/kvmclock.c
  17. ./arch/x86/kernel/.kvm.o.cmd
  18. ./virt/kvm
  19. ./virt/kvm/kvm_main.c
  20. ./drivers/s390/virtio/kvm_virtio.c
  21. # 头文件依赖
  22. ./include/linux/kvm_irqfd.h
  23. ./include/linux/kvm_types.h
  24. ./include/linux/kvm_host.h
  25. ./include/linux/kvm_para.h
  26. ./include/kvm
  27. ./include/asm-generic/kvm_para.h
  28. ./include/uapi/linux/kvm.h
  29. ./include/uapi/linux/kvm_para.h
  30. ./include/uapi/asm-generic/kvm_para.h
  31. ./include/config/kvm
  32. ./include/config/have/kvm
  33. ./include/config/have/kvm.h
  34. ./include/config/kvm.h
  35. ./include/trace/events/kvm.h
  36. # 测试工具
  37. ./tools/testing/selftests/rcutorture/bin/kvm-recheck-lock.sh
  38. ./tools/testing/selftests/rcutorture/bin/kvm-recheck-rcu.sh
  39. ./tools/testing/selftests/rcutorture/bin/kvm-build.sh
  40. ./tools/testing/selftests/rcutorture/bin/kvm-test-1-run.sh
  41. ./tools/testing/selftests/rcutorture/bin/kvm.sh
  42. ./tools/testing/selftests/rcutorture/bin/kvm-recheck.sh
  43. ./tools/testing/ktest/examples/kvm.conf
  44. ./tools/perf/arch/x86/util/kvm-stat.c
  45. ./tools/perf/util/kvm-stat.h
  46. ./tools/perf/builtin-kvm.c
  47. ./tools/perf/Documentation/perf-kvm.txt
  48. ./tools/lib/traceevent/plugin_kvm.c
  49. # 说明文档
  50. ./Documentation/virtual/kvm

当然不可能只有这些,毕竟有些文件名字不含kvm字样,但是可以推断出目录包括哪些

  1. ./include/kvm
  2. ./arch/x86/include/asm/
  3. ./arch/x86/include/uapi/asm/
  4. ./arch/x86/configs/
  5. ./arch/x86/kvms
  6. ./virt/kvm

KVM模块入口点搜索

每个内核模块都有入口。那么kvm如果是个单独内核模块,它的入口在哪里?

  1. root@ubuntu16x64:~/kernel/linux-4.4.124# find ./ -type f -name "*.c"|xargs grep "module_init" |grep kvm
  2. ./arch/s390/kvm/kvm-s390.c:module_init(kvm_s390_init);
  3. ./arch/powerpc/kvm/book3s.c:module_init(kvmppc_book3s_init);
  4. ./arch/powerpc/kvm/e500.c:module_init(kvmppc_e500_init);
  5. ./arch/powerpc/kvm/book3s_pr.c:module_init(kvmppc_book3s_init_pr);
  6. ./arch/powerpc/kvm/book3s_hv.c:module_init(kvmppc_book3s_init_hv);
  7. ./arch/powerpc/kvm/e500mc.c:module_init(kvmppc_e500mc_init);
  8. ./arch/arm/kvm/arm.c:module_init(arm_init);
  9. ./arch/x86/kvm/svm.c:module_init(svm_init)
  10. ./arch/x86/kvm/x86.c: r = kvm_mmu_module_init();
  11. ./arch/x86/kvm/mmu.c:int kvm_mmu_module_init(void)
  12. ./arch/x86/kvm/vmx.c:module_init(vmx_init)
  13. ./arch/mips/kvm/mips.c:module_init(kvm_mips_init);

删去平台无关

  1. ./arch/x86/kvm/svm.c:module_init(svm_init)
  2. ./arch/x86/kvm/x86.c: r = kvm_mmu_module_init();
  3. ./arch/x86/kvm/mmu.c:int kvm_mmu_module_init(void)
  4. ./arch/x86/kvm/vmx.c:module_init(vmx_init)

正常模块入口应该是module_init(),那就剩它两个了,如果有点虚拟化概念的都知道。SVM是AMD家的,VMX是intel家的。没毛病。这里肯定是个条件编译区分开来。

  1. ./arch/x86/kvm/svm.c:module_init(svm_init)
  2. ./arch/x86/kvm/vmx.c:module_init(vmx_init)

找到入口就好分析了,来看看vmx.c,毕竟我用的是intel的,只分析vmx

KVM模块Kconfig

切换到kvm架构平台目录下

  1. root@ubuntu16x64:~/kernel/linux-4.4.124/arch/x86/kvm# cat Kconfig
  2. #
  3. # KVM configuration
  4. #
  5. source "virt/kvm/Kconfig"
  6. menuconfig VIRTUALIZATION
  7. bool "Virtualization"
  8. depends on HAVE_KVM || X86
  9. default y
  10. ---help---
  11. Say Y here to get to see options for using your Linux host to run other
  12. operating systems inside virtual machines (guests).
  13. This option alone does not add any kernel code.
  14. If you say N, all options in this submenu will be skipped and disabled.
  15. if VIRTUALIZATION
  16. config KVM
  17. tristate "Kernel-based Virtual Machine (KVM) support"
  18. depends on HAVE_KVM
  19. depends on HIGH_RES_TIMERS
  20. # for TASKSTATS/TASK_DELAY_ACCT:
  21. depends on NET && MULTIUSER
  22. depends on X86_LOCAL_APIC
  23. select PREEMPT_NOTIFIERS
  24. select MMU_NOTIFIER
  25. select ANON_INODES
  26. select HAVE_KVM_IRQCHIP
  27. select HAVE_KVM_IRQFD
  28. select IRQ_BYPASS_MANAGER
  29. select HAVE_KVM_IRQ_BYPASS
  30. select HAVE_KVM_IRQ_ROUTING
  31. select HAVE_KVM_EVENTFD
  32. select KVM_APIC_ARCHITECTURE
  33. select KVM_ASYNC_PF
  34. select USER_RETURN_NOTIFIER
  35. select KVM_MMIO
  36. select TASKSTATS
  37. select TASK_DELAY_ACCT
  38. select PERF_EVENTS
  39. select HAVE_KVM_MSI
  40. select HAVE_KVM_CPU_RELAX_INTERCEPT
  41. select KVM_GENERIC_DIRTYLOG_READ_PROTECT
  42. select KVM_VFIO
  43. select SRCU
  44. ---help---
  45. Support hosting fully virtualized guest machines using hardware
  46. virtualization extensions. You will need a fairly recent
  47. processor equipped with virtualization extensions. You will also
  48. need to select one or more of the processor modules below.
  49. This module provides access to the hardware capabilities through
  50. a character device node named /dev/kvm.
  51. To compile this as a module, choose M here: the module
  52. will be called kvm.
  53. If unsure, say N.
  54. config KVM_INTEL
  55. tristate "KVM for Intel processors support"
  56. depends on KVM
  57. # for perf_guest_get_msrs():
  58. depends on CPU_SUP_INTEL
  59. ---help---
  60. Provides support for KVM on Intel processors equipped with the VT
  61. extensions.
  62. To compile this as a module, choose M here: the module
  63. will be called kvm-intel.
  64. config KVM_AMD
  65. tristate "KVM for AMD processors support"
  66. depends on KVM
  67. ---help---
  68. Provides support for KVM on AMD processors equipped with the AMD-V
  69. (SVM) extensions.
  70. To compile this as a module, choose M here: the module
  71. will be called kvm-amd.
  72. config KVM_MMU_AUDIT
  73. bool "Audit KVM MMU"
  74. depends on KVM && TRACEPOINTS
  75. ---help---
  76. This option adds a R/W kVM module parameter 'mmu_audit', which allows
  77. auditing of KVM MMU events at runtime.
  78. config KVM_DEVICE_ASSIGNMENT
  79. bool "KVM legacy PCI device assignment support (DEPRECATED)"
  80. depends on KVM && PCI && IOMMU_API
  81. default n
  82. ---help---
  83. Provide support for legacy PCI device assignment through KVM. The
  84. kernel now also supports a full featured userspace device driver
  85. framework through VFIO, which supersedes this support and provides
  86. better security.
  87. If unsure, say N.
  88. # OK, it's a little counter-intuitive to do this, but it puts it neatly under
  89. # the virtualization menu.
  90. source drivers/vhost/Kconfig
  91. source drivers/lguest/Kconfig
  92. endif # VIRTUALIZATION
  93. root@ubuntu16x64:~/kernel/linux-4.4.124/arch/x86/kvm#

KVM模块Makefile文件

  1. root@ubuntu16x64:~/kernel/linux-4.4.124/arch/x86/kvm# cat Makefile
  2. ccflags-y += -Iarch/x86/kvm
  3. CFLAGS_x86.o := -I.
  4. CFLAGS_svm.o := -I.
  5. CFLAGS_vmx.o := -I.
  6. KVM := ../../../virt/kvm
  7. kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \
  8. $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o
  9. kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
  10. kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
  11. i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \
  12. hyperv.o
  13. kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += assigned-dev.o iommu.o
  14. kvm-intel-y += vmx.o pmu_intel.o
  15. kvm-amd-y += svm.o pmu_amd.o
  16. obj-$(CONFIG_KVM) += kvm.o
  17. obj-$(CONFIG_KVM_INTEL) += kvm-intel.o
  18. obj-$(CONFIG_KVM_AMD) += kvm-amd.o
  19. root@ubuntu16x64:~/kernel/linux-4.4.124/arch/x86/kvm#

kvm模块入口文件vmx.c

1532753950385.png

一万多行,没毛病~果断用SourceInsight阅读源码

使用SourceInsght4阅读源码

1532756590153.png

END