kvm module之create vcpu

来源:互联网 发布:淘宝联盟一淘同时使用 编辑:程序博客网 时间:2024/06/04 18:47
上一节将了QEMU 会调用kvm_vm_fops 来创建虚拟cpu,kvm_vm_fops定义如下。
3038 static struct file_operations kvm_vm_fops = {
3039         .release        = kvm_vm_release,
3040         .unlocked_ioctl = kvm_vm_ioctl,
3041 #ifdef CONFIG_KVM_COMPAT
3042         .compat_ioctl   = kvm_vm_compat_ioctl,
3043 #endif
3044         .llseek         = noop_llseek,
3045 };
kvm_vm_ioctl 定义如下
2828 static long kvm_vm_ioctl(struct file *filp,
2829                            unsigned int ioctl, unsigned long arg)
2830 {
2831         struct kvm *kvm = filp->private_data;
2832         void __user *argp = (void __user *)arg;
2833         int r;
2834 
2835         if (kvm->mm != current->mm)
2836                 return -EIO;
2837         switch (ioctl) {
2838         case KVM_CREATE_VCPU:
2839                 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2840                 break;


2839行调用kvm_vm_ioctl_create_vcpu 来创建虚拟cpu
static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
2342 {
2343         int r;
2344         struct kvm_vcpu *vcpu;
2345 
2346         if (id >= KVM_MAX_VCPU_ID)
2347                 return -EINVAL;
2348 
2349         vcpu = kvm_arch_vcpu_create(kvm, id);
2350         if (IS_ERR(vcpu))
2351                 return PTR_ERR(vcpu);
2352 
2353         preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2354 
2355         r = kvm_arch_vcpu_setup(vcpu);
2356         if (r)
2357                 goto vcpu_destroy;
2358 
2359         mutex_lock(&kvm->lock);
2360         if (!kvm_vcpu_compatible(vcpu)) {
2361                 r = -EINVAL;
2362                 goto unlock_vcpu_destroy;
2363         }
2364         if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
2365                 r = -EINVAL;
2366                 goto unlock_vcpu_destroy;
2367         }
2368         if (kvm_get_vcpu_by_id(kvm, id)) {
2369                 r = -EEXIST;
2370                 goto unlock_vcpu_destroy;
2371         }
2372 
2373         BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
2374 
2375         /* Now it's all set up, let userspace reach it */
2376         kvm_get_kvm(kvm);
2377         r = create_vcpu_fd(vcpu);
2378         if (r < 0) {
2379                 kvm_put_kvm(kvm);
2380                 goto unlock_vcpu_destroy;
2381         }
2382 
2383         kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
2384 
2385         /*
2386          * Pairs with smp_rmb() in kvm_get_vcpu.  Write kvm->vcpus
2387          * before kvm->online_vcpu's incremented value.
2388          */
2389         smp_wmb();
2390         atomic_inc(&kvm->online_vcpus);
2391 
2392         mutex_unlock(&kvm->lock);
2393         kvm_arch_vcpu_postcreate(vcpu);
2394         return r;
2395 
2396 unlock_vcpu_destroy:
2397         mutex_unlock(&kvm->lock);
2398 vcpu_destroy:
2399         kvm_arch_vcpu_destroy(vcpu);
2400         return r;
2401 }
总体比较简单。
0 0
原创粉丝点击