對於一個kvm,就對應一個線程。
Kvm完全利用了硬件虛擬化技術,通過cat /proc/cpuinfo 查看信息,如果是intel處理器,那麼就加載kvm-intel.ko
用戶態創建一個虛擬機就是通過ioctl向/dev/kvm字符設備進行設置和管理kvm的。
點擊(此處)摺疊或打開
-
struct kvm {
-
spinlock_t mmu_lock;
-
spinlock_t requests_lock;
-
struct rw_semaphore slots_lock;
-
struct mm_struct *mm; /* userspace tied to this vm */
-
int nmemslots;
-
struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +
-
KVM_PRIVATE_MEM_SLOTS];
-
#ifdef CONFIG_KVM_APIC_ARCHITECTURE
-
u32 bsp_vcpu_id;
-
struct kvm_vcpu *bsp_vcpu;
-
#endif
-
struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
-
atomic_t online_vcpus;
-
struct list_head vm_list;
-
struct mutex lock;
-
struct kvm_io_bus mmio_bus;
-
struct kvm_io_bus pio_bus;
-
#ifdef CONFIG_HAVE_KVM_EVENTFD
-
struct {
-
spinlock_t lock;
-
struct list_head items;
-
} irqfds;
-
struct list_head ioeventfds;
-
#endif
-
struct kvm_vm_stat stat;
-
struct kvm_arch arch;
-
atomic_t users_count;
-
#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-
struct kvm_coalesced_mmio_dev *coalesced_mmio_dev;
-
struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
-
#endif
-
-
struct mutex irq_lock;
-
#ifdef CONFIG_HAVE_KVM_IRQCHIP
-
struct list_head irq_routing; /* of kvm_kernel_irq_routing_entry */
-
struct hlist_head mask_notifier_list;
-
#endif
-
-
#ifdef KVM_ARCH_WANT_MMU_NOTIFIER
-
struct mmu_notifier mmu_notifier;
-
unsigned long mmu_notifier_seq;
-
long mmu_notifier_count;
-
#endif
- };
kvm_x86_ops 結構體中的所有成員都是函數指針,在kvm-intel.ko 和 kvm-amd.ko這兩個不同的模塊中,針對各自體系做不同的函數。KVM子系統代碼將通過該結構體函數進行實際的硬件操作。
針對kvm的fd,通過KVM_CREATE_VCPU指令字可以創建KVM的vCPU,並且獲得該vcpu_fd,vcpu_fd的操作主要包含在kvm_vcpu_fops中,kvm_vcpu_fops的實現方法如下:
點擊(此處)摺疊或打開
-
static struct file_operations kvm_vcpu_fops = {
-
.release = kvm_vcpu_release,
-
.unlocked_ioctl = kvm_vcpu_ioctl,
-
.compat_ioctl = kvm_vcpu_ioctl,
-
.mmap = kvm_vcpu_mmap,
- };