KVM虛擬化原理

kvm這個結構體包含了vCPU,內存,APIC,IRQ,MMU,Event事件管理等信息。該結構體中的信息主要在kvm虛擬機內部使用,用於跟蹤虛擬機的狀態。函數

對於一個kvm,就對應一個線程。ui

Kvm徹底利用了硬件虛擬化技術,經過cat /proc/cpuinfo 查看信息,若是是intel處理器,那麼就加載kvm-intel.kothis

用戶態建立一個虛擬機就是經過ioctl向/dev/kvm字符設備進行設置和管理kvm的。atom


struct kvm {spa

    spinlock_t mmu_lock;線程

    spinlock_t requests_lock;指針

    struct rw_semaphore slots_lock;事件

    struct mm_struct *mm; /* userspace tied to this vm */內存

    int nmemslots;requests

    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

};

struct kvm_vm_stat stat;就是KVM虛擬機中的頁表、MMU等運行時狀態信息。

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,

};

相關文章
相關標籤/搜索