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,
};